Skip to content

Commit 04105fb

Browse files
committed
added options to statement API
1 parent 19fcd8a commit 04105fb

File tree

3 files changed

+181
-3
lines changed

3 files changed

+181
-3
lines changed

lib/ArangoDBClient/Cursor.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ class Cursor implements \Iterator
102102
* whether or not the query result was served from the AQL query result cache
103103
*/
104104
private $_cached;
105+
106+
/**
107+
* precalculated number of documents in the cursor, as returned by the server
108+
*
109+
* @var int
110+
*/
111+
private $_count;
105112

106113
/**
107114
* result entry for cursor id
@@ -177,12 +184,17 @@ public function __construct(Connection $connection, array $data, array $options)
177184
$this->_connection = $connection;
178185
$this->data = $data;
179186
$this->_id = null;
187+
$this->_count = null;
180188
$this->_extra = [];
181189
$this->_cached = false;
182190

183191
if (isset($data[self::ENTRY_ID])) {
184192
$this->_id = $data[self::ENTRY_ID];
185193
}
194+
195+
if (isset($data[Statement::ENTRY_COUNT])) {
196+
$this->_count = $data[Statement::ENTRY_COUNT];
197+
}
186198

187199
if (isset($data[self::ENTRY_EXTRA])) {
188200
$this->_extra = $data[self::ENTRY_EXTRA];
@@ -249,6 +261,10 @@ public function delete()
249261
*/
250262
public function getCount()
251263
{
264+
if ($this->_count !== null) {
265+
return $this->_count;
266+
}
267+
252268
while ($this->_hasMore) {
253269
$this->fetchOutstanding();
254270
}

lib/ArangoDBClient/Statement.php

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ class Statement
116116
* @var bool
117117
*/
118118
private $_cache;
119+
120+
/**
121+
* Whether or not the query results should be build up dynamically and streamed to the
122+
* client application whenever available, or build up entirely on the server first and
123+
* then streamed incrementally (false)
124+
*
125+
* @var bool
126+
*/
127+
private $_stream;
128+
129+
/**
130+
* Number of seconds the cursor will be kept on the server if the statement result
131+
* is bigger than the initial batch size. The default value is a server-defined
132+
* value
133+
*
134+
* @var double
135+
*/
136+
private $_ttl;
119137

120138
/**
121139
* Whether or not the cache should abort when it encounters a warning
@@ -179,6 +197,16 @@ class Statement
179197
* Full count option index
180198
*/
181199
const FULL_COUNT = 'fullCount';
200+
201+
/**
202+
* Stream attribute
203+
*/
204+
const ENTRY_STREAM = 'stream';
205+
206+
/**
207+
* TTL attribute
208+
*/
209+
const ENTRY_TTL = 'ttl';
182210

183211
/**
184212
* Initialise the statement
@@ -208,6 +236,10 @@ public function __construct(Connection $connection, array $data)
208236
if (isset($data[self::ENTRY_QUERY])) {
209237
$this->setQuery($data[self::ENTRY_QUERY]);
210238
}
239+
240+
if (isset($data[self::ENTRY_STREAM])) {
241+
$this->setStream($data[self::ENTRY_STREAM]);
242+
}
211243

212244
if (isset($data[self::ENTRY_COUNT])) {
213245
$this->setCount($data[self::ENTRY_COUNT]);
@@ -216,6 +248,10 @@ public function __construct(Connection $connection, array $data)
216248
if (isset($data[self::ENTRY_BATCHSIZE])) {
217249
$this->setBatchSize($data[self::ENTRY_BATCHSIZE]);
218250
}
251+
252+
if (isset($data[self::ENTRY_TTL])) {
253+
$this->setTtl($data[self::ENTRY_TTL]);
254+
}
219255

220256
if (isset($data[self::ENTRY_BINDVARS])) {
221257
$this->_bindVars->set($data[self::ENTRY_BINDVARS]);
@@ -224,11 +260,11 @@ public function __construct(Connection $connection, array $data)
224260
if (isset($data[self::FULL_COUNT])) {
225261
$this->_fullCount = (bool) $data[Cursor::FULL_COUNT];
226262
}
227-
263+
228264
if (isset($data[Cursor::ENTRY_SANITIZE])) {
229265
$this->_sanitize = (bool) $data[Cursor::ENTRY_SANITIZE];
230266
}
231-
267+
232268
if (isset($data[self::ENTRY_RETRIES])) {
233269
$this->_retries = (int) $data[self::ENTRY_RETRIES];
234270
}
@@ -473,6 +509,50 @@ public function getFullCount()
473509
{
474510
return $this->_fullCount;
475511
}
512+
513+
/**
514+
* Set the ttl option for the statement
515+
*
516+
* @param double $value - value for ttl option
517+
*
518+
* @return void
519+
*/
520+
public function setTtl($value)
521+
{
522+
$this->_ttl = (double) $value;
523+
}
524+
525+
/**
526+
* Get the ttl option value of the statement
527+
*
528+
* @return double - current value of ttl option
529+
*/
530+
public function getTtl()
531+
{
532+
return $this->_ttl;
533+
}
534+
535+
/**
536+
* Set the streaming option for the statement
537+
*
538+
* @param bool $value - value for stream option
539+
*
540+
* @return void
541+
*/
542+
public function setStream($value)
543+
{
544+
$this->_stream = (bool) $value;
545+
}
546+
547+
/**
548+
* Get the streaming option value of the statement
549+
*
550+
* @return bool - current value of stream option
551+
*/
552+
public function getStream()
553+
{
554+
return $this->_stream;
555+
}
476556

477557
/**
478558
* Set the caching option for the statement
@@ -592,11 +672,19 @@ private function buildData()
592672
self::ENTRY_FAIL_ON_WARNING => $this->_failOnWarning
593673
]
594674
];
675+
676+
if ($this->_stream !== null) {
677+
$data['options'][self::ENTRY_STREAM] = $this->_stream;
678+
}
679+
680+
if ($this->_ttl !== null) {
681+
$data[self::ENTRY_TTL] = $this->_ttl;
682+
}
595683

596684
if ($this->_cache !== null) {
597685
$data[Cursor::ENTRY_CACHE] = $this->_cache;
598686
}
599-
687+
600688
if ($this->_memoryLimit > 0) {
601689
$data[self::ENTRY_MEMORY_LIMIT] = $this->_memoryLimit;
602690
}

tests/StatementTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,87 @@ public function testStatementWithFullCount()
465465
]
466466
);
467467

468+
static::assertTrue($statement->getCount());
469+
static::assertTrue($statement->getFullcount());
468470
$cursor = $statement->execute();
469471

470472
static::assertCount(0, $cursor->getWarnings());
471473
static::assertEquals(2, $cursor->getCount(), 'The number of results in the cursor should be 2');
472474
static::assertEquals(3, $cursor->getFullCount(), 'The fullCount should be 3');
473475
}
476+
477+
public function testTtl()
478+
{
479+
$connection = $this->connection;
480+
$collection = $this->collection;
481+
482+
$statement = new Statement(
483+
$connection, [
484+
'query' => 'FOR i IN 1..1000 INSERT { value: i } IN ' . $collection->getName()
485+
]
486+
);
487+
static::assertNull($statement->getTtl());
474488

489+
$cursor = $statement->execute();
490+
491+
$statement = new Statement(
492+
$connection, [
493+
'query' => 'FOR doc IN ' . $collection->getName() . ' RETURN doc',
494+
'ttl' => 1,
495+
'count' => true,
496+
'batchSize' => 100
497+
]
498+
);
499+
500+
static::assertEquals(1, $statement->getTtl());
501+
static::assertEquals(100, $statement->getBatchSize());
502+
static::assertTrue($statement->getCount());
503+
504+
$cursor = $statement->execute();
505+
static::assertEquals(1000, $cursor->getCount());
506+
507+
// let the cursor time out
508+
sleep(8);
509+
510+
$excepted = false;
511+
try {
512+
$all = $cursor->getAll();
513+
static::assertEquals(1000, count($all));
514+
} catch (\Exception $e) {
515+
$excepted = true;
516+
}
517+
518+
static::assertTrue($excepted);
519+
}
520+
521+
public function testStatementStreaming()
522+
{
523+
$connection = $this->connection;
524+
$collection = $this->collection;
525+
526+
$statement = new Statement(
527+
$connection, [
528+
'query' => 'FOR i IN 1..5000 INSERT { value: i } IN ' . $collection->getName()
529+
]
530+
);
531+
static::assertNull($statement->getStream());
532+
533+
$cursor = $statement->execute();
534+
535+
$statement = new Statement(
536+
$connection, [
537+
'query' => 'FOR doc IN ' . $collection->getName() . ' RETURN doc',
538+
'count' => false,
539+
'stream' => true
540+
]
541+
);
542+
543+
static::assertTrue($statement->getStream());
544+
545+
$cursor = $statement->execute();
546+
static::assertEquals(5000, count($cursor->getAll()));
547+
static::assertEquals(5000, $cursor->getCount());
548+
}
475549

476550
public function testBindReservedValue()
477551
{

0 commit comments

Comments
 (0)