/** * @return \Aerospike * @throws Exception */ public function getConnection() { if (is_null($this->connection)) { // Init connection $this->connection = new \Aerospike($this->config, $this->persistent_connection, $this->options); if ($this->connection->errorno() != 0) { \Yii::error("Failed to open aerospike DB connection: " . $this->connection->errorno() . " - " . $this->connection->error(), __CLASS__); throw new Exception('Failed to open aerospike DB connection', $this->connection->error(), (int) $this->connection->errorno()); } } return $this->connection; }
/** * Validate query string, connect to aspike * * @param array $hosts * @return array */ public function connectAero($hosts) { $config = array("hosts" => $hosts); $opts = array(Aerospike::OPT_CONNECT_TIMEOUT => 1000000, Aerospike::OPT_WRITE_TIMEOUT => 2500); $db = new Aerospike($config, true, $opts); if (!$db->isConnected()) { echo "<div class='alert alert-danger'><strong>Aerospike Error!</strong> " . $db->errorno() . " : " . $db->error() . "</div>"; exit(1); } return $db; }
$options = getopt($shortopts, $longopts); return $options; } $args = parse_args(); if (isset($args["help"])) { echo "php standard.php [-h<HOST IP ADDRESS>|--host=<HOST IP ADDRESS> -p<HOST PORT NUMBER>|--port=<HOST PORT NUMBER> -a|--annotate -c|--clean]\n"; exit(1); } $HOST_ADDR = isset($args["h"]) ? (string) $args["h"] : (isset($args["host"]) ? (string) $args["host"] : "localhost"); $HOST_PORT = isset($args["p"]) ? (int) $args["p"] : (isset($args["port"]) ? (string) $args["port"] : 3000); echo colorize("Connecting to the host ≻", 'black', true); $start = __LINE__; $config = array("hosts" => array(array("addr" => $HOST_ADDR, "port" => $HOST_PORT))); $db = new Aerospike($config, false); if (!$db->isConnected()) { echo fail("Could not connect to host {$HOST_ADDR}:{$HOST_PORT} [{$db->errorno()}]: {$db->error()}"); exit(1); } echo success(); if (isset($args['a']) || isset($args['annotate'])) { display_code(__FILE__, $start, __LINE__); } echo colorize("Ensuring that a record is put at test.users with PK=1234 ≻", 'black', true); $start = __LINE__; $key = $db->initKey("test", "users", 1234); $put_vals = array("email" => "*****@*****.**", "name" => "Perceptron"); $status = $db->put($key, $put_vals); if ($status == Aerospike::OK) { echo success(); } else { echo standard_fail($db);
$args = parse_args(); if (isset($args["help"])) { echo "php read-write-mix.php [-hHOST] [-pPORT] [-wWRITE EVERY Nth RECORD] [-nRECORDS]\n"; echo " or\n"; echo "php read-write-mix.php [--host=HOST] [--port=PORT] [--write-every=WRITE EVERY Nth RECORD] [--num-ops=RECORDS]\n"; exit(1); } $addr = isset($args["h"]) ? (string) $args["h"] : (isset($args["host"]) ? (string) $args["host"] : "localhost"); $port = isset($args["p"]) ? (int) $args["p"] : (isset($args["port"]) ? (string) $args["port"] : 3000); $total_ops = isset($args["n"]) ? (int) $args["n"] : (isset($args["num-ops"]) ? (string) $args["num-ops"] : 100000); $write_every = isset($args["w"]) ? (int) $args["w"] : (isset($args["write-every"]) ? (string) $args["write-every"] : 10); echo colorize("Connecting to the host ≻", 'black', true); $config = array("hosts" => array(array("addr" => $addr, "port" => $port))); $db = new Aerospike($config); if (!$db->isConnected()) { echo fail("Could not connect to host {$addr}:{$port} [{$db->errorno()}]: {$db->error()}"); exit(1); } echo success(); $key = $db->initKey("test", "performance", "read-write"); echo colorize("Clear out the record that may exist at test.performance with PK=1 ≻", 'black', true); $res = $db->remove($key); if ($res == Aerospike::OK) { echo success(); } else { echo standard_fail($db); } $write_fails = 0; $writes = 1; $reads = 0; $read_fails = 0;
$app->get('/persistent', function () { $config = array("hosts" => array(array("addr" => "127.0.0.1", "port" => "3000"))); $db = new Aerospike($config); if (!$db->isConnected()) { echo "Aerospike failed to connect[{$db->errorno()}]: {$db->error()}\n"; exit(1); } else { echo "Connected to the Aerospike Database"; echo "<h1>Welcome!</h1>"; } }); $app->get('/nonpersistent', function () { $config = array("hosts" => array(array("addr" => "127.0.0.1", "port" => "3000"))); $db = new Aerospike($config, false); if (!$db->isConnected()) { echo "Aerospike failed to connect[{$db->errorno()}]: {$db->error()}\n"; exit(1); } else { echo "Connected to the Aerospike Database"; echo "<h1>Welcome!</h1>"; } }); $app->post('/put', function () use($db) { if ($db->isConnected()) { $key = $db->initKey("test", "demo", "key1"); $record = array("name" => "John", "age" => 32); $status = $db->put($key, $record); if ($status != Aerospike::OK) { echo "Single put failed"; } else { echo "Record has been successfully written to the Database\n";
* public static void Aerospike::setSerializer ( callback $serialize_cb ) * where callback method must follow the signature: * public function string aerodb_serialize ( mixed $value ) * SET DESERIALIZER USAGE: * public static void Aerospike::setDeserializer ( callback $unserialize_cb ) * where callback method must follow the signature: * public function string aerodb_deserialize ( mixed $value ) *---------------------------------------------------------------------------------------------------------------------------- */ /* * EXAMPLE 1: SET LOG LEVEL AND LOG HANDLER CALLBACK */ $config = array("hosts" => array(array("addr" => $HOST_ADDR, "port" => $HOST_PORT))); $db = new Aerospike($config, 'prod-db'); if (!$db->isConnected()) { echo "Aerospike failed to connect to host {$HOST_ADDR}:{$HOST_PORT} [{$db->errorno()}]: {$db->error()}\n"; $db->close(); exit(1); } else { echo "Aerospike connection to host {$HOST_ADDR}:{$HOST_PORT} successful\n"; $db->setLogLevel(Aerospike::LOG_LEVEL_DEBUG); $db->setLogHandler(function ($level, $file, $function, $line) { switch ($level) { case Aerospike::LOG_LEVEL_ERROR: $lvl_str = 'ERROR'; break; case Aerospike::LOG_LEVEL_WARN: $lvl_str = 'WARN'; break; case Aerospike::LOG_LEVEL_INFO: $lvl_str = 'INFO';