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