| 1 | package Zing::Savepoint; | ||
| 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::Entity'; | ||
| 15 | |||
| 16 | our $VERSION = '0.26'; # VERSION | ||
| 17 | |||
| 18 | # ATTRIBUTES | ||
| 19 | |||
| 20 | has 'lookup' => ( | ||
| 21 | is => 'ro', | ||
| 22 | isa => 'Lookup', | ||
| 23 | req => 1, | ||
| 24 | ); | ||
| 25 | |||
| 26 | # METHODS | ||
| 27 | |||
| 28 | method cached() { | ||
| 29 | return $self->{cached} ||= $self->recv; | ||
| 30 | } | ||
| 31 | |||
| 32 | method capture() { | ||
| 33 | my $properties = {}; | ||
| 34 | |||
| 35 | my $lookup = $self->lookup->apply; | ||
| 36 | |||
| 37 | $properties->{position} = $lookup->{position}; | ||
| 38 | $properties->{metadata} = $lookup->metadata; | ||
| 39 | $properties->{snapshot} = $lookup->state; | ||
| 40 | |||
| 41 | return $properties; | ||
| 42 | } | ||
| 43 | |||
| 44 | method drop() { | ||
| 45 | return $self->repo->drop; | ||
| 46 | } | ||
| 47 | |||
| 48 | method position() { | ||
| 49 | my $capture = $self->cached or return undef; | ||
| 50 | return $capture->{position}; | ||
| 51 | } | ||
| 52 | |||
| 53 | method metadata() { | ||
| 54 | my $capture = $self->cached or return undef; | ||
| 55 | return $capture->{metadata}; | ||
| 56 | } | ||
| 57 | |||
| 58 | method name() { | ||
| 59 | return join('#', $self->lookup->name, 'savepoint'); | ||
| 60 | } | ||
| 61 | |||
| 62 | method repo() { | ||
| 63 | return $self->app->keyval(name => $self->name); | ||
| 64 | } | ||
| 65 | |||
| 66 | method recv() { | ||
| 67 | return $self->repo->recv; | ||
| 68 | } | ||
| 69 | |||
| 70 | method send() { | ||
| 71 | my $capture = $self->capture; | ||
| 72 | $self->repo->send($capture); | ||
| 73 | return $self->{cached} = $capture; | ||
| 74 | } | ||
| 75 | |||
| 76 | method snapshot() { | ||
| 77 | my $capture = $self->cached or return undef; | ||
| 78 | return $capture->{snapshot}; | ||
| 79 | } | ||
| 80 | |||
| 81 | method test() { | ||
| 82 | return $self->repo->test; | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; | ||
| 86 | |||
| 87 | =encoding utf8 | ||
| 88 | |||
| 89 | =head1 NAME | ||
| 90 | |||
| 91 | Zing::Savepoint - Lookup Table Savepoint | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | =head1 ABSTRACT | ||
| 96 | |||
| 97 | Lookup Table Savepoint Construct | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | =head1 SYNOPSIS | ||
| 102 | |||
| 103 | use Zing::Lookup; | ||
| 104 | use Zing::Savepoint; | ||
| 105 | |||
| 106 | my $lookup = Zing::Lookup->new(name => 'users'); | ||
| 107 | |||
| 108 | $lookup->set('user-12345')->set(username => 'u12345'); | ||
| 109 | $lookup->set('user-12346')->set(username => 'u12346'); | ||
| 110 | $lookup->set('user-12347')->set(username => 'u12347'); | ||
| 111 | |||
| 112 | my $savepoint = Zing::Savepoint->new(lookup => $lookup); | ||
| 113 | |||
| 114 | # $savepoint->test; | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | =head1 DESCRIPTION | ||
| 119 | |||
| 120 | This package provides a savepoint mechanism for saving and restoring large | ||
| 121 | L<Zing::Lookup> indices. If a lookup has an associated savepoint it will be | ||
| 122 | used to build the index on (L<Zing::Lookup>) object construction automatically, | ||
| 123 | however, creating the savepoint (saving the state of the index) needs to be | ||
| 124 | done manually. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | =head1 LIBRARIES | ||
| 129 | |||
| 130 | This package uses type constraints from: | ||
| 131 | |||
| 132 | L<Zing::Types> | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | =head1 ATTRIBUTES | ||
| 137 | |||
| 138 | This package has the following attributes: | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | =head2 lookup | ||
| 143 | |||
| 144 | lookup(Lookup) | ||
| 145 | |||
| 146 | This attribute is read-only, accepts C<(Lookup)> values, and is required. | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | =head1 METHODS | ||
| 151 | |||
| 152 | This package implements the following methods: | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | =head2 capture | ||
| 157 | |||
| 158 | capture() : HashRef | ||
| 159 | |||
| 160 | The capture method returns the relevant state properties from the lookup. | ||
| 161 | |||
| 162 | =over 4 | ||
| 163 | |||
| 164 | =item capture example #1 | ||
| 165 | |||
| 166 | # given: synopsis | ||
| 167 | |||
| 168 | $savepoint->capture; | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | =head2 drop | ||
| 175 | |||
| 176 | drop() : Bool | ||
| 177 | |||
| 178 | The drop method removes the persisted savepoint. | ||
| 179 | |||
| 180 | =over 4 | ||
| 181 | |||
| 182 | =item drop example #1 | ||
| 183 | |||
| 184 | # given: synopsis | ||
| 185 | |||
| 186 | $savepoint->drop; | ||
| 187 | |||
| 188 | =back | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | =head2 metadata | ||
| 193 | |||
| 194 | metadata() : HashRef | ||
| 195 | |||
| 196 | The metadata method returns the cached metadata property for the lookup. | ||
| 197 | |||
| 198 | =over 4 | ||
| 199 | |||
| 200 | =item metadata example #1 | ||
| 201 | |||
| 202 | # given: synopsis | ||
| 203 | |||
| 204 | $savepoint->metadata; | ||
| 205 | |||
| 206 | =back | ||
| 207 | |||
| 208 | =cut | ||
| 209 | |||
| 210 | =head2 name | ||
| 211 | |||
| 212 | name() : Str | ||
| 213 | |||
| 214 | The name method returns the generated savepoint name. | ||
| 215 | |||
| 216 | =over 4 | ||
| 217 | |||
| 218 | =item name example #1 | ||
| 219 | |||
| 220 | # given: synopsis | ||
| 221 | |||
| 222 | $savepoint->name; | ||
| 223 | |||
| 224 | =back | ||
| 225 | |||
| 226 | =cut | ||
| 227 | |||
| 228 | =head2 position | ||
| 229 | |||
| 230 | position() : Int | ||
| 231 | |||
| 232 | The position method returns the cached position property for the lookup. | ||
| 233 | |||
| 234 | =over 4 | ||
| 235 | |||
| 236 | =item position example #1 | ||
| 237 | |||
| 238 | use Zing::Lookup; | ||
| 239 | use Zing::Savepoint; | ||
| 240 | |||
| 241 | my $lookup = Zing::Lookup->new(name => 'users'); | ||
| 242 | my $savepoint = Zing::Savepoint->new(lookup => $lookup); | ||
| 243 | |||
| 244 | $lookup->drop; | ||
| 245 | $savepoint->drop; | ||
| 246 | |||
| 247 | $lookup->set('user-12345')->set(username => 'u12345'); | ||
| 248 | $lookup->set('user-12346')->set(username => 'u12346'); | ||
| 249 | $lookup->set('user-12347')->set(username => 'u12347'); | ||
| 250 | |||
| 251 | $savepoint->send; | ||
| 252 | $savepoint->position; | ||
| 253 | |||
| 254 | =back | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | =head2 recv | ||
| 259 | |||
| 260 | recv() : Any | ||
| 261 | |||
| 262 | The recv method returns the data (if any) associated with the savepoint. | ||
| 263 | |||
| 264 | =over 4 | ||
| 265 | |||
| 266 | =item recv example #1 | ||
| 267 | |||
| 268 | # given: synopsis | ||
| 269 | |||
| 270 | $savepoint->recv; | ||
| 271 | |||
| 272 | =back | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | =head2 repo | ||
| 277 | |||
| 278 | repo() : KeyVal | ||
| 279 | |||
| 280 | The repo method returns the L<Zing::KeyVal> object used to manage the | ||
| 281 | savepoint. | ||
| 282 | |||
| 283 | =over 4 | ||
| 284 | |||
| 285 | =item repo example #1 | ||
| 286 | |||
| 287 | # given: synopsis | ||
| 288 | |||
| 289 | $savepoint->repo; | ||
| 290 | |||
| 291 | =back | ||
| 292 | |||
| 293 | =cut | ||
| 294 | |||
| 295 | =head2 send | ||
| 296 | |||
| 297 | send() : HashRef | ||
| 298 | |||
| 299 | The send method caches and stores the data from L</capture> as a savepoint and | ||
| 300 | returns the data. | ||
| 301 | |||
| 302 | =over 4 | ||
| 303 | |||
| 304 | =item send example #1 | ||
| 305 | |||
| 306 | # given: synopsis | ||
| 307 | |||
| 308 | $savepoint->send; | ||
| 309 | |||
| 310 | =back | ||
| 311 | |||
| 312 | =cut | ||
| 313 | |||
| 314 | =head2 snapshot | ||
| 315 | |||
| 316 | snapshot() : HashRef | ||
| 317 | |||
| 318 | The snapshot method returns the cached snapshot property for the lookup. | ||
| 319 | |||
| 320 | =over 4 | ||
| 321 | |||
| 322 | =item snapshot example #1 | ||
| 323 | |||
| 324 | # given: synopsis | ||
| 325 | |||
| 326 | $savepoint->snapshot; | ||
| 327 | |||
| 328 | =back | ||
| 329 | |||
| 330 | =cut | ||
| 331 | |||
| 332 | =head2 test | ||
| 333 | |||
| 334 | test() : Bool | ||
| 335 | |||
| 336 | The test method checks whether the savepoint exists and returns truthy or falsy. | ||
| 337 | |||
| 338 | =over 4 | ||
| 339 | |||
| 340 | =item test example #1 | ||
| 341 | |||
| 342 | # given: synopsis | ||
| 343 | |||
| 344 | $savepoint->repo->drop('state'); | ||
| 345 | |||
| 346 | $savepoint->test; | ||
| 347 | |||
| 348 | =back | ||
| 349 | |||
| 350 | =over 4 | ||
| 351 | |||
| 352 | =item test example #2 | ||
| 353 | |||
| 354 | # given: synopsis | ||
| 355 | |||
| 356 | $savepoint->send; | ||
| 357 | |||
| 358 | $savepoint->test; | ||
| 359 | |||
| 360 | =back | ||
| 361 | |||
| 362 | =cut | ||
| 363 | |||
| 364 | =head1 AUTHOR | ||
| 365 | |||
| 366 | Al Newkirk, C<awncorp@cpan.org> | ||
| 367 | |||
| 368 | =head1 LICENSE | ||
| 369 | |||
| 370 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 371 | |||
| 372 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 373 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 374 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 375 | |||
| 376 | =head1 PROJECT | ||
| 377 | |||
| 378 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 379 | |||
| 380 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 381 | |||
| 382 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 383 | |||
| 384 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 385 | |||
| 386 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 387 | |||
| 388 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 389 | |||
| 390 | =cut |