Timestamps - GenesisFlake
When you generate a database on the Genesis platform, every table in the database is given a TIMESTAMP and a RECORD_ID field.
- The
TIMESTAMP
field value is generated automatically by GenesisFlake every time a change is made to the database. - The
RECORD_ID
field is theTIMESTAMP
value when the record is first created, it will never change.
The database generates a new TIMESTAMP
for every modify operation, even if no other fields are changed.
To create these values, GenesisFlake generates IDs in a similar manner to Twitter’s snowflake. It is able to generate these IDs without having to perform database-level synchronisation - which ensures high performance.
An ID includes:
- a node number (which represents the node id within a Genesis cluster)
- a sequence number (used to differentiate IDs generated within the same millisecond)
Timestamps are essential if you use Optimistic Concurrency.
Format
The GenesisFlake timestamp is made up of:
- epoch time in milliseconds
- node id
- sequence id
The timestamp itself is stored in the most significant bits of a LONG variable, leaving the least significant bits to node id and sequence number.
A raw ID value looks like this, for example: 6626101958220449352.
You can extract the timestamp component using bitwise right-shift operations. For example:
The result in decimal corresponds to 1579785813861, which can be checked in https://www.epochconverter.com/
Finding the most recent change to a table
To find the most recent change to table in your database:
- Add an index on the TIMESTAMP field for the table.
- Perform
a getRangeFromEnd
for that index. This returns all the records, beginning with the most recent. - Keep only the first record.