File: Hash.pm

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (126 lines) | stat: -rw-r--r-- 2,253 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package Catmandu::Store::Hash;

use Catmandu::Sane;

our $VERSION = '1.2024';

use Moo;
use Catmandu::Util qw(:is);
use Catmandu::Store::Hash::Bag;
use namespace::clean;

with 'Catmandu::Store';
with 'Catmandu::Droppable';
with 'Catmandu::Transactional';

has _hashes =>
    (is => 'ro', lazy => 1, init_arg => undef, default => sub {+{}});
has init_data => (is => 'ro');

sub BUILD {
    my ($self) = @_;
    if (my $data = $self->init_data) {
        $self->bag->add($_) for @$data;
    }
}

sub drop {
    my ($self) = @_;
    $_->drop for values %{$self->bags};
    return;
}

sub transaction {
    my ($self, $coderef) = @_;
    &{$coderef} if is_code_ref($coderef);
}

1;

__END__

=pod

=head1 NAME

Catmandu::Store::Hash - An in-memory store

=head1 SYNOPSIS

   use Catmandu;

   my $store = Catmandu->store('Hash');

   my $obj1 = $store->bag->add({ name => 'Patrick' });

   printf "obj1 stored as %s\n" , $obj1->{_id};

   # Force an id in the store
   my $obj2 = $store->bag->add({ _id => 'test123' , name => 'Nicolas' });

   my $obj3 = $store->bag->get('test123');

   $store->bag->delete('test123');

   $store->bag->delete_all;

   # All bags are iterators
   $store->bag->each(sub { ... });
   $store->bag->take(10)->each(sub { ... });

=head1 DESCRIPTION

A Catmandu::Store::Hash is an in-memory L<Catmandu::Store> backed by a hash
for fast retrieval combined with a doubly linked list for fast traversal.

=head1 METHODS

=head2 new([init_data => [...] ])

Create a new Catmandu::Store::Hash. Optionally provide as init_data an array
ref of data:

    my $store = Catmandu->store('Hash', init_data => [
           { _id => 1, data => foo } ,
           { _id => 2, data => bar }
    ]);

    # or in a catmandu.yml configuration file:

    ---
    store:
       hash:
         package: Hash
         options:
            init_data:
               - _id: 1
                 data: foo
               - _id: 2
                 data: bar


=head1 INHERITED METHODS

This Catmandu::Store implements:

=over 3

=item L<Catmandu::Store>

=item L<Catmandu::Droppable>

=item L<Catmandu::Transactional>

=back

Each Catmandu::Bag in this Catmandu::Store implements:

=over 3

=item L<Catmandu::Bag>

=item L<Catmandu::Droppable>

=back

=cut