| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::CAS::Store::Nested; | ||
| 5 | use Any::Moose; | ||
| 6 | extends 'Jifty::CAS::Store'; | ||
| 7 | has 'parts' => (is => 'rw'); | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Jifty::CAS::Store::Nested - A layered CAS store | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This is a layered backend for L<Jifty::CAS>, which provides a way to | ||
| 16 | combine multiple CAS backends. Writes are passed through to every | ||
| 17 | layer, whereas reads stop on the first layer which contains the data. | ||
| 18 | This allows a fast in-memory store to be layered on top of a durable | ||
| 19 | file store, for instance. | ||
| 20 | |||
| 21 | Configuration requires providing two or more CAS classes: | ||
| 22 | |||
| 23 | framework: | ||
| 24 | CAS: | ||
| 25 | Default: | ||
| 26 | Class: Jifty::CAS::Store::Nested | ||
| 27 | Parts: | ||
| 28 | - Class: Jifty::CAS::Store::Memory | ||
| 29 | - Class: Jifty::CAS::Store::LocalFile | ||
| 30 | Path: %var/cas% | ||
| 31 | |||
| 32 | =head1 METHODS | ||
| 33 | |||
| 34 | =head2 BUILD | ||
| 35 | |||
| 36 | Constructs the sub-parts and stores them. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub BUILD { | ||
| 41 | my $self = shift; | ||
| 42 | my @parts; | ||
| 43 | for my $part (@{ $self->parts || [] }) { | ||
| 44 | my %part = %{ $part }; | ||
| 45 | my $storeclass = delete $part{Class}; | ||
| 46 | Jifty::Util->require( $storeclass ); | ||
| 47 | push @parts, $storeclass->new( | ||
| 48 | map {lc $_ => $part->{$_}} grep {$_ ne "Class"} keys %part | ||
| 49 | ); | ||
| 50 | } | ||
| 51 | $self->parts( \@parts ); | ||
| 52 | } | ||
| 53 | |||
| 54 | =head2 _store DOMAIN NAME BLOB | ||
| 55 | |||
| 56 | Stores the BLOB (a L<Jifty::CAS::Blob>) in all parts, starting at the | ||
| 57 | bottom. Returns the key on success or undef on failure. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub _store { | ||
| 62 | my ($self, $domain, $name, $blob) = @_; | ||
| 63 | # Writes start on the bottom | ||
| 64 | $_->_store($domain, $name, $blob) for reverse @{ $self->parts }; | ||
| 65 | } | ||
| 66 | |||
| 67 | =head2 key DOMAIN NAME | ||
| 68 | |||
| 69 | Returns the most recent key for the given pair of C<DOMAIN> and | ||
| 70 | C<NAME>, or undef if none such exists. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub key { | ||
| 75 | my ($self, $domain, $name) = @_; | ||
| 76 | # Reads start at the top | ||
| 77 | my @missing; | ||
| 78 | my $found; | ||
| 79 | for my $part (@{$self->parts}) { | ||
| 80 | if ($found = $part->key($domain, $name)) { | ||
| 81 | if (@missing) { | ||
| 82 | # If there were cache misses higher on the stack, write | ||
| 83 | # the correct value back to them | ||
| 84 | my $blob = $part->retrieve($domain, $found); | ||
| 85 | $_->_store($domain, $name, $blob) for @missing; | ||
| 86 | } | ||
| 87 | return $found; | ||
| 88 | } | ||
| 89 | push @missing, $part; | ||
| 90 | } | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | |||
| 94 | =head2 retrieve DOMAIN KEY | ||
| 95 | |||
| 96 | Returns a L<Jifty::CAS::Blob> for the given pair of C<DOMAIN> and | ||
| 97 | C<KEY>, or undef if none such exists. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub retrieve { | ||
| 102 | my ($self, $domain, $key) = @_; | ||
| 103 | # We don't have a way of storing just the blob at a key location, | ||
| 104 | # so we can't freshen the higher levels in this case. | ||
| 105 | for my $part (@{$self->parts}) { | ||
| 106 | my $found = $part->retrieve($domain, $key); | ||
| 107 | return $found if $found; | ||
| 108 | } | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | |||
| 112 | =head2 durable | ||
| 113 | |||
| 114 | If any of the parts are durable, the entire nested CAS backend is durable. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | sub durable { | ||
| 119 | my $self = shift; | ||
| 120 | for my $part (@{$self->parts}) { | ||
| 121 | return 1 if $part->durable; | ||
| 122 | } | ||
| 123 | return 0; | ||
| 124 | } | ||
| 125 | |||
| 126 | no Any::Moose; | ||
| 127 | __PACKAGE__->meta->make_immutable(inline_constructor => 0); | ||
| 128 | 1; |