Skip to content

Commit adf2766

Browse files
committed
Mainly work on batch operations
Some minor corrections + formatting
1 parent 6bed8f0 commit adf2766

File tree

7 files changed

+1370
-1259
lines changed

7 files changed

+1370
-1259
lines changed

CHANGELOG

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Release notes for the ArangoDB-PHP driver 3.0.9
1+
Release notes for the ArangoDB-PHP driver 3.0.x
22
===============================================
33

44
This version of the driver is compatible with ArangoDB 3.0.x
@@ -9,14 +9,18 @@ Please use ones of the `2.x` branches of the driver for 2.x-compatibility.
99

1010
Caution!!!
1111

12-
There are still deprecated functions in the code, which should have been removed in the 2.x series of the driver.
13-
These will now be removed with the 3.1 version of the driver, in order to clean up the codebase. Please check and change your code accordingly.
12+
There are still deprecated functions in the code, which should have been removed in the first 3.0 version of the driver, however, we missed this opportunity.
13+
These will now finally be removed with the 3.1 version of the driver, in order to clean up the codebase. Please check and change your code accordingly.
1414
All deprecated functions can now also be found in the docs inside the "Deprecated" view.
1515

1616
Changes
1717
=======
1818

19-
- This version is mainly a cleanup of code in order to produce a nicer documentation for the driver.
19+
- GraphHandler: Optimized code to do less work when not necessary
20+
- GraphHandler: Implemented optional cache that caches the Vertex/Edge-Collections instead of making expensive calls to the DB.
21+
- GraphHandler: Is now batch-able. However, if any collections need to be fetched, they will be done out-of-batch.
22+
If a lot of calls to the GraphHandler are being made, the use of the new caching functionality is encouraged.
23+
- Batches: Some work has been done, to optimize batches. This is still in development.
2024
- Switched from phpDocumentor to apigen
2125
- New Docs were generated
2226

lib/triagens/ArangoDb/Batch.php

Lines changed: 138 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ class Batch
4646

4747

4848
/**
49-
* The array of BatchPart objects
49+
* The next batch part id
5050
*
51-
* @var array $_batchParts
51+
* @var integer|string $_nextBatchPartId
5252
*/
5353
private $_nextBatchPartId;
5454

@@ -78,7 +78,7 @@ class Batch
7878
/**
7979
* The Batch NextId
8080
*
81-
* @var mixed $_nextId
81+
* @var integer|string $_nextId
8282
*/
8383
private $_nextId = 0;
8484

@@ -97,8 +97,6 @@ class Batch
9797
* When this option is defined, the batch mechanism will use an SplFixedArray instead of the normal PHP arrays.
9898
* In most cases, this will result in increased performance of about 5% to 15%, depending on batch size and data.</li>
9999
* </p>
100-
*
101-
* @return Batch
102100
*/
103101
public function __construct(Connection $connection, array $options = [])
104102
{
@@ -318,14 +316,16 @@ public function nextBatchPartCursorOptions($batchPartCursorOptions)
318316
}
319317

320318

321-
/**
322-
* Append the request to the batch-part
323-
*
324-
* @param mixed $method - The method of the request (GET, POST...)
325-
* @param mixed $request - The request that will get appended to the batch
326-
*
327-
* @return HttpResponse
328-
*/
319+
/**
320+
* Append the request to the batch-part
321+
*
322+
* @param mixed $method - The method of the request (GET, POST...)
323+
* @param mixed $request - The request that will get appended to the batch
324+
*
325+
* @return HttpResponse
326+
*
327+
* @throws \triagens\ArangoDb\ClientException
328+
*/
329329
public function append($method, $request)
330330
{
331331
preg_match('%/_api/simple/(?P<simple>\w*)|/_api/(?P<direct>\w*)%ix', $request, $regs);
@@ -339,43 +339,56 @@ public function append($method, $request)
339339
$type = 'get' . $type;
340340
}
341341

342-
$result = 'HTTP/1.1 202 Accepted' . HttpHelper::EOL;
343-
$result .= 'location: /_db/_system/_api/document/0/0' . HttpHelper::EOL;
344-
$result .= 'server: triagens GmbH High-Performance HTTP Server' . HttpHelper::EOL;
345-
$result .= 'content-type: application/json; charset=utf-8' . HttpHelper::EOL;
346-
$result .= 'etag: "0"' . HttpHelper::EOL;
347-
$result .= 'connection: Close' . HttpHelper::EOL . HttpHelper::EOL;
348-
$result .= '{"error":false,"_id":"0/0","id":"0","_rev":0,"hasMore":1, "result":[{}], "documents":[{}]}' . HttpHelper::EOL . HttpHelper::EOL;
342+
if (null === $this->_nextBatchPartId)
343+
{
344+
if (is_a($this->_batchParts, 'SplFixedArray'))
345+
{
346+
$nextNumeric = $this->_nextId;
347+
$this->_nextId++;
348+
}
349+
else
350+
{
351+
$nextNumeric = count($this->_batchParts);
352+
}
353+
$batchPartId = $nextNumeric;
354+
}
355+
else
356+
{
357+
$batchPartId = $this->_nextBatchPartId;
358+
$this->_nextBatchPartId = null;
359+
}
360+
361+
$eol = HttpHelper::EOL;
362+
363+
$result = 'HTTP/1.1 202 Accepted' . $eol;
364+
$result .= 'location: /_db/_system/_api/document/0/0' . $eol;
365+
$result .= 'server: triagens GmbH High-Performance HTTP Server' . $eol;
366+
$result .= 'content-type: application/json; charset=utf-8' . $eol;
367+
$result .= 'etag: "0"' . $eol;
368+
$result .= 'connection: Close' . $eol . $eol;
369+
$result .= '{"error":false,"_id":"0/0","id":"0","_rev":0,"hasMore":1, "result":[{}], "documents":[{}]}' . $eol . $eol;
349370

