Get Operation

In the previous section we reviewed the BnagDB objects and dealing with them. Now using the objects we will perform some operations. This section will review the *get* operation

get

connection (for Normal Table)

API - without transaction

char* get(const char *key);

FDT* get(FDT *key);

FDT *get_data(LONG_T key);

int get(const char *key, int keylen, char **val, int *vallen);

int get(const char *key, int keylen, DATA_VAR *data);

int get(LONG_T key, DATA_VAR *data);

API - with transaction

char* get(const char *key, void *txn_handle);

FDT* get(FDT *key, void *txn_handle);

int get(const char *key, int keylen, char **val, int *vallen, void *txn_handle);

int get(const char *key, int keylen, DATA_VAR *data, bangdb_txn *txn_handle);

int get(LONG_T key, DATA_VAR *data, bangdb_txn *txn_handle);

FDT is data type which is defined as below.

	struct FDT
	{
		void *data;
		DATALEN_T len;	//DATALEN_T is unsigned int
		void free();
		void copy(FDT *v);
		~FDT();
	}
	

DATA_VAR is for a scenario where user wishes to allocate buffer and use it for all operations. It also helps in reading or writing from a particular offset. The structure of DATA_VAR is as follows;

	struct DATA_VAR
	{
		//the pointer to pre allocated buffer. for write db will read from
		//it and for read db would write to it. User should clear the buffer	
			
		void *data_buf;					
						
		//the allocated buffer length (db doesn't touch this)
		
		int data_buf_len;	
		
		//length of the read data (returned by db) or length of data to write
		
		int data_len;		
		
		//for write db takes the bytes from data_buf from the data_offt and	
		//for read db reads data in db from data_offt
		//data_offt + data_len <= data_buf_len, else data will be curtailed	
		
		int data_offt; 						
					
		//db sets if more data is to come for read(MORE_DATA_TO_READ)
		
		int flag;		
	}
	

When we deal with opaque or binary data then it's good to use FDT type as we can specify the data and its length here. If the data assigned here has been created on heap (dynamically) then we should either release that data separately or call free() on FDT type. Copy() method is to copy a FDT type (more like copy contr)

When we deal with null terminated char strings then we can simple use the char* based API

