Apply some changes
[dbsrgits/DBM-Deep.git] / lib / DBM / Deep / Engine.pm
CommitLineData
bf941eae 1package DBM::Deep::Engine;
2
3use 5.006_000;
4
5use strict;
6use warnings FATAL => 'all';
7
29460253 8our $VERSION = $DBM::Deep::VERSION;
9
bf941eae 10use DBM::Deep::Iterator ();
11
12# File-wide notes:
13# * Every method in here assumes that the storage has been appropriately
14# safeguarded. This can be anything from flock() to some sort of manual
15# mutex. But, it's the caller's responsability to make sure that this has
16# been done.
17
a4d36ff6 18sub SIG_HASH () { 'H' }
19sub SIG_ARRAY () { 'A' }
20
64a531e5 21=head1 NAME
22
23DBM::Deep::Engine
24
25=head1 PURPOSE
26
1c62d370 27This is an internal-use-only object for L<DBM::Deep>. It mediates the low-level
28mapping between the L<DBM::Deep> objects and the storage medium.
64a531e5 29
30The purpose of this documentation is to provide low-level documentation for
31developers. It is B<not> intended to be used by the general public. This
32documentation and what it documents can and will change without notice.
33
34=head1 OVERVIEW
35
36The engine exposes an API to the DBM::Deep objects (DBM::Deep, DBM::Deep::Array,
37and DBM::Deep::Hash) for their use to access the actual stored values. This API
38is the following:
39
40=over 4
41
42=item * new
43
44=item * read_value
45
46=item * get_classname
47
48=item * make_reference
49
50=item * key_exists
51
52=item * delete_key
53
54=item * write_value
55
56=item * get_next_key
57
f4d0ac97 58=item * setup
64a531e5 59
60=item * begin_work
61
62=item * commit
63
64=item * rollback
65
66=item * lock_exclusive
67
68=item * lock_shared
69
70=item * unlock
71
72=back
73
74They are explained in their own sections below. These methods, in turn, may
75provide some bounds-checking, but primarily act to instantiate objects in the
76Engine::Sector::* hierarchy and dispatch to them.
77
78=head1 TRANSACTIONS
79
80Transactions in DBM::Deep are implemented using a variant of MVCC. This attempts
81to keep the amount of actual work done against the file low while stil providing
82Atomicity, Consistency, and Isolation. Durability, unfortunately, cannot be done
83with only one file.
84
85=head2 STALENESS
86
87If another process uses a transaction slot and writes stuff to it, then
88terminates, the data that process wrote it still within the file. In order to
89address this, there is also a transaction staleness counter associated within
90every write. Each time a transaction is started, that process increments that
91transaction's staleness counter. If, when it reads a value, the staleness
92counters aren't identical, DBM::Deep will consider the value on disk to be stale
93and discard it.
94
95=head2 DURABILITY
96
97The fourth leg of ACID is Durability, the guarantee that when a commit returns,
98the data will be there the next time you read from it. This should be regardless
99of any crashes or powerdowns in between the commit and subsequent read.
100DBM::Deep does provide that guarantee; once the commit returns, all of the data
101has been transferred from the transaction shadow to the HEAD. The issue arises
102with partial commits - a commit that is interrupted in some fashion. In keeping
103with DBM::Deep's "tradition" of very light error-checking and non-existent
104error-handling, there is no way to recover from a partial commit. (This is
105probably a failure in Consistency as well as Durability.)
106
107Other DBMSes use transaction logs (a separate file, generally) to achieve
108Durability. As DBM::Deep is a single-file, we would have to do something
109similar to what SQLite and BDB do in terms of committing using synchonized
110writes. To do this, we would have to use a much higher RAM footprint and some
111serious programming that make my head hurts just to think about it.
112
113=cut
114
f4d0ac97 115=head2 read_value( $obj, $key )
64a531e5 116
f4d0ac97 117This takes an object that provides _base_offset() and a string. It returns the
118value stored in the corresponding Sector::Value's data section.
119
120=cut
121
122sub read_value { die "read_value must be implemented in a child class" }
123
124=head2 get_classname( $obj )
125
126This takes an object that provides _base_offset() and returns the classname (if
127any) associated with it.
128
129It delegates to Sector::Reference::get_classname() for the heavy lifting.
130
131It performs a staleness check.
132
133=cut
134
135sub get_classname { die "get_classname must be implemented in a child class" }
136
137=head2 make_reference( $obj, $old_key, $new_key )
138
139This takes an object that provides _base_offset() and two strings. The
140strings correspond to the old key and new key, respectively. This operation
141is equivalent to (given C<< $db->{foo} = []; >>) C<< $db->{bar} = $db->{foo} >>.
142
143This returns nothing.
144
145=cut
146
147sub make_reference { die "make_reference must be implemented in a child class" }
148
149=head2 key_exists( $obj, $key )
150
151This takes an object that provides _base_offset() and a string for
152the key to be checked. This returns 1 for true and "" for false.
153
154=cut
155
156sub key_exists { die "key_exists must be implemented in a child class" }
157
158=head2 delete_key( $obj, $key )
159
160This takes an object that provides _base_offset() and a string for
161the key to be deleted. This returns the result of the Sector::Reference
162delete_key() method.
163
164=cut
165
166sub delete_key { die "delete_key must be implemented in a child class" }
167
168=head2 write_value( $obj, $key, $value )
169
170This takes an object that provides _base_offset(), a string for the
1c62d370 171key, and a value. This value can be anything storable within L<DBM::Deep>.
f4d0ac97 172
173This returns 1 upon success.
174
175=cut
176
177sub write_value { die "write_value must be implemented in a child class" }
178
179=head2 setup( $obj )
180
181This takes an object that provides _base_offset(). It will do everything needed
182in order to properly initialize all values for necessary functioning. If this is
183called upon an already initialized object, this will also reset the inode.
184
185This returns 1.
186
187=cut
188
189sub setup { die "setup must be implemented in a child class" }
190
191=head2 begin_work( $obj )
192
193This takes an object that provides _base_offset(). It will set up all necessary
194bookkeeping in order to run all work within a transaction.
195
196If $obj is already within a transaction, an error wiill be thrown. If there are
197no more available transactions, an error will be thrown.
198
199This returns undef.
200
201=cut
202
203sub begin_work { die "begin_work must be implemented in a child class" }
204
205=head2 rollback( $obj )
206
207This takes an object that provides _base_offset(). It will revert all
208actions taken within the running transaction.
209
210If $obj is not within a transaction, an error will be thrown.
211
212This returns 1.
213
214=cut
215
216sub rollback { die "rollback must be implemented in a child class" }
217
218=head2 commit( $obj )
219
220This takes an object that provides _base_offset(). It will apply all
221actions taken within the transaction to the HEAD.
222
223If $obj is not within a transaction, an error will be thrown.
224
225This returns 1.
226
227=cut
228
229sub commit { die "commit must be implemented in a child class" }
64a531e5 230
bf941eae 231=head2 get_next_key( $obj, $prev_key )
232
233This takes an object that provides _base_offset() and an optional string
234representing the prior key returned via a prior invocation of this method.
235
236This method delegates to C<< DBM::Deep::Iterator->get_next_key() >>.
237
238=cut
239
240# XXX Add staleness here
241sub get_next_key {
242 my $self = shift;
243 my ($obj, $prev_key) = @_;
244
f4d0ac97 245 # XXX Need to add logic about resetting the iterator if any key in the
246 # reference has changed
0b3cba50 247 unless ( defined $prev_key ) {
19b913ce 248 $obj->{iterator} = $self->iterator_class->new({
bf941eae 249 base_offset => $obj->_base_offset,
250 engine => $self,
251 });
252 }
253
254 return $obj->{iterator}->get_next_key( $obj );
255}
256
f4d0ac97 257=head2 lock_exclusive()
258
259This takes an object that provides _base_offset(). It will guarantee that
260the storage has taken precautions to be safe for a write.
261
262This returns nothing.
263
264=cut
265
266sub lock_exclusive {
267 my $self = shift;
268 my ($obj) = @_;
269 return $self->storage->lock_exclusive( $obj );
270}
271
272=head2 lock_shared()
273
274This takes an object that provides _base_offset(). It will guarantee that
275the storage has taken precautions to be safe for a read.
276
277This returns nothing.
278
279=cut
280
281sub lock_shared {
282 my $self = shift;
283 my ($obj) = @_;
284 return $self->storage->lock_shared( $obj );
285}
286
287=head2 unlock()
288
289This takes an object that provides _base_offset(). It will guarantee that
290the storage has released the most recently-taken lock.
291
292This returns nothing.
293
294=cut
295
296sub unlock {
297 my $self = shift;
298 my ($obj) = @_;
299
300 my $rv = $self->storage->unlock( $obj );
301
302 $self->flush if $rv;
303
304 return $rv;
305}
306
307=head1 INTERNAL METHODS
308
309The following methods are internal-use-only to DBM::Deep::Engine and its
310child classes.
311
312=cut
313
314=head2 flush()
315
316This takes no arguments. It will do everything necessary to flush all things to
317disk. This is usually called during unlock() and setup().
318
319This returns nothing.
320
321=cut
322
323sub flush {
324 my $self = shift;
325
326 # Why do we need to have the storage flush? Shouldn't autoflush take care of
327 # things? -RobK, 2008-06-26
328 $self->storage->flush;
329
330 return;
331}
332
d6ecf579 333=head2 load_sector( $loc )
334
335This takes an id/location/offset and loads the sector based on the engine's
336defined sector type.
337
338=cut
339
340sub load_sector { $_[0]->sector_type->load( @_ ) }
341
2ba14e04 342=head2 clear
343
344=cut
345
346=head2 clear( $obj )
347
348This takes an object that provides _base_offset() and deletes all its
349elements, returning nothing.
350
351=cut
352
353sub clear {
354 my $self = shift;
355 my $obj = shift;
356
357 my $sector = $self->_load_sector( $obj->_base_offset )
358 or return;
359
360 return unless $sector->staleness == $obj->_staleness;
361
362 $sector->clear;
363
364 return;
365}
366
4f034d8f 367=head2 cache / clear_cache
368
369This is the cache of loaded Reference sectors.
370
371=cut
372
373sub cache { $_[0]{cache} ||= {} }
374sub clear_cache { %{$_[0]->cache} = () }
375
580e5ee2 376=head2 supports( $option )
377
378This returns a boolean depending on if this instance of DBM::Dep supports
379that feature. C<$option> can be one of:
380
381=over 4
382
383=item * transactions
384
385=back
386
387=cut
388
389sub supports { die "supports must be implemented in a child class" }
390
d6ecf579 391=head2 ACCESSORS
392
393The following are readonly attributes.
394
395=over 4
396
397=item * storage
398
4f034d8f 399=item * sector_type
400
d6ecf579 401=back
402
403=cut
404
405sub storage { $_[0]{storage} }
406
407sub sector_type { die "sector_type must be implemented in a child class" }
408
c2472ede 409# This code is to make sure we write all the values in the $value to the
410# disk and to make sure all changes to $value after the assignment are
411# reflected on disk. This may be counter-intuitive at first, but it is
412# correct dwimmery.
413# NOTE - simply tying $value won't perform a STORE on each value. Hence,
414# the copy to a temp value.
415sub _descend {
416 my $self = shift;
417 my ($value, $value_sector) = @_;
418 my $r = Scalar::Util::reftype( $value ) || '';
419
420 if ( $r eq 'ARRAY' ) {
421 my @temp = @$value;
422 tie @$value, 'DBM::Deep', {
423 base_offset => $value_sector->offset,
424 staleness => $value_sector->staleness,
425 storage => $self->storage,
426 engine => $self,
427 };
428 @$value = @temp;
429 bless $value, 'DBM::Deep::Array' unless Scalar::Util::blessed( $value );
430 }
431 elsif ( $r eq 'HASH' ) {
432 my %temp = %$value;
433 tie %$value, 'DBM::Deep', {
434 base_offset => $value_sector->offset,
435 staleness => $value_sector->staleness,
436 storage => $self->storage,
437 engine => $self,
438 };
439 %$value = %temp;
440 bless $value, 'DBM::Deep::Hash' unless Scalar::Util::blessed( $value );
441 }
bb1daf85 442
443 return;
c2472ede 444}
445
bf941eae 4461;
447__END__