| 1 | package ODS::Storage::Directory; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 70 | ▶ | use YAOO; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 70 | ▶ | use Cwd qw/getcwd/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 70 | ▶ | use Parallel::ForkManager; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 70 | extends 'ODS::Storage::Base'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 70 | ▶ | use ODS::Utils qw/load move unique_class_name error/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 70 | auto_build; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 70 | has file_handle => isa(fh); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 70 | has directory => isa(string); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 70 | has cache_directory => isa(string); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 70 | has remove_regex => isa(string("\'\"\.")); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub all { | ||||||||||||||||||||||||||||
| 22 | 107 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
all: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 107 | ▶ | $params{type} ||= 'all'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params{"type"} ||= "all"
MC/DC0%0/1
$params{"type"} ||= "all"
$params{"type"}
Truth table50%1/2
A
A= $params{"type"}
| |||||||||||||||||||||||||||||
| 25 | 107 | ▶ | $params{sort} ||= $self->table->keyfield; | ||||||||||||||||||||||||||
|
Condition66%2/3
$params{"sort"} ||= $self->table->keyfield
MC/DC0%0/2
$params{"sort"} ||= $self->table->keyfield
$params{"sort"}
$self->table->keyfield
Truth table66%2/3
A ||= B
A= $params{"sort"}B= $self->table->keyfield
| |||||||||||||||||||||||||||||
| 26 | 107 | ▶ | $params{sort_direction} ||= 'asc'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$params{"sort_direction"} ||= "asc"
MC/DC100%1/1
$params{"sort_direction"} ||= "asc"
$params{"sort_direction"}
Truth table100%2/2
A
A= $params{"sort_direction"}
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 107 | my ($data, $from_cache) = $self->into_rows($self->cache_or_all(%params)); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 41 | return $data; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub create { | ||||||||||||||||||||||||||||
| 34 | 1714 | ▶ | my ($self, %params) = (shift, @_ > 1 ? @_ : %{ $_[0] }); | ||||||||||||||||||||||||||
|
Subroutine
create: called CC=6 SCAR=18.1
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1714 | ▶ | my $file = $params{__custom_file_name} || sprintf '%s_%s.%s', time, unique_class_name, $self->serialize_class->file_suffix; | ||||||||||||||||||||||||||
|
Condition66%2/3
$params{'__custom_file_name'} || sprintf('%s_%s.%s', time, unique_class_name(), $self->serialize_class->file_suffix)
MC/DC0%0/2
$params{'__custom_file_name'} || sprintf('%s_%s.%s', time, unique_class_name(), $self->serialize_class->file_suffix)
$params{'__custom_file_name'}
sprintf('%s_%s.%s', time, unique_class_name(), $self->serialize_class->file_suffix)
Truth table66%2/3
A || B
A= $params{'__custom_file_name'}B= sprintf('%s_%s.%s', time, unique_class_name(), $self->serialize_class->file_suffix)
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1714 | $params{__file} = $file; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1714 | $file .= '.tmp'; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1714 | my $data = $self->into_rows(\%params, 1); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1714 | $data->validate(); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1714 | ▶ | if ($self->table->rows) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 1713 | push @{ $self->table->rows }, $data; | |||||||||||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||||||||||
| 49 | 1 | ▶ | $self->table->rows(ref($data || "") eq 'ARRAY' ? $data : [$data]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$data || ''
MC/DC0%0/1
$data || ''
$data
Truth table50%1/2
A
A= $data
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1714 | $data = $self->into_storage($data); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1714 | $self->write_file(sprintf("%s/%s", $self->directory, $file), $data); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1714 | $self->cache_clear(); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1714 | $self->table; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub search { | ||||||||||||||||||||||||||||
| 62 | 2 | ▶ | my ($self, %params) = (shift, @_ > 1 ? @_ : %{ $_[0] }); | ||||||||||||||||||||||||||
|
Subroutine
search: called CC=4 SCAR=14.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | my $cache_prefix = $self->cache_prefix('search', %params); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | my ($data, $from_cache) = $self->cache_or_filter($cache_prefix, %params); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 2 | ▶ | if (ref $data eq 'ARRAY' && ref $data->[0] eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $data eq "ARRAY" and ref $data->[0] eq "HASH"
MC/DC50%1/2
ref $data eq "ARRAY" and ref $data->[0] eq "HASH"
ref $data eq "ARRAY"
ref $data->[0] eq "HASH"
Truth table66%2/3
A and B
A= ref $data eq "ARRAY"B= ref $data->[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 69 | 1 | $data = [ map { $self->into_rows($_) } @{ $self->into_rows($data) } ]; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 2 | my $table = $self->table->clone(); | |||||||||||||||||||||||||||
| 73 | 2 | $table->rows($data); | |||||||||||||||||||||||||||
| 74 | 2 | return ODS::Iterator->new(table => $table); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub find { | ||||||||||||||||||||||||||||
| 78 | 2 | ▶ | my ($self, %params) = (shift, @_ > 1 ? @_ : %{ $_[0] }); | ||||||||||||||||||||||||||
|
Subroutine
find: called CC=3 SCAR=12.3
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | my $cache_prefix = $self->cache_prefix('find', %params); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | my ($data, $from_cache) = $self->cache_or_find($cache_prefix, %params); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | if (ref $data eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | $data = $self->into_rows($data); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | return $data; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub update { | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my ($self, $update, %params) = (shift, pop, @_); | ||||||||||||||||||||||||||
|
Subroutine
update: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my $find = $self->find(%params); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | croak sprintf "No row found for search params %s", Dumper \%params | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | unless $find; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | $find->validate($update); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | $self->update_row($find); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub update_row { | ||||||||||||||||||||||||||||
| 105 | 2 | ▶ | my ($self, $row) = @_; | ||||||||||||||||||||||||||
|
Subroutine
update_row: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 2 | my $data = $self->into_storage($row); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | my $file = $row->__custom_file_name || $row->__file; | ||||||||||||||||||||||||||
|
Condition33%1/3
$row->__custom_file_name || $row->__file
MC/DC0%0/2
$row->__custom_file_name || $row->__file
$row->__custom_file_name
$row->__file
Truth table33%1/3
A || B
A= $row->__custom_file_nameB= $row->__file
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 2 | $self->write_file(sprintf("%s/%s", $self->directory, $file), $data); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 2 | $self->table; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub delete { | ||||||||||||||||||||||||||||
| 117 | 1 | ▶ | my ($self, %params) = (shift, @_ > 1 ? @_ : %{ $_[0] }); | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=3 SCAR=11.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | my $data = $self->table->rows ? ODS::Iterator->new(table => $self->table) : $self->all; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | my $index = $data->find_index(sub { | ||||||||||||||||||||||||||||
| 122 | 2 | ▶ | my $row = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 123 | 2 | my $select = 1; | |||||||||||||||||||||||||||
| 124 | 2 | for my $key ( keys %params ) { | |||||||||||||||||||||||||||
| 125 | 2 | ▶ | if ( $params{$key} ne $row->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 1 | $select = undef; | |||||||||||||||||||||||||||
| 127 | 1 | last; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 2 | $select; | |||||||||||||||||||||||||||
| 131 | 1 | }); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 1 | my $delete = $data->splice($index, 1); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | my $file = $delete->__file; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | $self->unlink_file(sprintf("%s/%s", $self->directory, $file)); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | $self->cache_clear($delete); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | $self->table; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | sub delete_row { | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | my ($self, $r) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delete_row: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | my $data = ODS::Iterator->new(table => $self->table); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | my $keyfield = $data->table->keyfield; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | my $index; | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ($keyfield) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | $index = $data->find_index(sub { | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | $_[0]->{$keyfield} eq $r->$keyfield; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 155 | 0 | }); | |||||||||||||||||||||||||||
| 156 | } else { | ||||||||||||||||||||||||||||
| 157 | $index = $data->find_index(sub { | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my $row = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 159 | 0 | my $select = 1; | |||||||||||||||||||||||||||
| 160 | 0 | for my $key ( keys %{ $row->columns } ) { | |||||||||||||||||||||||||||
| 161 | 0 | ▶ | if ( $r->$key ne $row->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | $select = undef; | |||||||||||||||||||||||||||
| 163 | 0 | last; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 0 | $select; | |||||||||||||||||||||||||||
| 167 | 0 | }); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | my $delete = $data->splice($index, 1); | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | my $file = $delete->__file; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | $self->unlink_file(sprintf("%s/%s", $self->directory, $file)); | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | $self->cache_clear($delete); | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | $self->table; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | sub parse_data_format { | ||||||||||||||||||||||||||||
| 182 | 0 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_data_format: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 183 | 0 | return $self->serialize_class->parse($data); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | sub stringify_data_format { | ||||||||||||||||||||||||||||
| 187 | 1716 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
stringify_data_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 188 | 1716 | return $self->serialize_class->stringify($data); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # methods very much specific to files | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | sub directory_files_last_updated { | ||||||||||||||||||||||||||||
| 194 | 111 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
directory_files_last_updated: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 195 | 111 | my $files = $self->read_directory($self->directory); | |||||||||||||||||||||||||||
| 196 | 111 | (my $last_update = $files->[-1]) =~ s/(\d+).*/$1/; | |||||||||||||||||||||||||||
| 197 | 111 | return ($files, $last_update); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | sub cache_write { | ||||||||||||||||||||||||||||
| 201 | 44 | ▶ | my ($self, $type, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cache_write: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 202 | 44 | my $file = sprintf "%s/%s__%s.%s.tmp", $self->cache_directory, $type, time, $self->serialize_class->file_suffix; | |||||||||||||||||||||||||||
| 203 | 44 | $self->write_file($file, $data); | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | sub cache_clear { | ||||||||||||||||||||||||||||
| 207 | 1715 | ▶ | my ($self, $row) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cache_clear: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 208 | 1715 | my $files = $self->read_directory($self->cache_directory); | |||||||||||||||||||||||||||
| 209 | 1715 | for (@{$files}) { | |||||||||||||||||||||||||||
| 210 | 6 | my %file_params = $self->cache_parse_filename($_); | |||||||||||||||||||||||||||
| 211 | 6 | my $clear = 1; | |||||||||||||||||||||||||||
| 212 | 6 | ▶ | if (scalar keys %file_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | PARAM: | ||||||||||||||||||||||||||||
| 214 | 3 | for my $key ( keys %file_params ) { | |||||||||||||||||||||||||||
| 215 | 5 | ▶ | next PARAM if $key =~ m/^__/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 3 | ▶ | if (!$row || $row->$key ne $file_params{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $row or $row->$key ne $file_params{$key}
MC/DC100%2/2
not $row or $row->$key ne $file_params{$key}
not $row
$row->$key ne $file_params{$key}
Truth table100%3/3
not(A) or B
A= not $rowB= $row->$key ne $file_params{$key}
| |||||||||||||||||||||||||||||
| 217 | 2 | $clear = 0; | |||||||||||||||||||||||||||
| 218 | 2 | last PARAM; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | 6 | ▶ | $self->unlink_file(sprintf("%s/%s", $self->cache_directory, $_)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | if $clear; | ||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | sub cache_parse_filename { | ||||||||||||||||||||||||||||
| 228 | 6 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cache_parse_filename: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 229 | 6 | my %file; | |||||||||||||||||||||||||||
| 230 | 6 | ▶ | return %file unless $name =~ s/^find__//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 3 | my @parts = split "__", $name; | |||||||||||||||||||||||||||
| 232 | 3 | ($file{__create_time} = pop @parts) =~ s/\.\w+$//; | |||||||||||||||||||||||||||
| 233 | 3 | for (@parts) { | |||||||||||||||||||||||||||
| 234 | 3 | my ($key, $value) = split "_", $_; | |||||||||||||||||||||||||||
| 235 | 3 | $file{$key} = $value; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | 3 | return %file; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | sub cache_prefix { | ||||||||||||||||||||||||||||
| 242 | 111 | ▶ | my ($self, $type, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cache_prefix: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 111 | my $regex = $self->remove_regex; | |||||||||||||||||||||||||||
| 245 | 111 | for my $key ( keys %args ) { | |||||||||||||||||||||||||||
| 246 | 218 | (my $value = $args{$key}) =~ s/$regex//g; | |||||||||||||||||||||||||||
| 247 | 218 | $type .= sprintf('__%s_%s', $key, $value); | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 111 | return $type; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | sub cache_file { | ||||||||||||||||||||||||||||
| 254 | 111 | ▶ | my ($self, $type) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cache_file: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | my @cache_file = grep { | ||||||||||||||||||||||||||||
| 257 | 51 | $_ =~ m/^$type/; | |||||||||||||||||||||||||||
| 258 | 111 | } @{ $self->read_directory($self->cache_directory) }; | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 111 | ▶ | return scalar @cache_file ? sprintf( "%s/%s", $self->cache_directory, $cache_file[0]) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | sub cache_or_all { | ||||||||||||||||||||||||||||
| 264 | 107 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cache_or_all: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 107 | my $type = delete $args{type}; | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 107 | my $file_prefix = $self->cache_prefix($type, %args); | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 107 | my ($files, $last_update) = $self->directory_files_last_updated(); | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 107 | my $cache_file = $self->cache_file($file_prefix); | |||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 107 | ▶ | if ($cache_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | 0 | return ($self->serialize_class->parse( | |||||||||||||||||||||||||||
| 276 | $self->read_file($cache_file) | ||||||||||||||||||||||||||||
| 277 | ), 1); | ||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 107 | my $fm = Parallel::ForkManager->new(5000); | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 107 | my @data; | |||||||||||||||||||||||||||
| 283 | $fm->run_on_finish(sub { | ||||||||||||||||||||||||||||
| 284 | 913 | ▶ | my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 913 | push @data, $data_structure_reference; | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 107 | }); | |||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | READ_FILE: | ||||||||||||||||||||||||||||
| 291 | 107 | for my $file (@{$files}) { | |||||||||||||||||||||||||||
| 292 | 1797 | ▶ | my $pid = $fm->start and next READ_FILE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 66 | my $d = $self->serialize_class->parse( | |||||||||||||||||||||||||||
| 295 | $self->read_file(sprintf("%s/%s", $self->directory, $file)) | ||||||||||||||||||||||||||||
| 296 | ); | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 66 | $d->{__file} = $file; | |||||||||||||||||||||||||||
| 299 | 66 | ▶ | if ($file !~ m/^\d{9}\d+/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 2 | $d->{__custom_file_name} = $file; | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 66 | $fm->finish(0, $d); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 41 | $fm->wait_all_children; | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 41 | ▶ | if ($args{sort_direction} eq 'asc') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 40 | @data = sort { $a->{$args{sort}} cmp $b->{$args{sort}} } @data; | |||||||||||||||||||||||||||
| 311 | } else { | ||||||||||||||||||||||||||||
| 312 | 1 | @data = sort { $b->{$args{sort}} <=> $a->{$args{sort}} } @data; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | 41 | $self->cache_write($file_prefix, $self->serialize_class->stringify(\@data)); | |||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 41 | return \@data; | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | sub cache_or_filter { | ||||||||||||||||||||||||||||
| 320 | 2 | ▶ | my ($self, $type, %params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cache_or_filter: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 2 | my ($files, $last_update) = $self->directory_files_last_updated(); | |||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 2 | my $cache_file = $self->cache_file($type); | |||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 2 | ▶ | if ($cache_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 1 | return $self->serialize_class->parse( | |||||||||||||||||||||||||||
| 328 | $self->read_file($cache_file) | ||||||||||||||||||||||||||||
| 329 | ); | ||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 1 | ▶ | my $data = $self->table->rows ? ODS::Iterator->new(table => $self->table) : $self->all(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | my $select = $data->filter(sub { | ||||||||||||||||||||||||||||
| 335 | 3 | ▶ | my $row = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 336 | 3 | my $select = 1; | |||||||||||||||||||||||||||
| 337 | 3 | for my $key ( keys %params ) { | |||||||||||||||||||||||||||
| 338 | 3 | ▶ | if ( $params{$key} ne $row->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | 2 | $select = undef; | |||||||||||||||||||||||||||
| 340 | 2 | last; | |||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | 3 | $select; | |||||||||||||||||||||||||||
| 344 | 1 | }); | |||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 1 | $self->cache_write($type, $self->serialize_class->stringify([ map { $_->as_hash } @{$select}])); | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 1 | return $select; | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | sub cache_or_find { | ||||||||||||||||||||||||||||
| 352 | 2 | ▶ | my ($self, $type, %params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cache_or_find: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 2 | my ($files, $last_update) = $self->directory_files_last_updated(); | |||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 2 | my $cache_file = $self->cache_file($type); | |||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 2 | ▶ | if ($cache_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | 0 | return $self->serialize_class->parse( | |||||||||||||||||||||||||||
| 360 | $self->read_file($cache_file) | ||||||||||||||||||||||||||||
| 361 | ); | ||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 2 | ▶ | my $data = $self->table->rows ? ODS::Iterator->new(table => $self->table) : $self->all; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | # this only works for JSON and YAML, CSS and JSONL we can stream/read rows/lines instead of reading/loading | ||||||||||||||||||||||||||||
| 367 | # all into memory. | ||||||||||||||||||||||||||||
| 368 | my $select = $data->find(sub { | ||||||||||||||||||||||||||||
| 369 | 3 | ▶ | my $row = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 370 | 3 | my $select = 1; | |||||||||||||||||||||||||||
| 371 | 3 | for my $key ( keys %params ) { | |||||||||||||||||||||||||||
| 372 | 3 | ▶ | if ( $params{$key} ne $row->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 1 | $select = undef; | |||||||||||||||||||||||||||
| 374 | 1 | last; | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | 3 | $select; | |||||||||||||||||||||||||||
| 378 | 2 | }); | |||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 2 | ▶ | $self->cache_write($type, $self->serialize_class->stringify($select->as_hash)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 381 | if ($select); | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 2 | return $select; | |||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | sub open_file { | ||||||||||||||||||||||||||||
| 387 | 67 | ▶ | my ($self, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
open_file: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 388 | 2 | ▶ | open my $fh, '<:encoding(UTF-8)', $file or die "Cannot open file $file for reading: $!"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | 67 | return $fh; | |||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | sub open_write_file { | ||||||||||||||||||||||||||||
| 393 | 1760 | ▶ | my ($self, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
open_write_file: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 394 | 1760 | ▶ | open my $fh, '>:encoding(UTF-8)', $file or die "Cannot open file $file for writing: $!"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | 1760 | return $fh; | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | sub seek_file { | ||||||||||||||||||||||||||||
| 399 | 0 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
seek_file: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 400 | 0 | ▶ | @args = (0, 0) if (!scalar @args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | 0 | seek $self->file_handle, shift @args, shift @args; | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | sub read_file { | ||||||||||||||||||||||||||||
| 405 | 67 | ▶ | my ($self, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 406 | 67 | my $fh = $self->open_file($file); | |||||||||||||||||||||||||||
| 407 | 67 | my $data = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 408 | 67 | return $data; | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | sub read_directory { | ||||||||||||||||||||||||||||
| 412 | 1937 | ▶ | my ($self, $directory) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read_directory: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 413 | 1937 | $self->write_directory($directory); | |||||||||||||||||||||||||||
| 414 | 1937 | ▶ | opendir(my $dh, $directory) || die "Can't opendir $directory: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 415 | 1937 | my @files = sort { $a cmp $b } grep { $_ !~ m/^\.+$/ } readdir($dh); | |||||||||||||||||||||||||||
| 416 | 1937 | closedir $dh; | |||||||||||||||||||||||||||
| 417 | 1937 | return \@files; | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | sub write_directory { | ||||||||||||||||||||||||||||
| 421 | 3697 | ▶ | my ($self, $directory, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
write_directory: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 422 | 3697 | my @parts = split '/', $directory; | |||||||||||||||||||||||||||
| 423 | 3697 | my $path = ''; | |||||||||||||||||||||||||||
| 424 | 3697 | ▶ | pop @parts if $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | 3697 | ▶ | while (@parts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 426 | 18485 | ▶ | $path .= "/" if $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 427 | 18485 | $path .= shift @parts; | |||||||||||||||||||||||||||
| 428 | 18485 | ▶ | mkdir $path unless -d $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | sub write_file { | ||||||||||||||||||||||||||||
| 433 | 1760 | ▶ | my ($self, $file, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
write_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 434 | 1760 | $self->write_directory($file, 1); | |||||||||||||||||||||||||||
| 435 | 1760 | my $fh = $self->open_write_file($file); | |||||||||||||||||||||||||||
| 436 | 1760 | print $fh $data; | |||||||||||||||||||||||||||
| 437 | 1760 | $self->close_file($fh); | |||||||||||||||||||||||||||
| 438 | 1760 | (my $real = $file) =~ s/\.tmp$//; | |||||||||||||||||||||||||||
| 439 | 1760 | move($file, $real); | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | sub unlink_file { | ||||||||||||||||||||||||||||
| 443 | 5 | ▶ | my ($self, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
unlink_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 444 | 5 | unlink $file; | |||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | sub close_file { | ||||||||||||||||||||||||||||
| 448 | 1760 | ▶ | close $_[1]; | ||||||||||||||||||||||||||
|
Subroutine
close_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | 70 | 1; | |||||||||||||||||||||||||||