Primary index on the data

Every data, value or document has a primary key associated with it in BangDB, explicitly or implicitly (timestamp). Therefore, we will need to store these data in such a manner where get() or scan() works efficiently.

When we wish to store in sorted manner then we should use BTREE as method else EHASH should be used.

To set the method for arranging the primary key, we will have to tell this while creating the table. This can be done by setting up the right parameter in the table_env object. The parameter that we set for this is idx_type.

By default it is set to BTREE, but still we can set it as per the value defined in enum bangdb_index_type.

//types of pk arrangement
    enum bangdb_index_type { 
       HASH, // Not supported anymore 
       EXTHASH,
       BTREE,
       HEAP, // Not supported anymore 
       INVALID_INDEX_TYPE, 
    };

As you see there are only two options here, BTREE or EXTHASH.

Query

The index on primary key is implicitly used when we call get()for a key. When we do scan(k1, k2); then db searches using the index. Let's see some actual query using this.

Let's say we have a table “mytable” where the primary key is indexed using BTREE, and we wish to scan the table for all the keys between k1 and k2. If it's a NORMAL_TABLE.

resultset *rs = NULL;
    scan_filter sf;
    // To set some simple filter criteria
    while (true) {
        rs = tbl->scan(rs, k1, k2, &sf);
        f(!rs) break;
        while (rs->hasNext())
        {
            // use rs key and val
            rs->moveNext();
        }
    }