| 1 | package Zing::Domain; | ||
| 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 | use Zing::Channel; | ||
| 15 | |||
| 16 | our $VERSION = '0.11'; # VERSION | ||
| 17 | |||
| 18 | # ATTRIBUTES | ||
| 19 | |||
| 20 | has 'name' => ( | ||
| 21 | is => 'ro', | ||
| 22 | isa => 'Str', | ||
| 23 | req => 1, | ||
| 24 | ); | ||
| 25 | |||
| 26 | has 'channel' => ( | ||
| 27 | is => 'ro', | ||
| 28 | isa => 'Channel', | ||
| 29 | new => 1, | ||
| 30 | ); | ||
| 31 | |||
| 32 | fun new_channel($self) { | ||
| 33 | Zing::Channel->new(name => $self->name) | ||
| 34 | } | ||
| 35 | |||
| 36 | # BUILDERS | ||
| 37 | |||
| 38 | fun BUILD($self) { | ||
| 39 | return $self->apply if $self->channel->reset; | ||
| 40 | } | ||
| 41 | |||
| 42 | # SHIMS | ||
| 43 | |||
| 44 | sub _copy { | ||
| 45 | my ($data) = @_; | ||
| 46 | |||
| 47 | if (!defined $data) { | ||
| 48 | return undef; | ||
| 49 | } | ||
| 50 | elsif (ref $data eq 'ARRAY') { | ||
| 51 | my $copy = []; | ||
| 52 | for (my $i = 0; $i < @$data; $i++) { | ||
| 53 | $copy->[$i] = _copy($data->[$i]); | ||
| 54 | } | ||
| 55 | return $copy; | ||
| 56 | } | ||
| 57 | elsif (ref $data eq 'HASH') { | ||
| 58 | my $copy = {}; | ||
| 59 | for my $key (keys %$data) { | ||
| 60 | $copy->{$key} = _copy($data->{$key}); | ||
| 61 | } | ||
| 62 | return $copy; | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | return $data; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | # METHODS | ||
| 70 | |||
| 71 | method apply() { | ||
| 72 | undef $self->{data} if $self->channel->renew; | ||
| 73 | |||
| 74 | while (my $data = $self->recv) { | ||
| 75 | my $op = $data->{op}; | ||
| 76 | my $key = $data->{key}; | ||
| 77 | my $val = $data->{val}; | ||
| 78 | |||
| 79 | if ($data->{snapshot} && !$self->{data}) { | ||
| 80 | $self->{data} = _copy($data->{snapshot}); | ||
| 81 | } | ||
| 82 | |||
| 83 | local $@; | ||
| 84 | |||
| 85 | if ($op eq 'decr') { | ||
| 86 | eval{($self->data->{$key} //= 0) -= ($val->[0] // 0)}; | ||
| 87 | } | ||
| 88 | elsif ($op eq 'del') { | ||
| 89 | eval{CORE::delete $self->data->{$key}}; | ||
| 90 | } | ||
| 91 | elsif ($op eq 'incr') { | ||
| 92 | eval{($self->data->{$key} //= 0 ) += ($val->[0] // 0)}; | ||
| 93 | } | ||
| 94 | elsif ($op eq 'pop') { | ||
| 95 | eval{CORE::pop @{$self->data->{$key}}}; | ||
| 96 | } | ||
| 97 | elsif ($op eq 'push') { | ||
| 98 | eval{CORE::push @{$self->data->{$key}}, @$val}; | ||
| 99 | } | ||
| 100 | elsif ($op eq 'set') { | ||
| 101 | eval{$self->data->{$key} = $val->[0]}; | ||
| 102 | } | ||
| 103 | elsif ($op eq 'shift') { | ||
| 104 | eval{CORE::shift @{$self->data->{$key}}}; | ||
| 105 | } | ||
| 106 | elsif ($op eq 'unshift') { | ||
| 107 | eval{CORE::unshift @{$self->data->{$key}}, @$val}; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | return $self; | ||
| 112 | } | ||
| 113 | |||
| 114 | method change(Str $op, Str $key, Any @val) { | ||
| 115 | my %fields = ( | ||
| 116 | key => $key, | ||
| 117 | snapshot => _copy($self->data), | ||
| 118 | time => time, | ||
| 119 | val => [@val], | ||
| 120 | ); | ||
| 121 | |||
| 122 | if ($op eq 'decr') { | ||
| 123 | $self->send({ %fields, op => 'decr' }); | ||
| 124 | } | ||
| 125 | elsif ($op eq 'del') { | ||
| 126 | $self->send({ %fields, op => 'del' }); | ||
| 127 | } | ||
| 128 | elsif ($op eq 'incr') { | ||
| 129 | $self->send({ %fields, op => 'incr' }); | ||
| 130 | } | ||
| 131 | elsif ($op eq 'pop') { | ||
| 132 | $self->send({ %fields, op => 'pop' }); | ||
| 133 | } | ||
| 134 | elsif ($op eq 'push') { | ||
| 135 | $self->send({ %fields, op => 'push' }); | ||
| 136 | } | ||
| 137 | elsif ($op eq 'set') { | ||
| 138 | $self->send({ %fields, op => 'set' }); | ||
| 139 | } | ||
| 140 | elsif ($op eq 'set') { | ||
| 141 | $self->send({ %fields, op => 'set' }); | ||
| 142 | } | ||
| 143 | elsif ($op eq 'shift') { | ||
| 144 | $self->send({ %fields, op => 'shift' }); | ||
| 145 | } | ||
| 146 | elsif ($op eq 'unshift') { | ||
| 147 | $self->send({ %fields, op => 'unshift' }); | ||
| 148 | } | ||
| 149 | |||
| 150 | return $self->apply; | ||
| 151 | } | ||
| 152 | |||
| 153 | method data() { | ||
| 154 | return $self->{data} ||= {}; | ||
| 155 | } | ||
| 156 | |||
| 157 | method get(Str $key) { | ||
| 158 | return $self->apply->data->{$key}; | ||
| 159 | } | ||
| 160 | |||
| 161 | method decr(Str $key, Int $val = 1) { | ||
| 162 | return $self->apply->change('decr', $key, $val); | ||
| 163 | } | ||
| 164 | |||
| 165 | method del(Str $key) { | ||
| 166 | return $self->apply->change('del', $key); | ||
| 167 | } | ||
| 168 | |||
| 169 | method incr(Str $key, Int $val = 1) { | ||
| 170 | return $self->apply->change('incr', $key, $val); | ||
| 171 | } | ||
| 172 | |||
| 173 | method pop(Str $key) { | ||
| 174 | return $self->apply->change('pop', $key); | ||
| 175 | } | ||
| 176 | |||
| 177 | method push(Str $key, Any @val) { | ||
| 178 | return $self->apply->change('push', $key, @val); | ||
| 179 | } | ||
| 180 | |||
| 181 | method recv() { | ||
| 182 | return $self->channel->recv; | ||
| 183 | } | ||
| 184 | |||
| 185 | method send(HashRef $data) { | ||
| 186 | return $self->channel->send($data); | ||
| 187 | } | ||
| 188 | |||
| 189 | method set(Str $key, Any $val) { | ||
| 190 | return $self->apply->change('set', $key, $val); | ||
| 191 | } | ||
| 192 | |||
| 193 | method shift(Str $key) { | ||
| 194 | return $self->apply->change('shift', $key); | ||
| 195 | } | ||
| 196 | |||
| 197 | method unshift(Str $key, Any @val) { | ||
| 198 | return $self->apply->change('unshift', $key, @val); | ||
| 199 | } | ||
| 200 | |||
| 201 | 1; | ||
| 202 | |||
| 203 | =encoding utf8 | ||
| 204 | |||
| 205 | =head1 NAME | ||
| 206 | |||
| 207 | Zing::Domain - Aggregate Root | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | =head1 ABSTRACT | ||
| 212 | |||
| 213 | Aggregate Root Construct | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | =head1 SYNOPSIS | ||
| 218 | |||
| 219 | use Zing::Domain; | ||
| 220 | |||
| 221 | my $domain = Zing::Domain->new(name => 'user-1'); | ||
| 222 | |||
| 223 | # $domain->recv; | ||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | =head1 DESCRIPTION | ||
| 228 | |||
| 229 | This package provides an aggregate abstraction and real-time cross-process | ||
| 230 | sharable data structure which offers many benefits, not least being able to see | ||
| 231 | a full history of state changes. | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | =head1 LIBRARIES | ||
| 236 | |||
| 237 | This package uses type constraints from: | ||
| 238 | |||
| 239 | L<Zing::Types> | ||
| 240 | |||
| 241 | =cut | ||
| 242 | |||
| 243 | =head1 ATTRIBUTES | ||
| 244 | |||
| 245 | This package has the following attributes: | ||
| 246 | |||
| 247 | =cut | ||
| 248 | |||
| 249 | =head2 channel | ||
| 250 | |||
| 251 | channel(Channel) | ||
| 252 | |||
| 253 | This attribute is read-only, accepts C<(Channel)> values, and is optional. | ||
| 254 | |||
| 255 | =cut | ||
| 256 | |||
| 257 | =head2 name | ||
| 258 | |||
| 259 | name(Str) | ||
| 260 | |||
| 261 | This attribute is read-only, accepts C<(Str)> values, and is required. | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | =head1 METHODS | ||
| 266 | |||
| 267 | This package implements the following methods: | ||
| 268 | |||
| 269 | =cut | ||
| 270 | |||
| 271 | =head2 apply | ||
| 272 | |||
| 273 | apply() : Object | ||
| 274 | |||
| 275 | The apply method receives events from the channel and applies the operations. | ||
| 276 | |||
| 277 | =over 4 | ||
| 278 | |||
| 279 | =item apply example #1 | ||
| 280 | |||
| 281 | # given: synopsis | ||
| 282 | |||
| 283 | $domain->apply; | ||
| 284 | |||
| 285 | =back | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | =head2 change | ||
| 290 | |||
| 291 | change(Str $op, Str $key, Any @val) : Object | ||
| 292 | |||
| 293 | The change method commits an operation (and snapshot) to the channel. This | ||
| 294 | method is used internally and shouldn't need to be called directly. | ||
| 295 | |||
| 296 | =over 4 | ||
| 297 | |||
| 298 | =item change example #1 | ||
| 299 | |||
| 300 | # given: synopsis | ||
| 301 | |||
| 302 | $domain->change('incr', 'karma', 1); | ||
| 303 | |||
| 304 | =back | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | =head2 data | ||
| 309 | |||
| 310 | data() : HashRef | ||
| 311 | |||
| 312 | The data method returns the raw aggregate data associated with the object. | ||
| 313 | |||
| 314 | =over 4 | ||
| 315 | |||
| 316 | =item data example #1 | ||
| 317 | |||
| 318 | # given: synopsis | ||
| 319 | |||
| 320 | $domain->data; | ||
| 321 | |||
| 322 | =back | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | =head2 decr | ||
| 327 | |||
| 328 | decr(Str $key, Int $val = 1) : Object | ||
| 329 | |||
| 330 | The decr method decrements the data associated with a specific key. | ||
| 331 | |||
| 332 | =over 4 | ||
| 333 | |||
| 334 | =item decr example #1 | ||
| 335 | |||
| 336 | # given: synopsis | ||
| 337 | |||
| 338 | $domain->decr('karma'); | ||
| 339 | |||
| 340 | =back | ||
| 341 | |||
| 342 | =over 4 | ||
| 343 | |||
| 344 | =item decr example #2 | ||
| 345 | |||
| 346 | # given: synopsis | ||
| 347 | |||
| 348 | $domain->decr('karma', 2); | ||
| 349 | |||
| 350 | =back | ||
| 351 | |||
| 352 | =cut | ||
| 353 | |||
| 354 | =head2 del | ||
| 355 | |||
| 356 | del(Str $key) : Object | ||
| 357 | |||
| 358 | The del method deletes the data associated with a specific key. | ||
| 359 | |||
| 360 | =over 4 | ||
| 361 | |||
| 362 | =item del example #1 | ||
| 363 | |||
| 364 | # given: synopsis | ||
| 365 | |||
| 366 | $domain->del('missing'); | ||
| 367 | |||
| 368 | =back | ||
| 369 | |||
| 370 | =over 4 | ||
| 371 | |||
| 372 | =item del example #2 | ||
| 373 | |||
| 374 | # given: synopsis | ||
| 375 | |||
| 376 | $domain->set('email', 'me@example.com'); | ||
| 377 | |||
| 378 | $domain->del('email'); | ||
| 379 | |||
| 380 | =back | ||
| 381 | |||
| 382 | =cut | ||
| 383 | |||
| 384 | =head2 get | ||
| 385 | |||
| 386 | get(Str $key) : Any | ||
| 387 | |||
| 388 | The get method return the data associated with a specific key. | ||
| 389 | |||
| 390 | =over 4 | ||
| 391 | |||
| 392 | =item get example #1 | ||
| 393 | |||
| 394 | # given: synopsis | ||
| 395 | |||
| 396 | $domain->get('email'); | ||
| 397 | |||
| 398 | =back | ||
| 399 | |||
| 400 | =over 4 | ||
| 401 | |||
| 402 | =item get example #2 | ||
| 403 | |||
| 404 | # given: synopsis | ||
| 405 | |||
| 406 | $domain->set('email', 'me@example.com'); | ||
| 407 | |||
| 408 | $domain->get('email'); | ||
| 409 | |||
| 410 | =back | ||
| 411 | |||
| 412 | =cut | ||
| 413 | |||
| 414 | =head2 incr | ||
| 415 | |||
| 416 | incr(Str $key, Int $val = 1) : Object | ||
| 417 | |||
| 418 | The incr method increments the data associated with a specific key. | ||
| 419 | |||
| 420 | =over 4 | ||
| 421 | |||
| 422 | =item incr example #1 | ||
| 423 | |||
| 424 | # given: synopsis | ||
| 425 | |||
| 426 | $domain->incr('karma'); | ||
| 427 | |||
| 428 | =back | ||
| 429 | |||
| 430 | =over 4 | ||
| 431 | |||
| 432 | =item incr example #2 | ||
| 433 | |||
| 434 | # given: synopsis | ||
| 435 | |||
| 436 | $domain->incr('karma', 5); | ||
| 437 | |||
| 438 | =back | ||
| 439 | |||
| 440 | =cut | ||
| 441 | |||
| 442 | =head2 pop | ||
| 443 | |||
| 444 | pop(Str $key) : Object | ||
| 445 | |||
| 446 | The pop method pops the data off of the stack associated with a specific key. | ||
| 447 | |||
| 448 | =over 4 | ||
| 449 | |||
| 450 | =item pop example #1 | ||
| 451 | |||
| 452 | # given: synopsis | ||
| 453 | |||
| 454 | $domain->pop('history'); | ||
| 455 | |||
| 456 | =back | ||
| 457 | |||
| 458 | =cut | ||
| 459 | |||
| 460 | =head2 push | ||
| 461 | |||
| 462 | push(Str $key, Any @val) : Object | ||
| 463 | |||
| 464 | The push method pushes data onto the stack associated with a specific key. | ||
| 465 | |||
| 466 | =over 4 | ||
| 467 | |||
| 468 | =item push example #1 | ||
| 469 | |||
| 470 | # given: synopsis | ||
| 471 | |||
| 472 | $domain->push('history', { updated => 1234567890 }); | ||
| 473 | |||
| 474 | =back | ||
| 475 | |||
| 476 | =cut | ||
| 477 | |||
| 478 | =head2 set | ||
| 479 | |||
| 480 | set(Str $key, Any $val) : Object | ||
| 481 | |||
| 482 | The set method commits the data associated with a specific key to the channel. | ||
| 483 | |||
| 484 | =over 4 | ||
| 485 | |||
| 486 | =item set example #1 | ||
| 487 | |||
| 488 | # given: synopsis | ||
| 489 | |||
| 490 | $domain->set('updated', 1234567890); | ||
| 491 | |||
| 492 | =back | ||
| 493 | |||
| 494 | =cut | ||
| 495 | |||
| 496 | =head2 shift | ||
| 497 | |||
| 498 | shift(Str $key) : Object | ||
| 499 | |||
| 500 | The shift method shifts data off of the stack associated with a specific key. | ||
| 501 | |||
| 502 | =over 4 | ||
| 503 | |||
| 504 | =item shift example #1 | ||
| 505 | |||
| 506 | # given: synopsis | ||
| 507 | |||
| 508 | $domain->shift('history'); | ||
| 509 | |||
| 510 | =back | ||
| 511 | |||
| 512 | =cut | ||
| 513 | |||
| 514 | =head2 unshift | ||
| 515 | |||
| 516 | unshift(Str $key, Any @val) : Object | ||
| 517 | |||
| 518 | The unshift method unshifts data onto the stack associated with a specific key. | ||
| 519 | |||
| 520 | =over 4 | ||
| 521 | |||
| 522 | =item unshift example #1 | ||
| 523 | |||
| 524 | # given: synopsis | ||
| 525 | |||
| 526 | $domain->unshift('history', { updated => 1234567890 }); | ||
| 527 | |||
| 528 | =back | ||
| 529 | |||
| 530 | =cut | ||
| 531 | |||
| 532 | =head1 AUTHOR | ||
| 533 | |||
| 534 | Al Newkirk, C<awncorp@cpan.org> | ||
| 535 | |||
| 536 | =head1 LICENSE | ||
| 537 | |||
| 538 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 539 | |||
| 540 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 541 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 542 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 543 | |||
| 544 | =head1 PROJECT | ||
| 545 | |||
| 546 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 547 | |||
| 548 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 549 | |||
| 550 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 551 | |||
| 552 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 553 | |||
| 554 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 555 | |||
| 556 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 557 | |||
| 558 | =cut |