-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathCollection.php
934 lines (812 loc) · 21.1 KB
/
Collection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
<?php
/**
* ArangoDB PHP client: single collection
*
* @package ArangoDBClient
* @author Jan Steemann
* @copyright Copyright 2012, triagens GmbH, Cologne, Germany
*/
namespace ArangoDBClient;
/**
* Value object representing a collection
*
* <br>
*
* @package ArangoDBClient
* @since 0.2
*/
class Collection
{
/**
* The collection id (might be NULL for new collections)
*
* @var mixed - collection id
*/
private $_id;
/**
* The collection name (might be NULL for new collections)
*
* @var string - collection name
*/
private $_name;
/**
* The collection type (might be NULL for new collections)
*
* @var int - collection type
*/
private $_type;
/**
* The collection waitForSync value (might be NULL for new collections)
*
* @var bool - waitForSync value
*/
private $_waitForSync;
/**
* The collection isSystem value (might be NULL for new collections)
*
* @var bool - isSystem value
*/
private $_isSystem;
/**
* The distributeShardsLike value (might be NULL for new collections)
*
* @var mixed - distributeShardsLike value
*/
private $_distributeShardsLike;
/**
* The collection numberOfShards value (might be NULL for new collections)
*
* @var mixed - numberOfShards value
*/
private $_numberOfShards;
/**
* The replicationFactor value (might be NULL for new collections)
*
* @var mixed - replicationFactor value
*/
private $_replicationFactor;
/**
* The write concern for writes to be considered successful
*
* @var mixed - write concern value
*/
private $_writeConcern;
/**
* The shardingStrategy value (might be NULL for new collections)
*
* @var mixed - shardingStrategy value
*/
private $_shardingStrategy;
/**
* The collection shardKeys value (might be NULL for new collections)
*
* @var array - shardKeys value
*/
private $_shardKeys;
/**
* The smartJoinAttribute value (might be NULL for new collections)
*
* @var mixed - smartJoinAttribute value
*/
private $_smartJoinAttribute;
/**
* The collection status value
*
* @var int - status value
*/
private $_status;
/**
* The collection keyOptions value
*
* @var array - keyOptions value
*/
private $_keyOptions;
/**
* The collection schema value
*
* @var mixed - schema
*/
private $_schema;
/**
* Collection id index
*/
const ENTRY_ID = 'id';
/**
* Collection name index
*/
const ENTRY_NAME = 'name';
/**
* Collection type index
*/
const ENTRY_TYPE = 'type';
/**
* Collection 'waitForSync' index
*/
const ENTRY_WAIT_SYNC = 'waitForSync';
/**
* Collection 'status' index
*/
const ENTRY_STATUS = 'status';
/**
* Collection 'keyOptions' index
*/
const ENTRY_KEY_OPTIONS = 'keyOptions';
/**
* Collection 'schema' index
*/
const ENTRY_SCHEMA = 'schema';
/**
* Collection 'isSystem' index
*/
const ENTRY_IS_SYSTEM = 'isSystem';
/**
* Collection 'distributeShardsLike' index
*/
const ENTRY_DISTRIBUTE_SHARDS_LIKE = 'distributeShardsLike';
/**
* Collection 'numberOfShards' index
*/
const ENTRY_NUMBER_OF_SHARDS = 'numberOfShards';
/**
* Collection 'replicationFactor' index
*/
const ENTRY_REPLICATION_FACTOR = 'replicationFactor';
/**
* Collection 'writeConcern' index
*/
const ENTRY_WRITE_CONCERN = 'writeConcern';
/**
* Collection 'sharding' index
*/
const ENTRY_SHARDING = 'sharding';
/**
* Collection 'shardingStrategy' index
*/
const ENTRY_SHARDING_STRATEGY = 'shardingStrategy';
/**
* Collection 'shardKeys' index
*/
const ENTRY_SHARD_KEYS = 'shardKeys';
/**
* Collection 'smartJoinAttribute' index
*/
const ENTRY_SMART_JOIN_ATTRIBUTE = 'smartJoinAttribute';
/**
* properties option
*/
const OPTION_PROPERTIES = 'properties';
/**
* document collection type
*/
const TYPE_DOCUMENT = 2;
/**
* edge collection type
*/
const TYPE_EDGE = 3;
/**
* New born collection
*/
const STATUS_NEW_BORN = 1;
/**
* Unloaded collection
*/
const STATUS_UNLOADED = 2;
/**
* Loaded collection
*/
const STATUS_LOADED = 3;
/**
* Collection being unloaded
*/
const STATUS_BEING_UNLOADED = 4;
/**
* Deleted collection
*/
const STATUS_DELETED = 5;
/**
* Constructs an empty collection
*
* @param string $name - name for the collection
*
* @throws \ArangoDBClient\ClientException
*/
public function __construct($name = null)
{
if ($name !== null) {
$this->setName($name);
}
}
/**
* Factory method to construct a new collection
*
* @throws ClientException
*
* @param array $values - initial values for collection
*
* @return Collection
*/
public static function createFromArray(array $values)
{
$collection = new self();
foreach ($values as $key => $value) {
$collection->set($key, $value);
}
return $collection;
}
/**
* Get the default collection type
*
* @return string - name
*/
public static function getDefaultType()
{
return self::TYPE_DOCUMENT;
}
/**
* Clone a collection
*
* Returns the clone
*
* @magic
*
* @return void
*/
public function __clone()
{
$this->_id = null;
$this->_name = null;
$this->_waitForSync = null;
$this->_isSystem = null;
$this->_distributeShardsLike = null;
$this->_numberOfShards = null;
$this->_replicationFactor = null;
$this->_writeConcern = null;
$this->_shardingStrategy = null;
$this->_shardKeys = null;
$this->_smartJoinAttribute = null;
$this->_schema = null;
}
/**
* Get a string representation of the collection
*
* Returns the collection as JSON-encoded string
*
* @magic
*
* @return string - JSON-encoded collection
*/
public function __toString()
{
return $this->toJson();
}
/**
* Returns the collection as JSON-encoded string
*
* @return string - JSON-encoded collection
*/
public function toJson()
{
return json_encode($this->getAll());
}
/**
* Returns the collection as a serialized string
*
* @return string - PHP serialized collection
*/
public function toSerialized()
{
return serialize($this->getAll());
}
/**
* Get all collection attributes
*
* @return array - array of all collection attributes
*/
public function getAll()
{
$result = [
self::ENTRY_ID => $this->_id,
self::ENTRY_NAME => $this->_name,
self::ENTRY_WAIT_SYNC => $this->_waitForSync,
self::ENTRY_IS_SYSTEM => $this->_isSystem,
self::ENTRY_TYPE => $this->_type,
self::ENTRY_STATUS => $this->_status,
self::ENTRY_KEY_OPTIONS => $this->_keyOptions,
self::ENTRY_SCHEMA => $this->_schema
];
if (null !== $this->_distributeShardsLike) {
$result[self::ENTRY_DISTRIBUTE_SHARDS_LIKE] = $this->_distributeShardsLike;
}
if (null !== $this->_numberOfShards) {
$result[self::ENTRY_NUMBER_OF_SHARDS] = $this->_numberOfShards;
}
if (null !== $this->_replicationFactor) {
$result[self::ENTRY_REPLICATION_FACTOR] = $this->_replicationFactor;
}
if (null !== $this->_writeConcern) {
$result[self::ENTRY_WRITE_CONCERN] = $this->_writeConcern;
}
if (null !== $this->_shardingStrategy) {
$result[self::ENTRY_SHARDING_STRATEGY] = $this->_shardingStrategy;
}
if (is_array($this->_shardKeys)) {
$result[self::ENTRY_SHARD_KEYS] = $this->_shardKeys;
}
if (null !== $this->_smartJoinAttribute) {
$result[self::ENTRY_SMART_JOIN_ATTRIBUTE] = $this->_smartJoinAttribute;
}
$result[self::ENTRY_SCHEMA] = $this->_schema;
return $result;
}
/**
* Set a collection attribute
*
* The key (attribute name) must be a string.
*
* This will validate the value of the attribute and might throw an
* exception if the value is invalid.
*
* @throws ClientException
*
* @param string $key - attribute name
* @param mixed $value - value for attribute
*
* @return void
*/
public function set($key, $value)
{
if (!is_string($key)) {
throw new ClientException('Invalid collection attribute type');
}
if ($key === self::ENTRY_ID) {
$this->setId($value);
return;
}
if ($key === self::ENTRY_NAME) {
$this->setName($value);
return;
}
if ($key === self::ENTRY_WAIT_SYNC) {
$this->setWaitForSync($value);
return;
}
if ($key === self::ENTRY_IS_SYSTEM) {
$this->setIsSystem($value);
return;
}
if ($key === self::ENTRY_TYPE) {
$this->setType($value);
return;
}
if ($key === self::ENTRY_STATUS) {
$this->setStatus($value);
return;
}
if ($key === self::ENTRY_KEY_OPTIONS) {
$this->setKeyOptions($value);
return;
}
if ($key === self::ENTRY_SCHEMA) {
$this->setSchema($value);
return;
}
if ($key === self::ENTRY_DISTRIBUTE_SHARDS_LIKE) {
$this->setDistributeShardsLike($value);
return;
}
if ($key === self::ENTRY_NUMBER_OF_SHARDS) {
$this->setNumberOfShards($value);
return;
}
if ($key === self::ENTRY_REPLICATION_FACTOR) {
$this->setReplicationFactor($value);
return;
}
if ($key === self::ENTRY_WRITE_CONCERN) {
$this->setWriteConcern($value);
return;
}
if ($key === self::ENTRY_SHARDING_STRATEGY) {
$this->setShardingStrategy($value);
return;
}
if ($key === self::ENTRY_SHARD_KEYS) {
$this->setShardKeys($value);
return;
}
if ($key === self::ENTRY_SMART_JOIN_ATTRIBUTE) {
$this->setSmartJoinAttribute($value);
return;
}
// unknown attribute, will be ignored
}
/**
* Set the collection id
*
* This will throw if the id of an existing collection gets updated to some other id
*
* @throws ClientException
*
* @param mixed $id - collection id
*
* @return bool
*/
public function setId($id)
{
if ($this->_id !== null && $this->_id !== $id) {
throw new ClientException('Should not update the id of an existing collection');
}
return $this->_id = (string) $id;
}
/**
* Get the collection id (if already known)
*
* Collection ids are generated on the server only.
*
* Collection ids are numeric but might be bigger than PHP_INT_MAX.
* To reliably store a collection id elsewhere, a PHP string should be used
*
* @return mixed - collection id, might be NULL if collection does not yet have an id
*/
public function getId()
{
return $this->_id;
}
/**
* Set the collection name
*
* @throws ClientException
*
* @param string $name - name
*
* @return void
*/
public function setName($name)
{
assert(is_string($name));
if ($this->_name !== null && $this->_name !== $name) {
throw new ClientException('Should not update the name of an existing collection');
}
$this->_name = (string) $name;
}
/**
* Get the collection name (if already known)
*
* @return string - name
*/
public function getName()
{
return $this->_name;
}
/**
* Set the collection schema
*
* @param mixed $schema - schema
*
* @return void
*/
public function setSchema($schema)
{
assert(is_null($schema) || is_array($schema));
$this->_schema = $schema;
}
/**
* Get the collection schema (if any)
*
* @return mixed - schema
*/
public function getSchema()
{
return $this->_schema;
}
/**
* Set the collection type.
*
* This is useful before a collection is create() 'ed in order to set a different type than the normal one.
* For example this must be set to 3 in order to create an edge-collection.
*
* @throws ClientException
*
* @param int $type - type = 2 -> normal collection, type = 3 -> edge-collection
*
* @return void
*/
public function setType($type)
{
if ($type === 'document') {
$type = self::TYPE_DOCUMENT;
} else if ($type === 'edge') {
$type = self::TYPE_EDGE;
}
if ($this->_type !== null && $this->_type !== $type) {
throw new ClientException('Should not update the type of an existing collection');
}
if ($type !== self::TYPE_DOCUMENT && $type !== self::TYPE_EDGE) {
throw new ClientException('Invalid type used for collection');
}
assert(is_int($type));
$this->_type = $type;
}
/**
* Get the collection type (if already known)
*
* @return string - name
*/
public function getType()
{
return $this->_type;
}
/**
* Set the collection status.
*
* This is useful before a collection is create()'ed in order to set a status.
*
* @throws ClientException
*
* @param int $status - statuses = 1 -> new born, status = 2 -> unloaded, status = 3 -> loaded, status = 4 -> being unloaded, status = 5 -> deleted
*
* @return void
*/
public function setStatus($status)
{
assert(is_int($status));
if ($this->_status !== null && $this->_status !== $status) {
throw new ClientException('Should not update the status of an existing collection');
}
if (!in_array(
$status,
[
self::STATUS_NEW_BORN,
self::STATUS_UNLOADED,
self::STATUS_LOADED,
self::STATUS_BEING_UNLOADED,
self::STATUS_DELETED
], true
)
) {
throw new ClientException('Invalid status used for collection');
}
$this->_status = $status;
}
/**
* Get the collection status (if already known)
*
* @return int - status
*/
public function getStatus()
{
return $this->_status;
}
/**
* Set the collection key options.
*
* @throws ClientException
*
* @param array $keyOptions - An associative array containing optional keys: type, allowUserKeys, increment, offset.
*
* @return void
*/
public function setKeyOptions($keyOptions)
{
assert(is_array($keyOptions));
$this->_keyOptions = $keyOptions;
}
/**
* Get the collection key options (if already known)
*
* @return array - keyOptions
*/
public function getKeyOptions()
{
return $this->_keyOptions;
}
/**
* Set the waitForSync value
*
* @param bool $value - waitForSync value
*
* @return void
*/
public function setWaitForSync($value)
{
assert(null === $value || is_bool($value));
$this->_waitForSync = $value;
}
/**
* Get the waitForSync value (if already known)
*
* @return bool - waitForSync value
*/
public function getWaitForSync()
{
return $this->_waitForSync;
}
/**
* Set the isSystem value
*
* @param bool $value - isSystem: false->user collection, true->system collection
*
* @return void
*/
public function setIsSystem($value)
{
assert(null === $value || is_bool($value));
$this->_isSystem = $value;
}
/**
* Get the isSystem value (if already known)
*
* @return bool - isSystem value
*/
public function getIsSystem()
{
return $this->_isSystem;
}
/**
* Set the distribute shards like value
*
* @param string $value - distributeShardsLike value
*
* @return void
*/
public function setDistributeShardsLike($value)
{
$this->_distributeShardsLike = $value;
}
/**
* Get the distributeShardsLike (if already known)
*
* @return mixed - distributeShardsLike value
*/
public function getDistributeShardsLike()
{
return $this->_distributeShardsLike;
}
/**
* Set the numberOfShards value
*
* @param int $value - numberOfShards value
*
* @return void
*/
public function setNumberOfShards($value)
{
assert(null === $value || is_numeric($value));
$this->_numberOfShards = $value;
}
/**
* Get the numberOfShards value (if already known)
*
* @return mixed - numberOfShards value
*/
public function getNumberOfShards()
{
return $this->_numberOfShards;
}
/**
* Set the replicationFactor value
*
* @param mixed $value - replicationFactor value (either a number, or "satellite")
*
* @return void
*/
public function setReplicationFactor($value)
{
assert(null === $value || is_numeric($value) || $value === 'satellite');
$this->_replicationFactor = $value;
}
/**
* Get the replicationFactor value (if already known)
*
* @return mixed - replicationFactor value
*/
public function getReplicationFactor()
{
return $this->_replicationFactor;
}
/**
* Set the write concern value
*
* @param int $value - write concern value
*
* @return void
*/
public function setWriteConcern($value)
{
assert(null === $value || is_numeric($value));
$this->_writeConcern = $value;
}
/**
* Set the write concern value
*
* @param int $value - write concern value
*
* @deprecated use setWriteConcern() instead
* @return void
*/
public function setMinReplicationFactor($value)
{
$this->setWriteConcern($value);
}
/**
* Get the write concern value (if already known)
*
* @return mixed - write concern value
*/
public function getWriteConcern()
{
return $this->_writeConcern;
}
/**
* Get the write concern value (if already known). this is an alias only
*
* @deprecated use getWriteConcern() instead
* @return mixed - write concern value
*/
public function getMinReplicationFactor()
{
return $this->getWriteConcern();
}
/**
* Set the shardingStrategy value
*
* @param string $value - shardingStrategy value
*
* @return void
*/
public function setShardingStrategy($value)
{
$this->_shardingStrategy = $value;
}
/**
* Get the sharding strategy value (if already known)
*
* @return mixed - shardingStrategy value
*/
public function getShardingStrategy()
{
return $this->_shardingStrategy;
}
/**
* Set the shardKeys value
*
* @param array $value - shardKeys value
*
* @return void
*/
public function setShardKeys($value)
{
assert(null === $value || is_array($value));
$this->_shardKeys = $value;
}
/**
* Get the shardKeys value (if already known)
*
* @return array - shardKeys value
*/
public function getShardKeys()
{
return $this->_shardKeys;
}
/**
* Set the smart join attribute value
*
* @param string $value - smartJoinAttribute value
*
* @return void
*/
public function setSmartJoinAttribute($value)
{
$this->_smartJoinAttribute = $value;
}
/**
* Get the smart join attribute value (if already known)
*
* @return mixed - smart join attribute value
*/
public function getSmartJoinAttribute()
{
return $this->_smartJoinAttribute;
}
}
class_alias(Collection::class, '\triagens\ArangoDb\Collection');