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

Writing Records

Use the Aerospike PHP client put methods to write record data in the Aerospike database. The Aerospike class includes several methods for writing records, updating, and overwriting record bins. These write methods act like PHP array operations.

The Aerospike database is schemaless. New record bins are created when inserted. Write operations overwrite bins unless the default policy is modified.

Aerospike natively supports string, integer, byte (binary data), list (indexed array), and map (associative array) data types. To retain their type, other data types (such as boolean and object) are serialized on writes and de-serialized on reads.

The Aerospike PHP client write methods are:

  • put(): Writes a record at the specified key.
  • increment(): Increments integer bin values.
  • append(): Adds a string to string bins.
  • prepend(): Prepends a string to the value of a string type bin.
  • operate(): Combines multiple bin operations into one operation.

Writing a Record

In this example, put() follows the default policy Aerospike::POLICY_EXISTS_IGNORE (or CREATE_OR_UPDATE) which has array-like behavior. The client policies are strict, like SQL. Use client policies to separate CREATE, UPDATE, and REPLACE behavior.

$key = $db->initKey("infosphere", "characters", 3);
$bins = ["name" => "Bender", "Occupation" => "Bender", "age" => 1055];
// store the key data with the record, rather than just its digest
$option = [Aerospike::OPT_POLICY_KEY => Aerospike::POLICY_KEY_SEND];
$db->put($key, $bins, 0, $option);

Updating and Adding Bins

This example transforms Occupation from a string to an array (to list on the Aerospike server), and adds the bin Alma Mater to the record.

$bins = [
  "Occupation" => ["Bender", "Criminal", "Iron Chef"],
  "Alma Mater" => "Bending State University"];
$db->put($key, $bins);

In this example, the Aerospike::OPT_POLICY_EXISTS option sets other Aerospike::POLICY_EXISTS_* values.

// the following should only work to 'INSERT' a brand new record
$bins = ["fail" => "because the record already exists"];
$option = [Aerospike::OPT_POLICY_EXISTS => Aerospike::POLICY_EXISTS_CREATE];
$status = $db->put($key, $bins, 120, $option);
if ($status == Aerospike::ERR_RECORD_EXISTS) {
  echo "This would have worked if we used Aerospike::POLICY_EXISTS_UPDATE\n";
}

Using Generation for Check-and-Set

Using the record generation metadata, subsequent write operations can implement a check-and-set (CAS) pattern to ensure that no other write occured since the last read. The Aerospike::OPT_POLICY_GEN option passes with put().

$current_gen = $record["metadata"]["generation"];
$ttl = $record["metadata"]["ttl"];
// only write if nothing has changed since the last read
$option = [Aerospike::OPT_POLICY_GEN => [Aerospike::POLICY_GEN_EQ, $current_gen]];
$status = $db->put($key, ["foo" => "bar"], $ttl, $option);
if ($status == Aerospike::ERR_RECORD_GENERATION) {
  echo "The generation policy check failed\n";
}

Appending and Prepending a Bin

$db->prepend($key, "name", "Captain ");
$db->append($key, "Alma Mater", ", Santa Cruz");

Multi-Ops

To combine multiple bin operations on a single record with optional post-write reads for the modified bin values:

$operations = [
  ["op" => Aerospike::OPERATOR_WRITE,
   "bin" => "Occupation", "val" => ["Bender", "Criminal", "Iron Chef"]],
  ["op" => Aerospike::OPERATOR_INCR,
   "bin" => "age", "val" => -1],
  ["op" => Aerospike::OPERATOR_APPEND,
   "bin" => "name", "val" => ' "Bending" Rodriguez'],
  ["op" => Aerospike::OPERATOR_READ,
   "bin" => "age"]
];
$status = $db->operate($key, $operations, $returned);
if ($status == Aerospike::OK) {
  var_dump($returned); // display the age
}