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

Normal Table – Embedded

chevron

Normal table operations

Embedded

C++

Selected

Java

Selected

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;

		System.loadLibrary("bangdb-java");
		System.out.println("load banagdb-java successful");
		boolean flag = true;
		DBParam dbp = new DBParam();
		dbp.setTransactionType(TransactionType.DB_MULTIOPS_TRANSACTION_NONE);
		BangDBDatabase bdb = new BangDBDatabase("mydb", dbp);
		if(bdb != null)
		{
			System.out.println("java - bdb created");
		}
		else
			flag = false;
		TableEnv tenv = new TableEnv();
		BangDBTable tbl = bdb.getTable("mynormal_table", tenv, OpenType.OPENCREATE);
		if(tbl != null)
		{
			System.out.println("java - tbl created");
		}
		else
			flag = false;
		
		System.out.println("table name = " + tbl.getName());
		System.out.println("table dir = " + tbl.getTableDir());
		System.out.println("table index type = " + tbl.getIndexType());
		String tblStats = tbl.getStats(true);
		System.out.println(tblStats);
		TableEnv te = bdb.getTableEnv("mynormal_table", 0);
		if(te != null)
			System.out.println(te.toString());
		
		//put data
		String k1 = "key1";
		String v1 = "this is value 1";
		if(tbl.put(k1, v1.getBytes(), InsertOptions.INSERT_UNIQUE, null) < 0)
		{
			System.out.println("put failed");
			flag = false;
		}
		else
			System.out.println("put successful");
		
		System.out.println("num of items in the table = " + tbl.count());
		byte[] rv1 = tbl.get(k1, null);
		String rv1_s = new String(rv1);
		if(!rv1_s.equals(v1))
		{
			System.out.println("get failed - value mismatch. got [ "+new String(rv1)+" ] expected [ "+v1+" ]");
			flag = false;
		}
		
		String k2 = "key2";
		String v2 = "this is value 2";
		if(tbl.put(k2, v2.getBytes(), InsertOptions.INSERT_UNIQUE, null) < 0)
		{
			System.out.println("put 2 failed");
			flag = false;
		}
		else
			System.out.println("put 2 successful");
		
		byte[] rv2 = tbl.get(k2, null);
		String rv2_s = new String(rv2);
		if(!rv2_s.equals(v2))
		{
			System.out.println("get failed - value 2  mismatch. got [ "+rv2_s+" ] expected [ "+v2+" ]");
			flag = false;
		}
		
		ScanFilter sf = new ScanFilter();
		ResultSet rs = tbl.scan(null, k1, k2, sf, null);
		while(rs.hasNext())
		{
			System.out.println("key = " + rs.getNextKeyStr() + ", val = " + rs.getNextValStr());
			rs.moveNext();
		}
		rs.clear();
		
		if(tbl.del(k1, null) < 0)
		{
			System.out.println("del failed");
			flag = false;
		}
		if(tbl.get(k1, null) != null)
		{
			System.out.println("got deleted data, get failed");
			flag = false;
		}
		
		// now create normal table with long key
		te.reset();
		te.setTable_type(TableType.NORMAL_TABLE);
		te.setKey_type(KeyType.NORMAL_KEY_LONG);
		BangDBTable tbl2 = bdb.getTable("normal_table2", te, OpenType.OPENCREATE);
		
		if(tbl2.put(1, "long value 1".getBytes(), InsertOptions.INSERT_UNIQUE, null) < 0)
		{
			System.out.println("put failed");
			flag = false;
		}
		System.out.println("get val = " + new String(tbl2.get(1, null)));
		rs = tbl2.scan(null, 1, 1, sf, null);
		while(rs.hasNext())
		{
			System.out.println("key = " + rs.getNextKeyLong() + ", val = " + rs.getNextValStr());
			rs.moveNext();
		}
		rs.clear();
		
		if(tbl2.del(1, null) < 0)
		{
			System.out.println("del long failed");
			flag = false;
		}
		if(tbl.get(1, null) != null)
		{
			System.out.println("got long deleted data, get failed");
			flag = false;
		}
                bdb.closeDatabase(CloseType.DEFAULT);