350371
$response = new HttpResponse($result);
351-
$batchPart = new BatchPart($this, $this->_nextBatchPartId, $type, $request, $response, ['cursorOptions' => $this->_batchPartCursorOptions]);
352-
if (null === $this->_nextBatchPartId) {
353-
if (is_a($this->_batchParts, 'SplFixedArray')) {
354-
$nextNumeric = $this->_nextId;
355-
$this->_nextId++;
356-
}
357-
else {
358-
$nextNumeric = count($this->_batchParts);
359-
}
360-
$this->_batchParts[$nextNumeric] = $batchPart;
361-
}
362-
else {
363-
$this->_batchParts[$this->_nextBatchPartId] = $batchPart;
364-
$this->_nextBatchPartId = null;
365-
}
372+
$batchPart = new BatchPart($this, $batchPartId, $type, $request, $response, ['cursorOptions' => $this->_batchPartCursorOptions]);
366373

367-
return $response;
374+
$this->_batchParts[$batchPartId] = $batchPart;
375+
376+
$response->setBatchPart($batchPart);
377+
378+
return $response;
368379
}
369380

370381

371-
/**
372-
* Split batch request and use ContentId as array key
373-
*
374-
* @param mixed $pattern
375-
* @param mixed $string
376-
*
377-
* @return array $array - Array of batch-parts
378-
*/
382+
/**
383+
* Split batch request and use ContentId as array key
384+
*
385+
* @param mixed $pattern
386+
* @param mixed $string
387+
*
388+
* @return array $array - Array of batch-parts
389+
*
390+
* @throws \triagens\ArangoDb\ClientException
391+
*/
379392
public function splitWithContentIdKey($pattern, $string)
380393
{
381394
$array = [];
@@ -396,64 +409,66 @@ public function splitWithContentIdKey($pattern, $string)
396409
}
397410

398411

