Example #1
0
 /**
  * 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;
 }
 /**
  * This method is called after the last test of this test class is run.
  */
 public static function tearDownAfterClass()
 {
     if (self::$aerospike->isConnected()) {
         foreach (self::$keys as $key) {
             // TODO: check existence of $key in Aerospike.
             self::$aerospike->remove($key);
         }
         self::$aerospike->close();
     }
     parent::tearDownAfterClass();
 }
Example #3
0
 /**
  * @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;
 }
    $options = getopt($shortopts, $longopts);
    return $options;
}
$args = parse_args();
if (isset($args["help"])) {
    echo "php llist.php [-hHOST] [-pPORT] [-a] [-c]\n";
    echo " or\n";
    echo "php llist.php [--host=HOST] [--port=PORT] [--annotate] [--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("Adding a record to test.shows with PK='futurama' ≻", 'black', true);
$start = __LINE__;
$key = $db->initKey("test", "shows", "futurama");
$futurama = array("channel" => array("Fox" => array(1999, 2000, 2001, 2002), "Comedy Central" => array(2008, 2009, 2010, 2011, 2012)), "creator" => array("Matt Groening", "David X. Cohen"), "show" => "Futurama");
$options = array(Aerospike::OPT_POLICY_KEY => Aerospike::POLICY_KEY_SEND);
$status = $db->put($key, $futurama, 0, $options);
if ($status === Aerospike::OK) {
    return $options;
}
$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;
    $options = getopt($shortopts, $longopts);
    return $options;
}
$args = parse_args();
if (isset($args["help"])) {
    echo "php batch-operations.php [-hHOST] [-pPORT] [-a] [-c]\n";
    echo " or\n";
    echo "php batch-operations.php [--host=HOST] [--port=PORT] [--annotate] [--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("Adding characters with PK=1-4 ≻", 'black', true);
$start = __LINE__;
$characters = array(1 => "Philip J. Fry", 2 => "Tarunga Leela", 3 => "Bender Bending Rodriguez", 4 => "Professor Hubert J. Farnsworth");
foreach ($characters as $id => $character) {
    $new_key = "key{$id}";
    var_dump($new_key);
    ${$new_key} = $db->initKey("test", "characters", $id);
    }
    if (strpos($response2, 'cluster_size=2') === false) {
        return false;
    }
    $start1 = strpos($response1, 'cluster_key=') + 12;
    $end1 = strpos($response1, ';', $start1);
    $len1 = $end1 - $start1;
    $cluster_key1 = substr($response1, $start1, $len1);
    $start2 = strpos($response2, 'cluster_key=') + 12;
    $end2 = strpos($response2, ';', $start2);
    $len2 = $end2 - $start2;
    $cluster_key2 = substr($response2, $start2, $len2);
    return $cluster_key1 == $cluster_key2;
}
$config = array("hosts" => array(array("addr" => AEROSPIKE_CONFIG_NAME, "port" => AEROSPIKE_CONFIG_PORT)));
$db = new Aerospike($config, false);
if (defined('AEROSPIKE_CONFIG_NAME2') && defined('AEROSPIKE_CONFIG_PORT2')) {
    $config['hosts'][] = array("addr" => AEROSPIKE_CONFIG_NAME2, "port" => AEROSPIKE_CONFIG_PORT2);
}
echo "Waiting on the Aerospike cluster..";
$keep_waiting = true;
while ($keep_waiting) {
    $host1_status = $db->info("statistics", $host1_response, $config['hosts'][0]);
    $host2_status = $db->info("statistics", $host2_response, $config['hosts'][1]);
    if ($host1_status === Aerospike::OK && $host2_status === Aerospike::OK) {
        if (servers_present($host1_response, $host2_response)) {
            $keep_waiting = false;
            break;
        }
    }
    echo ".";
    $options = getopt($shortopts, $longopts);
    return $options;
}
$args = parse_args();
if (isset($args["help"])) {
    echo "php rec-operations.php [-hHOST] [-pPORT] [-a] [-c]\n";
    echo " or\n";
    echo "php rec-operations.php [--host=HOST] [--port=PORT] [--annotate] [--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 Aerospike ≻", '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("Creating a new record in test.characters at PK=1, only if none exists ≻", 'black', true);
$start = __LINE__;
$needs_force = false;
$key = $db->initKey("test", "characters", 1);
$put_vals = array("title" => "Professor", "name" => "Hubert J. Farnsworth", "age" => 150, "is_senior" => true, "positions" => array("Inventor", "Founder", "Lecturer", "Scientist", "Hyper Yeti"));
$status = $db->put($key, $put_vals, 300, array(Aerospike::OPT_POLICY_EXISTS => Aerospike::POLICY_EXISTS_CREATE));
if ($status == Aerospike::OK) {
Example #9
0
    /* Optionally clean up before leaving */
    $longopts = array("host::", "port::", "clean", "annotate", "help");
    $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("Using info() to get a list of bins in the test namespace ≻", 'black', true);
$start = __LINE__;
$status = $db->info("bins/test", $response);
if ($status != AEROSPIKE::OK) {
    echo standard_fail($db);
} else {
    echo success();
$args = parse_args();
if (isset($args["help"])) {
    echo "hhvm rw-worker.php [-hHOST] [-pPORT] [-wWRITE EVERY Nth RECORD] [-nRECORDS] LOGFILE\n";
    echo " or\n";
    echo "hhvm rw-worker.php [--host=HOST] [--port=PORT] [--write-every=WRITE EVERY Nth RECORD] [--num-ops=RECORDS] LOGFILE\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);
$log = $args["_args"][0];
$pid = getmypid();
echo "rw-worker.php launched with process ID {$pid}\n";
$config = array("hosts" => array(array("addr" => $addr, "port" => $port)));
$db = new Aerospike($config, false);
if (!$db->isConnected()) {
    file_put_contents($log, "{$pid}|0|0|0|0|0|0|0\n", FILE_APPEND);
    exit(1);
}
$key = $db->initKey("test", "rw-performance", $pid);
$writes = 0;
$write_fails = 0;
$reads = 0;
$read_fails = 0;
$begin = microtime(true);
$kv = array('v' => 0);
$res = $db->put($key, $kv);
if ($res !== Aerospike::OK) {
    $end = microtime(true);
    $delta = $end - $begin;
    /* Optionally clean up before leaving */
    $longopts = array("host::", "port::", "clean", "annotate", "help");
    $options = getopt($shortopts, $longopts);
    return $options;
}
$args = parse_args();
if (isset($args["help"])) {
    echo "php simple.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("Registering the UDF module ≻", 'black', true);
$start = __LINE__;
$copied = copy(__DIR__ . '/lua/example_aggregate_udf.lua', ini_get('aerospike.udf.lua_user_path') . '/example_aggregate_udf.lua');
if (!$copied) {
    echo fail("Could not copy the local lua/example_aggregate_udf.lua to " . ini_get('aerospike.udf.lua_user_path'));
}
$status = $db->register(ini_get('aerospike.udf.lua_user_path') . '/example_aggregate_udf.lua', "example_aggregate_udf.lua");
    $options = getopt($shortopts, $longopts);
    return $options;
}
$args = parse_args();
if (isset($args["help"])) {
    echo "php bin-operations.php [-hHOST] [-pPORT] [-a] [-c]\n";
    echo " or\n";
    echo "php bin-operations.php [--host=HOST] [--port=PORT] [--annotate] [--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)), "user" => "admin", "pass" => "admin");
$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("Creating a user ≻", 'black', true);
$start = __LINE__;
$status = $db->createUser("example_user", "example_pass", array("read"));
sleep(1);
if ($status == Aerospike::OK) {
    echo success();
} else {
Example #13
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";
Example #14
0
    /* Optionally clean up before leaving */
    $longopts = array("host::", "port::", "clean", "annotate", "help");
    $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();
Example #15
0
    $options = getopt($shortopts, $longopts);
    return $options;
}
$args = parse_args();
if (isset($args["help"])) {
    echo "php llist.php [-hHOST] [-pPORT] [-a] [-c]\n";
    echo " or\n";
    echo "php llist.php [--host=HOST] [--port=PORT] [--annotate] [--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("Adding a record to test.vehicles with PK=M4G000 ≻", 'black', true);
$start = __LINE__;
$key = $db->initKey("test", "vehicles", "M4G000");
$vehicle = array("make" => "Mazda", "model" => "CX-3", "year" => 2014, "color" => "red");
$options = array(Aerospike::OPT_POLICY_KEY => Aerospike::POLICY_KEY_SEND);
$status = $db->put($key, $vehicle, 0, $options);
if ($status === Aerospike::OK) {
    exit(1);
}
$HOST_ADDR = isset($args["h"]) ? (string) $args["h"] : (isset($args["host"]) ? (string) $args["host"] : "127.0.0.1");
$HOST_PORT = isset($args["p"]) ? (int) $args["p"] : (isset($args["port"]) ? (string) $args["port"] : 3000);
if (isset($args['a']) || isset($args['annotate'])) {
    $annotate = true;
} else {
    $annotate = false;
}
echo colorize("***** Welcome to Aerospike Developer Training *****\n", 'blue', true);
echo colorize("Connecting to Aerospike cluster ≻", 'black', true);
if ($annotate) {
    display_code(__FILE__, __LINE__, 7);
}
$config = array("hosts" => array(array("addr" => $HOST_ADDR, "port" => $HOST_PORT)));
$client = new Aerospike($config, false);
if (!$client->isConnected()) {
    echo standard_fail($client);
    echo colorize("Connection to Aerospike cluster failed! Please check the server settings and try again!\n", 'red', true);
    exit(2);
}
echo success();
$selection = show_menu();
if ($selection === 0) {
    $client->close();
    exit(0);
}
$user_service = new \Aerospike\Training\UserService($client, $config, $annotate);
$tweet_service = new \Aerospike\Training\TweetService($client, $config, $annotate);
try {
    switch ($selection) {
 *              public function log_handler ( int $level, string $file, string $function, int $line )
 * SET SERIALIZER USAGE:
 *      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;