Created
August 24, 2015 12:15
-
-
Save petitchevalroux/9c83991972d0efd5083e to your computer and use it in GitHub Desktop.
Arangodb with ODM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace BenchmarkNosql\Libraries\Storage; | |
use triagens\ArangoDb\Connection; | |
use triagens\ArangoDb\ConnectionOptions; | |
use triagens\ArangoDb\UpdatePolicy; | |
use triagens\ArangoDb\Collection; | |
use triagens\ArangoDb\CollectionHandler; | |
use triagens\ArangoDb\DocumentHandler; | |
use triagens\ArangoDb\Document; | |
use Exception; | |
class Arangodb | |
{ | |
private $host = ''; | |
private $port = '8529'; | |
const COLLECTION_NAME = 'test'; | |
public function setConfig($options) | |
{ | |
foreach ($options as $optionName => $optionValue) { | |
$this->{$optionName} = $optionValue; | |
} | |
} | |
protected function getConnection() | |
{ | |
if (empty($this->connection)) { | |
$connectionOptions = array( | |
// server endpoint to connect to | |
ConnectionOptions::OPTION_ENDPOINT => 'tcp://'.$this->host.':'.$this->port, | |
// authorization type to use (currently supported: 'Basic') | |
ConnectionOptions::OPTION_AUTH_TYPE => 'Basic', | |
// user for basic authorization | |
ConnectionOptions::OPTION_AUTH_USER => $this->user, | |
// password for basic authorization | |
ConnectionOptions::OPTION_AUTH_PASSWD => $this->password, | |
// connection persistence on server. can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections) | |
ConnectionOptions::OPTION_CONNECTION => 'Keep-Alive', | |
// connect timeout in seconds | |
ConnectionOptions::OPTION_TIMEOUT => 1, | |
// whether or not to reconnect when a keep-alive connection has timed out on server | |
ConnectionOptions::OPTION_RECONNECT => true, | |
// optionally create new collections when inserting documents | |
ConnectionOptions::OPTION_CREATE => true, | |
// optionally create new collections when inserting documents | |
ConnectionOptions::OPTION_UPDATE_POLICY => UpdatePolicy::LAST, | |
); | |
// open connection | |
$this->connection = new Connection($connectionOptions); | |
} | |
return $this->connection; | |
} | |
private function getDocumentHandler() | |
{ | |
if (empty($this->documentHandler)) { | |
$connection = $this->getConnection(); | |
$this->documentHandler = new DocumentHandler($connection); | |
} | |
return $this->documentHandler; | |
} | |
private function getCollectionHandler() | |
{ | |
if (empty($this->collectionHandler)) { | |
$connection = $this->getConnection(); | |
$this->collectionHandler = new CollectionHandler($connection); | |
} | |
return $this->collectionHandler; | |
} | |
public function reset() | |
{ | |
$collectionHandler = $this->getCollectionHandler(); | |
if ($collectionHandler->has(static::COLLECTION_NAME)) { | |
$collectionHandler->drop(static::COLLECTION_NAME); | |
} | |
$collection = new Collection(static::COLLECTION_NAME); | |
$collectionHandler->create($collection); | |
} | |
public function insert($set) | |
{ | |
$documentHandler = $this->getDocumentHandler(); | |
foreach ($set as $doc) { | |
$document = new Document(); | |
$id = $doc['id']; | |
unset($doc['id']); | |
foreach ($doc as $property => $value) { | |
$document->set($property, $value); | |
} | |
$document->setInternalKey($id); | |
try { | |
$documentHandler->save(static::COLLECTION_NAME, $document); | |
} catch (\Exception $e) { | |
throw new \Exception('document: '.json_encode($doc), 0, $e); | |
} | |
} | |
} | |
public function read($set) | |
{ | |
$documentHandler = $this->getDocumentHandler(); | |
foreach ($set as $doc) { | |
$r = $documentHandler->get(static::COLLECTION_NAME, $doc['id'])->getAll(); | |
if ($doc['id'] != $r['_key']) { | |
throw new Exception('Doc id differ from result id'); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace BenchmarkNosql\Libraries\Storage; | |
use Exception; | |
class ArangodbCurl | |
{ | |
private $host = ''; | |
private $port = '8529'; | |
const COLLECTION_NAME = 'test'; | |
public function setConfig($options) | |
{ | |
foreach ($options as $optionName => $optionValue) { | |
$this->{$optionName} = $optionValue; | |
} | |
} | |
public function reset() | |
{ | |
$ch = curl_init(); | |
// On check si la collection existe | |
$this->get($ch, $this->getUrl('/_api/collection/'.self::COLLECTION_NAME)); | |
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 404) { | |
$ch = curl_init(); | |
$this->delete($ch, $this->getUrl('/_api/collection/').self::COLLECTION_NAME); | |
} | |
$this->post($ch, $this->getUrl('/_api/collection'), json_encode(['name' => static::COLLECTION_NAME])); | |
curl_close($ch); | |
} | |
public function insert($set) | |
{ | |
$ch = curl_init(); | |
foreach ($set as $doc) { | |
$doc['_key'] = (string) $doc['id']; | |
unset($doc['id']); | |
$this->post($ch, $this->getUrl('/_api/document?collection='.self::COLLECTION_NAME), json_encode($doc)); | |
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 202) { | |
throw new \Exception('Error while creating document '.json_encode(curl_getinfo($ch))); | |
} | |
} | |
curl_close($ch); | |
} | |
public function read($set) | |
{ | |
$ch = curl_init(); | |
foreach ($set as $doc) { | |
$r = $this->get($ch, $this->getUrl('/_api/document/'.self::COLLECTION_NAME.'/'.$doc['id'])); | |
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) { | |
throw new \Exception('Error while reading document '.json_encode(curl_getinfo($ch))); | |
} | |
$r = json_decode($r, true); | |
if ($doc['id'] != $r['_key']) { | |
throw new Exception('Doc id differ from result id'); | |
} | |
} | |
curl_close($ch); | |
} | |
private function post($ch, $url, $body) | |
{ | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
'Content-type: application/json', | |
'Accept: */*', | |
]); | |
return curl_exec($ch); | |
} | |
private function delete($ch, $url) | |
{ | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
return curl_exec($ch); | |
} | |
private function get($ch, $url) | |
{ | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-type: application/json', | |
'Accept: */*', | |
)); | |
return curl_exec($ch); | |
} | |
private function getUrl($path) | |
{ | |
return 'http://root:qmcHwhHNPjfnOM1y@'.$this->host.':'.$this->port.$path; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment