Inserting a value in HamsterDB with HAM_OVERWRITE

84 Views Asked by At

If I insert a value into a HamsterDB database with the flag HAM_OVERWRITE, what is the effect when the key doesn't already exists?

I am keeping track of the highest serial number in my program and persisting it in a HamsterDB database:

if (serial > high_serial_) {
    high_serial_ = serial;
    ham::key key((void*)HIGH_SERIAL_KEY, strlen(HIGH_SERIAL_KEY));
    ham::record record((void*)&high_serial_, sizeof(high_serial_));

    db.insert(&key, &record, HAM_OVERWRITE);
}

Upon starting up the application again, high_serial_ is initialised with the current value stored in the DB:

ham::key key((void*)HIGH_SERIAL_KEY, strlen(HIGH_SERIAL_KEY));
auto record = roll_file_db.find(&key);

But this call is throwing HAM_KEY_NOT_FOUND. The documentation states that it will simply be inserted if the key doesn't already exist. This is not what I'm experiencing though.

1

There are 1 best solutions below

0
On

If the key does not exist and you call ham_db_insert(db, txn, key, record, HAM_OVERWRITE) then the key will be inserted.

If the key does not exist and you call ham_db_find() then you get HAM_KEY_NOT_FOUND.

The documentation for ham_db_find lists HAM_KEY_NOT_FOUND as a return value "if the key does not exist": http://hamsterdb.com/public/scripts/html_www/group_ham_database.html#ga90dd239b79be2181461bbabc2eee1496

HTH Christoph