| 1 | package Zing::Store::Temp; | ||
| 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::Disk'; | ||
| 15 | |||
| 16 | our $VERSION = '0.26'; # VERSION | ||
| 17 | |||
| 18 | # ATTRIBUTES | ||
| 19 | |||
| 20 | has root => ( | ||
| 21 | is => 'ro', | ||
| 22 | isa => 'Str', | ||
| 23 | init_arg => undef, | ||
| 24 | new => 1, | ||
| 25 | ); | ||
| 26 | |||
| 27 | fun new_root($self) { | ||
| 28 | File::Spec->tmpdir | ||
| 29 | } | ||
| 30 | |||
| 31 | # BUILDERS | ||
| 32 | |||
| 33 | fun new_encoder($self) { | ||
| 34 | require Zing::Encoder::Dump; Zing::Encoder::Dump->new; | ||
| 35 | } | ||
| 36 | |||
| 37 | 1; | ||
| 38 | |||
| 39 | =encoding utf8 | ||
| 40 | |||
| 41 | =head1 NAME | ||
| 42 | |||
| 43 | Zing::Store::Temp - Temp Storage | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | =head1 ABSTRACT | ||
| 48 | |||
| 49 | Temp Storage Abstraction | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | =head1 SYNOPSIS | ||
| 54 | |||
| 55 | use Zing::Store::Temp; | ||
| 56 | use Zing::Encoder::Dump; | ||
| 57 | |||
| 58 | my $temp = Zing::Store::Temp->new( | ||
| 59 | encoder => Zing::Encoder::Dump->new | ||
| 60 | ); | ||
| 61 | |||
| 62 | # $temp->drop; | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | =head1 DESCRIPTION | ||
| 67 | |||
| 68 | This package provides an in-memory (only) storage adapter for use with data | ||
| 69 | persistence abstractions. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | =head1 INHERITS | ||
| 74 | |||
| 75 | This package inherits behaviors from: | ||
| 76 | |||
| 77 | L<Zing::Store::Disk> | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | =head1 LIBRARIES | ||
| 82 | |||
| 83 | This package uses type constraints from: | ||
| 84 | |||
| 85 | L<Zing::Types> | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | =head1 METHODS | ||
| 90 | |||
| 91 | This package implements the following methods: | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | =head2 decode | ||
| 96 | |||
| 97 | decode(Str $data) : HashRef | ||
| 98 | |||
| 99 | The decode method decodes the JSON data provided and returns the data as a hashref. | ||
| 100 | |||
| 101 | =over 4 | ||
| 102 | |||
| 103 | =item decode example #1 | ||
| 104 | |||
| 105 | # given: synopsis | ||
| 106 | |||
| 107 | $temp->decode('{"status"=>"ok"}'); | ||
| 108 | |||
| 109 | =back | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | =head2 drop | ||
| 114 | |||
| 115 | drop(Str $key) : Int | ||
| 116 | |||
| 117 | The drop method removes (drops) the item from the datastore. | ||
| 118 | |||
| 119 | =over 4 | ||
| 120 | |||
| 121 | =item drop example #1 | ||
| 122 | |||
| 123 | # given: synopsis | ||
| 124 | |||
| 125 | $temp->drop('zing:main:global:model:temp'); | ||
| 126 | |||
| 127 | =back | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | =head2 encode | ||
| 132 | |||
| 133 | encode(HashRef $data) : Str | ||
| 134 | |||
| 135 | The encode method encodes and returns the data provided as JSON. | ||
| 136 | |||
| 137 | =over 4 | ||
| 138 | |||
| 139 | =item encode example #1 | ||
| 140 | |||
| 141 | # given: synopsis | ||
| 142 | |||
| 143 | $temp->encode({ status => 'ok' }); | ||
| 144 | |||
| 145 | =back | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | =head2 keys | ||
| 150 | |||
| 151 | keys(Str @keys) : ArrayRef[Str] | ||
| 152 | |||
| 153 | The keys method returns a list of keys under the namespace of the datastore or | ||
| 154 | provided key. | ||
| 155 | |||
| 156 | =over 4 | ||
| 157 | |||
| 158 | =item keys example #1 | ||
| 159 | |||
| 160 | # given: synopsis | ||
| 161 | |||
| 162 | my $keys = $temp->keys('zing:main:global:model:temp'); | ||
| 163 | |||
| 164 | =back | ||
| 165 | |||
| 166 | =over 4 | ||
| 167 | |||
| 168 | =item keys example #2 | ||
| 169 | |||
| 170 | # given: synopsis | ||
| 171 | |||
| 172 | $temp->send('zing:main:global:model:temp', { status => 'ok' }); | ||
| 173 | |||
| 174 | my $keys = $temp->keys('zing:main:global:model:temp'); | ||
| 175 | |||
| 176 | =back | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | =head2 lpull | ||
| 181 | |||
| 182 | lpull(Str $key) : Maybe[HashRef] | ||
| 183 | |||
| 184 | The lpull method pops data off of the top of a list in the datastore. | ||
| 185 | |||
| 186 | =over 4 | ||
| 187 | |||
| 188 | =item lpull example #1 | ||
| 189 | |||
| 190 | # given: synopsis | ||
| 191 | |||
| 192 | $temp->lpull('zing:main:global:model:items'); | ||
| 193 | |||
| 194 | =back | ||
| 195 | |||
| 196 | =over 4 | ||
| 197 | |||
| 198 | =item lpull example #2 | ||
| 199 | |||
| 200 | # given: synopsis | ||
| 201 | |||
| 202 | $temp->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 203 | |||
| 204 | $temp->lpull('zing:main:global:model:items'); | ||
| 205 | |||
| 206 | =back | ||
| 207 | |||
| 208 | =cut | ||
| 209 | |||
| 210 | =head2 lpush | ||
| 211 | |||
| 212 | lpush(Str $key, HashRef $val) : Int | ||
| 213 | |||
| 214 | The lpush method pushed data onto the top of a list in the datastore. | ||
| 215 | |||
| 216 | =over 4 | ||
| 217 | |||
| 218 | =item lpush example #1 | ||
| 219 | |||
| 220 | # given: synopsis | ||
| 221 | |||
| 222 | $temp->lpush('zing:main:global:model:items', { status => '1' }); | ||
| 223 | |||
| 224 | =back | ||
| 225 | |||
| 226 | =over 4 | ||
| 227 | |||
| 228 | =item lpush example #2 | ||
| 229 | |||
| 230 | # given: synopsis | ||
| 231 | |||
| 232 | $temp->lpush('zing:main:global:model:items', { status => '0' }); | ||
| 233 | |||
| 234 | =back | ||
| 235 | |||
| 236 | =cut | ||
| 237 | |||
| 238 | =head2 recv | ||
| 239 | |||
| 240 | recv(Str $key) : Maybe[HashRef] | ||
| 241 | |||
| 242 | The recv method fetches and returns data from the datastore by its key. | ||
| 243 | |||
| 244 | =over 4 | ||
| 245 | |||
| 246 | =item recv example #1 | ||
| 247 | |||
| 248 | # given: synopsis | ||
| 249 | |||
| 250 | $temp->recv('zing:main:global:model:temp'); | ||
| 251 | |||
| 252 | =back | ||
| 253 | |||
| 254 | =over 4 | ||
| 255 | |||
| 256 | =item recv example #2 | ||
| 257 | |||
| 258 | # given: synopsis | ||
| 259 | |||
| 260 | $temp->send('zing:main:global:model:temp', { status => 'ok' }); | ||
| 261 | |||
| 262 | $temp->recv('zing:main:global:model:temp'); | ||
| 263 | |||
| 264 | =back | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | =head2 rpull | ||
| 269 | |||
| 270 | rpull(Str $key) : Maybe[HashRef] | ||
| 271 | |||
| 272 | The rpull method pops data off of the bottom of a list in the datastore. | ||
| 273 | |||
| 274 | =over 4 | ||
| 275 | |||
| 276 | =item rpull example #1 | ||
| 277 | |||
| 278 | # given: synopsis | ||
| 279 | |||
| 280 | $temp->rpull('zing:main:global:model:items'); | ||
| 281 | |||
| 282 | =back | ||
| 283 | |||
| 284 | =over 4 | ||
| 285 | |||
| 286 | =item rpull example #2 | ||
| 287 | |||
| 288 | # given: synopsis | ||
| 289 | |||
| 290 | $temp->rpush('zing:main:global:model:items', { status => 1 }); | ||
| 291 | $temp->rpush('zing:main:global:model:items', { status => 2 }); | ||
| 292 | |||
| 293 | $temp->rpull('zing:main:global:model:items'); | ||
| 294 | |||
| 295 | =back | ||
| 296 | |||
| 297 | =cut | ||
| 298 | |||
| 299 | =head2 rpush | ||
| 300 | |||
| 301 | rpush(Str $key, HashRef $val) : Int | ||
| 302 | |||
| 303 | The rpush method pushed data onto the bottom of a list in the datastore. | ||
| 304 | |||
| 305 | =over 4 | ||
| 306 | |||
| 307 | =item rpush example #1 | ||
| 308 | |||
| 309 | # given: synopsis | ||
| 310 | |||
| 311 | $temp->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 312 | |||
| 313 | =back | ||
| 314 | |||
| 315 | =over 4 | ||
| 316 | |||
| 317 | =item rpush example #2 | ||
| 318 | |||
| 319 | # given: synopsis | ||
| 320 | |||
| 321 | $temp->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 322 | |||
| 323 | $temp->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 324 | |||
| 325 | =back | ||
| 326 | |||
| 327 | =cut | ||
| 328 | |||
| 329 | =head2 send | ||
| 330 | |||
| 331 | send(Str $key, HashRef $val) : Str | ||
| 332 | |||
| 333 | The send method commits data to the datastore with its key and returns truthy. | ||
| 334 | |||
| 335 | =over 4 | ||
| 336 | |||
| 337 | =item send example #1 | ||
| 338 | |||
| 339 | # given: synopsis | ||
| 340 | |||
| 341 | $temp->send('zing:main:global:model:temp', { status => 'ok' }); | ||
| 342 | |||
| 343 | =back | ||
| 344 | |||
| 345 | =cut | ||
| 346 | |||
| 347 | =head2 size | ||
| 348 | |||
| 349 | size(Str $key) : Int | ||
| 350 | |||
| 351 | The size method returns the size of a list in the datastore. | ||
| 352 | |||
| 353 | =over 4 | ||
| 354 | |||
| 355 | =item size example #1 | ||
| 356 | |||
| 357 | # given: synopsis | ||
| 358 | |||
| 359 | my $size = $temp->size('zing:main:global:model:items'); | ||
| 360 | |||
| 361 | =back | ||
| 362 | |||
| 363 | =over 4 | ||
| 364 | |||
| 365 | =item size example #2 | ||
| 366 | |||
| 367 | # given: synopsis | ||
| 368 | |||
| 369 | $temp->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 370 | |||
| 371 | my $size = $temp->size('zing:main:global:model:items'); | ||
| 372 | |||
| 373 | =back | ||
| 374 | |||
| 375 | =cut | ||
| 376 | |||
| 377 | =head2 slot | ||
| 378 | |||
| 379 | slot(Str $key, Int $pos) : Maybe[HashRef] | ||
| 380 | |||
| 381 | The slot method returns the data from a list in the datastore by its index. | ||
| 382 | |||
| 383 | =over 4 | ||
| 384 | |||
| 385 | =item slot example #1 | ||
| 386 | |||
| 387 | # given: synopsis | ||
| 388 | |||
| 389 | my $model = $temp->slot('zing:main:global:model:items', 0); | ||
| 390 | |||
| 391 | =back | ||
| 392 | |||
| 393 | =over 4 | ||
| 394 | |||
| 395 | =item slot example #2 | ||
| 396 | |||
| 397 | # given: synopsis | ||
| 398 | |||
| 399 | $temp->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 400 | |||
| 401 | my $model = $temp->slot('zing:main:global:model:items', 0); | ||
| 402 | |||
| 403 | =back | ||
| 404 | |||
| 405 | =cut | ||
| 406 | |||
| 407 | =head2 test | ||
| 408 | |||
| 409 | test(Str $key) : Int | ||
| 410 | |||
| 411 | The test method returns truthy if the specific key (or datastore) exists. | ||
| 412 | |||
| 413 | =over 4 | ||
| 414 | |||
| 415 | =item test example #1 | ||
| 416 | |||
| 417 | # given: synopsis | ||
| 418 | |||
| 419 | $temp->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 420 | |||
| 421 | $temp->test('zing:main:global:model:items'); | ||
| 422 | |||
| 423 | =back | ||
| 424 | |||
| 425 | =over 4 | ||
| 426 | |||
| 427 | =item test example #2 | ||
| 428 | |||
| 429 | # given: synopsis | ||
| 430 | |||
| 431 | $temp->drop('zing:main:global:model:items'); | ||
| 432 | |||
| 433 | $temp->test('zing:main:global:model:items'); | ||
| 434 | |||
| 435 | =back | ||
| 436 | |||
| 437 | =cut | ||
| 438 | |||
| 439 | =head1 AUTHOR | ||
| 440 | |||
| 441 | Al Newkirk, C<awncorp@cpan.org> | ||
| 442 | |||
| 443 | =head1 LICENSE | ||
| 444 | |||
| 445 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 446 | |||
| 447 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 448 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 449 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 450 | |||
| 451 | =head1 PROJECT | ||
| 452 | |||
| 453 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 454 | |||
| 455 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 456 | |||
| 457 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 458 | |||
| 459 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 460 | |||
| 461 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 462 | |||
| 463 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 464 | |||
| 465 | =cut |