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