Skip to content

Instantly share code, notes, and snippets.

@mschoch
Created November 1, 2014 00:02
Show Gist options
  • Save mschoch/4445e897824c3abdbb4c to your computer and use it in GitHub Desktop.
Save mschoch/4445e897824c3abdbb4c to your computer and use it in GitHub Desktop.
ForestDB problem when reading from snapshot after rollback?
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "libforestdb/forestdb.h"
int main(int argc, char**argv) {
fdb_handle *db, *snapshot;
fdb_status status;
fdb_config config;
fdb_seqnum_t seqnum, rollback_seqnum;
fdb_info info;
// 1. Open Database
config = fdb_get_default_config();
status = fdb_open(&db, "/tmp/db", &config);
assert(status == FDB_RESULT_SUCCESS);
printf("opened database\n");
// 2. Create Key 'a' and Commit
status = fdb_set_kv(db, "a", 1, "val-a", 5);
assert(status == FDB_RESULT_SUCCESS);
status = fdb_commit(db, FDB_COMMIT_NORMAL);
assert(status == FDB_RESULT_SUCCESS);
printf("created key 'a'\n");
status = fdb_get_dbinfo(db, &info);
assert(status == FDB_RESULT_SUCCESS);
seqnum = info.last_seqnum;
printf("seqnum: %llu\n", seqnum);
// 3. Create Key 'b' and Commit
status = fdb_set_kv(db, "b", 1, "val-b", 5);
assert(status == FDB_RESULT_SUCCESS);
status = fdb_commit(db, FDB_COMMIT_NORMAL);
assert(status == FDB_RESULT_SUCCESS);
printf("created key 'b'\n");
status = fdb_get_dbinfo(db, &info);
assert(status == FDB_RESULT_SUCCESS);
seqnum = info.last_seqnum;
printf("seqnum: %llu\n", seqnum);
// 4. Remember this as our rollback point
rollback_seqnum = seqnum;
// 5. Create Key 'c' and Commit
status = fdb_set_kv(db, "c", 1, "val-c", 5);
assert(status == FDB_RESULT_SUCCESS);
status = fdb_commit(db, FDB_COMMIT_NORMAL);
assert(status == FDB_RESULT_SUCCESS);
printf("created key 'c'\n");
status = fdb_get_dbinfo(db, &info);
assert(status == FDB_RESULT_SUCCESS);
seqnum = info.last_seqnum;
printf("seqnum: %llu\n", seqnum);
// 6. Rollback to rollback point (seq 2)
printf("trying to rollback to %llu\n", rollback_seqnum);
status = fdb_rollback(&db, rollback_seqnum);
assert(status == FDB_RESULT_SUCCESS);
status = fdb_get_dbinfo(db, &info);
assert(status == FDB_RESULT_SUCCESS);
seqnum = info.last_seqnum;
printf("seqnum: %llu\n", seqnum);
// 7. Verify that Key 'c' is not found
void *val;
size_t vallen;
status = fdb_get_kv(db, "c", 1, &val, &vallen);
assert(status == FDB_RESULT_KEY_NOT_FOUND);
printf("it wasn't found\n");
// 8. Open a snapshot at the same point
status = fdb_snapshot_open(db, &snapshot, seqnum);
assert(status == FDB_RESULT_SUCCESS);
// 9. Verify that Key 'c' is not found
status = fdb_get_kv(snapshot, "c", 1, &val, &vallen);
assert(status == FDB_RESULT_KEY_NOT_FOUND);
printf("it wasn't found\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment