| 1 | package Zing::Store::Pg; | ||
| 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.03'; # VERSION | ||
| 17 | |||
| 18 | # ATTRIBUTES | ||
| 19 | |||
| 20 | has client => ( | ||
| 21 | is => 'ro', | ||
| 22 | isa => 'InstanceOf["DBI::db"]', | ||
| 23 | new => 1, | ||
| 24 | ); | ||
| 25 | |||
| 26 | fun new_client($self) { | ||
| 27 | my $dbname = $ENV{ZING_DBNAME} || 'zing'; | ||
| 28 | my $dbhost = $ENV{ZING_DBHOST} || 'localhost'; | ||
| 29 | my $dbport = $ENV{ZING_DBPORT} || '5432'; | ||
| 30 | my $dbuser = $ENV{ZING_DBUSER} || 'postgres'; | ||
| 31 | my $dbpass = $ENV{ZING_DBPASS}; | ||
| 32 | require DBI; DBI->connect( | ||
| 33 | join(';', | ||
| 34 | "dbi:Pg:dbname=$dbname", | ||
| 35 | $dbhost ? join('=', 'host', $dbhost) : (), | ||
| 36 | $dbport ? join('=', 'port', $dbport) : (), | ||
| 37 | ), | ||
| 38 | $dbuser, $dbpass, | ||
| 39 | { | ||
| 40 | AutoCommit => 1, | ||
| 41 | PrintError => 0, | ||
| 42 | RaiseError => 1 | ||
| 43 | } | ||
| 44 | ); | ||
| 45 | } | ||
| 46 | |||
| 47 | has meta => ( | ||
| 48 | is => 'ro', | ||
| 49 | isa => 'Str', | ||
| 50 | new => 1, | ||
| 51 | ); | ||
| 52 | |||
| 53 | fun new_meta($self) { | ||
| 54 | require Zing::ID; Zing::ID->new->string | ||
| 55 | } | ||
| 56 | |||
| 57 | has table => ( | ||
| 58 | is => 'ro', | ||
| 59 | isa => 'Str', | ||
| 60 | new => 1, | ||
| 61 | ); | ||
| 62 | |||
| 63 | fun new_table($self) { | ||
| 64 | $ENV{ZING_DBZONE} || 'entities' | ||
| 65 | } | ||
| 66 | |||
| 67 | # BUILDERS | ||
| 68 | |||
| 69 | fun new_encoder($self) { | ||
| 70 | require Zing::Encoder::Dump; Zing::Encoder::Dump->new; | ||
| 71 | } | ||
| 72 | |||
| 73 | fun BUILD($self) { | ||
| 74 | my $client = $self->client; | ||
| 75 | my $table = $self->table; | ||
| 76 | local $@; eval { | ||
| 77 | $client->do(qq{ | ||
| 78 | create table if not exists "$table" ( | ||
| 79 | "id" serial primary key, | ||
| 80 | "key" varchar not null, | ||
| 81 | "value" text not null, | ||
| 82 | "index" integer default 0, | ||
| 83 | "meta" varchar null | ||
| 84 | ) | ||
| 85 | }); | ||
| 86 | } | ||
| 87 | unless (defined(do{ | ||
| 88 | local $@; | ||
| 89 | local $client->{RaiseError} = 0; | ||
| 90 | local $client->{PrintError} = 0; | ||
| 91 | eval { | ||
| 92 | $client->do(qq{ | ||
| 93 | select 1 from "$table" where 1 = 1 | ||
| 94 | }) | ||
| 95 | } | ||
| 96 | })); | ||
| 97 | return $self; | ||
| 98 | } | ||
| 99 | |||
| 100 | fun DESTROY($self) { | ||
| 101 | $self->client->disconnect; | ||
| 102 | return $self; | ||
| 103 | } | ||
| 104 | |||
| 105 | # METHODS | ||
| 106 | |||
| 107 | my $retries = 10; | ||
| 108 | |||
| 109 | method drop(Str $key) { | ||
| 110 | my $table = $self->table; | ||
| 111 | my $client = $self->client; | ||
| 112 | my $sth = $client->prepare( | ||
| 113 | qq{delete from "$table" where "key" = ?} | ||
| 114 | ); | ||
| 115 | $sth->execute($key); | ||
| 116 | return $sth->rows > 0 ? 1 : 0; | ||
| 117 | } | ||
| 118 | |||
| 119 | method keys(Str $query) { | ||
| 120 | $query =~ s/\*/%/g; | ||
| 121 | my $table = $self->table; | ||
| 122 | my $client = $self->client; | ||
| 123 | my $data = $client->selectall_arrayref( | ||
| 124 | qq{select distinct("key") from "$table" where "key" like ?}, | ||
| 125 | {}, | ||
| 126 | $query, | ||
| 127 | ); | ||
| 128 | return [map $$_[0], @$data]; | ||
| 129 | } | ||
| 130 | |||
| 131 | method lpull(Str $key) { | ||
| 132 | my $table = $self->table; | ||
| 133 | my $client = $self->client; | ||
| 134 | for my $attempt (1..$retries) { | ||
| 135 | local $@; eval { | ||
| 136 | my $sth = $client->prepare( | ||
| 137 | qq{ | ||
| 138 | update "$table" set "meta" = ? where "id" = ( | ||
| 139 | select "me"."id" from "$table" "me" | ||
| 140 | where "me"."key" = ? and "me"."meta" is null | ||
| 141 | order by "me"."index" asc limit 1 | ||
| 142 | ) | ||
| 143 | } | ||
| 144 | ); | ||
| 145 | $sth->execute($self->meta, $key); | ||
| 146 | }; | ||
| 147 | if ($@) { | ||
| 148 | die $@ if $attempt == $retries; | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | last; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | my $data = $client->selectrow_arrayref( | ||
| 155 | qq{ | ||
| 156 | select "id", "value" | ||
| 157 | from "$table" where "meta" = ? and "key" = ? order by "index" asc limit 1 | ||
| 158 | }, | ||
| 159 | {}, | ||
| 160 | $self->meta, $key, | ||
| 161 | ); | ||
| 162 | if ($data) { | ||
| 163 | my $sth = $client->prepare( | ||
| 164 | qq{delete from "$table" where "id" = ?} | ||
| 165 | ); | ||
| 166 | $sth->execute($data->[0]); | ||
| 167 | } | ||
| 168 | return $data ? $self->decode($data->[1]) : undef; | ||
| 169 | } | ||
| 170 | |||
| 171 | method lpush(Str $key, HashRef $val) { | ||
| 172 | my $table = $self->table; | ||
| 173 | my $client = $self->client; | ||
| 174 | my $sth = $client->prepare( | ||
| 175 | qq{ | ||
| 176 | insert into "$table" ("key", "value", "index") values (?, ?, ( | ||
| 177 | select coalesce(min("me"."index"), 0) - 1 | ||
| 178 | from "$table" "me" where "me"."key" = ? | ||
| 179 | )) | ||
| 180 | } | ||
| 181 | ); | ||
| 182 | for my $attempt (1..$retries) { | ||
| 183 | local $@; eval { | ||
| 184 | $sth->execute($key, $self->encode($val), $key); | ||
| 185 | }; | ||
| 186 | if ($@) { | ||
| 187 | die $@ if $attempt == $retries; | ||
| 188 | } | ||
| 189 | else { | ||
| 190 | last; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | return $sth->rows; | ||
| 194 | } | ||
| 195 | |||
| 196 | method read(Str $key) { | ||
| 197 | my $table = $self->table; | ||
| 198 | my $client = $self->client; | ||
| 199 | my $data = $client->selectrow_arrayref( | ||
| 200 | qq{ | ||
| 201 | select "value" from "$table" | ||
| 202 | where "key" = ? order by "id" desc limit 1 | ||
| 203 | }, | ||
| 204 | {}, | ||
| 205 | $key, | ||
| 206 | ); | ||
| 207 | return $data ? $data->[0] : undef; | ||
| 208 | } | ||
| 209 | |||
| 210 | method recv(Str $key) { | ||
| 211 | my $data = $self->read($key); | ||
| 212 | return $data ? $self->decode($data) : $data; | ||
| 213 | } | ||
| 214 | |||
| 215 | method rpull(Str $key) { | ||
| 216 | my $table = $self->table; | ||
| 217 | my $client = $self->client; | ||
| 218 | for my $attempt (1..$retries) { | ||
| 219 | local $@; eval { | ||
| 220 | my $sth = $client->prepare( | ||
| 221 | qq{ | ||
| 222 | update "$table" set "meta" = ? where "id" = ( | ||
| 223 | select "me"."id" from "$table" "me" | ||
| 224 | where "me"."key" = ? and "me"."meta" is null | ||
| 225 | order by "me"."index" desc limit 1 | ||
| 226 | ) | ||
| 227 | } | ||
| 228 | ); | ||
| 229 | $sth->execute($self->meta, $key); | ||
| 230 | }; | ||
| 231 | if ($@) { | ||
| 232 | die $@ if $attempt == $retries; | ||
| 233 | } | ||
| 234 | else { | ||
| 235 | last; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | my $data = $client->selectrow_arrayref( | ||
| 239 | qq{ | ||
| 240 | select "id", "value" | ||
| 241 | from "$table" where "meta" = ? and "key" = ? order by "index" desc limit 1 | ||
| 242 | }, | ||
| 243 | {}, | ||
| 244 | $self->meta, $key, | ||
| 245 | ); | ||
| 246 | if ($data) { | ||
| 247 | my $sth = $client->prepare( | ||
| 248 | qq{delete from "$table" where "id" = ?} | ||
| 249 | ); | ||
| 250 | $sth->execute($data->[0]); | ||
| 251 | } | ||
| 252 | return $data ? $self->decode($data->[1]) : undef; | ||
| 253 | } | ||
| 254 | |||
| 255 | method rpush(Str $key, HashRef $val) { | ||
| 256 | my $table = $self->table; | ||
| 257 | my $client = $self->client; | ||
| 258 | my $sth = $client->prepare( | ||
| 259 | qq{ | ||
| 260 | insert into "$table" ("key", "value", "index") values (?, ?, ( | ||
| 261 | select coalesce(max("me"."index"), 0) + 1 | ||
| 262 | from "$table" "me" where "me"."key" = ? | ||
| 263 | )) | ||
| 264 | } | ||
| 265 | ); | ||
| 266 | for my $attempt (1..$retries) { | ||
| 267 | local $@; eval { | ||
| 268 | $sth->execute($key, $self->encode($val), $key); | ||
| 269 | }; | ||
| 270 | if ($@) { | ||
| 271 | die $@ if $attempt == $retries; | ||
| 272 | } | ||
| 273 | else { | ||
| 274 | last; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | return $sth->rows; | ||
| 278 | } | ||
| 279 | |||
| 280 | method send(Str $key, HashRef $val) { | ||
| 281 | my $set = $self->encode($val); | ||
| 282 | $self->write($key, $set); | ||
| 283 | return 'OK'; | ||
| 284 | } | ||
| 285 | |||
| 286 | method size(Str $key) { | ||
| 287 | my $table = $self->table; | ||
| 288 | my $client = $self->client; | ||
| 289 | my $data = $client->selectrow_arrayref( | ||
| 290 | qq{select count("key") from "$table" where "key" = ?}, | ||
| 291 | {}, | ||
| 292 | $key, | ||
| 293 | ); | ||
| 294 | return $data->[0]; | ||
| 295 | } | ||
| 296 | |||
| 297 | method slot(Str $key, Int $pos) { | ||
| 298 | my $table = $self->table; | ||
| 299 | my $client = $self->client; | ||
| 300 | my $data = $client->selectrow_arrayref( | ||
| 301 | qq{ | ||
| 302 | select "value" from "$table" | ||
| 303 | where "key" = ? order by "index" asc offset ? limit 1 | ||
| 304 | }, | ||
| 305 | {}, | ||
| 306 | $key, $pos | ||
| 307 | ); | ||
| 308 | return $data ? $self->decode($data->[0]) : undef; | ||
| 309 | } | ||
| 310 | |||
| 311 | method test(Str $key) { | ||
| 312 | my $table = $self->table; | ||
| 313 | my $client = $self->client; | ||
| 314 | my $data = $client->selectrow_arrayref( | ||
| 315 | qq{select count("id") from "$table" where "key" = ?}, | ||
| 316 | {}, | ||
| 317 | $key, | ||
| 318 | ); | ||
| 319 | return $data->[0] ? 1 : 0; | ||
| 320 | } | ||
| 321 | |||
| 322 | method write(Str $key, Str $data) { | ||
| 323 | my $table = $self->table; | ||
| 324 | my $client = $self->client; | ||
| 325 | $client->prepare( | ||
| 326 | qq{delete from "$table" where "key" = ?} | ||
| 327 | )->execute($key); | ||
| 328 | $client->prepare( | ||
| 329 | qq{insert into "$table" ("key", "value") values (?, ?)} | ||
| 330 | )->execute($key, $data); | ||
| 331 | return $self; | ||
| 332 | } | ||
| 333 | |||
| 334 | 1; | ||
| 335 | |||
| 336 | =encoding utf8 | ||
| 337 | |||
| 338 | =head1 NAME | ||
| 339 | |||
| 340 | Zing::Store::Pg - Postgres Storage | ||
| 341 | |||
| 342 | =cut | ||
| 343 | |||
| 344 | =head1 ABSTRACT | ||
| 345 | |||
| 346 | Postgres Storage Abstraction | ||
| 347 | |||
| 348 | =cut | ||
| 349 | |||
| 350 | =head1 SYNOPSIS | ||
| 351 | |||
| 352 | use Test::DB::Postgres; | ||
| 353 | use Zing::Encoder::Dump; | ||
| 354 | use Zing::Store::Pg; | ||
| 355 | |||
| 356 | my $testdb = Test::DB::Postgres->new; | ||
| 357 | my $store = Zing::Store::Pg->new( | ||
| 358 | client => $testdb->create->dbh, | ||
| 359 | encoder => Zing::Encoder::Dump->new | ||
| 360 | ); | ||
| 361 | |||
| 362 | # $store->drop; | ||
| 363 | |||
| 364 | =cut | ||
| 365 | |||
| 366 | =head1 DESCRIPTION | ||
| 367 | |||
| 368 | This package provides a Postgres-specific storage adapter for use with data | ||
| 369 | persistence abstractions. The L</client> attribute accepts a L<DBI> object | ||
| 370 | configured to connect to a L<DBD::Pg> backend. The C<ZING_DBNAME> environment | ||
| 371 | variable can be used to specify the database name (defaults to "zing"). The | ||
| 372 | C<ZING_DBHOST> environment variable can be used to specify the database host | ||
| 373 | (defaults to "localhost"). The C<ZING_DBPORT> environment variable can be used | ||
| 374 | to specify the database port (defaults to "5432"). The C<ZING_DBUSER> | ||
| 375 | environment variable can be used to specify the database username (defaults to | ||
| 376 | "postgres"). The C<ZING_DBPASS> environment variable can be used to specify the | ||
| 377 | database password. The C<ZING_DBZONE> environment variable can be used to | ||
| 378 | specify the database table name (defaults to "entities"). | ||
| 379 | |||
| 380 | =cut | ||
| 381 | |||
| 382 | =head1 INHERITS | ||
| 383 | |||
| 384 | This package inherits behaviors from: | ||
| 385 | |||
| 386 | L<Zing::Store> | ||
| 387 | |||
| 388 | =cut | ||
| 389 | |||
| 390 | =head1 LIBRARIES | ||
| 391 | |||
| 392 | This package uses type constraints from: | ||
| 393 | |||
| 394 | L<Zing::Types> | ||
| 395 | |||
| 396 | =cut | ||
| 397 | |||
| 398 | =head1 ATTRIBUTES | ||
| 399 | |||
| 400 | This package has the following attributes: | ||
| 401 | |||
| 402 | =cut | ||
| 403 | |||
| 404 | =head2 client | ||
| 405 | |||
| 406 | client(InstanceOf["DBI::db"]) | ||
| 407 | |||
| 408 | This attribute is read-only, accepts C<(InstanceOf["DBI::db"])> values, and is optional. | ||
| 409 | |||
| 410 | =cut | ||
| 411 | |||
| 412 | =head1 METHODS | ||
| 413 | |||
| 414 | This package implements the following methods: | ||
| 415 | |||
| 416 | =cut | ||
| 417 | |||
| 418 | =head2 decode | ||
| 419 | |||
| 420 | decode(Str $data) : HashRef | ||
| 421 | |||
| 422 | The decode method decodes the JSON data provided and returns the data as a hashref. | ||
| 423 | |||
| 424 | =over 4 | ||
| 425 | |||
| 426 | =item decode example #1 | ||
| 427 | |||
| 428 | # given: synopsis | ||
| 429 | |||
| 430 | $store->decode('{"status"=>"ok"}'); | ||
| 431 | |||
| 432 | =back | ||
| 433 | |||
| 434 | =cut | ||
| 435 | |||
| 436 | =head2 drop | ||
| 437 | |||
| 438 | drop(Str $key) : Int | ||
| 439 | |||
| 440 | The drop method removes (drops) the item from the datastore. | ||
| 441 | |||
| 442 | =over 4 | ||
| 443 | |||
| 444 | =item drop example #1 | ||
| 445 | |||
| 446 | # given: synopsis | ||
| 447 | |||
| 448 | $store->drop('zing:main:global:model:temp'); | ||
| 449 | |||
| 450 | =back | ||
| 451 | |||
| 452 | =cut | ||
| 453 | |||
| 454 | =head2 encode | ||
| 455 | |||
| 456 | encode(HashRef $data) : Str | ||
| 457 | |||
| 458 | The encode method encodes and returns the data provided as JSON. | ||
| 459 | |||
| 460 | =over 4 | ||
| 461 | |||
| 462 | =item encode example #1 | ||
| 463 | |||
| 464 | # given: synopsis | ||
| 465 | |||
| 466 | $store->encode({ status => 'ok' }); | ||
| 467 | |||
| 468 | =back | ||
| 469 | |||
| 470 | =cut | ||
| 471 | |||
| 472 | =head2 keys | ||
| 473 | |||
| 474 | keys(Str @keys) : ArrayRef[Str] | ||
| 475 | |||
| 476 | The keys method returns a list of keys under the namespace of the datastore or | ||
| 477 | provided key. | ||
| 478 | |||
| 479 | =over 4 | ||
| 480 | |||
| 481 | =item keys example #1 | ||
| 482 | |||
| 483 | # given: synopsis | ||
| 484 | |||
| 485 | my $keys = $store->keys('zing:main:global:model:temp'); | ||
| 486 | |||
| 487 | =back | ||
| 488 | |||
| 489 | =over 4 | ||
| 490 | |||
| 491 | =item keys example #2 | ||
| 492 | |||
| 493 | # given: synopsis | ||
| 494 | |||
| 495 | $store->send('zing:main:global:model:temp', { status => 'ok' }); | ||
| 496 | |||
| 497 | my $keys = $store->keys('zing:main:global:model:temp'); | ||
| 498 | |||
| 499 | =back | ||
| 500 | |||
| 501 | =cut | ||
| 502 | |||
| 503 | =head2 lpull | ||
| 504 | |||
| 505 | lpull(Str $key) : Maybe[HashRef] | ||
| 506 | |||
| 507 | The lpull method pops data off of the top of a list in the datastore. | ||
| 508 | |||
| 509 | =over 4 | ||
| 510 | |||
| 511 | =item lpull example #1 | ||
| 512 | |||
| 513 | # given: synopsis | ||
| 514 | |||
| 515 | $store->lpull('zing:main:global:model:items'); | ||
| 516 | |||
| 517 | =back | ||
| 518 | |||
| 519 | =over 4 | ||
| 520 | |||
| 521 | =item lpull example #2 | ||
| 522 | |||
| 523 | # given: synopsis | ||
| 524 | |||
| 525 | $store->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 526 | |||
| 527 | $store->lpull('zing:main:global:model:items'); | ||
| 528 | |||
| 529 | =back | ||
| 530 | |||
| 531 | =cut | ||
| 532 | |||
| 533 | =head2 lpush | ||
| 534 | |||
| 535 | lpush(Str $key, HashRef $val) : Int | ||
| 536 | |||
| 537 | The lpush method pushed data onto the top of a list in the datastore. | ||
| 538 | |||
| 539 | =over 4 | ||
| 540 | |||
| 541 | =item lpush example #1 | ||
| 542 | |||
| 543 | # given: synopsis | ||
| 544 | |||
| 545 | $store->lpush('zing:main:global:model:items', { status => '1' }); | ||
| 546 | |||
| 547 | =back | ||
| 548 | |||
| 549 | =over 4 | ||
| 550 | |||
| 551 | =item lpush example #2 | ||
| 552 | |||
| 553 | # given: synopsis | ||
| 554 | |||
| 555 | $store->lpush('zing:main:global:model:items', { status => '0' }); | ||
| 556 | |||
| 557 | $store->lpush('zing:main:global:model:items', { status => '0' }); | ||
| 558 | |||
| 559 | =back | ||
| 560 | |||
| 561 | =cut | ||
| 562 | |||
| 563 | =head2 recv | ||
| 564 | |||
| 565 | recv(Str $key) : Maybe[HashRef] | ||
| 566 | |||
| 567 | The recv method fetches and returns data from the datastore by its key. | ||
| 568 | |||
| 569 | =over 4 | ||
| 570 | |||
| 571 | =item recv example #1 | ||
| 572 | |||
| 573 | # given: synopsis | ||
| 574 | |||
| 575 | $store->recv('zing:main:global:model:temp'); | ||
| 576 | |||
| 577 | =back | ||
| 578 | |||
| 579 | =over 4 | ||
| 580 | |||
| 581 | =item recv example #2 | ||
| 582 | |||
| 583 | # given: synopsis | ||
| 584 | |||
| 585 | $store->send('zing:main:global:model:temp', { status => 'ok' }); | ||
| 586 | |||
| 587 | $store->recv('zing:main:global:model:temp'); | ||
| 588 | |||
| 589 | =back | ||
| 590 | |||
| 591 | =cut | ||
| 592 | |||
| 593 | =head2 rpull | ||
| 594 | |||
| 595 | rpull(Str $key) : Maybe[HashRef] | ||
| 596 | |||
| 597 | The rpull method pops data off of the bottom of a list in the datastore. | ||
| 598 | |||
| 599 | =over 4 | ||
| 600 | |||
| 601 | =item rpull example #1 | ||
| 602 | |||
| 603 | # given: synopsis | ||
| 604 | |||
| 605 | $store->rpull('zing:main:global:model:items'); | ||
| 606 | |||
| 607 | =back | ||
| 608 | |||
| 609 | =over 4 | ||
| 610 | |||
| 611 | =item rpull example #2 | ||
| 612 | |||
| 613 | # given: synopsis | ||
| 614 | |||
| 615 | $store->rpush('zing:main:global:model:items', { status => 1 }); | ||
| 616 | $store->rpush('zing:main:global:model:items', { status => 2 }); | ||
| 617 | |||
| 618 | $store->rpull('zing:main:global:model:items'); | ||
| 619 | |||
| 620 | =back | ||
| 621 | |||
| 622 | =cut | ||
| 623 | |||
| 624 | =head2 rpush | ||
| 625 | |||
| 626 | rpush(Str $key, HashRef $val) : Int | ||
| 627 | |||
| 628 | The rpush method pushed data onto the bottom of a list in the datastore. | ||
| 629 | |||
| 630 | =over 4 | ||
| 631 | |||
| 632 | =item rpush example #1 | ||
| 633 | |||
| 634 | # given: synopsis | ||
| 635 | |||
| 636 | $store->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 637 | |||
| 638 | =back | ||
| 639 | |||
| 640 | =over 4 | ||
| 641 | |||
| 642 | =item rpush example #2 | ||
| 643 | |||
| 644 | # given: synopsis | ||
| 645 | |||
| 646 | $store->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 647 | |||
| 648 | $store->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 649 | |||
| 650 | =back | ||
| 651 | |||
| 652 | =cut | ||
| 653 | |||
| 654 | =head2 send | ||
| 655 | |||
| 656 | send(Str $key, HashRef $val) : Str | ||
| 657 | |||
| 658 | The send method commits data to the datastore with its key and returns truthy. | ||
| 659 | |||
| 660 | =over 4 | ||
| 661 | |||
| 662 | =item send example #1 | ||
| 663 | |||
| 664 | # given: synopsis | ||
| 665 | |||
| 666 | $store->send('zing:main:global:model:temp', { status => 'ok' }); | ||
| 667 | |||
| 668 | =back | ||
| 669 | |||
| 670 | =cut | ||
| 671 | |||
| 672 | =head2 size | ||
| 673 | |||
| 674 | size(Str $key) : Int | ||
| 675 | |||
| 676 | The size method returns the size of a list in the datastore. | ||
| 677 | |||
| 678 | =over 4 | ||
| 679 | |||
| 680 | =item size example #1 | ||
| 681 | |||
| 682 | # given: synopsis | ||
| 683 | |||
| 684 | my $size = $store->size('zing:main:global:model:items'); | ||
| 685 | |||
| 686 | =back | ||
| 687 | |||
| 688 | =over 4 | ||
| 689 | |||
| 690 | =item size example #2 | ||
| 691 | |||
| 692 | # given: synopsis | ||
| 693 | |||
| 694 | $store->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 695 | |||
| 696 | my $size = $store->size('zing:main:global:model:items'); | ||
| 697 | |||
| 698 | =back | ||
| 699 | |||
| 700 | =cut | ||
| 701 | |||
| 702 | =head2 slot | ||
| 703 | |||
| 704 | slot(Str $key, Int $pos) : Maybe[HashRef] | ||
| 705 | |||
| 706 | The slot method returns the data from a list in the datastore by its index. | ||
| 707 | |||
| 708 | =over 4 | ||
| 709 | |||
| 710 | =item slot example #1 | ||
| 711 | |||
| 712 | # given: synopsis | ||
| 713 | |||
| 714 | my $model = $store->slot('zing:main:global:model:items', 0); | ||
| 715 | |||
| 716 | =back | ||
| 717 | |||
| 718 | =over 4 | ||
| 719 | |||
| 720 | =item slot example #2 | ||
| 721 | |||
| 722 | # given: synopsis | ||
| 723 | |||
| 724 | $store->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 725 | |||
| 726 | my $model = $store->slot('zing:main:global:model:items', 0); | ||
| 727 | |||
| 728 | =back | ||
| 729 | |||
| 730 | =cut | ||
| 731 | |||
| 732 | =head2 test | ||
| 733 | |||
| 734 | test(Str $key) : Int | ||
| 735 | |||
| 736 | The test method returns truthy if the specific key (or datastore) exists. | ||
| 737 | |||
| 738 | =over 4 | ||
| 739 | |||
| 740 | =item test example #1 | ||
| 741 | |||
| 742 | # given: synopsis | ||
| 743 | |||
| 744 | $store->rpush('zing:main:global:model:items', { status => 'ok' }); | ||
| 745 | |||
| 746 | $store->test('zing:main:global:model:items'); | ||
| 747 | |||
| 748 | =back | ||
| 749 | |||
| 750 | =over 4 | ||
| 751 | |||
| 752 | =item test example #2 | ||
| 753 | |||
| 754 | # given: synopsis | ||
| 755 | |||
| 756 | $store->drop('zing:main:global:model:items'); | ||
| 757 | |||
| 758 | $store->test('zing:main:global:model:items'); | ||
| 759 | |||
| 760 | =back | ||
| 761 | |||
| 762 | =cut | ||
| 763 | |||
| 764 | =head1 AUTHOR | ||
| 765 | |||
| 766 | Al Newkirk, C<awncorp@cpan.org> | ||
| 767 | |||
| 768 | =head1 LICENSE | ||
| 769 | |||
| 770 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 771 | |||
| 772 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 773 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 774 | file"|https://github.com/iamalnewkirk/zing-store-pg/blob/master/LICENSE>. | ||
| 775 | |||
| 776 | =head1 PROJECT | ||
| 777 | |||
| 778 | L<Wiki|https://github.com/iamalnewkirk/zing-store-pg/wiki> | ||
| 779 | |||
| 780 | L<Project|https://github.com/iamalnewkirk/zing-store-pg> | ||
| 781 | |||
| 782 | L<Initiatives|https://github.com/iamalnewkirk/zing-store-pg/projects> | ||
| 783 | |||
| 784 | L<Milestones|https://github.com/iamalnewkirk/zing-store-pg/milestones> | ||
| 785 | |||
| 786 | L<Contributing|https://github.com/iamalnewkirk/zing-store-pg/blob/master/CONTRIBUTE.md> | ||
| 787 | |||
| 788 | L<Issues|https://github.com/iamalnewkirk/zing-store-pg/issues> | ||
| 789 | |||
| 790 | =cut |