line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Freezer ; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
56202
|
use warnings ; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
90
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Data::Freezer - A namespace aware object freezer based on Pixie. |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Pixie ; |
12
|
|
|
|
|
|
|
use Data::Freezer ; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $pixie = ... ; # Make a Pixie storage engine (See Pixie ). |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# EXAMPLE: use Pixie with memory storage |
17
|
|
|
|
|
|
|
# my $pixie = Pixie->connect('memory'); |
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use Data::Freezer { debug => [0|1] }; |
21
|
|
|
|
|
|
|
my $freezer = Data::Freezer->new($pixie); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# OR DO ONCE AT THE BEGINING OF APP: |
24
|
|
|
|
|
|
|
Data::Freezer->instance()->pixie($pixie); |
25
|
|
|
|
|
|
|
# AND THEN |
26
|
|
|
|
|
|
|
$freezer = Data::Freezer->instance(); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$freezer->insert( new Carot(...) , 'vegies'); |
29
|
|
|
|
|
|
|
$freezer->insert( new Rabbit(...) , 'meat' ); |
30
|
|
|
|
|
|
|
$freezer->insert( new Tomato(...) , 'vegies'); |
31
|
|
|
|
|
|
|
$freezer->insert( new RumSteak(..) , 'meat' ); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 REQUIREMENTS |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Pixie release version 2.06 |
36
|
|
|
|
|
|
|
Class::AutoAccess 0.02 |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
2
|
|
13
|
use Carp ; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
140
|
|
41
|
2
|
|
|
2
|
|
13
|
use strict ; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
75
|
|
42
|
|
|
|
|
|
|
|
43
|
2
|
|
|
2
|
|
2034
|
use Pixie ; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
#use base qw/Class::AutoAccess/ ; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
use Data::Freezer::FreezingBag ; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
our $VERSION = '0.02' ; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
no strict ; |
52
|
|
|
|
|
|
|
my $debug = 0 ; |
53
|
|
|
|
|
|
|
sub import{ |
54
|
|
|
|
|
|
|
my ($class, $arg) = @_ ; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$debug = $arg->{'debug'} || 0 ; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $instance = Data::Freezer->new(); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head2 instance |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Class method. Returns a global unique instance of a freezer. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=cut |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub instance{ |
69
|
|
|
|
|
|
|
my ($class , $i ) = @_ ; |
70
|
|
|
|
|
|
|
if( $i ){ |
71
|
|
|
|
|
|
|
$instance = $i ; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
return $instance ; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
use strict ; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 new |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Returns a newly created freezer. |
80
|
|
|
|
|
|
|
You can give a pixie storage engine here. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Usage: |
83
|
|
|
|
|
|
|
my $f = Data::Freezer->new(); |
84
|
|
|
|
|
|
|
or |
85
|
|
|
|
|
|
|
my $f = Data::Freezer->new($pixie); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub new{ |
90
|
|
|
|
|
|
|
my ($class, $pixie) = @_ ; |
91
|
|
|
|
|
|
|
my $self = bless {} , $class ; |
92
|
|
|
|
|
|
|
if( $pixie ) { $self->pixie($pixie); } |
93
|
|
|
|
|
|
|
return $self ; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 pixie |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Gets/Sets the Pixie storage engine |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub pixie{ |
103
|
|
|
|
|
|
|
my ($self, $pixie) = @_ ; |
104
|
|
|
|
|
|
|
if( ! $pixie ){ return $self->{'pixie'} ;} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
$self->{'pixie'} = $pixie ; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
print "Binding namespace hash\n" if $debug ; |
109
|
|
|
|
|
|
|
if ( ! defined $pixie->get_object_named("_hNameSpaces_") ){ |
110
|
|
|
|
|
|
|
$pixie->bind_name("_hNameSpaces_" => bless( {} , "NameSpaces" ) ); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 getNameSpaces |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Returns a reference on a list of all namespaces available in this storage |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
usage: |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
my $nameSpaces = $store->getNameSpaces(); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub getNameSpaces{ |
127
|
|
|
|
|
|
|
my ($self) = @_ ; |
128
|
|
|
|
|
|
|
print "Getting namespace hash\n" if $debug ; |
129
|
|
|
|
|
|
|
my $h = $self->pixie()->get_object_named("_hNameSpaces_"); |
130
|
|
|
|
|
|
|
print "Retrieved hash is: ".$self->pixie()->get_object_named("_hNameSpaces_")."\n" if $debug; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
my %realH = %{$h} ; |
133
|
|
|
|
|
|
|
#while( my ($k, $v ) = each %realH ){ |
134
|
|
|
|
|
|
|
#print $k." -> ".$v."\n"; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
#} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
my @res = keys %realH ; |
139
|
|
|
|
|
|
|
return \@res ; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head2 insert |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Inserts an object into given name space storage. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Object must be Pixie friendly. ( See Pixie::Complicity in CPAN ). |
147
|
|
|
|
|
|
|
Usage: |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
my $o = ... ; |
150
|
|
|
|
|
|
|
my $nameSpace = ... ; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
my $cookie = $this->insert($o, $nameSpace); |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub insert{ |
158
|
|
|
|
|
|
|
my ($self, $o , $nameSpace ) = @_ ; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
$self->_assumeNameSpace($nameSpace); |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
print "Inserting $o\n" if $debug ; |
163
|
|
|
|
|
|
|
my $bag = Data::Freezer::FreezingBag->new(); |
164
|
|
|
|
|
|
|
$bag->content($o); |
165
|
|
|
|
|
|
|
my $cookie = $self->pixie()->insert($bag); |
166
|
|
|
|
|
|
|
$self->_addToNameSpace($nameSpace, $cookie); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
return $cookie ; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 _assumeNameSpace |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Assumes that given namespace exists. |
175
|
|
|
|
|
|
|
INTERNAL USAGE |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Usage: |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
$self->_assumeNameSpace($nameSpace); |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=cut |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub _assumeNameSpace{ |
184
|
|
|
|
|
|
|
my ($self, $nameSpace ) = @_ ; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# get the hash of nameSpaces |
187
|
|
|
|
|
|
|
print "Getting namespace hash\n" if $debug ; |
188
|
|
|
|
|
|
|
my $h = $self->pixie()->get_object_named("_hNameSpaces_"); |
189
|
|
|
|
|
|
|
if( ! $h->{$nameSpace} ){ |
190
|
|
|
|
|
|
|
$h->{$nameSpace} = 1 ; |
191
|
|
|
|
|
|
|
print "Updating namespace hash with $nameSpace\n" if $debug ; |
192
|
|
|
|
|
|
|
$self->pixie()->bind_name("_hNameSpaces_" => $h ); |
193
|
|
|
|
|
|
|
$self->pixie()->bind_name($nameSpace => bless([] , "CookieArray") ); |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head2 _addToNameSpace |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Adds given cookie to given namespace. |
200
|
|
|
|
|
|
|
INTERNAL USAGE |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Usage: |
203
|
|
|
|
|
|
|
$self->_addToNameSpace($nameSpace, $cookie); |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=cut |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
sub _addToNameSpace{ |
208
|
|
|
|
|
|
|
my ($self, $nameSpace , $cookie ) = @_ ; |
209
|
|
|
|
|
|
|
my $list = $self->pixie()->get_object_named($nameSpace) ; |
210
|
|
|
|
|
|
|
push @$list , $cookie ; |
211
|
|
|
|
|
|
|
print "Adding $cookie to namespace $nameSpace\n" if $debug ; |
212
|
|
|
|
|
|
|
$self->pixie()->bind_name($nameSpace , $list ); |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head2 _removeFromNameSpace |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Removes given cookie from namespace. Dies if this cookie does not belongs to this namespace |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
Usage: |
220
|
|
|
|
|
|
|
$self->_removeFromNameSpace($nameSpace, $cookie); |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=cut |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
sub _removeFromNameSpace{ |
225
|
|
|
|
|
|
|
my ($self, $nameSpace , $cookie ) = @_ ; |
226
|
|
|
|
|
|
|
my $list = $self->pixie()->get_object_named($nameSpace) ; |
227
|
|
|
|
|
|
|
print "Cookie is ".$cookie."\n" if $debug; |
228
|
|
|
|
|
|
|
print "LIST IS ".$list."\n" if $debug; |
229
|
|
|
|
|
|
|
my @arr = @$list ; |
230
|
|
|
|
|
|
|
print "ARRAY IS ".join(":",@arr)."\n" if $debug ; |
231
|
|
|
|
|
|
|
my @arr2 = grep ! ($_ eq $cookie) , @arr ; |
232
|
|
|
|
|
|
|
print "ARRAY2 : ".join(':',@arr2)."\n" if $debug ; |
233
|
|
|
|
|
|
|
if ( @arr2 == @arr ){ |
234
|
|
|
|
|
|
|
confess("No cookie $cookie in namespace ".$nameSpace); |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
$self->pixie()->bind_name($nameSpace , bless ( \@arr2 , 'CookieArray' )); |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head2 getCookies |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Gets the cookies associated with the given nameSpace. |
244
|
|
|
|
|
|
|
return undef if nameSpace does not exist. |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
Usage: |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
my $cookies = $this->getCookies($nameSpace); |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=cut |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
sub getCookies{ |
253
|
|
|
|
|
|
|
my ($self, $nameSpace ) = @_ ; |
254
|
|
|
|
|
|
|
print "Getting cookies for NameSpace $nameSpace\n" if $debug ; |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
my $a = $self->pixie()->get_object_named($nameSpace) ; |
257
|
|
|
|
|
|
|
print " Stored in : ".$a."\n" if $debug; |
258
|
|
|
|
|
|
|
my @realA = @{$a} ; |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
return \@realA ; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=head2 delete |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
Removes given cookie from given namespace. |
267
|
|
|
|
|
|
|
USER FUNCTION. |
268
|
|
|
|
|
|
|
Dies if cookie does not exist in given namespace |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Usage: |
272
|
|
|
|
|
|
|
$this->delete($cookie, $namespace); |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=cut |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
sub delete{ |
278
|
|
|
|
|
|
|
my ($self, $cookie , $nameSpace) = @_ ; |
279
|
|
|
|
|
|
|
print "Deleting cookie :".$cookie." from namespace ".$nameSpace."\n" if $debug ; |
280
|
|
|
|
|
|
|
$self->_removeFromNameSpace($nameSpace , $cookie); |
281
|
|
|
|
|
|
|
$self->pixie()->delete($cookie); |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=head2 get |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
Gets the object associated with the given cookie. |
288
|
|
|
|
|
|
|
Undef is object not here. |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
Usage: |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
my $o = $this->get($cookie); |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=cut |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
sub get{ |
297
|
|
|
|
|
|
|
my ($self , $cookie ) = @_ ; |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
print "Getting object for cookie: $cookie\n" if $debug ; |
300
|
|
|
|
|
|
|
my $o = $self->pixie()->get($cookie) ; |
301
|
|
|
|
|
|
|
if( ! $o ){ |
302
|
|
|
|
|
|
|
return undef ; |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
print " Retrived $o\n" if $debug ; |
306
|
|
|
|
|
|
|
print "Contains: ".$o->content()."\n" if $debug ; |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
my $ret = $o->content(); |
309
|
|
|
|
|
|
|
if( $ret->can('px_restore') ){ |
310
|
|
|
|
|
|
|
$ret = $ret->px_restore(); |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
return $ret ; |
313
|
|
|
|
|
|
|
} |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=head2 getObjects |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
Gets all objects associated to the given namespace. |
319
|
|
|
|
|
|
|
Undef if namespace does not exists. |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
Usage : |
322
|
|
|
|
|
|
|
my $objects = $this->getObjects($nameSpace); |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
=cut |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
sub getObjects{ |
327
|
|
|
|
|
|
|
my ($self , $nameSpace ) = @_ ; |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
my $cookies = $self->getCookies($nameSpace); |
330
|
|
|
|
|
|
|
if ( ! defined $cookies ){ return undef ;} |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
my @res = map { $self->get($_) ; } @$cookies ; |
333
|
|
|
|
|
|
|
return \@res ; |
334
|
|
|
|
|
|
|
} |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
=head1 AUTHOR |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
Jerome Eteve, C<< >> |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
=head1 BUGS |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
343
|
|
|
|
|
|
|
C, or through the web interface at |
344
|
|
|
|
|
|
|
L. |
345
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
346
|
|
|
|
|
|
|
your bug as I make changes. |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
Copyright 2005 Jerome Eteve, all rights reserved. |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
355
|
|
|
|
|
|
|
under the same terms as Perl itself. |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
=cut |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
1; |