bg
Normal Table – Embedded C++ – BangDB = NoSQL + AI + Stream

Normal Table – Embedded C++

chevron

Normal table operations

Embedded C++

Here is a sample program which does most of the operations to help you understand the api 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");
		// handle error, possibly return from here
	}
	// db_param sets the db environment, see the section to know more about it

	// create a normal table - text or opaque data, not documents
	table_env te;
	// most of the default values for table_env are good, we will only override following as per our need
	te.set_table_type(NORMAL_TABLE);
	// let's set string as the primary key type
	te.set_key_type(NORMAL_KEY);
	te.set_key_size_byte(32);
	bangdb_table *norm_tbl = bdb->getTable("my_normal_tbl", &te, OPENCREATE);
	if(!norm_tbl)
	{
		printf("we could not create the table my_normal_tbl\n");
		flag = false;
		// handle error
	}

	// There are some of the enums used in the code above, see here for more info
	const char *key1 = "key1";
	const char *val1 = "this is a sample data for bangdb normal table";
	FDT k, v;
	bangdb_table::add_string_to_fdt(key1, &k, false);
	bangdb_table::add_string_to_fdt(val1, &v, false);
	if(norm_tbl->put(&k, &v, INSERT_UNIQUE) < 0)
	{
		printf("put failed\n");
		flag = false;
		// handle error
	}

	// to get data now;
	FDT *outv = NULL;
	if(norm_tbl->get(&k, &outv) < 0)
	{
		printf("get failed\n");
		flag = false;
		// handle error
	}
	// to ensure here for test (not needed otherwise)
	else if(memcmp(outv->data, val1, outv->length) != 0)
	{
		printf("value mismatch\n");
		flag = false;
		// handle error
	}

	//clear data
	k.free();
	v.free();
	if(outv)
	{
		outv->free();
		delete outv;
	}

	// scan now
	resultset *rs = NULL;
	bangdb_table::add_string_to_fdt(key1, &k, false);
	while(true)
	{
		rs = norm_tbl->scan(rs, &k, NULL);	// scan from key1 till end (NULL for end_key)
		if(!rs)
			break;
		while(rs->hasNext())
		{
			printf("key = %s, val = %s\n", rs->getNextKeyStr(), rs->getNextValStr());
			rs->moveNext();
		}
	}

	bdb->closeDatabase(DEFAULT);
	delete bdb;