| 1 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 1 | ▶ | use 5.010; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | package FusqlFS::Artifact::Table::Lazy; | ||||||||||||
| 5 | 1 | ▶ | use FusqlFS::Version; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | our $VERSION = $FusqlFS::Version::VERSION; | |||||||||||
| 7 | 1 | ▶ | use parent 'FusqlFS::Artifact'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | =head1 NAME | ||||||||||||
| 10 | |||||||||||||
| 11 | FusqlFS::Artifact::Table::Lazy - lazily created table artifact abstract class | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||
| 14 | |||||||||||||
| 15 | package FusqlFS::Backend::PgSQL::Table::Indices; | ||||||||||||
| 16 | use parent 'FusqlFS::Artifact::Table::Lazy'; | ||||||||||||
| 17 | |||||||||||||
| 18 | sub new | ||||||||||||
| 19 | { | ||||||||||||
| 20 | my $class = shift; | ||||||||||||
| 21 | my $self = $class->SUPER::new(@_); | ||||||||||||
| 22 | |||||||||||||
| 23 | $self->{template} = { '.order' => [] }; | ||||||||||||
| 24 | |||||||||||||
| 25 | # Initialize indices specific resources | ||||||||||||
| 26 | bless $self, $class; | ||||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | sub get | ||||||||||||
| 30 | { | ||||||||||||
| 31 | my $self = shift; | ||||||||||||
| 32 | unless ($self->SUPER::get(@_)) | ||||||||||||
| 33 | { | ||||||||||||
| 34 | # get and return object from database | ||||||||||||
| 35 | } | ||||||||||||
| 36 | } | ||||||||||||
| 37 | |||||||||||||
| 38 | sub list | ||||||||||||
| 39 | { | ||||||||||||
| 40 | my $self = shift; | ||||||||||||
| 41 | my ($table) = @_; | ||||||||||||
| 42 | return [ @{$self->do($self->{list_expr}, [$table])}, @{$self->SUPER::list($table)} ]; | ||||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | sub store | ||||||||||||
| 46 | { | ||||||||||||
| 47 | my $self = shift; | ||||||||||||
| 48 | my ($table, $name, $data) = @_; | ||||||||||||
| 49 | |||||||||||||
| 50 | # validate input $data and build query, e.g. | ||||||||||||
| 51 | # my $struct = $self->validate($data, { ... }) or return; | ||||||||||||
| 52 | |||||||||||||
| 53 | $self->drop($table, $name) and $self->do($self->{store_expr}, [$table, $name, $struct]); | ||||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | sub drop | ||||||||||||
| 57 | { | ||||||||||||
| 58 | my $self = shift; | ||||||||||||
| 59 | my ($table, $name) = @_; | ||||||||||||
| 60 | $self->SUPER::drop($table, $name) or $self->do($self->{drop_expr}, [$table, $name]); | ||||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | sub rename | ||||||||||||
| 64 | { | ||||||||||||
| 65 | my $self = shift; | ||||||||||||
| 66 | my ($table, $name, $newname) = @_; | ||||||||||||
| 67 | $self->SUPER::rename($table, $name, $newname) or $self->do($self->{rename_expr}, [$table, $name, $newname]); | ||||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | =head1 DESCRIPTION | ||||||||||||
| 71 | |||||||||||||
| 72 | Some database artifacts can't be created without any initial data, like | ||||||||||||
| 73 | indices, so it is impossible to implement L<FusqlFS::Artifact/create> to create | ||||||||||||
| 74 | "empty" artifact. | ||||||||||||
| 75 | |||||||||||||
| 76 | This class implements "lazy" table artifacts creation. When new database object | ||||||||||||
| 77 | is to be created, this class's C<create()> creates empty placeholder in special | ||||||||||||
| 78 | inner cache by cloning C<template> instance property you should initialize in | ||||||||||||
| 79 | C<new()> (default is empty hashref, so new object will be visible as empty | ||||||||||||
| 80 | directory), so no actual database object is created at all. It should be | ||||||||||||
| 81 | created in overriden C<store> method or this object will disappear after file | ||||||||||||
| 82 | system is unmounted, as there's no corresponding database artifact behind it. | ||||||||||||
| 83 | |||||||||||||
| 84 | Overriden C<store> method should also either remove cache entry on successful | ||||||||||||
| 85 | object creation by calling C<$self-E<gt>SUPER::drop> or update this cache entry | ||||||||||||
| 86 | by calling C<$self-E<gt>SUPER::store> with already available data if these data | ||||||||||||
| 87 | are not enough to create actual database artifact. | ||||||||||||
| 88 | |||||||||||||
| 89 | All the other methods of this class should be consulted by overriden methods to | ||||||||||||
| 90 | make sure user will see underlying "creation" cache entry in case there's no | ||||||||||||
| 91 | actual database object by given name. | ||||||||||||
| 92 | |||||||||||||
| 93 | The rule of thumb is to drop this cache's entry with C<drop> when database | ||||||||||||
| 94 | artifact is created, so every time this cache is checked, no entry for already | ||||||||||||
| 95 | created object should be found in it. | ||||||||||||
| 96 | |||||||||||||
| 97 | =head1 METHODS | ||||||||||||
| 98 | |||||||||||||
| 99 | =over | ||||||||||||
| 100 | |||||||||||||
| 101 | =item new | ||||||||||||
| 102 | |||||||||||||
| 103 | Constructor. | ||||||||||||
| 104 | |||||||||||||
| 105 | Output: $lazy_artifact_instance. | ||||||||||||
| 106 | |||||||||||||
| 107 | You should usually override this constructor, to set C<$self-E<gt>{template}> | ||||||||||||
| 108 | to artifact placeholder template. | ||||||||||||
| 109 | |||||||||||||
| 110 | =cut | ||||||||||||
| 111 | |||||||||||||
| 112 | sub new | ||||||||||||
| 113 | 1 | ▶ | { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 114 | 1 | my $class = shift; | |||||||||||
| 115 | 1 | my $self = {}; | |||||||||||
| 116 | |||||||||||||
| 117 | 1 | $self->{create_cache} = {}; | |||||||||||
| 118 | 1 | $self->{template} = {}; | |||||||||||
| 119 | |||||||||||||
| 120 | 1 | bless $self, $class; | |||||||||||
| 121 | 1 | $self->init(@_); | |||||||||||
| 122 | 1 | return $self; | |||||||||||
| 123 | } | ||||||||||||
| 124 | |||||||||||||
| 125 | =item clone | ||||||||||||
| 126 | |||||||||||||
| 127 | Static method, clones given structure and returns this clone. | ||||||||||||
| 128 | |||||||||||||
| 129 | Input: $data. | ||||||||||||
| 130 | Output: $cloned_data. | ||||||||||||
| 131 | |||||||||||||
| 132 | This method implements deep recursive cloning of input data. It is used to | ||||||||||||
| 133 | clone C<template> property to keep it intact and avoid it's erroneous | ||||||||||||
| 134 | modification. | ||||||||||||
| 135 | |||||||||||||
| 136 | =begin testing clone | ||||||||||||
| 137 | |||||||||||||
| 138 | is_deeply {_tpkg}::clone({ a => 1, b => 2, c => 3 }), { a => 1, b => 2, c => 3 }; | ||||||||||||
| 139 | is_deeply {_tpkg}::clone([ 3, 2, 1 ]), [ 3, 2, 1 ]; | ||||||||||||
| 140 | is_deeply {_tpkg}::clone(\'string'), \'string'; | ||||||||||||
| 141 | is_deeply {_tpkg}::clone({ a => [ 3, 2, 1 ], b => { c => 1, d => [ 6, \5, 4 ] }, c => \"string" }), | ||||||||||||
| 142 | { a => [ 3, 2, 1 ], b => { c => 1, d => [ 6, \5, 4 ] }, c => \"string" }; | ||||||||||||
| 143 | |||||||||||||
| 144 | =end testing | ||||||||||||
| 145 | =cut | ||||||||||||
| 146 | sub clone | ||||||||||||
| 147 | 22 | ▶ | { | ||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 148 | 22 | my $ref = $_[0]; | |||||||||||
| 149 | 22 | my $result; | |||||||||||
| 150 | 22 | given (ref $ref) | |||||||||||
| 151 | { | ||||||||||||
| 152 | 22 | when ('HASH') { $result = { map { $_ => clone($ref->{$_}) } keys %$ref } } | |||||||||||
| 153 | 18 | when ('ARRAY') { $result = [ map { clone($_) } @$ref ] } | |||||||||||
| 154 | 15 | when ('SCALAR') { my $tmp = $$ref; $result = \$tmp; } | |||||||||||
| 155 | 12 | default { $result = $ref; } | |||||||||||
| 156 | } | ||||||||||||
| 157 | 22 | return $result; | |||||||||||
| 158 | } | ||||||||||||
| 159 | |||||||||||||
| 160 | =item create | ||||||||||||
| 161 | |||||||||||||
| 162 | Create cache entry using C<template> instance property as a template for placeholder. | ||||||||||||
| 163 | |||||||||||||
| 164 | Input: $table, $name. | ||||||||||||
| 165 | Output: $success. | ||||||||||||
| 166 | |||||||||||||
| 167 | This methods uses L</clone> method to clone C<$self-E<gt>{template}> and put | ||||||||||||
| 168 | this fresh placeholder value into creation cache, returns true on success | ||||||||||||
| 169 | or undef on failure. | ||||||||||||
| 170 | |||||||||||||
| 171 | =begin testing create after get list | ||||||||||||
| 172 | |||||||||||||
| 173 | isnt $_tobj->create('table', 'name'), undef; | ||||||||||||
| 174 | isnt $_tobj->get('table', 'name'), $_tobj->{template}; | ||||||||||||
| 175 | is_deeply $_tobj->get('table', 'name'), $_tobj->{template}; | ||||||||||||
| 176 | is_deeply $_tobj->list('table'), [ 'name' ]; | ||||||||||||
| 177 | |||||||||||||
| 178 | =end testing | ||||||||||||
| 179 | =cut | ||||||||||||
| 180 | sub create | ||||||||||||
| 181 | 1 | ▶ | { | ||||||||||
|
Subroutine
create: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 182 | 1 | my $self = shift; | |||||||||||
| 183 | 1 | my ($table, $name) = @_; | |||||||||||
| 184 | 1 | ▶ | $self->{create_cache}->{$table} ||= {}; | ||||||||||
|
Condition50%1/2
$self->{"create_cache"}{$table} ||= {}
MC/DC0%0/1
$self->{"create_cache"}{$table} ||= {}
$self->{"create_cache"}{$table}
Truth table50%1/2
A
A= $self->{"create_cache"}{$table}
| |||||||||||||
| 185 | 1 | $self->{create_cache}->{$table}->{$name} = clone($self->{template}); | |||||||||||
| 186 | 1 | return 1; | |||||||||||
| 187 | } | ||||||||||||
| 188 | |||||||||||||
| 189 | =item drop | ||||||||||||
| 190 | |||||||||||||
| 191 | Drops given item from creation cache by name. | ||||||||||||
| 192 | |||||||||||||
| 193 | Input: $table, $name. | ||||||||||||
| 194 | Output: $success. | ||||||||||||
| 195 | |||||||||||||
| 196 | This method removes given object from inner creation cache and returns true | ||||||||||||
| 197 | on success or undef on failure (in case given cache entry doesn't exist). | ||||||||||||
| 198 | |||||||||||||
| 199 | You should use this method in your own C<store> method in this class's subclass | ||||||||||||
| 200 | to drop creation cache item if it was correctly created in database. | ||||||||||||
| 201 | |||||||||||||
| 202 | =begin testing drop after rename | ||||||||||||
| 203 | |||||||||||||
| 204 | isnt $_tobj->drop('table', 'newname'), undef; | ||||||||||||
| 205 | is $_tobj->get('table', 'newname'), undef; | ||||||||||||
| 206 | is_deeply $_tobj->list('table'), []; | ||||||||||||
| 207 | |||||||||||||
| 208 | =end testing | ||||||||||||
| 209 | =cut | ||||||||||||
| 210 | sub drop | ||||||||||||
| 211 | 1 | ▶ | { | ||||||||||
|
Subroutine
drop: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||
| 212 | 1 | my $self = shift; | |||||||||||
| 213 | 1 | my ($table, $name) = @_; | |||||||||||
| 214 | 1 | ▶ | if (exists $self->{create_cache}->{$table}->{$name}) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 215 | { | ||||||||||||
| 216 | 1 | delete $self->{create_cache}->{$table}->{$name}; | |||||||||||
| 217 | 1 | return 1; | |||||||||||
| 218 | } | ||||||||||||
| 219 | 0 | return; | |||||||||||
| 220 | } | ||||||||||||
| 221 | |||||||||||||
| 222 | =item rename | ||||||||||||
| 223 | |||||||||||||
| 224 | Renames given item in creation cache. | ||||||||||||
| 225 | |||||||||||||
| 226 | Input: $table, $name, $newname. | ||||||||||||
| 227 | Output: $success. | ||||||||||||
| 228 | |||||||||||||
| 229 | This method drops old creation cache entry by given name and stores it under | ||||||||||||
| 230 | new name. It returns true on success or undef on failure (in case given cache | ||||||||||||
| 231 | entry doesn't exist). | ||||||||||||
| 232 | |||||||||||||
| 233 | =begin testing rename after create | ||||||||||||
| 234 | |||||||||||||
| 235 | is $_tobj->rename('table', 'aname', 'anewname'), undef; | ||||||||||||
| 236 | is $_tobj->get('table', 'aname'), undef; | ||||||||||||
| 237 | is $_tobj->get('table', 'anewname'), undef; | ||||||||||||
| 238 | |||||||||||||
| 239 | isnt $_tobj->rename('table', 'name', 'newname'), undef; | ||||||||||||
| 240 | is $_tobj->get('table', 'name'), undef; | ||||||||||||
| 241 | is_deeply $_tobj->get('table', 'newname'), $_tobj->{template}; | ||||||||||||
| 242 | is_deeply $_tobj->list('table'), [ 'newname' ]; | ||||||||||||
| 243 | |||||||||||||
| 244 | =end testing | ||||||||||||
| 245 | =cut | ||||||||||||
| 246 | sub rename | ||||||||||||
| 247 | 2 | ▶ | { | ||||||||||
|
Subroutine
rename: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 248 | 2 | my $self = shift; | |||||||||||
| 249 | 2 | my ($table, $name, $newname) = @_; | |||||||||||
| 250 | 2 | ▶ | if (exists $self->{create_cache}->{$table}->{$name}) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 251 | { | ||||||||||||
| 252 | 1 | $self->{create_cache}->{$table}->{$newname} = $self->{create_cache}->{$table}->{$name}; | |||||||||||
| 253 | 1 | delete $self->{create_cache}->{$table}->{$name}; | |||||||||||
| 254 | 1 | return 1; | |||||||||||
| 255 | } | ||||||||||||
| 256 | 1 | return; | |||||||||||
| 257 | } | ||||||||||||
| 258 | |||||||||||||
| 259 | =item list | ||||||||||||
| 260 | |||||||||||||
| 261 | Returns arrayref with all objects contained in creation cache under given table | ||||||||||||
| 262 | name. | ||||||||||||
| 263 | |||||||||||||
| 264 | Input: $table, $name. | ||||||||||||
| 265 | Output: $arrayref. | ||||||||||||
| 266 | |||||||||||||
| 267 | This method accepts table name on input and returns keys from creation cache | ||||||||||||
| 268 | stored under this name packed into single arrayref. Returns empty arrayref if | ||||||||||||
| 269 | no objects in creation cache under given table name. | ||||||||||||
| 270 | |||||||||||||
| 271 | =begin testing list | ||||||||||||
| 272 | |||||||||||||
| 273 | is_deeply $_tobj->list('table'), [], 'list is sane'; | ||||||||||||
| 274 | |||||||||||||
| 275 | =end testing | ||||||||||||
| 276 | =cut | ||||||||||||
| 277 | sub list | ||||||||||||
| 278 | 4 | ▶ | { | ||||||||||
|
Subroutine
list: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 279 | 4 | my $self = shift; | |||||||||||
| 280 | 4 | my ($table) = @_; | |||||||||||
| 281 | 4 | ▶ | return [ keys %{$self->{create_cache}->{$table}||{}} ]; | ||||||||||
|
Condition50%1/2
$self->{'create_cache'}{$table} || {}
MC/DC0%0/1
$self->{'create_cache'}{$table} || {}
$self->{'create_cache'}{$table}
Truth table50%1/2
A
A= $self->{'create_cache'}{$table}
| |||||||||||||
| 282 | } | ||||||||||||
| 283 | |||||||||||||
| 284 | =item get | ||||||||||||
| 285 | |||||||||||||
| 286 | Returns cache entry by given name or undef it cache is missed. | ||||||||||||
| 287 | |||||||||||||
| 288 | Input: $table, $name. | ||||||||||||
| 289 | Output: $cache_entry. | ||||||||||||
| 290 | |||||||||||||
| 291 | This method checks creation cache and returns cache entry by given name. | ||||||||||||
| 292 | If cache entry by the name is absent, it returns undef. | ||||||||||||
| 293 | |||||||||||||
| 294 | =begin testing get | ||||||||||||
| 295 | |||||||||||||
| 296 | is $_tobj->get('table', 'name'), undef, 'get is sane'; | ||||||||||||
| 297 | is $_tobj->get('table', 'name'), undef, 'get has no side effects'; | ||||||||||||
| 298 | |||||||||||||
| 299 | =end testing | ||||||||||||
| 300 | =cut | ||||||||||||
| 301 | sub get | ||||||||||||
| 302 | 9 | ▶ | { | ||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 303 | 9 | my $self = shift; | |||||||||||
| 304 | 9 | my ($table, $name) = @_; | |||||||||||
| 305 | 9 | ▶ | return $self->{create_cache}->{$table}->{$name}||undef; | ||||||||||
|
Condition100%2/2
$self->{'create_cache'}{$table}{$name} || undef
MC/DC100%1/1
$self->{'create_cache'}{$table}{$name} || undef
$self->{'create_cache'}{$table}{$name}
Truth table100%2/2
A
A= $self->{'create_cache'}{$table}{$name}
| |||||||||||||
| 306 | } | ||||||||||||
| 307 | |||||||||||||
| 308 | =item store | ||||||||||||
| 309 | |||||||||||||
| 310 | Updates creation cache under given name with new data. | ||||||||||||
| 311 | |||||||||||||
| 312 | Input: $table, $name, $data. | ||||||||||||
| 313 | Output: $success. | ||||||||||||
| 314 | |||||||||||||
| 315 | This method stores new data in creation cache under given name and returns true | ||||||||||||
| 316 | on success or undef on failure (which should never happen). | ||||||||||||
| 317 | |||||||||||||
| 318 | You should use this method in your own C<store> method in this class's subclass | ||||||||||||
| 319 | to update creation cache item if there were not enough data to create the | ||||||||||||
| 320 | object in database immediately. | ||||||||||||
| 321 | |||||||||||||
| 322 | =cut | ||||||||||||
| 323 | sub store | ||||||||||||
| 324 | 0 | ▶ | { | ||||||||||
|
Subroutine
store: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 325 | 0 | my $self = shift; | |||||||||||
| 326 | 0 | my ($table, $name, $data) = @_; | |||||||||||
| 327 | 0 | ▶ | $self->{create_cache}->{$table} ||= {}; | ||||||||||
|
Condition0%0/2
$self->{"create_cache"}{$table} ||= {}
MC/DC0%0/1
$self->{"create_cache"}{$table} ||= {}
$self->{"create_cache"}{$table}
Truth table0%0/2
A
A= $self->{"create_cache"}{$table}
| |||||||||||||
| 328 | 0 | $self->{create_cache}->{$table}->{$name} = $data; | |||||||||||
| 329 | } | ||||||||||||
| 330 | |||||||||||||
| 331 | 1 | 1; | |||||||||||
| 332 | |||||||||||||
| 333 | __END__ | ||||||||||||