399-
/**
400-
* Processes this batch. This sends the captured requests to the server as one batch.
401-
*
402-
* @throws ClientException
403-
* @return HttpResponse|Batch - Batch if processing of the batch was successful or the HttpResponse object in case of a failure. A successful process just means that tha parts were processed. Each part has it's own response though and should be checked on its own.
404-
*/
405-
public function process()
406-
{
407-
$this->stopCapture();
408-
$this->setBatchRequest(true);
409-
$data = '';
410-
$batchParts = $this->getBatchParts();
411-
412-
if (count($batchParts) === 0) {
413-
throw new ClientException('Can\'t process empty batch.');
414-
}
415-
416-
/** @var $partValue BatchPart */
417-
foreach ($batchParts as $partValue) {
418-
if (null !== $partValue) {
419-
$data .= '--' . HttpHelper::MIME_BOUNDARY . HttpHelper::EOL;
420-
$data .= 'Content-Type: application/x-arango-batchpart' . HttpHelper::EOL;
421-
422-
if (null !== $partValue->getId()) {
423-
$data .= 'Content-Id: ' . (string) $partValue->getId() . HttpHelper::EOL . HttpHelper::EOL;
424-
}
425-
else {
426-
$data .= HttpHelper::EOL;
427-
}
428-
429-
$data .= (string) $partValue->getRequest() . HttpHelper::EOL;
430-
}
431-
}
432-
$data .= '--' . HttpHelper::MIME_BOUNDARY . '--' . HttpHelper::EOL . HttpHelper::EOL;
433-
434-
$params = [];
435-
$url = UrlHelper::appendParamsUrl(Urls::URL_BATCH, $params);
436-
$this->_batchResponse = $this->_connection->post($url, $data);
437-
if ($this->_batchResponse->getHttpCode() !== 200) {
438-
return $this->_batchResponse;
439-
}
440-
$body = $this->_batchResponse->getBody();
441-
$body = trim($body, '--' . HttpHelper::MIME_BOUNDARY . '--');
442-
$batchParts = $this->splitWithContentIdKey('--' . HttpHelper::MIME_BOUNDARY . HttpHelper::EOL, $body);
443-
444-
foreach ($batchParts as $partKey => $partValue) {
445-
$response = new HttpResponse($partValue);
446-
$body = $response->getBody();
447-
$response = new HttpResponse($body);
448-
$batchPartResponses[$partKey] = $response;
449-
$this->getPart($partKey)->setResponse($batchPartResponses[$partKey]);
450-
}
451-
452-
return $this;
453-
}
454-
455-
456-
/**
412+
/**
413+
* Processes this batch. This sends the captured requests to the server as one batch.
414+
*
415+
* @return HttpResponse|Batch - Batch if processing of the batch was successful or the HttpResponse object in case of a failure. A successful process just means that tha parts were processed. Each part has it's own response though and should be checked on its own.
416+
*
417+
* @throws ClientException
418+
* @throws \triagens\ArangoDb\Exception
419+
*/
420+
public function process()
421+
{
422+
$this->stopCapture();
423+
$this->setBatchRequest(true);
424+
$data = '';
425+
$batchParts = $this->getBatchParts();
426+
427+
if (count($batchParts) === 0) {
428+
throw new ClientException('Can\'t process empty batch.');
429+
}
430+
431+
/** @var $partValue BatchPart */
432+
foreach ($batchParts as $partValue) {
433+
if (null !== $partValue) {
434+
$data .= '--' . HttpHelper::MIME_BOUNDARY . HttpHelper::EOL;
435+
$data .= 'Content-Type: application/x-arango-batchpart' . HttpHelper::EOL;
436+
437+
if (null !== $partValue->getId()) {
438+
$data .= 'Content-Id: ' . (string) $partValue->getId() . HttpHelper::EOL . HttpHelper::EOL;
439+
}
440+
else {
441+
$data .= HttpHelper::EOL;
442+
}
443+
444+
$data .= (string) $partValue->getRequest() . HttpHelper::EOL;
445+
}
446+
}
447+
$data .= '--' . HttpHelper::MIME_BOUNDARY . '--' . HttpHelper::EOL . HttpHelper::EOL;
448+
449+
$params = [];
450+
$url = UrlHelper::appendParamsUrl(Urls::URL_BATCH, $params);
451+
$this->_batchResponse = $this->_connection->post($url, $data);
452+
if ($this->_batchResponse->getHttpCode() !== 200) {
453+
return $this->_batchResponse;
454+
}
455+
$body = $this->_batchResponse->getBody();
456+
$body = trim($body, '--' . HttpHelper::MIME_BOUNDARY . '--');
457+
$batchParts = $this->splitWithContentIdKey('--' . HttpHelper::MIME_BOUNDARY . HttpHelper::EOL, $body);
458+
459+
foreach ($batchParts as $partKey => $partValue) {
460+
$response = new HttpResponse($partValue);
461+
$body = $response->getBody();
462+
$response = new HttpResponse($body);
463+
$batchPartResponses[$partKey] = $response;
464+
$this->getPart($partKey)->setResponse($batchPartResponses[$partKey]);
465+
}
466+
467+
return $this;
468+
}
469+
470+
471+
/**
457472
* Get the total count of the batch parts
458473
*
459474
* @return integer $count
@@ -464,13 +479,14 @@ public function countParts()
464479
}
465480

466481

467-
/**
482+
/**
468483
* Get the batch part identified by the array key (0...n) or its id (if it was set with nextBatchPartId($id) )
469484
*
470485
* @param mixed $partId the batch part id. Either it's numeric key or a given name.
471486
*
472-
* @throws ClientException
473487
* @return mixed $batchPart
488+
*
489+
* @throws ClientException
474490
*/
475491
public function getPart($partId)
476492
{
@@ -482,26 +498,30 @@ public function getPart($partId)
482498
}
483499

484500

485-
/**
486-
* Get the batch part identified by the array key (0...n) or its id (if it was set with nextBatchPartId($id) )
487-
*
488-
* @param mixed $partId the batch part id. Either it's numeric key or a given name.
489-
*
490-
* @return mixed $partId
491-
*/
501+
/**
502+
* Get the batch part identified by the array key (0...n) or its id (if it was set with nextBatchPartId($id) )
503+
*
504+
* @param mixed $partId the batch part id. Either it's numeric key or a given name.
505+
*
506+
* @return mixed $partId
507+
*
508+
* @throws \triagens\ArangoDb\ClientException
509+
*/
492510
public function getPartResponse($partId)
493511
{
494512
return $this->getPart($partId)->getResponse();
495513
}
496514

497515

498-
/**
499-
* Get the batch part identified by the array key (0...n) or its id (if it was set with nextBatchPartId($id) )
500-
*
501-
* @param mixed $partId the batch part id. Either it's numeric key or a given name.
502-
*
503-
* @return mixed $partId
504-
*/
516+
/**
517+
* Get the batch part identified by the array key (0...n) or its id (if it was set with nextBatchPartId($id) )
518+
*
519+
* @param mixed $partId the batch part id. Either it's numeric key or a given name.
520+
*
521+
* @return mixed $partId
522+
*
523+
* @throws \triagens\ArangoDb\ClientException
524+
*/
505525
public function getProcessedPartResponse($partId)
506526
{
507527
return $this->getPart($partId)->getProcessedResponse();

0 commit comments

Comments
 (0)