Skip to content

Commit e2957a6

Browse files
committed
new JobHandler,
CollectionHandler, new Methods, first, last, fullext
1 parent 213c0eb commit e2957a6

File tree

4 files changed

+513
-2
lines changed

4 files changed

+513
-2
lines changed

lib/triagens/ArangoDb/CollectionHandler.php

Lines changed: 171 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ class CollectionHandler extends
9494
*/
9595
const OPTION_SKIP = 'skip';
9696

97+
/**
98+
* index parameter
99+
*/
100+
const OPTION_INDEX = 'index';
101+
97102
/**
98103
* limit parameter
99104
*/
@@ -169,6 +174,11 @@ class CollectionHandler extends
169174
*/
170175
const OPTION_COUNT = 'count';
171176

177+
/**
178+
* query option
179+
*/
180+
const OPTION_QUERY = 'query';
181+
172182
/**
173183
* checksum option
174184
*/
@@ -938,6 +948,76 @@ public function byExample($collectionId, $document, $options = array())
938948
}
939949

940950

951+
/**
952+
* Get document(s) by a fulltext query
953+
*
954+
* This will find all documents from the collection that match the fulltext query specified in query.
955+
* In order to use the fulltext operator, a fulltext index must be defined for the collection and the specified attribute.
956+
*
957+
*
958+
* @throws Exception
959+
*
960+
* @param mixed $collectionId - collection id as string or number
961+
* @param mixed $attribute - The attribute that contains the texts.
962+
* @param mixed $query - The fulltext query.
963+
* @param bool|array $options - optional, prior to v1.0.0 this was a boolean value for sanitize, since v1.0.0 it's an array of options.
964+
* <p>Options are :<br>
965+
* <li>'_sanitize' - True to remove _id and _rev attributes from result documents. Defaults to false.</li>
966+
* <li>'sanitize' - Deprecated, please use '_sanitize'.</li>
967+
* <li>'_hiddenAttributes' - Set an array of hidden attributes for created documents.
968+
* <li>'hiddenAttributes' - Deprecated, please use '_hiddenAttributes'.</li>
969+
* <p>
970+
* This is actually the same as setting hidden attributes using setHiddenAttributes() on a document. <br>
971+
* The difference is, that if you're returning a resultset of documents, the getAll() is already called <br>
972+
* and the hidden attributes would not be applied to the attributes.<br>
973+
* </p>
974+
* </li>
975+
* <li>'batchSize' - can optionally be used to tell the server to limit the number of results to be transferred in one batch</li>
976+
* <li>'skip' - Optional, The number of documents to skip in the query.</li>
977+
* <li>'limit' - Optional, The maximal amount of documents to return. 'skip' is applied before the limit restriction.</li>
978+
* <li>'index' - If given, the identifier of the fulltext-index to use.</li>
979+
* </p>
980+
*
981+
* @return cursor - Returns a cursor containing the result
982+
*/
983+
public function fulltext($collectionId, $attribute, $query, $options = array())
984+
{
985+
// This preserves compatibility for the old sanitize parameter.
986+
if (!is_array($options)) {
987+
$sanitize = $options;
988+
$options = array();
989+
$options = array_merge($options, $this->getCursorOptions($sanitize));
990+
} else {
991+
$options = array_merge($options, $this->getCursorOptions($options));
992+
}
993+
994+
$body = array(
995+
self::OPTION_COLLECTION => $collectionId,
996+
self::OPTION_ATTRIBUTE => $attribute,
997+
self::OPTION_QUERY => $query,
998+
);
999+
1000+
$body = $this->includeOptionsInBody(
1001+
$options,
1002+
$body,
1003+
array(
1004+
ConnectionOptions::OPTION_BATCHSIZE => $this->getConnectionOption(
1005+
ConnectionOptions::OPTION_BATCHSIZE
1006+
),
1007+
self::OPTION_LIMIT => null,
1008+
self::OPTION_SKIP => null,
1009+
self::OPTION_INDEX => null,
1010+
)
1011+
);
1012+
1013+
$response = $this->getConnection()->put(Urls::URL_FULLTEXT, $this->json_encode_wrapper($body));
1014+
1015+
$options['isNew'] = false;
1016+
1017+
return new Cursor($this->getConnection(), $response->getJson(), $options);
1018+
}
1019+
1020+
9411021
/**
9421022
* Get the first document matching a given example.
9431023
*
@@ -991,7 +1071,7 @@ public function firstExample($collectionId, $document, $options = array())
9911071
$response = $this->getConnection()->put(Urls::URL_FIRST_EXAMPLE, $this->json_encode_wrapper($data));
9921072
$data = $response->getJson();
9931073

994-
$options['isNew'] = false;
1074+
$options['_isNew'] = false;
9951075

9961076
return Document::createFromArray($data['document'], $options);
9971077
}
@@ -1026,6 +1106,96 @@ public function any($collectionId)
10261106
}
10271107
}
10281108

1109+
/**
1110+
* This will return the first documents from the collection, in the order of insertion/update time.
1111+
* When the count argument is supplied, the result will be a list of documents, with the "oldest" document being
1112+
* first in the result list.
1113+
* If the count argument is not supplied, the result is the "oldest" document of the collection,
1114+
* or null if the collection is empty.
1115+
*
1116+
*
1117+
* @throws Exception
1118+
*
1119+
* @param mixed $collectionId - collection id as string or number
1120+
* @param int $count - the number of documents to return at most. Specifiying count is optional.
1121+
*
1122+
* @return array - array of documents in the collection
1123+
* @since 1.2
1124+
*/
1125+
public function first($collectionId, $count = null)
1126+
{
1127+
1128+
$data = array(
1129+
self::OPTION_COLLECTION => $collectionId,
1130+
);
1131+
if ($count != null) {
1132+
$data[self::OPTION_COUNT] = $count;
1133+
}
1134+
1135+
$response = $this->getConnection()->put(Urls::URL_FIRST, $this->json_encode_wrapper($data));
1136+
$data = $response->getJson();
1137+
1138+
$result = array();
1139+
if ($data["result"] == null) {
1140+
return array();
1141+
}
1142+
$options = array();
1143+
$options['_isNew'] = false;
1144+
if ($count != null && $count > 1) {
1145+
foreach ($data["result"] as $doc) {
1146+
$result[] = Document::createFromArray($doc, $options);
1147+
}
1148+
} else {
1149+
return Document::createFromArray($data["result"], $options);
1150+
}
1151+
return $result;
1152+
}
1153+
1154+
/**
1155+
* This will return the last documents from the collection, in the order of insertion/update time.
1156+
* When the count argument is supplied, the result will be a list of documents, with the "latest" document being
1157+
* first in the result list.
1158+
* If the count argument is not supplied, the result is the "latest" document of the collection,
1159+
* or null if the collection is empty.
1160+
*
1161+
*
1162+
* @throws Exception
1163+
*
1164+
* @param mixed $collectionId - collection id as string or number
1165+
* @param int $count - the number of documents to return at most. Specifiying count is optional.
1166+
*
1167+
* @return array - array of documents in the collection
1168+
* @since 1.2
1169+
*/
1170+
public function last($collectionId, $count = null)
1171+
{
1172+
1173+
$data = array(
1174+
self::OPTION_COLLECTION => $collectionId,
1175+
);
1176+
if ($count != null) {
1177+
$data[self::OPTION_COUNT] = $count;
1178+
}
1179+
1180+
$response = $this->getConnection()->put(Urls::URL_LAST, $this->json_encode_wrapper($data));
1181+
$data = $response->getJson();
1182+
1183+
$result = array();
1184+
if ($data["result"] == null) {
1185+
return array();
1186+
}
1187+
$options = array();
1188+
$options['_isNew'] = false;
1189+
if ($count != null && $count > 1) {
1190+
foreach ($data["result"] as $doc) {
1191+
$result[] = Document::createFromArray($doc, $options);
1192+
}
1193+
} else {
1194+
return Document::createFromArray($data["result"], $options);
1195+
}
1196+
return $result;
1197+
}
1198+
10291199

10301200
/**
10311201
* Update document(s) matching a given example

lib/triagens/ArangoDb/JobHandler.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/**
4+
* ArangoDB PHP client: endpoint
5+
*
6+
* @package triagens\ArangoDb
7+
* @author Jan Steemann
8+
* @copyright Copyright 2012, triagens GmbH, Cologne, Germany
9+
*/
10+
11+
namespace triagens\ArangoDb;
12+
13+
/**
14+
* Job specification
15+
*
16+
* A job is a arango db job which is created by calling arango api with the x-arango-async:store header
17+
* These jobs will be executed asynchronously. This class enables the user to get the jobs result, delete them or get
18+
* all pending or done jobs from arango.
19+
*
20+
* @package triagens\ArangoDb
21+
* @since 0.2
22+
*/
23+
class JobHandler
24+
{
25+
26+
/**
27+
* Returns the result of an async job
28+
*
29+
* Returns the result of an async job identified by job-id. If the async job result is present on the server,
30+
* the result will be removed from the list of result.
31+
* That means this method can be called for each job-id once.
32+
* The method will return the original job result's headers and body,
33+
* plus the additional HTTP header x-arango-async-job-id. If this header is present, then the job was found
34+
* and the response contains the original job's result. If the header is not present, the job was not found
35+
* and the response contains status information from the job amanger.
36+
* @param Connection $connection - the connection to be used
37+
* @param string $jobId - the Job ID
38+
* *
39+
*
40+
* @return array $responseArray - The response array.
41+
*/
42+
public static function getJobResult(Connection $connection, $jobId)
43+
{
44+
$url = UrlHelper::buildUrl(Urls::URL_DOCUMENT, array($jobId));
45+
$response = $connection->put($url, '');
46+
47+
$responseArray = $response->getJson();
48+
49+
return $responseArray;
50+
}
51+
52+
53+
54+
55+
/**
56+
* Deletes jobs
57+
*
58+
* Deletes either all job results, expired job results, or the result of a specific job.
59+
* Clients can use this method to perform an eventual garbage collection of job results.
60+
*
61+
* @param Connection $connection - the connection to be used
62+
* @param string $type - The type of jobs to delete. The type can be either done, all, pending or expired.
63+
* @param int $timestamp - A UNIX timestamp specifying the expiration threshold when type is expired.
64+
* *
65+
*
66+
* @return array $responseArray - The response array.
67+
*/
68+
public static function deleteJobsByType(Connection $connection, $type, $timestamp = null)
69+
{
70+
$url = UrlHelper::buildUrl(Urls::URL_DOCUMENT, array($type));
71+
if ($timestamp && $type == "expired") {
72+
$url = UrlHelper::appendParamsUrl($url, array("stamp" => $timestamp));
73+
}
74+
$response = $connection->delete($url, '');
75+
76+
$responseArray = $response->getJson();
77+
78+
return $responseArray;
79+
}
80+
81+
/**
82+
* Deletes a job
83+
*
84+
* Deletes a job specified by id
85+
*
86+
* @param Connection $connection - the connection to be used
87+
* @param string $jobId - The id of a job to delete
88+
*
89+
* @return array $responseArray - The response array.
90+
*/
91+
public static function deleteJobsById(Connection $connection, $jobId)
92+
{
93+
$url = UrlHelper::buildUrl(Urls::URL_DOCUMENT, array($jobId));
94+
95+
$response = $connection->delete($url);
96+
97+
$responseArray = $response->getJson();
98+
99+
return $responseArray;
100+
}
101+
102+
/**
103+
* Returns the list of ids of async jobs
104+
*
105+
* Returns the list of ids of async jobs with a specific status (either done or pending).
106+
* The list can be used by the client to get an overview of the job system status and to
107+
* retrieve completed job results later.
108+
* @param Connection $connection - the connection to be used
109+
* @param string $type - The type of jobs to return. The type can be either done or pending.
110+
* *
111+
*
112+
* @return array $responseArray - The response array.
113+
*/
114+
public static function getAllJobsByState(Connection $connection, $type)
115+
{
116+
$url = UrlHelper::buildUrl(Urls::URL_DOCUMENT, array($type));
117+
$response = $connection->get($url);
118+
119+
$responseArray = $response->getJson();
120+
121+
return $responseArray;
122+
}
123+
124+
}

lib/triagens/ArangoDb/Urls.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ abstract class Urls
9595
*/
9696
const URL_ANY = '/_api/simple/any';
9797

98+
/**
99+
* base URL part for fulltext
100+
*/
101+
const URL_FULLTEXT = '/_api/simple/fulltext';
102+
103+
/**
104+
* base URL part for first
105+
*/
106+
const URL_FIRST = '/_api/simple/first';
107+
108+
/**
109+
* base URL part for last
110+
*/
111+
const URL_LAST = '/_api/simple/last';
112+
98113
/**
99114
* base URL part for remove-by-example
100115
*/
@@ -198,6 +213,11 @@ abstract class Urls
198213
*/
199214
const URL_ENDPOINT = '/_api/endpoint';
200215

216+
/**
217+
* base URL part for job management
218+
*/
219+
const URL_JOB = '/_api/job';
220+
201221
/**
202222
* base URL part for database management
203223
*/

0 commit comments

Comments
 (0)