On this page

Composite Index

Composite Keys

Efficient way to structure data for faster queries

Composite keys / index again can be created for WIDE_TABLE type. To create composite index for primary key, we need to create table with following setting;

table_env te;
te.set_table_type(WIDE_TABLE);
te.set_key_type(COMPOSITE_KEY);
bangdb_table *tbl = bdb->getTable((char*)"topview", &te);
There are several helper function to create composite key before putting the data into the table. Here data could be json doc or normal text or opaque data.
Here is an example for putting data into the table
char * k, * v;
FDT fk, fv;
k = makeComposite((char * )
   "1", (char * )
   "a", NULL);
v = (char * )
"val13";
fk.data = k;
fk.length = strlen(k);
fv.data = v;
fv.length = strlen(v);
tbl -> put(fk, fv, INSERT_UNIQUE);
fk.free();

// more data

insert k = makeComposite((char * )
   "2", (char * )
   "a", NULL);
v = (char * )
"val9";
fk.data = k;
fk.length = strlen(k);
fv.data = v;
fv.length = strlen(v);
tbl -> put(fk, fv, INSERT_UNIQUE);
fk.free();
k = makeComposite((char * )
   "1", (char * )
   "b", NULL);
v = (char * )
"val7";
fk.data = k;
fk.length = strlen(k);
fv.data = v;
fv.length = strlen(v);
tbl -> put( & fk, & fv, INSERT_UNIQUE);
fk.free();
Now, let’s fire some query;
resultset * rs = NULL;

//to scan all data 
while (true) {
   rs = tbl -> scan_doc(NULL);
   if (!rs) break;
   while (rs -> hasNext()) {
      rs -> moveNext();
   }
}
rs = NULL;
char * tval1, * tval2;
FDT skey, ekey;
scan_filter sf;
sf.skey_op = GTE;
sf.ekey_op = LTE;
tval1 = (char * )"1:*";
tval2 = (char * )"1:*";
skey.data = tval1;
skey.length = strlen(tval1);
ekey.data = tval2;
ekey.length = strlen(tval2);
while (true) {
   rs = tbl -> scan_doc(NULL, skey, ekey);
   if (!rs) break;
   while (rs -> hasNext()) {
      rs -> moveNext();
   }
}
We can also match with the second part of the key using % char, like following;
tval1 = (char * )"1:b";
tval2 = (char * )"3:b";

// to match all data with 1:b... until 3:b 
skey.data = tval1;
skey.length = strlen(tval1);
ekey.data = tval2;
ekey.length = strlen(tval2);
while (true) {
   rs = tbl -> scan_doc(NULL, skey, ekey);
   if (!rs) break;
   while (rs -> hasNext()) {
      rs -> moveNext();
   }
}
Similarly;
tval1 = (char * ) "*:a";
tval2 = (char * )"*:a";
skey.data = tval1;
skey.length = strlen(tval1);
ekey.data = tval2;
ekey.length = strlen(tval2);
while (true) {
   rs = tbl -> scan_doc(NULL, & skey, & ekey);
   if (!rs) break;
   while (rs -> hasNext()) {
      rs -> moveNext();
   }
}

Was this article helpful to you? Yes No