int hammy_del(ErlNifEnv *env, hammy_db *db, unsigned char *key, int key_size) { ham_key_t k; ham_txn_t *txn; int retval = HAMMY_FALSE; setup_key(&k, key, key_size); ham_txn_begin(&txn, db->databases[0], HAM_TXN_READ_ONLY); if (ham_erase(db->databases[0], txn, &k, 0) == HAM_SUCCESS) { ham_txn_commit(txn, 0); retval = HAMMY_TRUE; } else { ham_txn_abort(txn, 0); } return retval; }
int main(int argc, char **argv) { int i; ham_status_t st; /* status variable */ ham_db_t *db; /* hamsterdb database object */ ham_key_t key; /* the structure for a key */ ham_record_t record; /* the structure for a record */ memset(&key, 0, sizeof(key)); memset(&record, 0, sizeof(record)); /* * first step: create a new hamsterdb object */ st=ham_new(&db); if (st!=HAM_SUCCESS) error("ham_new", st); /* * second step: create a new hamsterdb database * * we could also use ham_create_ex() if we wanted to specify the * page size, key size or cache size limits */ st=ham_create_ex(db, "test.db", 0, 0664, 0); if (st!=HAM_SUCCESS) error("ham_create", st); /* * now we can insert, delete or lookup values in the database * * for our test program, we just insert a few values, then look them * up, then delete them and try to look them up again (which will fail). */ for (i=0; i<LOOP; i++) { key.data=&i; key.size=sizeof(i); record.data=&i; record.size=sizeof(i); st=ham_insert(db, 0, &key, &record, 0); if (st!=HAM_SUCCESS) error("ham_insert", st); } /* * now lookup all values * * for ham_find(), we could use the flag HAM_RECORD_USER_ALLOC, if WE * allocate record.data (otherwise the memory is automatically allocated * by hamsterdb) */ for (i=0; i<LOOP; i++) { key.data=&i; key.size=sizeof(i); st=ham_find(db, 0, &key, &record, 0); if (st!=HAM_SUCCESS) error("ham_find", st); /* * check if the value is ok */ if (*(int *)record.data!=i) { printf("ham_find() ok, but returned bad value\n"); return (-1); } } /* * close the database handle, then re-open it (to demonstrate the * call ham_open) */ st=ham_close(db, 0); if (st!=HAM_SUCCESS) error("ham_close", st); st=ham_open_ex(db, "test.db", 0, 0); if (st!=HAM_SUCCESS) error("ham_open", st); /* * now erase all values */ for (i=0; i<LOOP; i++) { key.size=sizeof(i); key.data=&i; st=ham_erase(db, 0, &key, 0); if (st!=HAM_SUCCESS) error("ham_erase", st); } /* * once more we try to find all values... every ham_find() call must * now fail with HAM_KEY_NOT_FOUND */ for (i=0; i<LOOP; i++) { key.size=sizeof(i); key.data=&i; st=ham_find(db, 0, &key, &record, 0); if (st!=HAM_KEY_NOT_FOUND) error("ham_find", st); } /* * we're done! close the database handle */ st=ham_close(db, 0); if (st!=HAM_SUCCESS) error("ham_close", st); /* * delete the database object to avoid memory leaks */ ham_delete(db); #if UNDER_CE error("success", 0); #endif printf("success!\n"); return (0); }