Skip to content

Commit 966ebf3

Browse files
author
Jan Steemann
committed
added optional restrictions for export API
1 parent 2c2e9ba commit 966ebf3

File tree

2 files changed

+274
-2
lines changed

2 files changed

+274
-2
lines changed

lib/triagens/ArangoDb/Export.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ class Export
6262
*/
6363
private $_type;
6464

65+
/**
66+
* export restrictions - either null for no restrictions or an array with a "type" and a "fields" index
67+
*
68+
* @var mixed
69+
*/
70+
private $_restrictions;
71+
6572
/**
6673
* Count option index
6774
*/
@@ -77,6 +84,11 @@ class Export
7784
*/
7885
const ENTRY_FLUSH = 'flush';
7986

87+
/**
88+
* Export restrictions
89+
*/
90+
const ENTRY_RESTRICT = 'restrict';
91+
8092
/**
8193
* Initialize the export
8294
*
@@ -107,6 +119,26 @@ public function __construct(Connection $connection, $collection, array $data = a
107119
if (isset($data[self::ENTRY_BATCHSIZE])) {
108120
$this->setBatchSize($data[self::ENTRY_BATCHSIZE]);
109121
}
122+
123+
if (isset($data[self::ENTRY_RESTRICT]) &&
124+
is_array($data[self::ENTRY_RESTRICT])) {
125+
$restrictions = $data[self::ENTRY_RESTRICT];
126+
127+
if (! isset($restrictions["type"]) ||
128+
! in_array($restrictions["type"], array("include", "exclude"), true)) {
129+
// validate restrictions.type
130+
throw new ClientException('Invalid restrictions type definition');
131+
}
132+
133+
if (! isset($restrictions["fields"]) ||
134+
! is_array($restrictions["fields"])) {
135+
// validate restrictions.fields
136+
throw new ClientException('Invalid restrictions fields definition');
137+
}
138+
139+
// all valid
140+
$this->_restrictions = $restrictions;
141+
}
110142

111143
if (isset($data[ExportCursor::ENTRY_FLAT])) {
112144
$this->_flat = (bool) $data[ExportCursor::ENTRY_FLAT];
@@ -142,11 +174,15 @@ public function execute()
142174
$data[self::ENTRY_BATCHSIZE] = $this->_batchSize;
143175
}
144176

177+
if (is_array($this->_restrictions)) {
178+
$data[self::ENTRY_RESTRICT] = $this->_restrictions;
179+
}
180+
145181
$collection = $this->_collection;
146182
if ($collection instanceof Collection) {
147183
$collection = $collection->getName();
148-
}
149-
184+
}
185+
150186
$url = UrlHelper::appendParamsUrl(Urls::URL_EXPORT, array("collection" => $collection));
151187
$response = $this->_connection->post($url, $this->getConnection()->json_encode_wrapper($data));
152188

tests/ExportTest.php

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,242 @@ public function testExportFlat()
302302

303303
$this->assertFalse($cursor->getNextBatch());
304304
}
305+
306+
/**
307+
* Test export with include restriction
308+
*/
309+
public function testExportRestrictInclude()
310+
{
311+
if (! $this->hasExportApi) {
312+
return;
313+
}
314+
for ($i = 0; $i < 200; ++$i) {
315+
$this->documentHandler->save($this->collection, array("value1" => $i, "value2" => "test" . $i));
316+
}
317+
318+
$export = new Export($this->connection, $this->collection, array(
319+
"batchSize" => 50,
320+
"_flat" => true,
321+
"restrict" => array("type" => "include", "fields" => array("_key", "value2"))
322+
));
323+
$cursor = $export->execute();
324+
325+
$this->assertEquals(1, $cursor->getFetches());
326+
$this->assertNotNull($cursor->getId());
327+
328+
$this->assertEquals(200, $cursor->getCount());
329+
$this->assertEquals(1, $cursor->getFetches());
330+
331+
$all = array();
332+
while ($more = $cursor->getNextBatch()) {
333+
$all = array_merge($all, $more);
334+
}
335+
$this->assertEquals(200, count($all));
336+
337+
foreach ($all as $doc) {
338+
$this->assertTrue(is_array($doc));
339+
$this->assertEquals(2, count($doc));
340+
$this->assertFalse(isset($doc["_id"]));
341+
$this->assertTrue(isset($doc["_key"]));
342+
$this->assertFalse(isset($doc["_rev"]));
343+
$this->assertFalse(isset($doc["value1"]));
344+
$this->assertTrue(isset($doc["value2"]));
345+
}
346+
347+
$this->assertFalse($cursor->getNextBatch());
348+
}
349+
350+
/**
351+
* Test export with include restriction
352+
*/
353+
public function testExportRestrictIncludeNonExisting()
354+
{
355+
if (! $this->hasExportApi) {
356+
return;
357+
}
358+
for ($i = 0; $i < 200; ++$i) {
359+
$this->documentHandler->save($this->collection, array("value1" => $i, "value2" => "test" . $i));
360+
}
361+
362+
$export = new Export($this->connection, $this->collection, array(
363+
"batchSize" => 50,
364+
"_flat" => true,
365+
"restrict" => array("type" => "include", "fields" => array("foobar", "baz"))
366+
));
367+
$cursor = $export->execute();
368+
369+
$this->assertEquals(1, $cursor->getFetches());
370+
$this->assertNotNull($cursor->getId());
371+
372+
$this->assertEquals(200, $cursor->getCount());
373+
$this->assertEquals(1, $cursor->getFetches());
374+
375+
$all = array();
376+
while ($more = $cursor->getNextBatch()) {
377+
$all = array_merge($all, $more);
378+
}
379+
$this->assertEquals(200, count($all));
380+
381+
foreach ($all as $doc) {
382+
$this->assertTrue(is_array($doc));
383+
$this->assertEquals(array(), $doc);
384+
}
385+
386+
$this->assertFalse($cursor->getNextBatch());
387+
}
388+
389+
/**
390+
* Test export with exclude restriction
391+
*/
392+
public function testExportRestrictExclude()
393+
{
394+
if (! $this->hasExportApi) {
395+
return;
396+
}
397+
for ($i = 0; $i < 200; ++$i) {
398+
$this->documentHandler->save($this->collection, array("value1" => $i, "value2" => "test" . $i));
399+
}
400+
401+
$export = new Export($this->connection, $this->collection, array(
402+
"batchSize" => 50,
403+
"_flat" => true,
404+
"restrict" => array("type" => "exclude", "fields" => array("_key", "value2"))
405+
));
406+
$cursor = $export->execute();
407+
408+
$this->assertEquals(1, $cursor->getFetches());
409+
$this->assertNotNull($cursor->getId());
410+
411+
$this->assertEquals(200, $cursor->getCount());
412+
$this->assertEquals(1, $cursor->getFetches());
413+
414+
$all = array();
415+
while ($more = $cursor->getNextBatch()) {
416+
$all = array_merge($all, $more);
417+
}
418+
$this->assertEquals(200, count($all));
419+
420+
foreach ($all as $doc) {
421+
$this->assertTrue(is_array($doc));
422+
$this->assertEquals(3, count($doc));
423+
$this->assertFalse(isset($doc["_key"]));
424+
$this->assertTrue(isset($doc["_rev"]));
425+
$this->assertTrue(isset($doc["_id"]));
426+
$this->assertTrue(isset($doc["value1"]));
427+
$this->assertFalse(isset($doc["value2"]));
428+
}
429+
430+
$this->assertFalse($cursor->getNextBatch());
431+
}
432+
433+
/**
434+
* Test export with non-existing fields restriction
435+
*/
436+
public function testExportRestrictExcludeNonExisting()
437+
{
438+
if (! $this->hasExportApi) {
439+
return;
440+
}
441+
for ($i = 0; $i < 200; ++$i) {
442+
$this->documentHandler->save($this->collection, array("value1" => $i, "value2" => "test" . $i));
443+
}
444+
445+
$export = new Export($this->connection, $this->collection, array(
446+
"batchSize" => 50,
447+
"_flat" => true,
448+
"restrict" => array("type" => "include", "fields" => array("_id", "foobar", "baz"))
449+
));
450+
$cursor = $export->execute();
451+
452+
$this->assertEquals(1, $cursor->getFetches());
453+
$this->assertNotNull($cursor->getId());
454+
455+
$this->assertEquals(200, $cursor->getCount());
456+
$this->assertEquals(1, $cursor->getFetches());
457+
458+
$all = array();
459+
while ($more = $cursor->getNextBatch()) {
460+
$all = array_merge($all, $more);
461+
}
462+
$this->assertEquals(200, count($all));
463+
464+
foreach ($all as $doc) {
465+
$this->assertTrue(is_array($doc));
466+
$this->assertEquals(1, count($doc));
467+
$this->assertTrue(isset($doc["_id"]));
468+
$this->assertFalse(isset($doc["foobar"]));
469+
}
470+
471+
$this->assertFalse($cursor->getNextBatch());
472+
}
473+
474+
/**
475+
* Test export with invalid restriction definition
476+
*
477+
* @expectedException \triagens\ArangoDb\ClientException
478+
*/
479+
public function testExportRestrictInvalidType()
480+
{
481+
if (! $this->hasExportApi) {
482+
return;
483+
}
484+
485+
$export = new Export($this->connection, $this->collection, array(
486+
"restrict" => array("type" => "foo", "fields" => array("_key"))
487+
));
488+
$cursor = $export->execute();
489+
}
490+
491+
/**
492+
* Test export with invalid restriction definition
493+
*
494+
* @expectedException \triagens\ArangoDb\ClientException
495+
*/
496+
public function testExportRestrictMissingType()
497+
{
498+
if (! $this->hasExportApi) {
499+
return;
500+
}
501+
502+
$export = new Export($this->connection, $this->collection, array(
503+
"restrict" => array("fields" => array("_key"))
504+
));
505+
$cursor = $export->execute();
506+
}
507+
508+
/**
509+
* Test export with invalid restriction definition
510+
*
511+
* @expectedException \triagens\ArangoDb\ClientException
512+
*/
513+
public function testExportRestrictInvalidFields()
514+
{
515+
if (! $this->hasExportApi) {
516+
return;
517+
}
518+
519+
$export = new Export($this->connection, $this->collection, array(
520+
"restrict" => array("type" => "include", "fields" => "foo")
521+
));
522+
$cursor = $export->execute();
523+
}
524+
525+
/**
526+
* Test export with invalid restriction definition
527+
*
528+
* @expectedException \triagens\ArangoDb\ClientException
529+
*/
530+
public function testExportRestrictMissingFields()
531+
{
532+
if (! $this->hasExportApi) {
533+
return;
534+
}
535+
536+
$export = new Export($this->connection, $this->collection, array(
537+
"restrict" => array("type" => "include")
538+
));
539+
$cursor = $export->execute();
540+
}
305541

306542
public function tearDown()
307543
{

0 commit comments

Comments
 (0)