Skip to content

Commit e037c4a

Browse files
committed
added experimental $connection->enableCustomQueue() and $handler->enableCustomQueue()
1 parent acaf401 commit e037c4a

File tree

8 files changed

+303
-65
lines changed

8 files changed

+303
-65
lines changed

lib/triagens/ArangoDb/Connection.php

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ class Connection
8080
*/
8181
private $_batchRequest = false;
8282

83+
/**
84+
* custom queue name (leave empty if no custom queue is required)
85+
*
86+
* @var string
87+
*/
88+
private $_customQueue = null;
89+
8390
/**
8491
* $_database string
8592
*
@@ -157,14 +164,44 @@ public function setOption($name, $value) {
157164
}
158165
else if ($name === ConnectionOptions::OPTION_CONNECTION) {
159166
// set keep-alive flag
160-
$this->_useKeepAlive = ($value === 'Keep-Alive');
167+
$this->_useKeepAlive = (strtolower($value) === 'keep-alive');
161168
}
162169
else if ($name === ConnectionOptions::OPTION_DATABASE) {
163170
// set database
164171
$this->setDatabase($value);
165172
}
166173
}
167174

175+
176+
/**
177+
* Enables a custom queue name for all actions of the connection
178+
*
179+
* @param string $queueName - queue name
180+
* @param number $count - number of requests the custom queue will be used for
181+
*/
182+
183+
public function enableCustomQueue($queueName, $count = null)
184+
{
185+
$this->_options[ConnectionOptions::OPTION_CUSTOM_QUEUE] = $queueName;
186+
187+
if ($count !== null) {
188+
if (! is_numeric($count) || $count <= 0) {
189+
throw new ClientException('Invalid value for count value of custom queues');
190+
}
191+
$this->_options[ConnectionOptions::OPTION_CUSTOM_QUEUE_COUNT] = $count;
192+
}
193+
}
194+
195+
/**
196+
* Disable usage of custom queue for all actions of the connection
197+
*/
198+
public function disableCustomQueue()
199+
{
200+
$this->_options[ConnectionOptions::OPTION_CUSTOM_QUEUE] = null;
201+
$this->_options[ConnectionOptions::OPTION_CUSTOM_QUEUE_COUNT] = null;
202+
}
203+
204+
168205
/**
169206
* Issue an HTTP GET request
170207
*
@@ -175,9 +212,9 @@ public function setOption($name, $value) {
175212
*
176213
* @return HttpResponse
177214
*/
178-
public function get($url, $customHeader = array())
215+
public function get($url, array $customHeaders = array())
179216
{
180-
$response = $this->executeRequest(HttpHelper::METHOD_GET, $url, '', $customHeader);
217+
$response = $this->executeRequest(HttpHelper::METHOD_GET, $url, '', $customHeaders);
181218

182219
return $this->parseResponse($response);
183220
}
@@ -193,9 +230,9 @@ public function get($url, $customHeader = array())
193230
*
194231
* @return HttpResponse
195232
*/
196-
public function post($url, $data, $customHeader = array())
233+
public function post($url, $data, array $customHeaders = array())
197234
{
198-
$response = $this->executeRequest(HttpHelper::METHOD_POST, $url, $data, $customHeader);
235+
$response = $this->executeRequest(HttpHelper::METHOD_POST, $url, $data, $customHeaders);
199236

200237
return $this->parseResponse($response);
201238
}
@@ -211,9 +248,9 @@ public function post($url, $data, $customHeader = array())
211248
*
212249
* @return HttpResponse
213250
*/
214-
public function put($url, $data, $customHeader = array())
251+
public function put($url, $data, array $customHeaders = array())
215252
{
216-
$response = $this->executeRequest(HttpHelper::METHOD_PUT, $url, $data, $customHeader);
253+
$response = $this->executeRequest(HttpHelper::METHOD_PUT, $url, $data, $customHeaders);
217254

218255
return $this->parseResponse($response);
219256
}
@@ -228,9 +265,9 @@ public function put($url, $data, $customHeader = array())
228265
*
229266
* @return HttpResponse
230267
*/
231-
public function head($url, $customHeader = array())
268+
public function head($url, array $customHeaders = array())
232269
{
233-
$response = $this->executeRequest(HttpHelper::METHOD_HEAD, $url, '', $customHeader);
270+
$response = $this->executeRequest(HttpHelper::METHOD_HEAD, $url, '', $customHeaders);
234271

235272
return $this->parseResponse($response);
236273
}
@@ -246,9 +283,9 @@ public function head($url, $customHeader = array())
246283
*
247284
* @return HttpResponse
248285
*/
249-
public function patch($url, $data, $customHeader = array())
286+
public function patch($url, $data, array $customHeaders = array())
250287
{
251-
$response = $this->executeRequest(HttpHelper::METHOD_PATCH, $url, $data, $customHeader);
288+
$response = $this->executeRequest(HttpHelper::METHOD_PATCH, $url, $data, $customHeaders);
252289

253290
return $this->parseResponse($response);
254291
}
@@ -263,9 +300,9 @@ public function patch($url, $data, $customHeader = array())
263300
*
264301
* @return HttpResponse
265302
*/
266-
public function delete($url, $customHeader = array())
303+
public function delete($url, array $customHeaders = array())
267304
{
268-
$response = $this->executeRequest(HttpHelper::METHOD_DELETE, $url, '', $customHeader);
305+
$response = $this->executeRequest(HttpHelper::METHOD_DELETE, $url, '', $customHeaders);
269306

270307
return $this->parseResponse($response);
271308
}
@@ -356,33 +393,52 @@ public function parseResponse(HttpResponse $response)
356393
*
357394
* @throws Exception
358395
*
359-
* @param string $method - HTTP request method
360-
* @param string $url - HTTP URL
361-
* @param string $data - data to post in body
362-
* @param array $customHeader - any array containing header elements
396+
* @param string $method - HTTP request method
397+
* @param string $url - HTTP URL
398+
* @param string $data - data to post in body
399+
* @param array $customHeaders - any array containing header elements
363400
*
364401
* @return HttpResponse
365402
*/
366-
private function executeRequest($method, $url, $data, $customHeader = array())
403+
private function executeRequest($method, $url, $data, array $customHeaders = array())
367404
{
368405
HttpHelper::validateMethod($method);
369406
$database = $this->getDatabase();
370407
if ($database === '') {
371408
$url = '/_db/' . '_system' . $url;
372409
} else {
373-
$url = '/_db/' . $database . $url;
410+
$url = '/_db/' . urlencode($database) . $url;
374411
}
375412

413+
// check if a custom queue should be used
414+
if (! isset($customHeaders[ConnectionOptions::OPTION_CUSTOM_QUEUE]) &&
415+
$this->_options[ConnectionOptions::OPTION_CUSTOM_QUEUE] !== null) {
416+
417+
$customHeaders[HttpHelper::QUEUE_HEADER] = $this->_options[ConnectionOptions::OPTION_CUSTOM_QUEUE];
418+
419+
// check if a counter is set for the custom queue
420+
$count = $this->_options[ConnectionOptions::OPTION_CUSTOM_QUEUE_COUNT];
421+
if ($count !== null) {
422+
// yes, now decrease the counter
423+
424+
if ($count === 1) {
425+
$this->disableCustomQueue();
426+
}
427+
else {
428+
$this->_options->offsetSet(ConnectionOptions::OPTION_CUSTOM_QUEUE_COUNT, $count - 1);
429+
}
430+
}
431+
}
376432

377433
// create request data
378434
if ($this->_batchRequest === false) {
379435

380436
if ($this->_captureBatch === true) {
381437
$this->_options->offsetSet(ConnectionOptions::OPTION_BATCHPART, true);
382-
$request = HttpHelper::buildRequest($this->_options, $method, $url, $data, $customHeader);
438+
$request = HttpHelper::buildRequest($this->_options, $method, $url, $data, $customHeaders);
383439
$this->_options->offsetSet(ConnectionOptions::OPTION_BATCHPART, false);
384440
} else {
385-
$request = HttpHelper::buildRequest($this->_options, $method, $url, $data, $customHeader);
441+
$request = HttpHelper::buildRequest($this->_options, $method, $url, $data, $customHeaders);
386442
}
387443

388444
if ($this->_captureBatch === true) {
@@ -396,7 +452,7 @@ private function executeRequest($method, $url, $data, $customHeader = array())
396452

397453
$this->_options->offsetSet(ConnectionOptions::OPTION_BATCH, true);
398454

399-
$request = HttpHelper::buildRequest($this->_options, $method, $url, $data, $customHeader);
455+
$request = HttpHelper::buildRequest($this->_options, $method, $url, $data, $customHeaders);
400456
$this->_options->offsetSet(ConnectionOptions::OPTION_BATCH, false);
401457
}
402458

lib/triagens/ArangoDb/ConnectionOptions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ class ConnectionOptions implements
178178
*/
179179
const OPTION_CHECK_UTF8_CONFORM = 'CheckUtf8Conform';
180180

181+
/**
182+
* custom queue name
183+
*/
184+
const OPTION_CUSTOM_QUEUE = 'customQueue';
185+
186+
/**
187+
* custom queue count
188+
*/
189+
const OPTION_CUSTOM_QUEUE_COUNT = 'customQueueCount';
190+
181191
/**
182192
* Set defaults, use options provided by client and validate them
183193
*
@@ -312,6 +322,8 @@ private static function getDefaults()
312322
self::OPTION_BATCHPART => false,
313323
self::OPTION_DATABASE => '_system',
314324
self::OPTION_CHECK_UTF8_CONFORM => DefaultValues::DEFAULT_CHECK_UTF8_CONFORM,
325+
self::OPTION_CUSTOM_QUEUE => null,
326+
self::OPTION_CUSTOM_QUEUE_COUNT => null
315327
);
316328
}
317329

lib/triagens/ArangoDb/Cursor.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ class Cursor implements
129129
*/
130130
const ENTRY_SANITIZE = '_sanitize';
131131

132+
/**
133+
* custom queue option entry
134+
*/
135+
const ENTRY_CUSTOM_QUEUE = 'customQueue';
136+
132137
/**
133138
* "flat" option entry (will treat the results as a simple array, not documents)
134139
*/
@@ -204,7 +209,7 @@ public function delete()
204209
{
205210
if ($this->_id) {
206211
try {
207-
$this->_connection->delete($this->url() . '/' . $this->_id);
212+
$this->_connection->delete($this->url() . '/' . $this->_id, $this->buildHeaders());
208213

209214
return true;
210215
} catch (Exception $e) {
@@ -614,7 +619,7 @@ private function sanitize(array $rows)
614619
private function fetchOutstanding()
615620
{
616621
// continuation
617-
$response = $this->_connection->put($this->url() . "/" . $this->_id, '');
622+
$response = $this->_connection->put($this->url() . "/" . $this->_id, '', $this->buildHeaders());
618623
++$this->_fetches;
619624

620625
$data = $response->getJson();
@@ -631,6 +636,27 @@ private function fetchOutstanding()
631636
}
632637

633638

639+
/**
640+
* Build headers for the cursor requests
641+
*
642+
* @return array - headers used when executing further cursor fetches
643+
*/
644+
private function buildHeaders()
645+
{
646+
$result = array();
647+
648+
if (isset($this->_options[self::ENTRY_CUSTOM_QUEUE])) {
649+
$value = $this->_options[self::ENTRY_CUSTOM_QUEUE];
650+
651+
if ($value != null && $value !== '') {
652+
$result[HttpHelper::QUEUE_HEADER] = $this->_options[self::ENTRY_CUSTOM_QUEUE];
653+
}
654+
}
655+
656+
return $result;
657+
}
658+
659+
634660
/**
635661
* Set the length of the (fetched) result set
636662
*

lib/triagens/ArangoDb/Handler.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
/**
1414
* A base class for REST-based handlers
1515
*
16-
* <br>
17-
*
1816
* @package triagens\ArangoDb
1917
* @since 0.2
2018
*/
@@ -40,6 +38,28 @@ public function __construct(Connection $connection)
4038
$this->_connection = $connection;
4139
}
4240

41+
/**
42+
* Enables a custom queue name for all actions of this handler and other actions
43+
* that use the same connection
44+
*
45+
* @param string $queueName - queue name
46+
* @param number $count - number of requests the custom queue will be used for
47+
*/
48+
public function enableCustomQueue($queueName, $count = null)
49+
{
50+
// pass it on to the connection
51+
$this->_connection->enableCustomQueue($queueName, $count);
52+
}
53+
54+
/**
55+
* Disable usage of custom queue for this handler and other actions that use the
56+
* same connection
57+
*/
58+
public function disableCustomQueue()
59+
{
60+
// pass it on to the connection
61+
$this->_connection->disableCustomQueue();
62+
}
4363

4464
/**
4565
* Return the connection object
@@ -166,7 +186,6 @@ protected function validateAndIncludeOldSingleParameterInParams($options, $param
166186
*/
167187
protected function includeOptionsInParams($options, $params, $includeArray = array())
168188
{
169-
#$value = null;
170189
if (is_array($options)) {
171190
foreach ($options as $key => $value) {
172191
if (array_key_exists($key, $includeArray)) {
@@ -196,7 +215,6 @@ protected function includeOptionsInParams($options, $params, $includeArray = arr
196215
*/
197216
protected function includeOptionsInBody($options, $body, $includeArray = array())
198217
{
199-
#$value = null;
200218
if (is_array($options)) {
201219
foreach ($options as $key => $value) {
202220
if (array_key_exists($key, $includeArray)) {

lib/triagens/ArangoDb/HttpHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ class HttpHelper
6666
const PROTOCOL = 'HTTP/1.1';
6767

6868
/**
69-
* HTTP protocol version used, hard-coded to version 1.1
69+
* Boundary string for batch request parts
7070
*/
7171
const MIME_BOUNDARY = 'XXXsubpartXXX';
7272

73+
/**
74+
* HTTP Header for specifying a custom queue
75+
*/
76+
const QUEUE_HEADER = 'X-Arango-Queue';
7377

7478
/**
7579
* Validate an HTTP request method name

0 commit comments

Comments
 (0)