Skip to content

Instantly share code, notes, and snippets.

@jmiettinen
Last active October 12, 2016 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmiettinen/23a38c10d18c9f5b048480e7da0fa15c to your computer and use it in GitHub Desktop.
Save jmiettinen/23a38c10d18c9f5b048480e7da0fa15c to your computer and use it in GitHub Desktop.
Given start timestamp and end timestamp, we go through all snapshots between those two until a given block returns true. The block is given a Trx which can then be queried
# BE CAREFUL WITH THE END TIMESTAMP SO THAT YOU DON'T GO TOO FAR INTO THE PAST
# IN CASE THERE'S NO MATCHING SNAPSHOT
def find_snapshot(start_timestamp, end_timestamp = start_timestamp - 1, &block);
end_timestamp = end_timestamp > 0 ? end_timestamp : 0;
earlier = nil;
current_timestamp = start_timestamp;
while current_timestamp > end_timestamp do;
current_ss = FastormDb.database.get_snapshot(current_timestamp);
if block.call(current_ss.begin_trx);
return {:current => current_timestamp, :earlier => earlier };
end;
current_timestamp, earlier = current_ss.parent_timestamp, current_timestamp;
end;
return {:current => current_timestamp, :earlier => earlier };
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment