1. Home
  2. About
  3. Blog
  4. Keyspace
    1. Documentation
    2. Whitepapers
    3. Downloads
    4. License
    5. Help
    6. FAQ

Navigation

  • next
  • previous |
  • home »

11. PHP API¶

11.1. Installing from source on Linux and other UNIX platforms¶

You will need the php-config program to make the PHP libraries. To check if you have it, type:

$ which php-config

If not, you must install the PHP dev packages. On Debian, type:

$ sudo apt-get install php5-dev

On Redhat-like systems, type:

$ sudo yum install php-devel

Note that on some systems, the name of the package may have the PHP version number appended. In this case you can specify it as an argument to make eg. make phplib PHP_CONFIG=php5-config.

Once you have verified you have php-config installed, make the PHP client libraries:

$ make phplib

in the Keyspace folder. This will create the files keyspace_client.so, keyspace_client.php and keyspace.php in bin/php. Copy these to your PHP project, and you are ready to use Keyspace!

11.2. Installing from source on Windows¶

Currently not supported.

11.3. Return values¶

All Keyspace functions return NULL on failure.

11.4. Connecting to the Keyspace cluster¶

First, import the keyspace client library:

@include_once("keyspace.php");

Then, create a client object by specifying the Keyspace cluster:

$client = new KeyspaceClient(array("192.168.1.50:7080",
                                   "192.168.1.51:7080",
                                   "192.168.1.52:7080"));

11.5. Setting timeout values¶

Next, if you would like to, change the global timeout. The global timeout specifies the maximum time, in msec, that a Keyspace client call will block your application. The default is 120 seconds:

$client->setGlobalTimeout(120*1000)

Next, if you would like to, change the master timeout. The master timeout specifies the maximum time, in msec, that the library will spend trying to find the master node. The default is 21 seconds:

$client->setMasterTimeout(21*1000)

At this point, you are ready to start issuing commands.

11.6. Issuing single write commands¶

The Keyspace write commands are: set, testAndSet, rename, add, delete, remove, prune and key expiry commands. Note that all Keyspace keys and values do not have to be NULL-terminated strings (eg. you can set a value to be a binary file).

11.6.1. set command¶

The set command sets a key => value pair, creating a new pair if key did not previously exist, overwriting the old value if it did:

$client->set("key", "value");

11.6.2. testAndSet command¶

The testAndSet command conditionally and atomically sets a key => value pair, but only if the current value matches the user specified value test:

$client->testAndSet("key", "test", "value");

11.6.3. rename command¶

The rename command atomically renames a key, leaving its value alone:

$client->rename("from", "to");

If the database looked like from => value at the beginning, then it changed to to => value after the successfull rename operation.

11.6.4. add command¶

The add command takes the value of the key, parses it as a number and atomically increments it by the given offset:

$client->set("key", "10");
$result = $client->add("key", 3); // returns 13

If the database looked like key => 10 at the beginning, then it changed to key => 13 after the successfull add operation and the variable result holds the value 13.

11.6.5. delete command¶

The delete command deletes a key => value pair by its key:

$client->delete("key")

11.6.6. remove command¶

The remove command deletes a key => value pair by its key and returns the old value:

$client->set("key", "value");
$client->remove("key"); // returns "value"

11.6.7. prune command¶

The prune command deletes all key => value pairs where the key starts with the given prefix:

$client->prune("prefix");

For example:

$client->set("john", "john_data");
$client->set("jane", "jane_data");
$client->set("mark", "mark_data");
$client->prune("j"); // deletes "john" => "john_data" and "jane" => "jane_data"

11.7. Issuing key expiry commands¶

11.7.1. setExpiry command¶

The setExpiry sets an expiry on the key key to occur in t seconds. The command will succeed and set the expiry irrespective of whether the key exists. If the key is created in the meantime, it will be expired when the timeout occurs. The command replaces any active expiry on the key:

$client->setExpiry("key", 60);

Key will be deleted in 60 seconds.

11.7.2. removeExpiry command¶

Removes any outstanding expiry on the key. The command will succeed irrespective of whether an expiry is set for the key:

$client->removeExpiry("key")

11.7.3. clearExpiries command¶

Clears all expiries in the database:

$client->clearExpiries()

11.8. Issuing single read commands¶

The only Keyspace single read command is get.

11.8.1. get command¶

The get command retrieves a single value from the Keyspace cluster:

$client->set("key", "value");
$client->get("key"); // returns "value"

You can also issue the identical dirtyGet command, which will be serviced by all nodes, not just the master:

$client->set("key", "value");
$client->dirtyGet("key"); // may return "value"

11.9. Issuing list commands¶

There are two list commands: listKeys and listKeyValues and one count command, all have the same set of parameters.

A list operation retrieves all keys from the Keyspace cluster which start with a given prefix. Optionally:

  • listing can start at a specified startKey
  • the maximum number of keys to return can be specified with the count parameter
  • listing can proceed forward or backward
  • listing can skip the first key

List type functions take an associative array as their arguemnts, which can contain the following parameters: prefix, start_key, count, skip, forward.

The default values are:

"prefix" => ""

"start_key" => ""

"count" => 0 // no limit

"skip" => false

"forward" => true

11.9.1. listKeys command¶

The signature of the function is:

public function listKeys($params) /* returns an array */

The result of a list operation is a standard array:

$client->set("/user:mtrencseni", "mtrencseni_data");
$client->set("/user:agazso",     "agazso_data");
$client->listKeys(array("prefix" => "/user:"));
// array("/user:agazso", "/user:mtrencseni")

You can also issue the identical dirtyListKeys command, which will be serviced by all nodes, not just the master.

11.9.2. listKeyValues command¶

The listKeyValues command in nearly identical to listKeys, except it also returns the values.

The listKeyValues command retrieves all keys and values from the Keyspace cluster which start with a given prefix. The signature of the function is:

public function listKeys($params) /* returns an associative array */

The result of a list operation is a standard array:

$client->set("/user:mtrencseni", "mtrencseni_data");
$client->set("/user:agazso",     "agazso_data");
$client->listKeyValues(array("prefix" => "/user:"));
// array("/user:mtrencseni" => "mtrencseni_data",
//       "/user:agazso"     => "agazso_data")

You can also issue the identical dirtyListKeyValues command, which will be serviced by all nodes, not just the master.

11.9.3. count command¶

The count command has the same parameters as listKeys or listKeyValues, but returns the number of keys (or key-value pairs) that they would return. The signature of the function is:

public function count($params) /* returns int */

$client->count(array("prefix" => "/user:"));

You can also issue the identical dirtyCount command, which will be serviced by all nodes, not just the master.

11.10. Issuing batched write commands¶

For maximum thruput performance, it is possible to issue many write commands together; this is called batched writing. It will be faster then issuing single write commands because

  1. The Keyspace cluster will replicate them together
  2. The client library will not wait for the previous’ write commands response before send the next write command (saves rount-trip times).

In practice batched set can achieve 5-10x higher throughput than single set.

To send batched write commands, first call begin() function, then issue the write commands, and finally call submit(). The commands are sent on submit():

$client->begin();
$client->set("a1", "a1_value");
$client->set("a2", "a2_value");
...
$client->set("a99", "a99_value");
$client->submit(); // commands are sent in batch

11.11. Issuing batched read commands¶

It is possible to issue get read commands in a batched fashion. Since get commands are not replicated, only the round-trip time is saved. Nevertheless, batched get can achieve 3-5x higher throughput than single get.

To send batched get commands, first call begin() function, then issue the get commands, and finally call submit(). The commands are sent on submit(). After the commands complete, the results are returned as an associative array

$client->set("/user:mtrencseni", "mtrencseni_data");
$client->set("/user:agazso",     "agazso_data");
$client->begin();
$client->get("/user:mtrencseni");
$client->get("/user:agazso");
$client->submit();

// fetch result
$client->result->keyValues();
// array("/user:mtrencseni" => "mtrencseni_data",
//       "/user:agazso"     => "agazso_data")

11.12. Understanding Keyspace status codes¶

Keyspace exposes a rich set of status codes through the client library. These are especially useful for batched operations. After issuing command(s), there are four types of status codes which give information about the state of the Keyspace cluster.

To print the constant name of the status, use:

KeyspaceClient :: statusToString($status) /* returns string */

11.12.1. transportStatus code¶

transportStatus tells the application the portion of commands that were sent to the Keyspace cluster:

KEYSPACE_SUCCESS: all commands were sent
KEYSPACE_PARTIAL: only a portion of the commands
                  could be sent before a timeout occured
KEYSPACE_FAILURE: no commands could be sent

To retrieve the transportStatus, use:

$status = $client->result->transportStatus()
print(KeyspaceClient::statusToString($status))

11.12.2. connectivityStatus code¶

connectivityStatus tells the application the network conditions between the client and the Keyspace cluster:

KEYSPACE_SUCCESS:      the master could be found
KEYSPACE_NOMASTER:     some nodes were reachable,
                       but there was no master or it went down
KEYSPACE_NOCONNECTION: the entire grid was unreachable within timeouts

To retrieve the connectivityStatus, use:

$status = $client->result->connectivityStatus()
print(KeyspaceClient::statusToString($status))

11.12.3. timeoutStatus code¶

timeoutStatus tells the application what timeouts occured, if any:

KEYSPACE_SUCCESS:        no timeout occured, everything went fine
KEYSPACE_MASTER_TIMEOUT: a master could not be found
                         within the master timeout
KEYSPACE_GLOBAL_TIMEOUT: the blocking client library call
                         returned because the global timeout
                         has expired

To retrieve the timeoutStatus, use:

$status = $client->result->timeoutStatus()
print(KeyspaceClient::statusToString($status))

11.12.4. commandStatus code¶

commandStatus is the actual return value of a command:

KEYSPACE_SUCCESS:   command succeeded
KEYSPACE_FAILED:    the command was executed, but
                    its return value was FAILED;
                    eg. can happen for test_and_set if the test value
                    does not match or for get if the key does not exist
KEYSPACE_NOSERVICE: the command was not executed

When using single or batched commands, retrieve the commandStatus like:

$status = $client->result->commandStatus()
print(KeyspaceClient::statusToString($status))

11.13. Header files¶

Check out src/Application/Keyspace/Client/PHP/keyspace.php for a full reference!

Table Of Contents

  • 11. PHP API
    • 11.1. Installing from source on Linux and other UNIX platforms
    • 11.2. Installing from source on Windows
    • 11.3. Return values
    • 11.4. Connecting to the Keyspace cluster
    • 11.5. Setting timeout values
    • 11.6. Issuing single write commands
      • 11.6.1. set command
      • 11.6.2. testAndSet command
      • 11.6.3. rename command
      • 11.6.4. add command
      • 11.6.5. delete command
      • 11.6.6. remove command
      • 11.6.7. prune command
    • 11.7. Issuing key expiry commands
      • 11.7.1. setExpiry command
      • 11.7.2. removeExpiry command
      • 11.7.3. clearExpiries command
    • 11.8. Issuing single read commands
      • 11.8.1. get command
    • 11.9. Issuing list commands
      • 11.9.1. listKeys command
      • 11.9.2. listKeyValues command
      • 11.9.3. count command
    • 11.10. Issuing batched write commands
    • 11.11. Issuing batched read commands
    • 11.12. Understanding Keyspace status codes
      • 11.12.1. transportStatus code
      • 11.12.2. connectivityStatus code
      • 11.12.3. timeoutStatus code
      • 11.12.4. commandStatus code
    • 11.13. Header files

Previous topic

10. C API

Next topic

12. Ruby API

Quick search

Enter search terms or a module, class or function name.

Navigation

  • next
  • previous |
  • home »
Who are we?


Scalien is a startup developing open-source, cutting-edge distributed systems.
More

info@scalien.com

Follow us on Twitter
Copyright © Scalien, 2009-2010. All rights reserved --- Icons courtesy of dryicons.com