| 1 | package Myriad::Storage::Implementation::Memory; | ||
| 2 | |||
| 3 | 2 | ▶ | use Myriad::Class extends => 'IO::Async::Notifier', does => [ 'Myriad::Role::Storage', 'Myriad::Util::Defer']; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '1.001'; # VERSION | ||
| 6 | our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY | ||
| 7 | |||
| 8 | =encoding utf8 | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Myriad::Storage::Implementation::Memory - microservice storage abstraction | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | This is intended for use in tests and standalone local services. | ||
| 19 | There is no persistence, and no shared data across multiple | ||
| 20 | processes, but the full L<Myriad::Storage> API should be exposed | ||
| 21 | correctly. | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | # Common datastore | ||
| 26 | has %data; | ||
| 27 | |||
| 28 | # FIXME Need to update :Defer for Object::Pad | ||
| 29 | sub MODIFY_CODE_ATTRIBUTES { } | ||
| 30 | |||
| 31 | =head2 get | ||
| 32 | |||
| 33 | Takes the following parameters: | ||
| 34 | |||
| 35 | =over 4 | ||
| 36 | |||
| 37 | =item * C<< $k >> - the relative key in storage | ||
| 38 | |||
| 39 | =back | ||
| 40 | |||
| 41 | Returns a L<Future> which will resolve to the corresponding value, or C<undef> if none. | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | async method get : Defer ($k) { | ||
| 46 | return $data{$k}; | ||
| 47 | } | ||
| 48 | |||
| 49 | =head2 set | ||
| 50 | |||
| 51 | Takes the following parameters: | ||
| 52 | |||
| 53 | =over 4 | ||
| 54 | |||
| 55 | =item * C<< $k >> - the relative key in storage | ||
| 56 | |||
| 57 | =item * C<< $v >> - the scalar value to set | ||
| 58 | |||
| 59 | =back | ||
| 60 | |||
| 61 | Note that references are currently B<not> supported - attempts to write an arrayref, hashref | ||
| 62 | or object will fail. | ||
| 63 | |||
| 64 | Returns a L<Future> which will resolve on completion. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | async method set : Defer ($k, $v) { | ||
| 69 | die 'value cannot be a reference for ' . $k . ' - ' . ref($v) if ref $v; | ||
| 70 | return $data{$k} = $v; | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 getset | ||
| 74 | |||
| 75 | Takes the following parameters: | ||
| 76 | |||
| 77 | =over 4 | ||
| 78 | |||
| 79 | =item * C<< $k >> - the relative key in storage | ||
| 80 | |||
| 81 | =item * C<< $v >> - the scalar value to set | ||
| 82 | |||
| 83 | =back | ||
| 84 | |||
| 85 | Note that references are currently B<not> supported - attempts to write an arrayref, hashref | ||
| 86 | or object will fail. | ||
| 87 | |||
| 88 | Returns a L<Future> which will resolve on completion. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | async method getset : Defer ($k, $v) { | ||
| 93 | die 'value cannot be a reference for ' . $k . ' - ' . ref($v) if ref $v; | ||
| 94 | my $original = delete $data{$k}; | ||
| 95 | $data{$k} = $v; | ||
| 96 | return $original; | ||
| 97 | } | ||
| 98 | |||
| 99 | =head2 incr | ||
| 100 | |||
| 101 | Takes the following parameters: | ||
| 102 | |||
| 103 | =over 4 | ||
| 104 | |||
| 105 | =item * C<< $k >> - the relative key in storage | ||
| 106 | |||
| 107 | =back | ||
| 108 | |||
| 109 | Returns a L<Future> which will resolve to the corresponding incremented value, or C<undef> if none. | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | async method incr : Defer ($k) { | ||
| 114 | return ++$data{$k}; | ||
| 115 | } | ||
| 116 | |||
| 117 | =head2 observe | ||
| 118 | |||
| 119 | Observe a specific key. | ||
| 120 | |||
| 121 | Returns a L<Ryu::Source> which will emit the current and all subsequent values. | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | method observe ($k) { | ||
| 126 | die 'no observation'; | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | =head2 watch_keyspace | ||
| 131 | |||
| 132 | Returns update about keyspace | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | async method watch_keyspace { | ||
| 137 | die 'no watch_keyspace'; | ||
| 138 | } | ||
| 139 | |||
| 140 | =head2 push | ||
| 141 | |||
| 142 | Takes the following parameters: | ||
| 143 | |||
| 144 | =over 4 | ||
| 145 | |||
| 146 | =item * C<< $k >> - the relative key in storage | ||
| 147 | |||
| 148 | =item * C<< $v >> - the scalar value to set | ||
| 149 | |||
| 150 | =back | ||
| 151 | |||
| 152 | Returns a L<Future> which will resolve to . | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | async method push : Defer ($k, @v) { | ||
| 157 | die 'value cannot be a reference for ' . $k . ' - ' . ref($_) for grep { ref } @v; | ||
| 158 | push $data{$k}->@*, @v; | ||
| 159 | return 0+$data{$k}->@*; | ||
| 160 | } | ||
| 161 | |||
| 162 | =head2 unshift | ||
| 163 | |||
| 164 | Takes the following parameters: | ||
| 165 | |||
| 166 | =over 4 | ||
| 167 | |||
| 168 | =item * | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | Returns a L<Future> which will resolve to . | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | async method unshift : Defer ($k, @v) { | ||
| 177 | die 'value cannot be a reference for ' . $k . ' - ' . ref($_) for grep { ref } @v; | ||
| 178 | unshift $data{$k}->@*, @v; | ||
| 179 | return 0+$data{$k}->@*; | ||
| 180 | } | ||
| 181 | |||
| 182 | =head2 pop | ||
| 183 | |||
| 184 | Takes the following parameters: | ||
| 185 | |||
| 186 | =over 4 | ||
| 187 | |||
| 188 | =item * | ||
| 189 | |||
| 190 | =back | ||
| 191 | |||
| 192 | Returns a L<Future> which will resolve to . | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | async method pop : Defer ($k) { | ||
| 197 | return pop $data{$k}->@*; | ||
| 198 | } | ||
| 199 | |||
| 200 | =head2 shift | ||
| 201 | |||
| 202 | Takes the following parameters: | ||
| 203 | |||
| 204 | =over 4 | ||
| 205 | |||
| 206 | =item * | ||
| 207 | |||
| 208 | =back | ||
| 209 | |||
| 210 | Returns a L<Future> which will resolve to . | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | async method shift : Defer ($k) { | ||
| 215 | return shift $data{$k}->@*; | ||
| 216 | } | ||
| 217 | |||
| 218 | =head2 hash_set | ||
| 219 | |||
| 220 | Takes the following parameters: | ||
| 221 | |||
| 222 | =over 4 | ||
| 223 | |||
| 224 | =item * | ||
| 225 | |||
| 226 | =back | ||
| 227 | |||
| 228 | Returns a L<Future> which will resolve to . | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | async method hash_set : Defer ($k, %args) { | ||
| 233 | for my $hash_key (sort keys %args) { | ||
| 234 | my $v = $args{$hash_key}; | ||
| 235 | die 'value cannot be a reference for ' . $k . ' hash key ' . $hash_key . ' - ' . ref($v) if ref $v; | ||
| 236 | } | ||
| 237 | @{$data{$k}}{keys %args} = values %args; | ||
| 238 | return 0 + keys %args; | ||
| 239 | } | ||
| 240 | |||
| 241 | =head2 hash_get | ||
| 242 | |||
| 243 | Takes the following parameters: | ||
| 244 | |||
| 245 | =over 4 | ||
| 246 | |||
| 247 | =item * | ||
| 248 | |||
| 249 | =back | ||
| 250 | |||
| 251 | Returns a L<Future> which will resolve to the scalar value for this key. | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | async method hash_get : Defer ($k, $hash_key) { | ||
| 256 | return $data{$k}{$hash_key}; | ||
| 257 | } | ||
| 258 | |||
| 259 | =head2 hash_add | ||
| 260 | |||
| 261 | Takes the following parameters: | ||
| 262 | |||
| 263 | =over 4 | ||
| 264 | |||
| 265 | =item * | ||
| 266 | |||
| 267 | =back | ||
| 268 | |||
| 269 | Returns a L<Future> indicating success or failure. | ||
| 270 | |||
| 271 | =cut | ||
| 272 | |||
| 273 | async method hash_add : Defer ($k, $hash_key, $v) { | ||
| 274 | $v //= 1; | ||
| 275 | die 'value cannot be a reference for ' . $k . ' - ' . ref($v) if ref $v; | ||
| 276 | return $data{$k}{$hash_key} += $v; | ||
| 277 | } | ||
| 278 | |||
| 279 | =head2 hash_keys | ||
| 280 | |||
| 281 | Takes the following parameters: | ||
| 282 | |||
| 283 | =over 4 | ||
| 284 | |||
| 285 | =item * | ||
| 286 | |||
| 287 | =back | ||
| 288 | |||
| 289 | Returns a L<Future> which will resolve to a list of the keys in no defined order. | ||
| 290 | |||
| 291 | =cut | ||
| 292 | |||
| 293 | async method hash_keys : Defer ($k) { | ||
| 294 | return keys $data{$k}->%*; | ||
| 295 | } | ||
| 296 | |||
| 297 | =head2 hash_values | ||
| 298 | |||
| 299 | Takes the following parameters: | ||
| 300 | |||
| 301 | =over 4 | ||
| 302 | |||
| 303 | =item * | ||
| 304 | |||
| 305 | =back | ||
| 306 | |||
| 307 | Returns a L<Future> which will resolve to a list of the values in no defined order. | ||
| 308 | |||
| 309 | =cut | ||
| 310 | |||
| 311 | async method hash_values : Defer ($k) { | ||
| 312 | return values $data{$k}->%*; | ||
| 313 | } | ||
| 314 | |||
| 315 | =head2 hash_exists | ||
| 316 | |||
| 317 | Takes the following parameters: | ||
| 318 | |||
| 319 | =over 4 | ||
| 320 | |||
| 321 | =item * | ||
| 322 | |||
| 323 | =back | ||
| 324 | |||
| 325 | Returns a L<Future> which will resolve to true if the key exists in this hash. | ||
| 326 | |||
| 327 | =cut | ||
| 328 | |||
| 329 | async method hash_exists : Defer ($k, $hash_key) { | ||
| 330 | return exists $data{$k}{$hash_key}; | ||
| 331 | } | ||
| 332 | |||
| 333 | =head2 hash_count | ||
| 334 | |||
| 335 | Takes the following parameters: | ||
| 336 | |||
| 337 | =over 4 | ||
| 338 | |||
| 339 | =item * | ||
| 340 | |||
| 341 | =back | ||
| 342 | |||
| 343 | Returns a L<Future> which will resolve to the count of the keys in this hash. | ||
| 344 | |||
| 345 | =cut | ||
| 346 | |||
| 347 | async method hash_count : Defer ($k) { | ||
| 348 | return 0 + keys $data{$k}->%*; | ||
| 349 | } | ||
| 350 | |||
| 351 | =head2 hash_as_list | ||
| 352 | |||
| 353 | Takes the following parameters: | ||
| 354 | |||
| 355 | =over 4 | ||
| 356 | |||
| 357 | =item * | ||
| 358 | |||
| 359 | =back | ||
| 360 | |||
| 361 | Returns a L<Future> which will resolve to a list of key/value pairs, | ||
| 362 | suitable for assigning to a hash. | ||
| 363 | |||
| 364 | =cut | ||
| 365 | |||
| 366 | async method hash_as_list : Defer ($k) { | ||
| 367 | return $data{$k}->%*; | ||
| 368 | } | ||
| 369 | |||
| 370 | =head2 orderedset_add | ||
| 371 | |||
| 372 | Takes the following parameters: | ||
| 373 | |||
| 374 | =over 4 | ||
| 375 | |||
| 376 | =item * C<< $k >> - the relative key in storage | ||
| 377 | |||
| 378 | =item * C<< $s >> - the scalar score value | ||
| 379 | |||
| 380 | =item * C<< $m >> - the scalar member value | ||
| 381 | |||
| 382 | =back | ||
| 383 | |||
| 384 | Note that references are currently B<not> supported - attempts to write an arrayref, hashref | ||
| 385 | or object will fail. | ||
| 386 | |||
| 387 | Returns a L<Future> which will resolve on completion. | ||
| 388 | |||
| 389 | =cut | ||
| 390 | |||
| 391 | async method orderedset_add : Defer ($k, $s, $m) { | ||
| 392 | die 'score & member values cannot be a reference for ' . $k . ' - ' . ref($s) . ref($m) if (ref $s or ref $m); | ||
| 393 | $data{$k} = {} unless defined $data{$k}; | ||
| 394 | return $data{$k}->{$s} = $m; | ||
| 395 | } | ||
| 396 | |||
| 397 | =head2 orderedset_remove_member | ||
| 398 | |||
| 399 | Takes the following parameters: | ||
| 400 | |||
| 401 | =over 4 | ||
| 402 | |||
| 403 | =item * C<< $k >> - the relative key in storage | ||
| 404 | |||
| 405 | =item * C<< $m >> - the scalar member value | ||
| 406 | |||
| 407 | =back | ||
| 408 | |||
| 409 | Returns a L<Future> which will resolve on completion. | ||
| 410 | |||
| 411 | =cut | ||
| 412 | |||
| 413 | async method orderedset_remove_member : Defer ($k, $m) { | ||
| 414 | my @keys_before = keys $data{$k}->%*; | ||
| 415 | $data{$k} = { map { $data{$k}->{$_} !~ /$m/ ? ($_ => $data{$k}->{$_}) : () } keys $data{$k}->%* }; | ||
| 416 | my @keys_after = keys $data{$k}->%*; | ||
| 417 | return 0 + @keys_before - @keys_after; | ||
| 418 | } | ||
| 419 | |||
| 420 | =head2 orderedset_remove_byscore | ||
| 421 | |||
| 422 | Takes the following parameters: | ||
| 423 | |||
| 424 | =over 4 | ||
| 425 | |||
| 426 | =item * C<< $k >> - the relative key in storage | ||
| 427 | |||
| 428 | =item * C<< $min >> - the minimum score to remove | ||
| 429 | |||
| 430 | =item * C<< $max >> - the maximum score to remove | ||
| 431 | |||
| 432 | =back | ||
| 433 | |||
| 434 | Returns a L<Future> which will resolve on completion. | ||
| 435 | |||
| 436 | =cut | ||
| 437 | |||
| 438 | async method orderedset_remove_byscore : Defer ($k, $min, $max) { | ||
| 439 | $min = -100000 if $min =~ /-inf/; | ||
| 440 | $max = 100000 if $max =~ /\+inf/; | ||
| 441 | my @keys_before = keys $data{$k}->%*; | ||
| 442 | $data{$k} = { map { ($_ >= $min and $_ <= $max ) ? () : ($_ => $data{$k}->{$_}) } keys $data{$k}->%* }; | ||
| 443 | my @keys_after = keys $data{$k}->%*; | ||
| 444 | return 0 + @keys_before - @keys_after; | ||
| 445 | |||
| 446 | } | ||
| 447 | |||
| 448 | =head2 orderedset_member_count | ||
| 449 | |||
| 450 | Takes the following parameters: | ||
| 451 | |||
| 452 | =over 4 | ||
| 453 | |||
| 454 | =item * C<< $k >> - the relative key in storage | ||
| 455 | |||
| 456 | =item * C<< $min >> - minimum score for selection | ||
| 457 | |||
| 458 | =item * C<< $max >> - maximum score for selection | ||
| 459 | |||
| 460 | =back | ||
| 461 | |||
| 462 | Returns a L<Future> which will resolve on completion. | ||
| 463 | |||
| 464 | =cut | ||
| 465 | |||
| 466 | async method orderedset_member_count : Defer ($k, $min, $max) { | ||
| 467 | $min = -100000 if $min =~ /-inf/; | ||
| 468 | $max = 100000 if $max =~ /\+inf/; | ||
| 469 | return scalar map { ($_ >= $min and $_ <= $max) ? (1) : () } keys $data{$k}->%*; | ||
| 470 | } | ||
| 471 | |||
| 472 | =head2 orderedset_members | ||
| 473 | |||
| 474 | Takes the following parameters: | ||
| 475 | |||
| 476 | =over 4 | ||
| 477 | |||
| 478 | =item * C<< $k >> - the relative key in storage | ||
| 479 | |||
| 480 | =item * C<< $min >> - minimum score for selection | ||
| 481 | |||
| 482 | =item * C<< $max >> - maximum score for selection | ||
| 483 | |||
| 484 | =back | ||
| 485 | |||
| 486 | Returns a L<Future> which will resolve on completion. | ||
| 487 | |||
| 488 | =cut | ||
| 489 | |||
| 490 | async method orderedset_members : Defer ($k, $min = '-inf', $max = '+inf', $with_score = 0) { | ||
| 491 | $min = -100000 if $min =~ /-inf/; | ||
| 492 | $max = 100000 if $max =~ /\+inf/; | ||
| 493 | return [ map { ($_ >= $min and $_ <= $max ) ? $with_score ? ($data{$k}->{$_}, $_) : ($data{$k}->{$_}) : () } sort keys $data{$k}->%* ]; | ||
| 494 | } | ||
| 495 | |||
| 496 | 1; | ||
| 497 | |||
| 498 | =head1 AUTHOR | ||
| 499 | |||
| 500 | Deriv Group Services Ltd. C<< DERIV@cpan.org >>. | ||
| 501 | |||
| 502 | See L<Myriad/CONTRIBUTORS> for full details. | ||
| 503 | |||
| 504 | =head1 LICENSE | ||
| 505 | |||
| 506 | Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself. | ||
| 507 | |||