holmes840
holmes840

Reputation: 1073

Aerospike : Retrieve a set of keys from LDT Bin in one call

Suppose In my LDT(LargeMap) Bin I have following values,

key1, value1   
key2, value2   
key3, value3   
key4, value4   
. .   
key50, value50

Now, I get my required data using following snippet :

Map<?, ?> myFinalRecord = new HashMap<?, ?>();
// First call to client to get the largeMap associated with the bin
LargeMap largeMap = myDemoClient.getLargeMap(myPolicy, myKey, myLDTBinName, null);

for (String myLDTKey : myRequiredKeysFromLDTBin) {
    try {
        // Here each get call results in one call to aerospike
        myFinalRecord.putAll(largeMap.get(Value.get(myLDTKey)));
    } catch (Exception e) {
        log.warn("Key does not exist in LDT Bin");
    }
}

The problem is here if myRequiredKeysFromLDTBin contains say 20 keys. Then largeMap.get(Value.get(myLDTKey)) will make 20 calls to aerospike.

Thus if I go by retrieval time of 1 ms per transaction , here my one call to retrieve 20 ids from a record will result in 20 calls to aerospike. This will increase my response time to approx. 20 ms !

So is there any way where I can just pass a set of ids to be retrieved from a LDT Bin and it takes only one call to do so ?

Upvotes: 3

Views: 648

Answers (1)

DB Guy
DB Guy

Reputation: 116

There is no direct API to do multi-get. A way of doing this would be call lmap API directly from server multiple time through UDF.

Example 'mymap.lua'

local lmap = require('ldt/lib_lmap');
function getmany(rec, binname, keys)
    local resultmap = map()
    local keycount  = #keys
    for i = 1,keycount,1 do
        local rc = lmap.exists(rec, binname, keys[i])
        if (rc == 1) then
            resultmap[keys[i]] = lmap.get(rec, binname, keys[i]);
        else
            resultmap[keys[i]] = nil;
        end
    end
    return resultmap;
end

Register this lua file

aql> register module 'mymap.lua'
OK, 1 module added.

aql> execute lmap.put('bin', 'c', 'd') on test.demo where PK='1'
+-----+
| put |
+-----+
| 0   |
+-----+
1 row in set (0.000 secs)

aql> execute lmap.put('bin', 'b', 'c') on test.demo where PK='1'
+-----+
| put |
+-----+
| 0   |
+-----+
1 row in set (0.001 secs)

aql> execute mymap.getmany('bin', 'JSON["b","a"]') on test.demo where PK='1'
+--------------------------+
| getmany                  |
+--------------------------+
| {"a":NIL, "b":{"b":"c"}} |
+--------------------------+
1 row in set (0.000 secs)

aql> execute mymap.getmany('bin', 'JSON["b","c"]') on test.demo where PK='1'
+--------------------------------+
| getmany                        |
+--------------------------------+
| {"b":{"b":"c"}, "c":{"c":"d"}} |
+--------------------------------+
1 row in set (0.000 secs)

Java Code to invoke this would be

 try {
     resultmap = myClient.execute(myPolicy, myKey, 'mymap', 'getmany', Value.get(myLDTBinName), Value.getAsList(myRequiredKeysFromLDTBin)
 } catch (Exception e) {
    log.warn("One of the key does not exist in LDT bin");
 }

Value will be set if key exists and it would return NIL if it does not.

Upvotes: 4

Related Questions