bg
Primitive Table – Embedded – BangDB = NoSQL + AI + Stream

Primitive Table – Embedded

chevron

Primitive table operations

Embedded

C++

Selected

Java

Selected

Here is a sample program which does most of the operations to help you understand the apis and their usage.

	bool flag = true;

	db_param dp;
	dp.setTransactionType(DB_TRANSACTION_NONE);
	bangdb_database *bdb = bangdb_database::getInstance("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
	table_env 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.set_table_type(PRIMITIVE_TABLE_STRING);
	te.set_key_size_byte(24);
	bangdb_table *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
	bangdb_table::add_string_to_fdt("prim_key1", &k, false);
	bangdb_table::add_long_to_fdt(1, &v);
	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();
		}
	}

	bdb->closeDatabase(DEFAULT);
	delete bdb;