@@ -75,6 +75,13 @@ class Batch
75
75
*/
76
76
private $ _sanitize = false ;
77
77
78
+ /**
79
+ * The Batch NextId
80
+ *
81
+ * @var mixed $_nextId
82
+ */
83
+ private $ _nextId = 0 ;
84
+
78
85
79
86
/**
80
87
* Constructor for Batch instance. Batch instance by default starts capturing request after initiated.
@@ -85,8 +92,10 @@ class Batch
85
92
*
86
93
* <p>Options are :
87
94
* <li>'_sanitize' - True to remove _id and _rev attributes from result documents returned from this batch. Defaults to false.</li>
88
- * <li>'$startCapture' - Start batch capturing immediately after batch instantiation. Defaults to true.
89
- * </li>
95
+ * <li>'startCapture' - Start batch capturing immediately after batch instantiation. Defaults to true.</li>
96
+ * <li>'batchSize' - Defines a fixed array size for holding the batch parts. The id's of the batch parts can only be integers.
97
+ * When this option is defined, the batch mechanism will use an SplFixedArray instead of the normal PHP arrays.
98
+ * In most cases, this will result in increased performance of about 5% to 15%, depending on batch size and data.</li>
90
99
* </p>
91
100
*
92
101
* @return Batch
@@ -95,9 +104,15 @@ public function __construct(Connection $connection, $options = array())
95
104
{
96
105
$ startCapture = true ;
97
106
$ sanitize = false ;
107
+ $ batchSize = 0 ;
98
108
$ options = array_merge ($ options , $ this ->getCursorOptions ($ sanitize ));
99
109
extract ($ options , EXTR_IF_EXISTS );
100
110
$ this ->_sanitize = $ sanitize ;
111
+ $ this ->batchSize = $ batchSize ;
112
+
113
+ if ($ this ->batchSize > 0 ) {
114
+ $ this ->_batchParts = new \SplFixedArray ($ this ->batchSize );
115
+ }
101
116
102
117
$ this ->setConnection ($ connection );
103
118
@@ -335,7 +350,13 @@ public function append($method, $request)
335
350
$ response = new HttpResponse ($ result );
336
351
$ batchPart = new BatchPart ($ this , $ this ->_nextBatchPartId , $ type , $ request , $ response , array ('cursorOptions ' => $ this ->_batchPartCursorOptions ));
337
352
if (null === $ this ->_nextBatchPartId ) {
338
- $ nextNumeric = count ($ this ->_batchParts );
353
+ if (is_a ($ this ->_batchParts , 'SplFixedArray ' )) {
354
+ $ nextNumeric = $ this ->_nextId ;
355
+ $ this ->_nextId ++;
356
+ }
357
+ else {
358
+ $ nextNumeric = count ($ this ->_batchParts );
359
+ }
339
360
$ this ->_batchParts [$ nextNumeric ] = $ batchPart ;
340
361
}
341
362
else {
@@ -394,17 +415,19 @@ public function process()
394
415
395
416
/** @var $partValue BatchPart */
396
417
foreach ($ batchParts as $ partValue ) {
397
- $ data .= '-- ' . HttpHelper::MIME_BOUNDARY . HttpHelper::EOL ;
398
- $ data .= 'Content-Type: application/x-arango-batchpart ' . HttpHelper::EOL ;
399
-
400
- if (null !== $ partValue ->getId ()) {
401
- $ data .= 'Content-Id: ' . (string ) $ partValue ->getId () . HttpHelper::EOL . HttpHelper::EOL ;
418
+ if (isset ($ 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 ;
402
430
}
403
- else {
404
- $ data .= HttpHelper::EOL ;
405
- }
406
-
407
- $ data .= (string ) $ partValue ->getRequest () . HttpHelper::EOL ;
408
431
}
409
432
$ data .= '-- ' . HttpHelper::MIME_BOUNDARY . '-- ' . HttpHelper::EOL . HttpHelper::EOL ;
410
433
0 commit comments