How to build server client

The BangDB server client would connect to the server over network (or locally) and do various operations. This section would explore the steps for building and running the clients

libbangdb-client.so.0.7

This is the library that client would need to link to while building the app. If building using g++ need to use the switch -lbangdb-client to link

BangDB configuration

BangDB has many configurable parameters and user can just use defaults or set many or all of them to tune the db. However, some of the basic configurations are needed in order to run the client/server. You can look at BangDB Configuration

The configuration parameters are appliod across database but there are some which user might wish to set different for different tables or set at the run time. And in order to set few parameters at run time we use BangDB Environment and Table Environment variables which set some of the configurations for db and table respectiovely

BangDB Environment

BangDB environment is used to get the handle of database. It can be used simply without providing various parameters for configuration in case the values in the bangdb.config would be used, else you can override some of these parameters here by setting the values in db environment

BangDB environment can set following parameters;

  • hostname - host ip of the server
  • portnum - port num at which server listens
  • configpath - path where the bangdb.config is kept. If it's NULL then config should be in app local dir
  • db_transaction_type - set whether you would like to open db in transactional or non transactional mode

you can set all or part or none before opening the database

Now run the command line tool and create a table named mytbl before running the sample app

Open a database


	bangdbEnv dbenv;	//using all the values defined in bangdb.config file kept locally
	database *db = dbenv.openDatabase("userdb");
	

Table Environment

Following configurable parameters can be set differently for different tables

  • BANGDB_PERSIST_TYPE - INMEM_ONLY / INMEM_PERSIST (default) / P
  • BANGDB_INDEX_TYPE - BTREE / EXTHASH
  • BANGDB_LOG - Enabled (default) or Disabled
  • Log Type - Shared (default) or Private
  • LOG_BUF_SIZE - buff size for log
  • KEY_SIZE - key size, you should set this properly
  • BANGDB_AUTOCOMMIT - On (default) / Off
  • Table Size Hint - leave it to default

Open Table


	table_env tenv;
	tenv.set_persist_type(INMEM_PERSIST);
	tenv.set_idx_type(BTREE);
	tenv.set_log_state(1);
	
	table *tbl = db->gettable("mytable", JUSTOPEN, &tenv);
	

Connection

Conenctions are used to perform all the operations

	
	connection *conn = tbl->getconnection();
	

Do some operations

	
	# insert key val
	int retval = conn->put("key1", "value1", INSERT_UNIQUE);
	
	# get the val using key
	char *outval = conn->get("key1");
	
	# update the val for the key
	retval = conn->put("key1", "value2", UPDATE_EXISTING);
	
	# delete the key
	retval = conn->del("key1"); 
	

close handles

Note that you can close connections whenever you are done with them. Similarly tables can be closed whenever user is done with the table. Closing table closes all coneections opened for that table as well. Finally db can be closed which in turn closes all tables and all connections

	
	conn->closeconnection();
	delete conn;
		
	tbl->closetable();
	delete tbl;
		
	db->closedatabase();
	delete db;
		

Using the above simple APIs, user can create app for dealing with data at the server. Note that connection for client is not thread safe hence if you require parallel processing, open connection per thread

The API set (excpet BangDB Environment) is similar to that of embedded db, hence look at the complete API set for bangdb

Sample app for client


	/* Create db, table and connection */
	bangdbEnv dbenv;	//using all the values defined in bangdb.config file kept locally
	database *db = dbenv.openDatabase("userdb");
	
	table *tbl = db->gettable("mytable");
	if(tbl == NULL)
	{
		printf("Table is NULL, quitting");
		return -1;
	}				
	
	connection *conn = tbl->getconnection();
	if(conn == NULL)
	{
		printf("Connection is NULL, quitting");
		return -1;
	}		
			
	/* do some operations */
	char *key = "key_1";
	char *val = "This is the test value for key_1";
				
	/* put */
	if(conn->put(key, val, INSERT_UNIQUE) < 0)
		printf("Put error");
					
	/* get */
	char *out = conn->get(key);
	if(out == NULL)
		printf("Get error");
				
	/* update */
	val = "Update value for the test key";
	if(conn->put(key, val, UPDATE_EXISTING) < 0)
		printf("Update error");
					
	/* del */
	if(conn->del(key) < 0)
		printf("Del error");
					
	/* try to get the del key */
	if(conn->get(key) != NULL)
		printf("Got the deleted value, error");
		
	
	/* clean up */	
	conn->closeconnection();
	delete conn;
		
	tbl->closetable();
	delete tbl;
		
	db->closedatabase();
	delete db;

The above example is for char* data type, however for opaque binary data use FDT type as provided by the db