Strange Aerospike behaviour data getting sorted automatically

Hi,

I am trying to insert some data like below

My key is like

$key = "testkey";
my value is like mixed 
$value = ['testdatasort'=>['testsorting'=>[3248=>'test1',2348=>'test2',1234=>'test3']]];

before inserting my data printed as

 Array
        (
            [testdatasort] => Array
                (
                    [testsorting] => Array
                        (
                            [3248] => test1
                            [2348] => test2
                            [1234] => test3
                        )
                )
        );


After insertion into a bin and once i fetch data  result is


array(3) {
  ["key"]=>
  array(4) {
    ["ns"]=>
    string(13) "somenamesapce"
    ["set"]=>
    string(4) "test"
    ["key"]=>
    NULL
    ["digest"]=>
    string(20) "ҕ����am-L��}$'ڵ2"
  }
  ["metadata"]=>
  array(2) {
    ["ttl"]=>
    int(604800)
    ["generation"]=>
    int(4)
  }
  ["bins"]=>
  array(1) {
    ["bin"]=>
    array(1) {
      ["testdatasort"]=>
      array(1) {
        ["testsorting"]=>
        array(3) {
          [2348]=>
          string(5) "test2"
          [3248]=>
          string(5) "test1"
          [1234]=>
          string(5) "test3"
        }
      }
    }
  }
}

So here data changed like

 array(1) {
  ["testdatasort"]=>
  array(1) {
    ["testsorting"]=>
    array(3) {
      [2348]=>
      string(5) "test2"
      [3248]=>
      string(5) "test1"
      [1234]=>
      string(5) "test3"
    }
  }
}

Is this behavior is expected or something i missing here? Here key 3248 and 2348 sorted out and 1234 remains same .

Huh? Can you be more clear about what the change was. What is the operation you performed. If you’re quoting PHP code, do it all the way, including for the transitions.

Read this about how PHP associative arrays cast to Aerospike types.

Hi,

I am doing normal put operation.I am passing an associate array inside that array contains id value. When i call get api my result is getting changed and returning in some unknown order . I think map containing list of items with integer as key is ordering is not guaranteed?

I still don’t see anything wrong. Please post the source code to reproduce the problem.

I see that you create a bin called ‘bin’ whose value is a map with one key, ‘testdatasort’. The value for that key is another map with one key, ‘testsorting’. The value of that key is yet another map, which has three keys: the integers 2348, 3248, 1234. Their values are strings.

What you showed for before and after is exactly the same.

Hi,

Sorry to make the confusion here keys before are 3248,2348,1234 and keys after are 2348,3248,1234.

Another strange case i observed is when i do some data serialize and push to aerospike may be aerospike internally again doing serialize. when i deserialize data it is writtening truncated data.

A map, by default, is unsorted. You have the same key-value pairs, but the order isn’t necessarily going to be preserved. Aerospike maps can also be defined as K-ORDERED (ordered by keys) or KV-ORDERED (ordered by keys and values) ahead of storage - this means when you put() a map value, depending on that type, it will be stored either without order, or ordered. This affects the complexity (big-O) of the map operations (see the link I added for Maps).

The PHP client does not yet have the map operations, so it casts a PHP associative array to either the Aerospike map or list data types. If your array is a list it has integer indexed value monotonously increasing from 0. So:

  • [0 => ‘a’, 1 => ‘b’, 2 => ‘c’] will be cast to an Aerospike list.
  • [‘a’, ‘b’, ‘c’] is basically the same as above, so it’ll be an Aerospike list.
  • [100 => ‘a’, 207 => ‘b’] will be cast to an Aerospike map.

By the way, a PHP associative array is also flexible about whether it’s a list or a map. It doesn’t matter to PHP array or list operations how the data is ordered in the array().

As for your latest comment - please open that as either a bug in the PHP client’s repo at Github aerospike/aerospike-client-php, or a separate discussion under Client Libraries in this forum.

1 Like

As data is internally converted to aerospike Map this behavior can be accepted, Its is completely missing in the documentation.

Second thing I observed with serialized data/binary data is data got truncated at \0 position.

Any specific reason for same?

It is documented :slight_smile:. From doc/README.md:

There is phpdoc documentation on the way, currently on the phpdoc branch of that repo.

Thanks, @rbotzer this is helpful to understand same.