Zing/Store/Disk.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 167 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Zing::Store::Disk;
2  
3 use 5.014;
4  
5 use strict;
6 use warnings;
7  
8 use registry 'Zing::Types';
9 use routines;
10  
11 use Data::Object::Class;
12 use Data::Object::ClassHas;
13  
14 extends 'Zing::Store';
15  
16 use File::Spec;
17  
18 our $VERSION = '0.26'; # VERSION
19  
20 # ATTRIBUTES
21  
22 has root => (
23 is => 'ro',
24 isa => 'Str',
25 new => 1,
26 );
27  
28 fun new_root($self) {
29 File::Spec->curdir
30 }
31  
32 # BUILDERS
33  
34 fun new_encoder($self) {
35 require Zing::Encoder::Dump; Zing::Encoder::Dump->new;
36 }
37  
38 # METHODS
39  
40 method drop(Str $key) {
41 return int(!!unlink $self->path($key));
42 }
43  
44 method keys(Key $query) {
45 my @paths = glob(File::Spec->catfile(
46 $self->root, (map +($_ || '*'), (split(':', $query))[0..4])
47 ));
48 return [
49 map {
50 join(':', (reverse((reverse(File::Spec->splitdir($_)))[0..4])))
51 }
52 grep -f, @paths
53 ];
54 }
55  
56 method lpull(Str $key) {
57 if ($self->test($key)) {
58 if (my $data = $self->recv($key)) {
59 my $result = shift @{$data->{list}};
60 $self->send($key, $data);
61 return $result;
62 }
63 }
64 return undef;
65 }
66  
67 method lpush(Str $key, HashRef $val) {
68 if ($self->test($key)) {
69 if (my $data = $self->recv($key)) {
70 my $result = unshift @{$data->{list}}, $val;
71 $self->send($key, $data);
72 return $result;
73 }
74 else {
75 return undef;
76 }
77 }
78 else {
79 my $data = {list => []};
80 my $result = unshift @{$data->{list}}, $val;
81 $self->send($key, $data);
82 return $result;
83 }
84 }
85  
86 method path(Key $key) {
87 my $dir = $self->root;
88 mkdir $dir;
89 for my $next ((split(':', $key))[0..3]) {
90 $dir = File::Spec->catfile($dir, $next);
91 mkdir $dir;
92 }
93 return File::Spec->catfile($self->root, split(':', $key));
94 }
95  
96 method read(Str $file) {
97 open my $fh, '<', $file
98 or die "Can't open file ($file): $!";
99 my $ret = my $data = '';
100 while ($ret = $fh->sysread(my $buffer, 131072, 0)) {
101 $data .= $buffer;
102 }
103 unless (defined $ret) {
104 die "Can't read from file ($file): $!";
105 }
106 return $data;
107 }
108  
109 method recv(Str $key) {
110 my $data = $self->read($self->path($key));
111 return $data ? $self->decode($data) : $data;
112 }
113  
114 method rpull(Str $key) {
115 if ($self->test($key)) {
116 if (my $data = $self->recv($key)) {
117 my $result = pop @{$data->{list}};
118 $self->send($key, $data);
119 return $result;
120 }
121 }
122 return undef;
123 }
124  
125 method rpush(Str $key, HashRef $val) {
126 if ($self->test($key)) {
127 if (my $data = $self->recv($key)) {
128 my $result = push @{$data->{list}}, $val;
129 $self->send($key, $data);
130 return $result;
131 }
132 else {
133 return undef;
134 }
135 }
136 else {
137 my $data = {list => []};
138 my $result = push @{$data->{list}}, $val;
139 $self->send($key, $data);
140 return $result;
141 }
142 }
143  
144 method send(Str $key, HashRef $val) {
145 my $set = $self->encode($val);
146 $self->write($self->path($key), $set);
147 return 'OK';
148 }
149  
150 method size(Str $key) {
151 if ($self->test($key)) {
152 if (my $data = $self->recv($key)) {
153 return scalar(@{$data->{list}});
154 }
155 }
156 return 0;
157 }
158  
159 method slot(Str $key, Int $pos) {
160 if ($self->test($key)) {
161 if (my $data = $self->recv($key)) {
162 return $data->{list}[$pos];
163 }
164 }
165 return undef;
166 }
167  
168 method test(Str $key) {
169 return -f $self->path($key) ? 1 : 0;
170 }
171  
172 method write(Str $file, Str $data) {
173 open my $fh, '>', $file
174 or die "Can't open file ($file): $!";
175 ($fh->syswrite($data) // -1) == length $data
176 or die "Can't write to file ($file): $!";
177 return $self;
178 }
179  
180 1;
181  
182 =encoding utf8
183  
184 =head1 NAME
185  
186 Zing::Store::Disk - Disk Storage
187  
188 =cut
189  
190 =head1 ABSTRACT
191  
192 Disk Storage Abstraction
193  
194 =cut
195  
196 =head1 SYNOPSIS
197  
198 use Zing::Store::Disk;
199 use Zing::Encoder::Dump;
200  
201 my $disk = Zing::Store::Disk->new(
202 encoder => Zing::Encoder::Dump->new
203 );
204  
205 # $disk->drop;
206  
207 =cut
208  
209 =head1 DESCRIPTION
210  
211 This package provides an in-memory (only) storage adapter for use with data
212 persistence abstractions.
213  
214 =cut
215  
216 =head1 INHERITS
217  
218 This package inherits behaviors from:
219  
220 L<Zing::Store>
221  
222 =cut
223  
224 =head1 LIBRARIES
225  
226 This package uses type constraints from:
227  
228 L<Zing::Types>
229  
230 =cut
231  
232 =head1 ATTRIBUTES
233  
234 This package has the following attributes:
235  
236 =cut
237  
238 =head2 root
239  
240 root(Str)
241  
242 This attribute is read-only, accepts C<(Str)> values, and is optional.
243  
244 =cut
245  
246 =head1 METHODS
247  
248 This package implements the following methods:
249  
250 =cut
251  
252 =head2 decode
253  
254 decode(Str $data) : HashRef
255  
256 The decode method decodes the JSON data provided and returns the data as a hashref.
257  
258 =over 4
259  
260 =item decode example #1
261  
262 # given: synopsis
263  
264 $disk->decode('{"status"=>"ok"}');
265  
266 =back
267  
268 =cut
269  
270 =head2 drop
271  
272 drop(Str $key) : Int
273  
274 The drop method removes (drops) the item from the datastore.
275  
276 =over 4
277  
278 =item drop example #1
279  
280 # given: synopsis
281  
282 $disk->drop('zing:main:global:model:temp');
283  
284 =back
285  
286 =cut
287  
288 =head2 encode
289  
290 encode(HashRef $data) : Str
291  
292 The encode method encodes and returns the data provided as JSON.
293  
294 =over 4
295  
296 =item encode example #1
297  
298 # given: synopsis
299  
300 $disk->encode({ status => 'ok' });
301  
302 =back
303  
304 =cut
305  
306 =head2 keys
307  
308 keys(Str @keys) : ArrayRef[Str]
309  
310 The keys method returns a list of keys under the namespace of the datastore or
311 provided key.
312  
313 =over 4
314  
315 =item keys example #1
316  
317 # given: synopsis
318  
319 my $keys = $disk->keys('zing:main:global:model:temp');
320  
321 =back
322  
323 =over 4
324  
325 =item keys example #2
326  
327 # given: synopsis
328  
329 $disk->send('zing:main:global:model:temp', { status => 'ok' });
330  
331 my $keys = $disk->keys('zing:main:global:model:temp');
332  
333 =back
334  
335 =cut
336  
337 =head2 lpull
338  
339 lpull(Str $key) : Maybe[HashRef]
340  
341 The lpull method pops data off of the top of a list in the datastore.
342  
343 =over 4
344  
345 =item lpull example #1
346  
347 # given: synopsis
348  
349 $disk->lpull('zing:main:global:model:items');
350  
351 =back
352  
353 =over 4
354  
355 =item lpull example #2
356  
357 # given: synopsis
358  
359 $disk->rpush('zing:main:global:model:items', { status => 'ok' });
360  
361 $disk->lpull('zing:main:global:model:items');
362  
363 =back
364  
365 =cut
366  
367 =head2 lpush
368  
369 lpush(Str $key, HashRef $val) : Int
370  
371 The lpush method pushed data onto the top of a list in the datastore.
372  
373 =over 4
374  
375 =item lpush example #1
376  
377 # given: synopsis
378  
379 $disk->lpush('zing:main:global:model:items', { status => '1' });
380  
381 =back
382  
383 =over 4
384  
385 =item lpush example #2
386  
387 # given: synopsis
388  
389 $disk->lpush('zing:main:global:model:items', { status => '0' });
390  
391 =back
392  
393 =cut
394  
395 =head2 recv
396  
397 recv(Str $key) : Maybe[HashRef]
398  
399 The recv method fetches and returns data from the datastore by its key.
400  
401 =over 4
402  
403 =item recv example #1
404  
405 # given: synopsis
406  
407 $disk->recv('zing:main:global:model:temp');
408  
409 =back
410  
411 =over 4
412  
413 =item recv example #2
414  
415 # given: synopsis
416  
417 $disk->send('zing:main:global:model:temp', { status => 'ok' });
418  
419 $disk->recv('zing:main:global:model:temp');
420  
421 =back
422  
423 =cut
424  
425 =head2 rpull
426  
427 rpull(Str $key) : Maybe[HashRef]
428  
429 The rpull method pops data off of the bottom of a list in the datastore.
430  
431 =over 4
432  
433 =item rpull example #1
434  
435 # given: synopsis
436  
437 $disk->rpull('zing:main:global:model:items');
438  
439 =back
440  
441 =over 4
442  
443 =item rpull example #2
444  
445 # given: synopsis
446  
447 $disk->rpush('zing:main:global:model:items', { status => 1 });
448 $disk->rpush('zing:main:global:model:items', { status => 2 });
449  
450 $disk->rpull('zing:main:global:model:items');
451  
452 =back
453  
454 =cut
455  
456 =head2 rpush
457  
458 rpush(Str $key, HashRef $val) : Int
459  
460 The rpush method pushed data onto the bottom of a list in the datastore.
461  
462 =over 4
463  
464 =item rpush example #1
465  
466 # given: synopsis
467  
468 $disk->rpush('zing:main:global:model:items', { status => 'ok' });
469  
470 =back
471  
472 =over 4
473  
474 =item rpush example #2
475  
476 # given: synopsis
477  
478 $disk->rpush('zing:main:global:model:items', { status => 'ok' });
479  
480 $disk->rpush('zing:main:global:model:items', { status => 'ok' });
481  
482 =back
483  
484 =cut
485  
486 =head2 send
487  
488 send(Str $key, HashRef $val) : Str
489  
490 The send method commits data to the datastore with its key and returns truthy.
491  
492 =over 4
493  
494 =item send example #1
495  
496 # given: synopsis
497  
498 $disk->send('zing:main:global:model:temp', { status => 'ok' });
499  
500 =back
501  
502 =cut
503  
504 =head2 size
505  
506 size(Str $key) : Int
507  
508 The size method returns the size of a list in the datastore.
509  
510 =over 4
511  
512 =item size example #1
513  
514 # given: synopsis
515  
516 my $size = $disk->size('zing:main:global:model:items');
517  
518 =back
519  
520 =over 4
521  
522 =item size example #2
523  
524 # given: synopsis
525  
526 $disk->rpush('zing:main:global:model:items', { status => 'ok' });
527  
528 my $size = $disk->size('zing:main:global:model:items');
529  
530 =back
531  
532 =cut
533  
534 =head2 slot
535  
536 slot(Str $key, Int $pos) : Maybe[HashRef]
537  
538 The slot method returns the data from a list in the datastore by its index.
539  
540 =over 4
541  
542 =item slot example #1
543  
544 # given: synopsis
545  
546 my $model = $disk->slot('zing:main:global:model:items', 0);
547  
548 =back
549  
550 =over 4
551  
552 =item slot example #2
553  
554 # given: synopsis
555  
556 $disk->rpush('zing:main:global:model:items', { status => 'ok' });
557  
558 my $model = $disk->slot('zing:main:global:model:items', 0);
559  
560 =back
561  
562 =cut
563  
564 =head2 test
565  
566 test(Str $key) : Int
567  
568 The test method returns truthy if the specific key (or datastore) exists.
569  
570 =over 4
571  
572 =item test example #1
573  
574 # given: synopsis
575  
576 $disk->rpush('zing:main:global:model:items', { status => 'ok' });
577  
578 $disk->test('zing:main:global:model:items');
579  
580 =back
581  
582 =over 4
583  
584 =item test example #2
585  
586 # given: synopsis
587  
588 $disk->drop('zing:main:global:model:items');
589  
590 $disk->test('zing:main:global:model:items');
591  
592 =back
593  
594 =cut
595  
596 =head1 AUTHOR
597  
598 Al Newkirk, C<awncorp@cpan.org>
599  
600 =head1 LICENSE
601  
602 Copyright (C) 2011-2019, Al Newkirk, et al.
603  
604 This is free software; you can redistribute it and/or modify it under the terms
605 of the The Apache License, Version 2.0, as elucidated in the L<"license
606 file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>.
607  
608 =head1 PROJECT
609  
610 L<Wiki|https://github.com/iamalnewkirk/zing/wiki>
611  
612 L<Project|https://github.com/iamalnewkirk/zing>
613  
614 L<Initiatives|https://github.com/iamalnewkirk/zing/projects>
615  
616 L<Milestones|https://github.com/iamalnewkirk/zing/milestones>
617  
618 L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md>
619  
620 L<Issues|https://github.com/iamalnewkirk/zing/issues>
621  
622 =cut