JsonDb::Transaction::Transaction(std::string const &filename, ValuePointer const &_null_element) { /* The null element */ null_element = _null_element; /* open the database */ db = StorageDbPointer(vlopen(filename.c_str(), VL_OWRITER | VL_OCREAT, VL_CMPINT), CloseDatabase); /* Start the transaction */ vltranbegin(db.get()); if(db.get() == NULL) throw std::runtime_error((boost::format("Failed to open database: %s") % dperrmsg(dpecode)).str().c_str()); ValuePointer root = Retrieve(root_key); if(root.get() == NULL) { root = ValuePointer(new ValueObject(root_key)); Store(root->GetKey(), root); } ValuePointer next_id_value = Retrieve(next_id_key); if(next_id_value.get() == NULL) { // Initialize values next_id = initial_next_id; start_next_id = next_id; } else { // Retrieve values from database next_id = next_id_value->GetValueInt(); start_next_id = next_id; } // std::cout << "Start transaction, next id: " << next_id << std::endl; }
int main(void) { VILLA *villa; int size; char *s; printf("compareFunc(\"a\", \"b\") = %i\n", compareFunc("a", 1, "b", 1)); printf("compareFunc(\"b\", \"a\") = %i\n", compareFunc("b", 1, "a", 1)); printf("compareFunc(\"a\", \"a\") = %i\n", compareFunc("a", 1, "a", 1)); printf("compareFunc(\"b\", \"b\") = %i\n", compareFunc("b", 1, "b", 1)); remove("test.qdbm"); if(!(villa = vlopen("test.qdbm", VL_OWRITER | VL_OCREAT, compareFunc))) { printf("vlopen failed %s\n", dperrmsg(dpecode)); return 1; } if(!vltranbegin(villa)) { printf("vltranbegin failed %s\n", dperrmsg(dpecode)); return 1; } if(!vlput(villa, "a", 1, "1", 1, DP_DOVER)) { printf("vlput failed %s\n", dperrmsg(dpecode)); return 1; } if(!vlput(villa, "b", 1, "2", 1, DP_DOVER)) { printf("vlput failed %s\n", dperrmsg(dpecode)); return 1; } if(!vltrancommit(villa)) { printf("vltrancommit failed %s\n", dperrmsg(dpecode)); return 1; } if(!vlclose(villa)) { printf("vlclose failed %s\n", dperrmsg(dpecode)); return 1; } if(!(villa = vlopen("test.qdbm", VL_OWRITER | VL_OCREAT, compareFunc))) { printf("vlopen failed %s\n", dperrmsg(dpecode)); return 1; } s = vlget(villa, "a", 1, &size); printf("a = '%s'\n", s ? s : "NULL"); s = vlget(villa, "b", 1, &size); printf("b = '%s'\n", s ? s : "NULL"); if(!vlclose(villa)) { printf("vlclose failed %s\n", dperrmsg(dpecode)); return 1; } return 0; }