1 package DBM::Deep::File;
8 use Fcntl qw( :DEFAULT :flock :seek );
10 our $VERSION = '0.01';
27 filter_store_key => undef,
28 filter_store_value => undef,
29 filter_fetch_key => undef,
30 filter_fetch_value => undef,
32 # These are values that are not expected to be passed in through
33 # $args. They are here for documentation purposes.
35 transaction_offset => 0,
36 transaction_audit => undef,
40 # Grab the parameters we want to use
41 foreach my $param ( keys %$self ) {
42 next unless exists $args->{$param};
43 $self->{$param} = $args->{$param};
46 if ( $self->{fh} && !$self->{file_offset} ) {
47 $self->{file_offset} = tell( $self->{fh} );
50 $self->open unless $self->{fh};
52 if ( $self->{audit_file} && !$self->{audit_fh} ) {
53 my $flags = O_WRONLY | O_APPEND | O_CREAT;
56 sysopen( $fh, $self->{audit_file}, $flags )
57 or die "Cannot open audit file '$self->{audit_file}' for read/write: $!";
59 # Set the audit_fh to autoflush
64 $self->{audit_fh} = $fh;
74 unless ( $self->{base_db_obj} ) {
75 $self->{base_db_obj} = shift;
76 Scalar::Util::weaken( $self->{base_db_obj} );
85 # Adding O_BINARY should remove the need for the binmode below. However,
86 # I'm not going to remove it because I don't have the Win32 chops to be
87 # absolutely certain everything will be ok.
88 my $flags = O_RDWR | O_CREAT | O_BINARY;
91 sysopen( $fh, $self->{file}, $flags )
92 or die "DBM::Deep: Cannot sysopen file '$self->{file}': $!\n";
95 # Even though we use O_BINARY, better be safe than sorry.
98 if ($self->{autoflush}) {
121 unless ( $self->{inode} ) {
122 my @stats = stat($self->{fh});
123 $self->{inode} = $stats[1];
124 $self->{end} = $stats[7];
136 my $fh = $self->{fh};
137 if ( defined $loc ) {
138 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
148 my ($loc, $size) = @_;
152 my $fh = $self->{fh};
153 if ( defined $loc ) {
154 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
158 read( $fh, $buffer, $size);
163 sub increment_pointer {
167 if ( defined $size ) {
168 seek( $self->{fh}, $size, SEEK_CUR );
187 #XXX Do I need to reset $self->{end} here? I need a testcase
188 my $loc = $self->{end};
189 $self->{end} += $size;
196 # my ($size, $loc) = @_;
202 # my $fh = $self->{fh};
203 # seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
204 # print( $fh SIG_FREE
205 # . pack($self->{long_pack}, $size )
206 # . pack($self->{long_pack}, $next_loc )
213 # If db locking is set, flock() the db file. If called multiple
214 # times before unlock(), then the same number of unlocks() must
215 # be called before the lock is released.
219 my ($obj, $type) = @_;
220 $type = LOCK_EX unless defined $type;
222 if (!defined($self->{fh})) { return; }
224 if ($self->{locking}) {
225 if (!$self->{locked}) {
226 flock($self->{fh}, $type);
228 # refresh end counter in case file has changed size
229 my @stats = stat($self->{fh});
230 $self->{end} = $stats[7];
232 # double-check file inode, in case another process
233 # has optimize()d our file while we were waiting.
234 if ($stats[1] != $self->{inode}) {
239 $obj->{engine}->setup_fh( $obj );
241 flock($self->{fh}, $type); # re-lock
243 # This may not be necessary after re-opening
244 $self->{end} = (stat($self->{fh}))[7]; # re-end
256 # If db locking is set, unlock the db file. See note in lock()
257 # regarding calling lock() multiple times.
262 if (!defined($self->{fh})) { return; }
264 if ($self->{locking} && $self->{locked} > 0) {
266 if (!$self->{locked}) { flock($self->{fh}, LOCK_UN); }
274 sub set_transaction_offset {
276 $self->{transaction_offset} = shift;
283 if ( my $afh = $self->{audit_fh} ) {
284 flock( $afh, LOCK_EX );
286 if ( $string =~ /^#/ ) {
287 print( $afh "$string " . localtime(time) . "\n" );
290 print( $afh "$string # " . localtime(time) . "\n" );
293 flock( $afh, LOCK_UN );
296 if ( $self->{transaction_audit} ) {
297 push @{$self->{transaction_audit}}, $string;
303 sub begin_transaction {
306 my $fh = $self->{fh};
310 my $buffer = $self->read_at( $self->{transaction_offset}, 4 );
311 my ($next, @trans) = unpack( 'C C C C', $buffer );
313 $self->{transaction_id} = ++$next;
315 die if $trans[-1] != 0;
317 for ( my $i = 0; $i <= $#trans; $i++ ) {
318 next if $trans[$i] != 0;
324 $self->{transaction_offset},
325 pack( 'C C C C', $next, @trans),
330 $self->{transaction_audit} = [];
332 return $self->{transaction_id};
335 sub end_transaction {
338 my $fh = $self->{fh};
342 my $buffer = $self->read_at( $self->{transaction_offset}, 4 );
343 my ($next, @trans) = unpack( 'C C C C', $buffer );
345 @trans = grep { $_ != $self->{transaction_id} } @trans;
348 $self->{transaction_offset},
349 pack( 'C C C C', $next, @trans),
352 #XXX Need to free the space used by the current transaction
356 $self->{transaction_id} = 0;
357 $self->{transaction_audit} = undef;
359 # $self->{base_db_obj}->optimize;
360 # $self->{inode} = undef;
366 sub current_transactions {
369 my $fh = $self->{fh};
373 my $buffer = $self->read_at( $self->{transaction_offset}, 4 );
374 my ($next, @trans) = unpack( 'C C C C', $buffer );
378 return grep { $_ && $_ != $self->{transaction_id} } @trans;
381 sub transaction_id { return $_[0]->{transaction_id} }
383 sub commit_transaction {
386 my @audit = @{$self->{transaction_audit}};
388 $self->end_transaction;
391 my $db = $self->{base_db_obj};
394 warn "$_: $@\n" if $@;