How to use Map operations using the PHP client?


#1

This question is in relationship to this other thread:

This documentation explains how to do operation on map bins: http://www.aerospike.com/docs/guide/cdt-map.html

I have a potentially huge map stored in memory and I need to be able to add/remove elements from it without reading it first or re-write it entirely.

I believe the operations add() and remove_by_key() will allow me to do that, but the client documentation for PHP doesn’t mention any of it, and doesn’t give any example of usage.

Thanks in advance.


#2

I’ve been trying things like this:

$key = $db->initKey(DB_TEST_NS, DB_TEST_SET, "test");

//PUT (before adding)
/*$map_test = array(
    "field1" => "value1",
    "field2" => "value2",
    "field3" => "value3",
    "field4" => "value4"
);
$status = $db->put($key, array("map_test" => $map_test));*/

//ADD
$new_value = array('field5' => 'value5');
$status = $db->apply($key, 'map', 'add', array('map_test', $new_value));

echo "DB status = $status\n";

Trying to reproduce the code in LList.php… The “put” works but not the “add”. The status returned is 100, and the test map remains unchanged.


#3

The PHP client doesn’t yet have the atomic map operations. The atomic list operations are implemented, though, so if you’re trying to simulate (the deprecated) LList that’s possible.

You would have a record with two bins, n (an integer, set to 1) and val (a list). You can listAppend elements to the end of the list until you hit the Aerospike:: ERR_RECORD_TOO_BIG error. When that happens you can create a ‘sub-key’, another record whose key is the same plus the suffix ‘-2’. You increment the n bin in the ‘top record’, then listAppend the element to that ‘sub-key’ record. So now you have keys ‘abc’ and ‘abc-2’ expressing the same logical object.

On the read side, you always get the ‘top record’ first (in this example you read record with key ‘abc’). If the n value is greater than 1, you create a list of keys and use batch-read (getMany) to fetch all of them together.

Similarly, before writing you can get the value of the bin n in the top record, and using that figure out whether you listAppend to the top record or a sub record.