/** * Constructs an instance of the Response class. * @param CPS_Connection &$connection CPS connection object * @param CPS_Request &$request The original request that the response is to * @param string &$responseXml The raw XML response * @param bool $noCdata set this to TRUE if you want cdata returned as text */ public function __construct(CPS_Connection &$connection, CPS_Request &$request, &$responseXml, $noCdata = FALSE) { try { $this->_simpleXml = @new SimpleXMLElement($responseXml, ($noCdata ? LIBXML_NOCDATA : 0) | LIBXML_COMPACT | LIBXML_PARSEHUGE); } catch (Exception $e) { throw new CPS_Exception(array(array('long_message' => 'Invalid response XML', 'code' => ERROR_CODE_INVALID_RESPONSE, 'level' => 'ERROR', 'source' => 'CPS_API')), $e); } $cpsChildren = $this->_simpleXml->children('www.clusterpoint.com'); if (count($cpsChildren) == 0) { throw new CPS_Exception(array(array('long_message' => 'Invalid response XML', 'code' => ERROR_CODE_INVALID_RESPONSE, 'level' => 'ERROR', 'source' => 'CPS_API'))); } $this->_textParamNames = array('hits', 'from', 'to', 'found', 'real_query', 'iterator_id', 'more', 'cursor_id', 'cursor_data'); $this->_connection = $connection; $this->_storageName = (string) $cpsChildren->storage; $this->_command = (string) $cpsChildren->command; $this->_seconds = (double) $cpsChildren->seconds; $this->_errors = $cpsChildren->error; $errors = array(); foreach ($this->_errors as $error) { $error = $error->children(); if ($error->level == 'REJECTED' || $error->level == 'FAILED' || $error->level == 'ERROR' || $error->level == 'FATAL') { $errorArray = array('long_message' => (string) $error->message, 'short_message' => (string) $error->text, 'code' => (int) $error->code, 'source' => (string) $error->source, 'level' => (string) $error->level); if (isset($error->document_id)) { $errorArray['document_id'] = (string) $error->document_id; // this line = backwards compatibility $errorArray['document_ids'] = array(); foreach ($error->document_id as $errDocId) { $errorArray['document_ids'][] = (string) $errDocId; } } $errors[] = $errorArray; } } if (count($errors) > 0) { throw new CPS_Exception($errors); } $this->_documents = array(); $this->_facets = array(); $this->_aggregate = array(); $this->_textParams = array(); $this->_words = array(); $this->_wordCounts = array(); $this->_contentArray = array(); $this->_paths = array(); if (isset($cpsChildren->content)) { $cpsContent = $cpsChildren->content->children(); $docs = NULL; $saveDocs = true; // extracting documents / document IDs $idPath = $connection->getDocumentIdXpath(); switch ($this->_command) { case 'update': case 'insert': $this->_contentArray = CPS_Response::simpleXmlToArray($cpsContent); case 'delete': case 'partial-replace': case 'partial-xreplace': case 'replace': $docs = $cpsContent; $idPath[0] = 'document'; // TODO: Jasalabo datubazes puse lai atgriez korekti // TODO: Jasalabo datubazes puse lai atgriez korekti case 'lookup': if (!isset($cpsContent->results)) { $docs = $cpsContent; } if ($this->_command != 'lookup') { $saveDocs = false; } case 'search': case 'retrieve': case 'retrieve-last': case 'retrieve-first': case 'list-last': case 'list-first': case 'similar': case 'show-history': case 'cursor-next-batch': if (is_null($docs)) { if (isset($cpsContent->results)) { $docs = $cpsContent->results[0]; } else { $docs = array(); } } $this->_rawResults = $docs; $curpos = 0; foreach ($docs as $rootTag => $doc) { if ($rootTag == $idPath[0]) { if ($this->_command == 'show-history') { $id = (string) $doc->children('www.clusterpoint.com')->revision_id; } else { $id = ''; $cur = $doc; for ($i = 1; $i < count($idPath); ++$i) { $path = $idPath[$i]; if (!isset($cur->{$path})) { break; } if ($i == count($idPath) - 1) { $id = (string) $cur->{$path}; } else { $cur = $cur->{$path}; } } } if (strlen($id) > 0) { $index = $id; } else { $index = $curpos; } $this->_documents[$index] = $saveDocs ? $doc : NULL; } ++$curpos; } break; case 'alternatives': if (isset($cpsContent->alternatives_list)) { foreach ($cpsContent->alternatives_list->alternatives as $alt) { $alts = array(); foreach ($alt->word as $word) { $alts[(string) $word] = array('h' => (string) $word['h'], 'idif' => (string) $word['idif'], 'cr' => (string) $word['cr'], 'count' => (string) $word['count']); } $this->_words[(string) $alt->to] = $alts; $this->_wordCounts[(string) $alt->to] = intval($alt->count); } } break; case 'list-words': if (isset($cpsContent->list)) { foreach ($cpsContent->list as $list) { $words = array(); foreach ($list->word as $word) { $words[(string) $word] = (string) $word['count']; } $this->_words[(string) $list['to']] = $words; } } break; case 'status': case 'list-backups': case 'make-backup': case 'restore': case 'list-paths': case 'list-databases': case 'list-collections': case 'create-database': case 'create-collection': case 'edit-database': case 'edit-collection': case 'edit-database-layout': case 'rename-database': case 'rename-collection': case 'drop-database': case 'drop-collection': case 'list-nodes': case 'list-hubs': case 'list-hosts': case 'set-offline': case 'set-online': case 'list-accounts': case 'list-users': case 'create-account': case 'create-user': case 'edit-user': case 'delete-user': case 'get-account-info': case 'get-user-info': case 'get-verification-code': case 'get-password-reset-code': case 'password-reset': case 'verify-user': case 'verify-account': case 'login': case 'list-alerts': case 'reset-hmac-keys': case 'get-hmac-keys': case 'delete-operation': $this->_contentArray = CPS_Response::simpleXmlToArray($cpsContent); break; case 'describe-database': case 'describe-collection': $this->_contentArray = CPS_Response::simpleXmlToArray($cpsContent); $rawDataModelXML = substr($responseXml, strpos($responseXml, '<overrides>') + 11, strpos($responseXml, '</overrides>') - strpos($responseXml, '<overrides>') - 11); $this->_contentArray['overrides'] = $rawDataModelXML; break; case 'begin-transaction': $connection->setTransactionId((string) $cpsContent->transaction_id); $this->_contentArray = CPS_Response::simpleXmlToArray($cpsContent); break; case 'commit-transaction': case 'rollback-transaction': $connection->setTransactionId(null); $this->_contentArray = CPS_Response::simpleXmlToArray($cpsContent); break; default: // no special parsing } if ($this->_command == 'search' || $this->_command == 'list-facets') { if (isset($cpsContent->facet)) { foreach ($cpsContent->facet as $facet) { $path = (string) $facet['path']; $terms = array(); foreach ($facet->term as $term) { if ($this->_command == 'search') { $terms[(string) $term] = intval($term['hits']); } else { if ($this->_command == 'list-facets') { $terms[] = (string) $term; } } } $this->_facets[$path] = $terms; } } if (isset($cpsContent->aggregate)) { foreach ($cpsContent->aggregate as $aggr) { $this->_aggregate[(string) $aggr->query] = $aggr->data; } } } $this->_textParams = array(); foreach ($cpsContent as $name => $value) { if (in_array($name, $this->_textParamNames)) { $this->_textParams[$name] = (string) $value; } } } }
<?php // includes require_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // $cpsConnection->setDebug(1); //=========== adding a new subdocument $subdocument = array('person' => array('name' => 'Josh', 'surname' => 'Smith')); $ids = array('id1', 'id2'); // Perform additions $prxRequest = new CPS_PartialXRequest($ids, new CPS_PRX_Operation('/document/persons', 'append_children', $subdocument)); $response = $cpsConnection->sendRequest($prxRequest); //=========== sending multiple changesets at once $subdocument = array('person' => array('name' => 'George', 'surname' => 'Smith')); $changeset1 = new CPS_PRX_Changeset('id1', new CPS_PRX_Operation('/document/persons', 'append_children', $subdocument)); $subdocument2 = array('birth_year' => '1960'); $changeset2 = new CPS_PRX_Changeset('id2', new CPS_PRX_Operation('/document/persons/person[name="Josh"]', 'merge_children', $subdocument2)); // Perform changes $prxRequest = new CPS_PartialXRequest(array($changeset1, $changeset2)); $response = $cpsConnection->sendRequest($prxRequest); } catch (CPS_Exception $e) { var_dump($e->errors()); exit; } ?>
<?php // includes include_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); $cpsConnection->setDebug(1); // Setting parameters // search for items with category == 'cars' and car_params/year >= 2010 $query = CPS_Term('cars', 'category') . CPS_Term('>=2010', 'car_params/year'); // return documents starting with the first one - offset 0 $offset = 0; // return not more than 5 documents $docs = 5; // return these fields from the documents $list = array('id' => 'yes', 'car_params/make' => 'yes', 'car_params/model' => 'yes', 'car_params/year' => 'yes'); $listXml = '<id>yes</id> <car_params><make>yes</make></car_params> <car_params><model listas="modelis">yes</model></car_params> <car_params><year>yes</year></car_params>'; // order by year, from largest to smallest $ordering = CPS_NumericOrdering('car_params/year', 'descending'); // Searching for documents // note that only the query parameter is mandatory - the rest are optional $searchRequest = new CPS_SearchRequest($query, $offset, $docs); $searchRequest->setParam('list', $listXml); $searchRequest->setOrdering($ordering); $searchResponse = $cpsConnection->sendRequest($searchRequest); if ($searchResponse->getHits() > 0) { // getHits returns the total number of documents in the storage that match the query echo 'Found ' . $searchResponse->getHits() . ' documents<br />'; echo 'showing from ' . $searchResponse->getFrom() . ' to ' . $searchResponse->getTo() . '<br />';
<?php // includes require_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // looking up 10 last documents - list only the name $listLastRequest = new CPS_ListLastRequest(array('document' => 'no', 'name' => 'yes'), 0, 10); $listLastResponse = $cpsConnection->sendRequest($listLastRequest); foreach ($listLastResponse->getDocuments() as $id => $document) { echo $document->name . '<br />'; } } catch (CPS_Exception $e) { var_dump($e->errors()); exit; }
<?php // includes include_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // Setting parameters // search for items with category == 'cars' and car_params/year >= 2010 $query = CPS_Term('cars', 'category') . CPS_Term('>=2010', 'car_params/year'); // return documents starting with the first one - offset 0 $offset = 0; // return not more than 5 documents $docs = 5; // return these fields from the documents $list = array('id' => 'yes', 'car_params/make' => 'yes', 'car_params/model' => 'yes', 'car_params/year' => 'yes'); // order by year, from largest to smallest $ordering = CPS_NumericOrdering('car_params/year', 'descending'); // Searching for documents // note that only the query parameter is mandatory - the rest are optional $searchRequest = new CPS_SearchRequest($query, $offset, $docs, $list); $searchRequest->setOrdering($ordering); $searchResponse = $cpsConnection->sendRequest($searchRequest); if ($searchResponse->getHits() > 0) { // getHits returns the total number of documents in the storage that match the query echo 'Found ' . $searchResponse->getHits() . ' documents<br />'; echo 'showing from ' . $searchResponse->getFrom() . ' to ' . $searchResponse->getTo() . '<br />'; foreach ($searchResponse->getDocuments() as $id => $document) { echo $document->car_params->make . ' ' . $document->car_params->model . '<br />'; echo 'first registration in ' . $document->car_params->year . '<br />';
<?php // includes require_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // Reindexing storage $reindexRequest = new CPS_Request('reindex'); $resp = $cpsConnection->sendRequest($reindexRequest); } catch (CPS_Exception $e) { var_dump($e->errors()); exit; } ?>
<?php // includes require_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // Looking up one document - listing name only $lookupRequest = new CPS_LookupRequest('id1', array('document' => 'no', 'name' => 'yes')); $lookupResponse = $cpsConnection->sendRequest($lookupRequest); foreach ($lookupResponse->getDocuments() as $id => $document) { echo $document->name . '<br />'; } // Looking up multiple documents - listing name only $lookupRequest = new CPS_LookupRequest(array('id2', 'id3'), array('document' => 'no', 'name' => 'yes')); $lookupResponse = $cpsConnection->sendRequest($lookupRequest); foreach ($lookupResponse->getDocuments() as $id => $document) { echo $document->name . '<br />'; } } catch (CPS_Exception $e) { var_dump($e->errors()); exit; }
<?php // includes require_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // Retrieving paths $listPathsRequest = new CPS_ListPathsRequest(); $listPathsResponse = $cpsConnection->sendRequest($listPathsRequest); $paths = $listPathsResponse->getPaths(); foreach ($paths as $path) { echo $path . '<br>'; } } catch (CPS_Exception $e) { var_dump($e->errors()); exit; }
<?php // includes require_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // Clearing storage $clearRequest = new CPS_Request('clear'); $cpsConnection->sendRequest($clearRequest); } catch (CPS_Exception $e) { var_dump($e->errors()); sleep(10); exit; } sleep(10);
<?php // includes require_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // Retrieving status information $statusRequest = new CPS_StatusRequest(); $statusResponse = $cpsConnection->sendRequest($statusRequest); $status = $statusResponse->getStatus(); foreach ($status['shard'] as $shard) { foreach ($shard['replica'] as $replica) { echo 'Status ' . $replica['status']['index']['status'] . ' <br>'; } } } catch (CPS_Exception $e) { var_dump($e->errors()); exit; } ?>
<?php // includes require_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // Retrieving one document $retrieveRequest = new CPS_RetrieveRequest('id1'); $retrieveResponse = $cpsConnection->sendRequest($retrieveRequest); foreach ($retrieveResponse->getDocuments() as $id => $document) { echo $document->category . '<br />'; } // Retrieving multiple documents $retrieveRequest = new CPS_RetrieveRequest(array('id2', 'id3')); $retrieveResponse = $cpsConnection->sendRequest($retrieveRequest); foreach ($retrieveResponse->getDocuments() as $id => $document) { echo $document->category . '<br />'; } } catch (CPS_Exception $e) { var_dump($e->errors()); exit; }
<?php // includes require_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // $cpsConnection->setDebug(1); // creating a new document $document = array('title' => 'Test document', 'body' => array('text' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a nisl magna.')); $document2 = array('title' => 'Document 2', 'body' => array('text' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a nisl magna.')); $document3 = array('title' => 'Document 3', 'body' => array('text' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a nisl magna.')); // Insert $insertRequest = new CPS_InsertRequest('id1', $document); $cpsConnection->sendRequest($insertRequest); $insertRequest = new CPS_InsertRequest(array('id2' => $document2, 'id3' => $document3)); $cpsConnection->sendRequest($insertRequest); // Update $document['title'] = 'changed title'; $updateRequest = new CPS_UpdateRequest('id1', $document); $cpsConnection->sendRequest($updateRequest); $updateRequest = new CPS_UpdateRequest(array('id1' => $document, 'id3' => $document3)); $cpsConnection->sendRequest($updateRequest); // Replace $document['title'] = 'changed title for replace'; $replaceRequest = new CPS_ReplaceRequest('id1', $document); $cpsConnection->sendRequest($replaceRequest); $replaceRequest = new CPS_ReplaceRequest(array('id1' => $document, 'id3' => $document3)); $cpsConnection->sendRequest($replaceRequest); // Partial Replace
<?php // includes require_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); // Deleting one $deleteRequest = new CPS_DeleteRequest('id1'); $cpsConnection->sendRequest($deleteRequest); // Deleting multiple $deleteRequest = new CPS_DeleteRequest(array('id2', 'id3')); $cpsConnection->sendRequest($deleteRequest); } catch (CPS_Exception $e) { var_dump($e->errors()); exit; }
require_once 'cps_simple.php'; try { define('DO_CPS', 1); define('DO_MYSQL', 0); define('DO_MONGO', 0); define('MONGO_SAFE', true); define('DELAYED_REQUESTS', false); define('DELAYED_REQUESTS_FILE', '/tmp/insert_input.data'); define('TOTAL_DOCS', 100000); define('ID_OFFSET', 1); define('DOCS_PER_REQUEST', 1000); define('RANDOM_SEED', 123456); // creating a CPS_Connection instance // $cpsConnection = new CPS_Connection('http://192.168.0.52/cgi-bin/cps2-andrejs/cps2-cgi', 'leta3m', 'root', 'root123'); // $cpsConnection = new CPS_Connection('tcp://192.168.0.52:5570', 'leta3m', 'root', 'root123'); $cpsConnection = new CPS_Connection('tcp://192.168.0.55:5570', 'performance_test3', 'root', 'password'); // $cpsConnection = new CPS_Connection('unix:///home/andrejs/cps2root/storages/test/storage.sock', 'test2', 'root', 'password'); // $cpsConnection->setDebug(true); $cpsSimple = new CPS_Simple($cpsConnection); /* $ids = array(); for ($x = 0; $x < TOTAL_DOCS; ++$x) { $ids[] = 'perf_id' . ($x + ID_OFFSET); } $startTime = microtime(true); $cpsSimple->retrieveMultiple($ids); echo 'Total : ' . sprintf('%01.6f', microtime(true) - $startTime) . ' seconds.<br />'; exit;*/ if (DO_MYSQL) { $mysqlConnection = mysql_connect('127.0.0.1', 'root', 'password'); mysql_select_db('cps2_perf_test');
$config = $app->config('cps'); $lat = (double) $lat; $lng = (double) $lng; $radius = (int) $radius; $q = trim((string) $app->request->get('q', '')); // search phrase $amenities = (array) $app->request->get('amenities', []); // amenities filter $page = (int) $app->request->get('page', 1); // page if ($page < 1) { $page = 1; } // structure of response $objects = ['total' => 0, 'from' => 0, 'to' => 0, 'pages' => 0, 'current_page' => 0, 'list' => [], 'amenities' => []]; $cps_connection = new CPS_Connection(new CPS_LoadBalancer($config['connection']), $config['database'], $config['username'], $config['password'], 'document', '//document/id', ['account' => $config['account']]); $docs = 50; $offset = $docs * ($page - 1); $radius = $radius / 1000; // radius is received in meters, but request to Clusterpoint should be in km // Shape (circle) definition. This will need to be attached to search query. $circle = CPS_CircleDefinition('circle', [$lat, $lng], $radius . ' km', 'lat', 'lon'); $query = CPS_Term(' ><circle'); // search inside of defined circle if ($q !== '') { $query .= CPS_Term($q); // if there was search phrase, attach it to the query } $query_without_amenities = $query; $aggregate = 'DISTINCT tags.amenity'; // aggregate distinct values of amenity tag
<?php // includes require_once 'config.php'; require_once '../cps_api.php'; try { // creating a CPS_Connection instance $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account'])); $insertDocs = array(); $insertDocs['id1'] = array('category' => 'cars', 'name' => 'Janis', 'car_params' => array('year' => '2011', 'make' => 'Audi', 'model' => 'A6'), 'persons' => array('person' => array('name' => 'Janis', 'surname' => 'Karklins'))); $insertDocs['id2'] = array('category' => 'cars', 'name' => 'Karlis', 'car_params' => array('year' => '2009', 'make' => 'Audi', 'model' => 'A6'), 'persons' => array('person' => array('name' => 'Janis', 'surname' => 'Karklins'))); $insertDocs['id3'] = array('category' => 'mopeds', 'name' => 'Aigars', 'car_params' => array('year' => '2012', 'make' => 'Audi', 'model' => 'A4'), 'persons' => array('person' => array('name' => 'Janis', 'surname' => 'Karklins'))); $insertDocs['id4'] = array('category' => 'cars', 'name' => 'Janis', 'car_params' => array('year' => '2010', 'make' => 'Audi', 'model' => 'A5'), 'persons' => array('person' => array('name' => 'Janis', 'surname' => 'Karklins'))); $insertDocs['id5'] = array('category' => 'mopeds', 'name' => 'Janis', 'car_params' => array('year' => '2011', 'make' => 'Audi', 'model' => 'A6'), 'persons' => array('person' => array('name' => 'Janis', 'surname' => 'Karklins'))); $insertDocs['id6'] = array('category' => 'mopeds', 'name' => 'Janis', 'persons' => array('year' => '2011', 'make' => 'Audi', 'model' => 'A6'), 'persons' => array('person' => array('name' => 'Janis', 'surname' => 'Karklins'))); // Insert $insertRequest = new CPS_InsertRequest($insertDocs); $cpsConnection->sendRequest($insertRequest); } catch (CPS_Exception $e) { var_dump($e->errors()); exit; }
<?php // includes require_once 'cps_simple.php'; try { // creating a CPS_Connection instance // if using HMAC keys, USERNAME and PASSWORD can be left empty $cpsConnection = new CPS_Connection("tcp://ENDPOINT_IP:PORT", "DATABASE", "USERNAME", "PASSWORD", "document", "//document/id", array("account" => "ACCOUNTID")); // setting HMAC keys, comment out is and standard username/password will be used $cpsConnection->setHMACKeys("USERKEY", "SIGNKEY"); // uncomment this, if you would like to see request/response and verify, that username and password is not set //$cpsConnection->setDebug(true); // perform status command $cpsSimple = new CPS_Simple($cpsConnection); echo "Status response:\n"; var_dump($cpsSimple->status()); } catch (CPS_Exception $e) { var_dump($e->errors()); exit; }