Primitive Table
C++
Java
Here is a sample program which does most of the operations to help you understand the APIs and their usage.
bool flag = true;
DBParam dp;
dp.setTransactionType(DB_TRANSACTION_NONE);
BangDBDatabase * bdb = new BangDBDatabase("mydb", & dp);
if (!bdb) {
printf("db could not be created, quitting\n");
//return or handle error
}
// db_param sets the db environment, see the section to know more about it
// create primitive table
TableEnv te;
// primitive table has val always int or long type, only key we can be string, opaque or fixed type
// PRIMITIVE_TABLE_STRING - means key is staring or opaque type
te.setTableType(PRIMITIVE_TABLE_STRING);
te.setKeySize(24);
BangDBTable * prim_tbl = bdb -> getTable("my_prim_tbl", & te, OPENCREATE);
if (!prim_tbl) {
printf("we could not create the table my_prim_tbl\n");
flag = false;
// handle error
}
FDT k, v;
// since we created PRIMITIVE_TABLE_STRING, therefore key is of string type
_set_fdt( & k, "prim_key1");
_set_fdt( & v, 1);
if (prim_tbl -> put( & k, & v, INSERT_UNIQUE) < 0) {
printf("put for prim table failed\n");
flag = false;
// handle error
}
FDT * outv = NULL;
if (prim_tbl -> get( & k, & outv) < 0) {
printf("get failed for prim table\n");
flag = false;
// handle error
}
if (outv) {
outv -> free();
delete outv;
}
// now scan
ResultSet * rs = NULL;
while (true) {
rs = prim_tbl -> scan(rs, & k, NULL);
if (!rs)
break;
while (rs -> hasNext()) {
printf("key = %s, val = %ld\n", rs -> getNextKeyStr(), rs -> getNextValLong());
rs -> moveNext();
}
}
delete prim_tbl;
bdb -> closeDatabase(DEFAULT);
delete bdb;