File Coverage

blib/lib/ArangoDB2/Collection.pm
Criterion Covered Total %
statement 34 98 34.6
branch 3 12 25.0
condition 0 18 0.0
subroutine 12 42 28.5
pod 33 33 100.0
total 82 203 40.3


line stmt bran cond sub pod time code
1             package ArangoDB2::Collection;
2              
3 20     20   84 use strict;
  20         24  
  20         688  
4 20     20   74 use warnings;
  20         23  
  20         482  
5              
6 20         1388 use base qw(
7             ArangoDB2::Base
8 20     20   83 );
  20         28  
9              
10 20     20   94 use Data::Dumper;
  20         30  
  20         798  
11 20     20   91 use JSON::XS;
  20         21  
  20         757  
12              
13 20     20   7092 use ArangoDB2::Document;
  20         32  
  20         544  
14 20     20   6801 use ArangoDB2::Edge;
  20         39  
  20         445  
15 20     20   6605 use ArangoDB2::Index;
  20         34  
  20         35383  
16              
17             my $JSON = JSON::XS->new->utf8;
18              
19             # params that can be set when creating collection
20             my @PARAMS = qw(
21             doCompact isSystem isVolatile journalSize keyOptions name
22             numberOfShards shardKeys type waitForSync
23             );
24              
25              
26              
27             # checksum
28             #
29             # GET /_api/collection/{collection-name}/checksum
30             sub checksum
31             {
32 0     0 1 0 my($self, $args) = @_;
33             # process args
34 0         0 $args = $self->_build_args($args, ['withData','withRevisions']);
35             # make request
36 0         0 return $self->arango->http->get(
37             $self->api_path('collection', $self->name, 'checksum'),
38             $args,
39             );
40             }
41              
42             # count
43             #
44             # get/set count
45 0     0 1 0 sub count { shift->_get_set_bool('count', @_) }
46              
47             # create
48             #
49             # POST /_api/collection
50             #
51             # return self on success, undef on failure
52             sub create
53             {
54 0     0 1 0 my($self, $args) = @_;
55             # process args
56 0         0 $args = $self->_build_args($args, \@PARAMS);
57             # make request
58 0 0       0 my $res = $self->arango->http->post(
59             $self->api_path('collection'),
60             undef,
61             $JSON->encode($args),
62             ) or return;
63             # copy response data to instance
64 0         0 $self->_build_self($res, [@PARAMS, 'id']);
65              
66 0         0 return $self;
67             }
68              
69             # delete
70             #
71             # DELETE /_api/collection/{collection-name}
72             sub delete
73             {
74 0     0 1 0 my($self) = @_;
75              
76 0         0 return $self->arango->http->delete(
77             $self->api_path('collection', $self->name),
78             );
79             }
80              
81             # document
82             #
83             # get a specific ArangoDB2::Document by name (_key) or create a
84             # new blank ArangoDB2::Document
85             sub document
86             {
87 3     3 1 16 my($self, $name) = @_;
88              
89             # if name (_key) is passed then instantiate a new
90             # object with that name, which will retrieve the object
91 3 50       9 if (defined $name) {
92 0   0     0 return $self->documents->{$name} ||= ArangoDB2::Document->new(
93             $self->arango,
94             $self->database,
95             $self,
96             $name,
97             );
98             }
99             # otherwise create a new empty document that can be used to
100             # create a new document
101             else {
102 3         14 return ArangoDB2::Document->new(
103             $self->arango,
104             $self->database,
105             $self,
106             );
107             }
108             }
109              
110             # documents
111             #
112             # register of ArangoDB2::Document objects by name (_key)
113 0   0 0 1 0 sub documents { $_[0]->{documents} ||= {} }
114              
115             # documentCount
116             #
117             # GET /_api/collection/{collection-name}/count
118             sub documentCount
119             {
120 0     0 1 0 my($self) = @_;
121              
122 0         0 return $self->arango->http->get(
123             $self->api_path('collection', $self->name, 'count'),
124             );
125             }
126              
127             # doCompact
128             #
129             # get/set doCompact
130 0     0 1 0 sub doCompact { shift->_get_set_bool('doCompact', @_) }
131              
132             # edge
133             #
134             # get a specific ArangoDB2::Edge by name (_key) or create a
135             # new blank ArangoDB2::Edge
136             sub edge
137             {
138 1     1 1 5 my($self, $name) = @_;
139              
140             # if name (_key) is passed then instantiate a new
141             # object with that name, which will retrieve the object
142 1 50       3 if (defined $name) {
143 0   0     0 return $self->edges->{$name} ||= ArangoDB2::Edge->new(
144             $self->arango,
145             $self->database,
146             $self,
147             $name,
148             );
149             }
150             # otherwise create a new empty document that can be used to
151             # create a new document
152             else {
153 1         5 return ArangoDB2::Edge->new(
154             $self->arango,
155             $self->database,
156             $self,
157             );
158             }
159             }
160              
161             # edges
162             #
163             # register of ArangoDB2::Edge objects by name (_key)
164 0   0 0 1 0 sub edges { $_[0]->{edges} ||= {} }
165              
166             # excludeSystem
167             #
168             # get/set excludeSystem
169 0     0 1 0 sub excludeSystem { shift->_get_set_bool('excludeSystem', @_) }
170              
171             # figures
172             #
173             # GET /_api/collection/{collection-name}/figures
174             sub figures
175             {
176 0     0 1 0 my($self) = @_;
177              
178 0         0 return $self->arango->http->get(
179             $self->api_path('collection', $self->name, 'figures'),
180             );
181             }
182              
183             # index
184             #
185             # get an ArangoDB::Index by name or create new empty instance
186             sub index
187             {
188 1     1 1 8 my($self, $name) = @_;
189              
190             # if name then create/retrieve named instance
191 1 50       5 if (defined $name) {
192 0   0     0 return $self->indexes->{$name} ||= ArangoDB2::Index->new(
193             $self->arango,
194             $self->database,
195             $self,
196             $name,
197             );
198             }
199             # otherwise create a new empty instance
200             else {
201 1         10 return ArangoDB2::Index->new(
202             $self->arango,
203             $self->database,
204             $self,
205             );
206             }
207             }
208              
209             # indexes
210             #
211             # register of ArangoDB2::Index objects by name
212 0   0 0 1 0 sub indexes { $_[0]->{indexes} ||= {} }
213              
214             # info
215             #
216             # GET /_api/collection/{collection-name}
217             sub info
218             {
219 0     0 1 0 my($self) = @_;
220              
221 0         0 return $self->arango->http->get(
222             $self->api_path('collection', $self->name),
223             );
224             }
225              
226             # isSystem
227             #
228             # get/set isSystem
229 0     0 1 0 sub isSystem { shift->_get_set_bool('isSystem', @_) }
230              
231             # isVolatile
232             #
233             # get/set isVolatile
234 0     0 1 0 sub isVolatile { shift->_get_set_bool('isVolatile', @_) }
235              
236             # journalSize
237             #
238             # get/set journalSize
239 0     0 1 0 sub journalSize { shift->_get_set('journalSize', @_) }
240              
241             # keyOptions
242             #
243             # get/set keyOptions
244 0     0 1 0 sub keyOptions { shift->_get_set('keyOptions', @_) }
245              
246             # list
247             #
248             # GET /_api/collection
249             sub list
250             {
251 0     0 1 0 my($self, $args) = @_;
252             # process args
253 0         0 $args = $self->_build_args($args, ['excludeSystem']);
254             # make request
255 0         0 return $self->arango->http->get(
256             $self->api_path('collection'),
257             $args,
258             );
259             }
260              
261             # load
262             #
263             # PUT /_api/collection/{collection-name}/load
264             sub load
265             {
266 0     0 1 0 my($self, $args) = @_;
267             # process args
268 0         0 $args = $self->_build_args($args, ['count']);
269             # make request
270 0         0 return $self->arango->http->put(
271             $self->api_path('collection', $self->name, 'load'),
272             $args,
273             );
274             }
275              
276             # numberOfShards
277             #
278             # get/set numberOfShards
279 0     0 1 0 sub numberOfShards { shift->_get_set('numberOfShards', @_) }
280              
281             # properties
282             #
283             # GET /_api/collection/{collection-name}/properties
284             #
285             # or
286             #
287             # PUT /_api/collection/{collection-name}/properties
288             sub properties
289             {
290 0     0 1 0 my($self, $args) = @_;
291             # process args
292 0         0 $args = $self->_build_args($args, ['journalSize', 'waitForSync']);
293             # build path
294 0         0 my $path = $self->api_path('collection', $self->name, 'properties');
295             # make request
296 0 0       0 my $res = %$args
297             # if args are passed then set with PUT
298             ? $self->arango->http->put($path, undef, $JSON->encode($args))
299             # otherwise get properties
300             : $self->arango->http->get($path);
301             # copy response data to instance
302 0         0 $self->_build_self($res, \@PARAMS);
303              
304 0         0 return $self;
305             }
306              
307             # rename
308             #
309             # PUT /_api/collection/{collection-name}/rename
310             sub rename
311             {
312 0     0 1 0 my($self, $args) = @_;
313             # make a copy of current name
314 0         0 my $old_name = $self->name;
315             # process args
316 0         0 $args = $self->_build_args($args, ['name']);
317              
318 0         0 my $res = $self->arango->http->put(
319             $self->api_path('collection', $self->name, 'rename'),
320             undef,
321             $JSON->encode($args),
322             );
323              
324             # if rename successful apply changes locally
325 0 0 0     0 if ($res && $res->{name} eq $args->{name}) {
326             # change internal name
327 0         0 $self->name($res->{name});
328             # unregister old name
329 0         0 delete $self->database->collections->{$old_name};
330             # register new name
331 0         0 $self->database->collections->{ $res->{name} } = $self;
332             }
333              
334 0         0 return $self;
335             }
336              
337             # revision
338             #
339             # GET /_api/collection/{collection-name}/revision
340             sub revision
341             {
342 0     0 1 0 my($self) = @_;
343              
344 0         0 return $self->arango->http->get(
345             $self->api_path('collection', $self->name, 'revision'),
346             );
347             }
348              
349             # rotate
350             #
351             # PUT /_api/collection/{collection-name}/rotate
352             sub rotate
353             {
354 0     0 1 0 my($self) = @_;
355              
356 0         0 return $self->arango->http->put(
357             $self->api_path('collection', $self->name, 'rotate'),
358             );
359             }
360              
361             # shardKeys
362             #
363             # get/set shardKeys
364 0     0 1 0 sub shardKeys { shift->_get_set('shardKeys', @_) }
365              
366             # truncate
367             #
368             # PUT /_api/collection/{collection-name}/truncate
369             sub truncate
370             {
371 0     0 1 0 my($self) = @_;
372              
373 0         0 return $self->arango->http->put(
374             $self->api_path('collection', $self->name, 'truncate'),
375             );
376             }
377              
378             # type
379             #
380             # get/set type
381 0     0 1 0 sub type { shift->_get_set('type', @_) }
382              
383             # unload
384             #
385             # PUT /_api/collection/{collection-name}/unload
386             sub unload
387             {
388 0     0 1 0 my($self) = @_;
389              
390 0         0 return $self->arango->http->put(
391             $self->api_path('collection', $self->name, 'unload'),
392             );
393             }
394              
395             # waitForSync
396             #
397             # get/set waitForSync
398 0     0 1 0 sub waitForSync { shift->_get_set_bool('waitForSync', @_) }
399              
400             # withData
401             #
402             # get/set withData
403 0     0 1 0 sub withData { shift->_get_set_bool('withData', @_) }
404              
405             # withRevisions
406             #
407             # get/set withRevisions
408 0     0 1 0 sub withRevisions { shift->_get_set_bool('withRevisions', @_) }
409              
410             # _class
411             #
412             # internal name for class
413 5     5   21 sub _class { 'collection' }
414              
415             1;
416              
417             __END__