Model/Driver/Memory.pm untested

filter: Statementstmt 0.0% 0 / 392 Branchbran 0.0% 0 / 164 Conditioncond 0.0% 0 / 69 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 33 totaltotal 0.0% 0 / 741 CC 83 SCAR 50.5
CC
83
Cov
0%
_grep_grep
650.0
_grep
56.0
set
42.0
CRAP 156.1
1 package Data::Model::Driver::Memory;
2 use strict;
3 use warnings;
4 use base 'Data::Model::Driver';
5  
6 use Carp ();
7 $Carp::Internal{(__PACKAGE__)}++;
8  
9 ## data loader
10  
11 sub _load_data {
12 my($self, $model, $type, $name) = @_;
13  
14 $self->{models}->{$model} ||= +{};
15 if ($type eq 'data') {
16 return +{
17 records => +{},
18 seq => 0,
19 record_id => 0,
20 };
21 } else {
22 return +{
23 key => +{},
24 prefix => +{},
25 };
26 }
27 }
28  
29 sub load_data {
30 my($self, $schema) = @_;
31 $self->{models}->{$schema->model}->{data} ||= $self->_load_data($schema->model, 'data');
32 }
33  
34 sub load_key {
35 my($self, $schema) = @_;
36 $self->{models}->{$schema->model}->{key} ||= $self->_load_data($schema->model, 'key');
37 }
38  
39 sub load_index {
40 my($self, $schema, $name) = @_;
41 $self->{models}->{$schema->model}->{index}->{$name} ||= $self->_load_data($schema->model, 'index', $name);
42 }
43  
44 sub load_unique {
45 my($self, $schema, $name) = @_;
46 $self->{models}->{$schema->model}->{unique}->{$name} ||= $self->_load_data($schema->model, 'unique', $name);
47 }
48  
49 sub new {
50 my $class = shift;
51 bless {
52 models => +{},
53 }, $class;
54 }
55  
56 sub save {}
57  
58 sub generate_record_id {
59 my($self, $schema) = @_;
60 my $data = $self->load_data($schema);
61 ++($data->{record_id});
62 }
63  
64 sub generate_auto_increment {
65 my($self, $schema) = @_;
66 my $data = $self->load_data($schema);
67 ++($data->{seq});
68 }
69  
70 ## get, set, delete
71  
72 sub fetch {
73 my($self, $schema, $key, $columns, %args) = @_;
74  
75 # fetch record id
76 my $result_id_list = $self->get_record_id_list($schema, $key, $columns);
77 return unless $result_id_list && @{ $result_id_list };
78  
79 my $results = $self->get_result_list($schema, $columns, $result_id_list);
80 return unless $results && @{ $results };
81  
82 $results = [ map { $_->[1] } @{ $results } ];
83 }
84  
85  
86 sub lookup {
87 my $self = shift;
88 my $results = $self->fetch(@_);
89 $results->[0];
90 }
91  
92 sub lookup_multi {
93 my($self, $schema, $ids) = @_;
94  
95 my %resultlist;
96 for my $id (@{ $ids }) {
97 my $key = join "\0", @{ $id };
98 my $results = $self->fetch($schema, $id);
99 next unless $results;
100 $resultlist{$key} = $results->[0];
101 }
102 \%resultlist;
103 }
104  
105 sub get {
106 my $self = shift;
107 my $results = $self->fetch(@_);
108 return unless $results;
109 return $self->_generate_result_iterator($results), +{};
110 }
111  
112 sub set {
113 my($self, $schema, $key, $columns, %args) = @_;
114  
115 # initilaize
116  
117 # check unique
118 if (@{ $schema->key } && grep { defined $_ } @{ $key }) {
119 my $result_id_list = $self->get_record_id_list($schema, $key, +{});
120 Carp::croak 'not unique columns' if @{ $result_id_list };
121 }
122 if (scalar(%{ $schema->unique })) {
123 while (my($unique_name, $unique_columns) = each %{ $schema->unique }) {
124 my $index = [];
125 for my $column (@{ $unique_columns }) {
126 push @{ $index }, $columns->{$column};
127 }
128 my $result_id_list = $self->get_record_id_list($schema, undef, +{ index => { $unique_name => $ index } });
129 Carp::croak 'not unique columns' if @{ $result_id_list };
130 }
131 }
132  
133 # delete old record
134  
135 # record_id
136 my $record_id = $self->generate_record_id($schema);
137  
138 # auto_increment
139 if ($self->_set_auto_increment($schema, $columns, sub { $self->generate_auto_increment($schema) })) {
140 # remake $key
141 $key = $schema->get_key_array_by_hash($columns);
142 }
143  
144 # write to index, key and unique
145 $self->set_memory_index($schema, $key, $columns, $record_id);
146  
147 # write data
148 my $data = $self->load_data($schema);
149 $data->{records}->{$record_id} = +{ %{ $columns } };
150 }
151  
152 sub replace {
153 my($self, $schema, $key, $columns, %args) = @_;
154 $self->delete($schema, $key, +{}, %args);
155 $self->set($schema, $key, $columns, %args);
156 }
157  
158 sub update {
159 my($self, $schema, $old_key, $key, $old_columns, $columns, $changed_columns, %args) = @_;
160  
161 # fetch record id
162 my $result_id_list = $self->get_record_id_list($schema, $old_key, +{});
163 return unless $result_id_list && @{ $result_id_list };
164 return if @{ $result_id_list } != 1; # not unique key
165 my $id = $result_id_list->[0];
166  
167 # reindex
168 $self->delete_memory_index($schema, $old_key, $old_columns, $id);
169 $self->set_memory_index($schema, $key, $columns, $id);
170  
171 # set data
172 my $data = $self->load_data($schema);
173 $data->{records}->{$id} = +{ %{ $columns } };
174 }
175  
176 sub _uodate_delete_visitor {
177 my($self, $schema, $key, $query, $code) = @_;
178  
179 # fetch record id
180 my $result_id_list = $self->get_record_id_list($schema, $key, $query);
181 return unless $result_id_list && @{ $result_id_list };
182  
183 my $results = $self->get_result_list($schema, $query, $result_id_list);
184 return unless $results && @{ $results };
185  
186 # delete data
187 my $data = $self->load_data($schema);
188 my @rows;
189 for my $id ( map { $_->[0] } @{ $results }) {
190 my @ret = $code->($data, $id);
191 push @rows, @ret if @ret;
192 }
193 return @rows ? [ @rows ] : undef;
194 }
195  
196 sub update_direct {
197 my($self, $schema, $key, $query, $columns, %args) = @_;
198  
199 $self->_uodate_delete_visitor(
200 $schema, $key, $query,
201 sub {
202 my($data, $id) = @_;
203 $self->delete_memory_index($schema, $key, $data->{records}->{$id}, $id);
204 while (my($key, $val) = each %{ $columns }) {
205 $data->{records}->{$id}->{$key} = $val;
206 }
207 $key = $schema->get_key_array_by_hash($data->{records}->{$id});
208 $self->set_memory_index($schema, $key, $data->{records}->{$id}, $id);
209 }
210 );
211 }
212  
213  
214 sub delete {
215 my($self, $schema, $key, $columns, %args) = @_;
216  
217 $self->_uodate_delete_visitor(
218 $schema, $key, $columns,
219 sub {
220 my($data, $id) = @_;
221 $self->delete_memory_index($schema, $key, $data->{records}->{$id}, $id);
222 delete $data->{records}->{$id};
223 }
224 );
225 }
226  
227 ## for memory index
228  
229 sub get_record_id_list {
230 my($self, $schema, $key, $columns) = @_;
231  
232 my $result_id_list = [];
233 if ($key) {
234 $result_id_list = $self->get_memory_index($schema, 'key', undef, $key);
235 } else {
236 # hash
237 $columns ||= +{};
238 if (exists $columns->{index} && ref($columns->{index}) eq 'HASH') {
239 my($index, $index_key) = %{ $columns->{index} };
240 $index_key = [ $index_key ] unless ref($index_key) eq 'ARRAY';
241 for my $index_type (qw/ unique index /) {
242 if (exists $schema->$index_type->{$index}) {
243 $result_id_list = $self->get_memory_index($schema, $index_type, $index, $index_key);
244 last;
245 }
246 }
247 } else {
248 my $data = $self->load_data($schema);
249 $result_id_list = [
250 sort { $a <=> $b } keys %{ $data->{records} }
251 ];
252 }
253 }
254 $result_id_list;
255 }
256  
257 sub get_memory_index {
258 my($self, $schema, $index_type, $index, $key) = @_;
259 my $columns = $index_type eq 'key' ? $schema->key : $schema->$index_type->{$index};
260  
261 my $method = "load_$index_type";
262 my $key_hash = $self->$method($schema, $index);
263 my $key_data = $self->_generate_key_data($key);
264  
265 my $type = scalar(@{ $key }) == scalar(@{ $columns }) ? 'key' : 'prefix';
266 my $result = $key_hash->{$type}->{$key_data};
267 $result ? ref($result) eq 'HASH' ? [ keys %{ $result } ] : [ $result ] : [];
268 }
269  
270 sub set_memory_index {
271 my($self, $schema, $key, $columns, $id) = @_;
272 $self->_set_memory_index($schema, 'key', undef, $key, $id);
273  
274 for my $index_type (qw/ unique index /) {
275 for my $index (keys %{ $schema->$index_type }) {
276 my @index_key = map {
277 $columns->{$_}
278 } @{ $schema->$index_type->{$index} };
279 $self->_set_memory_index($schema, $index_type, $index, [ @index_key ], $id);
280 }
281 }
282 }
283  
284 sub _set_memory_index {
285 my($self, $schema, $index_type, $index, $key, $id) = @_;
286 my $columns = $index_type eq 'key' ? $schema->key : $schema->{$index_type}->{$index};
287  
288 my $method = "load_$index_type";
289 my $key_hash = $self->$method($schema, $index);
290  
291 my @prefix = ();
292 for my $k (@{ $key }) {
293 push @prefix, $k;
294 my $key_data = $self->_generate_key_data([ @prefix ]);
295  
296 my $type = scalar(@prefix) == scalar(@{ $key }) ? 'key' : 'prefix';
297 my $hash = $key_hash->{$type};
298 if (exists $hash->{$key_data}) {
299 unless (ref($hash->{$key_data}) eq 'HASH') {
300 my $oid = $hash->{$key_data};
301 $hash->{$key_data} = +{
302 $oid => $oid,
303 };
304 }
305 $hash->{$key_data}->{$id} = $id;
306 } else {
307 $hash->{$key_data} = $id;
308 }
309 }
310 }
311  
312 sub delete_memory_index {
313 my($self, $schema, $key, $columns, $id) = @_;
314 $self->_delete_memory_index($schema, 'key', undef, $key, $id);
315  
316 for my $index_type (qw/ unique index /) {
317 for my $index (keys %{ $schema->$index_type }) {
318 my @index_key = map {
319 $columns->{$_}
320 } @{ $schema->$index_type->{$index} };
321 $self->_delete_memory_index($schema, $index_type, $index, [ @index_key ], $id);
322 }
323 }
324 }
325  
326 sub _delete_memory_index {
327 my($self, $schema, $index_type, $index, $key, $id) = @_;
328 my $columns = $index_type eq 'key' ? $schema->key : $schema->{$index_type}->{$index};
329  
330 my $method = "load_$index_type";
331 my $key_hash = $self->$method($schema, $index);
332  
333 my @prefix = ();
334 for my $k (@{ $key }) {
335 push @prefix, $k;
336 my $key_data = $self->_generate_key_data([ @prefix ]);
337  
338 my $type = scalar(@prefix) == scalar(@{ $key }) ? 'key' : 'prefix';
339 my $hash = $key_hash->{$type};
340 if (ref($hash->{$key_data}) eq 'HASH') {
341 delete $hash->{$key_data}->{$id};
342 if (keys(%{ $hash->{$key_data} }) == 1) {
343 my($k) = keys %{ $hash->{$key_data} };
344 $hash->{$key_data} = $k;
345 }
346 } else {
347 delete $hash->{$key_data};
348 }
349 }
350 }
351  
352 # grep, sort, limit
353  
354 sub get_result_list {
355 my($self, $schema, $query, $id_list) = @_;
356  
357 # merge data
358 my $data = $self->load_data($schema);
359 my $results = [];
360 for my $id (@ { $id_list }) {
361 push @{ $results }, [ $id => $data->{records}->{$id} ];
362 }
363  
364 return $results unless $query && ref($query) eq 'HASH';
365 return $self->limit($schema, $query, $self->sort($schema, $query, $self->grep($schema, $query, $results)));
366 }
367  
368 ## grep
369 sub _grep_merge_and {
370 my($self, $l, $r) = @_;
371 return [] unless @{ $l } && @{ $r };
372 if ($l->[0]->[0] > $r->[0]->[0]) {
373 my $t = $l;
374 $l = $r;
375 $r = $t;
376 }
377  
378 my @results;
379 my $ridx = 0;
380 my $rmax = @{ $r };
381 for my $lrow (@{ $l }) {
382 my $lid = $lrow->[0];
383 while ( $ridx < $rmax) {
384 my $rid = $r->[$ridx]->[0];
385 if ($rid == $lid) {
386 push @results, $lrow;
387 $ridx++;
388 } elsif ($rid < $lid) {
389 $ridx++;
390 } else {
391 last;
392 }
393 }
394 }
395 return \@results;
396 }
397 sub _grep_merge_or {
398 my($self, $l, $r) = @_;
399 return $l if @{ $l } && !@{ $r };
400 return $r if !@{ $l } && @{ $r };
401 if ($l->[0]->[0] > $r->[0]->[0]) {
402 my $t = $l;
403 $l = $r;
404 $r = $t;
405 }
406  
407 my @results;
408 my $ridx = 0;
409 my $rmax = @{ $r };
410 for my $lrow (@{ $l }) {
411 my $lid = $lrow->[0];
412 while ( $ridx < $rmax) {
413 my $rid = $r->[$ridx]->[0];
414 if ($rid == $lid) {
415 $ridx++;
416 last;
417 } elsif ($rid < $lid) {
418 push @results, $r->[$ridx];
419 $ridx++;
420 } else {
421 last;
422 }
423 }
424 push @results, $lrow;
425 }
426 return \@results;
427 }
428  
429 sub _grep_grep {
430 my($self, $col, $val, $rows) = @_;
431 my @result;
432 for my $row (@{ $rows }) {
433 my $ok = 0;
434 unless (exists $row->[1]->{$col}) {
435 next;
436 }
437 my $rval = $row->[1]->{$col};
438 if (ref($val)) {
439 if (ref($val) eq 'HASH') {
440 my($op, $value) = (%{ $val });
441 $op = uc($op);
442 if ($op eq 'LIKE') {
443 my $is_prefix = !($value =~ s/^%//);
444 my $is_suffix = !($value =~ s/%$//);
445 my $meta_str = join '.', map { quotemeta $_ } split '_', $value;
446 $meta_str = '^' . $meta_str if $is_prefix;
447 $meta_str .= '$' if $is_suffix;
448 $ok = 1 if $rval =~ /$meta_str/;
449  
450 } elsif ($op eq '=') {
451 $ok = 1 if $rval eq $value;
452  
453 } elsif ($op eq '!=') {
454 $ok = 1 unless $rval eq $value;
455  
456 } elsif ($op eq '>') {
457 $ok = 1 if $rval > $value;
458  
459 } elsif ($op eq '<') {
460 $ok = 1 if $rval < $value;
461  
462 } elsif ($op eq '>=') {
463 $ok = 1 if $rval >= $value;
464  
465 } elsif ($op eq '<=') {
466 $ok = 1 if $rval <= $value;
467  
468 } elsif (($op eq 'IN' || $op eq 'NOT IN') && ref($value) eq 'ARRAY') {
469 for my $v (@{ $value }) {
470 $ok = 1 if $rval eq $v;
471 }
472 $ok = !$ok unless $op eq 'IN';
473 }
474 }
475 } else {
476 $ok = 1 if $rval eq $val;
477 }
478 push @result, $row if $ok;
479 }
480 \@result;
481 }
482 sub _grep {
483 my($self, $col, $val, $rows) = @_;
484 if (lc($col) eq '-and' || lc($col) eq '-or') {
485 my $results;
486 my $ret;
487 while (my($ccol, $cval) = splice @{ $val }, 0, 2) {
488 $ret = $self->_grep( $ccol, $cval, $rows );
489 if ($results) {
490 $results = (lc($col) eq '-and') ? $self->_grep_merge_and($results, $ret) : $self->_grep_merge_or($results, $ret);
491 } else {
492 $results = $ret;
493 }
494 }
495 $results = $ret unless $results;
496 return $results;
497 } else {
498 ## xxx Need to support old range and transform behaviors.
499 Carp::croak("Invalid/unsafe column name $col") unless $col =~ /^[\w\.]+$/ || ref($col) eq 'SCALAR';
500 Carp::croak("Invalid/unsafe column value $col (unused Data::Model::SQL->_mk_term parse data)") unless !ref($val) || ref($val) eq 'HASH';
501 return $self->_grep_grep($col, $val, $rows);
502 }
503 }
504 sub grep {
505 my($self, $schema, $query, $rows) = @_;
506 return $rows unless exists $query->{where};
507 my $ret = $self->_grep( -and => $query->{where}, $rows );
508 return [] unless $ret;
509 return $ret;
510 }
511  
512 sub sort {
513 my($self, $schema, $query, $rows) = @_;
514 return $rows unless exists $query->{order};
515  
516 my $sort_data = [];
517 for my $data (@{ $query->{order} }) {
518 my($column, $vec) = (%{ $data });
519 push @{ $sort_data }, +{
520 column => $column,
521 vec => uc($vec),
522 int => !!($schema->column_type($column) =~ /int/i),
523 };
524 }
525  
526 my @ordered = sort {
527 my $v = 0;
528 for my $data (@{ $sort_data }) {
529 my $column = $data->{column};
530 if ($data->{int}) {
531 next if $a->[1]->{$column} == $b->[1]->{$column};
532 $v = $a->[1]->{$column} <=> $b->[1]->{$column};
533 } else {
534 next if $a->[1]->{$column} eq $b->[1]->{$column};
535 $v = $a->[1]->{$column} cmp $b->[1]->{$column};
536 }
537 $v *= -1 if $data->{vec} eq 'DESC';
538 last;
539 }
540 $v;
541 } @{ $rows };
542 \@ordered;
543 }
544  
545 sub limit {
546 my($self, $schema, $query, $rows) = @_;
547 return $rows unless exists $query->{limit} || exists $query->{offset};
548  
549 my @limitted;
550 if (exists $query->{offset}) {
551 for (1..$query->{offset}) {
552 shift @{ $rows };
553 }
554 }
555 if (exists $query->{limit}) {
556 for (1..$query->{limit}) {
557 push @limitted, shift @{ $rows };
558 }
559 } else {
560 push @limitted, @{ $rows };
561 }
562 return \@limitted;
563 }
564  
565 1;
566  
567 =head1 NAME
568  
569 Data::Model::Driver::Memory - storage driver for memory
570  
571 =head1 SYNOPSIS
572  
573 package MyDB;
574 use base 'Data::Model';
575 use Data::Model::Schema;
576 use Data::Model::Driver::Memory;
577
578 my $dbi_connect_options = {};
579 my $driver = Data::Model::Driver::Memory->new;
580
581 base_driver $driver;
582 install_model model_name => schema {
583 ....
584 };
585  
586 =head1 SEE ALSO
587  
588 L<Data::Model>
589  
590 =head1 AUTHOR
591  
592 Kazuhiro Osawa E<lt>yappo <at> shibuya <döt> plE<gt>
593  
594 =head1 LICENSE
595  
596 This library is free software; you can redistribute it and/or modify
597 it under the same terms as Perl itself.
598  
599 =cut