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

Wide Table – Embedded C++

chevron

Wide table operations

Embedded C++

Here is a sample program which does most of the operations to help you understand the apis and their usage.
You may copy and paste and run to check. It is advisable that you go though this thoroughly to get hold of this. It's very simple, dealing with just put_doc(), get() and scan_doc() apis, that's all

	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
	table_env te;
	te.set_table_type(WIDE_TABLE);
	// lste' create long primary key type
	te.set_key_type(NORMAL_KEY_LONG);
	// enable reversed index
	te.set_rev_idx(true);
	bangdb_table *wide_tbl = bdb->getTable("my_wide_tbl", &te, OPENCREATE);
	if(!wide_tbl)
	{
		printf("we could not create the table my_wide_tbl\n");
		flag = false;
		// handle error
	}

	// now put in wide table
	const char *doc1 = "{\"name\":\"sachin\", \"org\":\"bangdb\", \"address\":{\"city\":\"Bangalore\",\"state\":\"KA\"}, \"fav_quote\":\"We build too many walls and not enough bridges\"}";
	long pk = 123;
	FDT k, v;
	bangdb_table::add_long_to_fdt(pk, &k);
	// since we wish to put document, hence we will call put_doc
	if(wide_tbl->put_doc(doc1, &k) < 0)
	{
		printf("put_doc failed\n");
		flag = false;
		// handle error
	}

	// suppose we wish to now create index for name and address.city and reverse index fav_quote text then we will need to do following;
	// let's create the index first, name of the index should be the field name on which we wish to add index
	// supply max size of the index and specify whether to allows duplicate index or not
	wide_tbl->addIndex_str("name", 32, true);
	wide_tbl->addIndex_str("address.city", 32, true);
	// also prepare to tell db to reverse index "fav_quote" and "state" fields
	const char *rev_idx = "{\"_rev_idx_all\":0, \"_rev_idx_key_list\":[\"fav_quote\", \"state\"]}";

	const char *doc2 = "{\"name\":\"arjun\", \"org\":\"bangdb\", \"address\":{\"city\":\"San jose\",\"state\":\"CA\"}, \"fav_quote\":\"Uneasy lies the head that wears the crown\"}";
	bangdb_table::add_long_to_fdt(124, &k);
	if(wide_tbl->put_doc(doc2, &k, rev_idx) < 0)
	{
		printf("put doc failed with rev_idx\n");
		flag = false;
		// handle error
	}

	// to get data now
	FDT *outv = NULL;
	bangdb_table::add_long_to_fdt(123, &k);
	if(wide_tbl->get(&k, &outv) < 0)
	{
		printf("get failed\n");
		flag = false;
		// handle error
	}
	// to ensure here for test (not needed otherwise)
	else
	{
		printf("doc = %s\n", (char*)outv->data);
	}
	if(outv)
	{
		outv->free();
		delete outv;
	}

	// please note that the doc has the pk also added, this happens with documents only

	// query1 = scan all documents
	resultset *rs = NULL;
	while(true)
	{
		rs = wide_tbl->scan_doc(rs);
		if(!rs)
			break;
		while(rs->hasNext())
		{
			printf("key = %ld, val = %s\n", rs->getNextKeyLong(), rs->getNextValStr());
			rs->moveNext();
		}
	}

	// query2 = scan for documents having name equal to sachin
	rs = NULL;
	dataQuery dq;
	dq.addQuery("name", EQ, "sachin", JO_INVALID);
	while(true)
	{
		rs = wide_tbl->scan_doc(rs, NULL, NULL, dq.getQuery());
		if(!rs)
			break;
		while(rs->hasNext())
		{
			printf("key = %ld, val = %s\n", rs->getNextKeyLong(), rs->getNextValStr());
			rs->moveNext();
		}
	}

	// you will notice that even though we didn't create index on name until the first doc with name "sachin" was inserted,
	// therefore we didn't get the result, however there is a way to still fetch, we can use following
	rs = NULL;
	scan_filter sf;
	sf.reserved = 13; // magic num to tell db to fetch anyway, but note, this is not as efficient hence avoid unless required
	while(true)
	{
		rs = wide_tbl->scan_doc(rs, NULL, NULL, dq.getQuery(), &sf);
		if(!rs)
			break;
		while(rs->hasNext())
		{
			printf("key = %ld, val = %s\n", rs->getNextKeyLong(), rs->getNextValStr());
			rs->moveNext();
		}
	}


	// query3 = scan for document with name equal to arjun and city san jose
	rs = NULL;
	dq.reset();
	dq.addQuery("name", EQ, "arjun", JO_INVALID);
	dq.addQuery("address.city", EQ, "San jose", JO_AND);
	while(true)
	{
		rs = wide_tbl->scan_doc(rs, NULL, NULL, dq.getQuery());
		if(!rs)
			break;
		while(rs->hasNext())
		{
			printf("key = %ld, val = %s\n", rs->getNextKeyLong(), rs->getNextValStr());
			rs->moveNext();
		}
	}

	// query4 = scan for documents where the fav_quote has "crown" token/word and city "san jose"
        rs = NULL:
	dq.reset();
	dq.addQuery("address.city", EQ, "San jose", JO_INVALID);
	dq.addQuery("crown, head", JO_AND, JO_AND, "fav_quote");
	while(true)
	{
		rs = wide_tbl->scan_doc(rs, NULL, NULL, dq.getQuery());
		if(!rs)
			break;
		while(rs->hasNext())
		{
			printf("key = %ld, val = %s\n", rs->getNextKeyLong(), rs->getNextValStr());
			rs->moveNext();
		}
	}

	bdb->closeDatabase(DEFAULT);
	delete bdb;