Reading Records - aerospike-community/aerospike-client-php GitHub Wiki

Reading Records

These methods get data from the Aerospike database:

  • get(): Reads specified bins in a record.
  • exists(): Checks whether record metadata exists.
  • getMany(): Reads a batch of records.
  • existsMany(): Retrieve metadata for a batch records.

Defining the Key

To define the tuple (namespace, set, key) that uniquely identify a record:

$key = $db->initKey("infosphere", "characters", 1);

Reading a Record

This option gets the key component stored with the record on the previous write, applies a filter, and gets the record using the digest. If no key component is found, only ["key"]["digest"] is filled.

$option = [Aerospike::OPT_POLICY_KEY => Aerospike::POLICY_KEY_SEND];
$status = $db->get($key, $record, [], $option);
if ($status == Aerospike::ERR_RECORD_NOT_FOUND) {
    echo "A character with key ". $key['key']. " does not exist in the database\n";
    exit(2);
}

// filter the record down to a few specific bins
$status = $db->get($key, $record, ["name", "jobs"]);
if ($status == Aerospike::ERR_RECORD_NOT_FOUND) {
    var_dump($record);
}

// get the record using the digest value
$key_digest = $db->initKey("infosphere", "characters", $record["key"]["digest"], true);
$db->get($key_digest, $again);

Checking Record Existence

This example checks record existence by attempting to retrieve its metadata.

exists() is a faster operation than get() because it only scans the record Primary Index.

$status = $db->exists($key, $metadata);
if ($status == Aerospike::OK) {
    var_dump($metadata);
}

Example return:

array(2) {
  ["generation"]=>
  int(2)
  ["ttl"]=>
  int(1337)
}

Batch Operations

Use getMany() and existsMany() for multiple key assessment.

$keys = [];
for ($i = 1; $i <= 4; $i++) {
  $keys[] = $db->initKey('infosphere', 'characters', $i);
}
var_dump($db->existsMany($keys));
var_dump($db->getMany($keys));