Now we will get the values for given keys. Assume that db objects have been created earlier as shown previously

	FDT *k = new FDT("my key", 6);
	FDT *rout = conn->get(k);
	if(!rout)
		bangdb_logger("couldn't get for k");
	
	//with transaction
	FDT *k1 = new FDT("my key for table1, 17);
	FDT *k2 = new FDT("my key for table2, 17);
	
	bangdb_txn txn; 
	db->begin_transaction(&txn);
	
	//conn1 is connection for table1
	FDT *v1 = conn1->get(k1, &txn);
	
	//conn2 is connection for table2
	FDT *v2 = conn2->get(k2, &txn);
	
	db->commit_transaction(&txn);
	
	//note we have left out the error condition check for get and commit
	

primConnection (for Primitive Table)

int get(LONG_T key, LONG_T *val);

int get(int key, int *val);

int get(FDT *key, LONG_T *val);

int get(const char *key, LONG_T *val);

Note that for primitive table, the key and value will be of fixed size. Though key could be int, long and char* (of fixed size) type but the value would always be either int or long. Hence all the put API mentioned above has value as long or int

simple example could be as follows;

	table *ptbl = db->getPrimitiveTable("prim_table");
	primConnection *pconn = ptbl->getPrimConnection();
	LONG_T v = 0;
	if(pconn->get(10, &v) < 0)
		bangdb_logger("error in get");
	
	printf("value = %ld", v);
	

wideConnection (for Wide Table)

get for wide table is similar to that of normal table

Connection (for Normal Table)

API - without transaction

bool Get(long key, byte[] val)

bool Get(long key, out string vout)

bool Get(string key, out byte[] val)

bool Get(byte[] key, out string val)

bool Get(byte[] key, out byte[] val)

bool Get(string key, out string val)

bool Get(string key, ref DataVar dv)

bool Get(byte[] key, ref DataVar dv)

API - with transaction

bool Get(long key, byte[] val, Transaction txn)

bool Get(long key, out string vout, Transaction txn)

bool Get(string key, out byte[] val, Transaction txn)

bool Get(byte[] key, out string val, Transaction txn)

bool Get(byte[] key, out byte[] val, Transaction txn)

bool Get(string key, out string val, Transaction txn)

Now we will get the values for given keys. Assume that db objects have been created earlier as shown previously

	string k = "my key";
	string v = null;
	if(!conn.Get(k, out v))
		Common.BangDBLogger("error in get");
	
	//or
	byte[] v2 = null;
	if(!conn.Get(k, out v2))
		Common.BangDBLogger("couldn't fetch value for key k");
		
	//or
	long k2 = 10020;
	if(!conn.Get(k2, out v2))
		Common.BangDBLogger("couldn't fetch value for key k2");
	
	//or
	byte[] k4 = Encoding.UTF8.GetBytes("dummy key");
	byte[] v4 = null;
	if(!conn.Get(k4, out v4))
		Common.BangDBLogger("error in get");
	
	//with transaction
	Transaction txn = new Transaction();
	db.BeginTransaction(txn);
	
	//conn1 is connection for table1
	if(!conn1.Get(k, out v, txn))
		Common.BangDBTransaction("error in get with txn");
	
	//conn2 is connection for table2
	if(!conn2.Get(k4, out v4, txn))
		Common.BangDBTransaction("error in get with txn");
	
	db.CommitTransaction(ref txn);
	
	//note we have left out the error condition check for get and commit
	

PrimConnection (for primitive table)

public bool Get(long key, ref long val);

public bool Get(int key, ref int val);

public bool Get(string key, ref long val);

public bool Get(byte[] key, ref long val);

Note that for primitive table, the key and value will be of fixed size. Though key could be int, long and string (of fixed size) type but the value would always be either int or long. Hence all the put API mentioned above has value as long or int

simple example could be as follows;

	Table ptbl = db.GetPrimitiveTable("prim_table");	//default creates for PRIMITIVE_LONG data type
	PrimConnection pconn = ptbl.GetPrimConnection();
	long v = 0;
	if(!pconn.Get(10, ref v))
		bangdb_logger("error in put");
	

WideConnection (for Wide Table)

The get APIs for Wide Connection are same as of Connection (for Normal Table)

Connection (for Normal Table)

API - without transaction

public String getStr(long key);

public byte[] getByte(long key);

public int get(String key, DataVar dv);

public int get(byte[] key, DataVar dv);

public String get(String key);

public byte[] get(byte[] key);

public int get(long key, DataVar dv);

API - with transaction

String get(String key, Transaction txn)

byte[] get(byte[] key, Transaction txn)

public int get(long key, DataVar dv, Transaction txn);

Now we will get the values for given keys. Assume that db objects have been created earlier as shown previously

	String k1 = "my key";
	String v1 = conn.get(k1);
	
	//or
	byte[] k2 = "my key".getBytes();
	byte[] v2 = conn.get(k2);		
		
	//or
	String k3 = Long.toString("10201");
	String v3 = conn.get(k3);
	
	//etc...
	
	//with transaction
	Transaction txn = new Transaction();
	db.beginTransaction(txn);
	
	//conn1 is connection for table1
	String v1 = conn1.get(k1, txn);
	
	//conn2 is connection for table2
	byte[] v2 = conn2.get(k2, txn);
	
	db.CommitTransaction(txn);
	
	//note we have left out the error condition check for get and commit
	

PrimConnection (for Primitive Connection)

public Integer get(int key);

public Long get(long key);

public Long get(String key);

public Long get(byte[] key);

Note that for primitive table, the key and value will be of fixed size. Though key could be int, long and char* (of fixed size) type but the value would always be either int or long. Hence all the put API mentioned above has value as long or int

simple example could be as follows;

	Table ptbl = db.getPrimitiveTable("prim_table", BangDBPrimitiveDataType.PRIMITIVE_LONG, DBAccess.OPENCREATE, null);
	PrimConnection pconn = ptbl.getPrimConnection();
	Long v = null;
	if((v = pconn.get(10, InsertOptions.INSERT_UNIQUE)) == null)
		BangDBCommon.bangdb_logger("error in put");
	

WideConnection (for Wide Table)

The get APIs for wide connection is same as of normal connection (Connection)