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,
39 # Grab the parameters we want to use
40 foreach my $param ( keys %$self ) {
41 next unless exists $args->{$param};
42 $self->{$param} = $args->{$param};
45 if ( $self->{fh} && !$self->{file_offset} ) {
46 $self->{file_offset} = tell( $self->{fh} );
49 $self->open unless $self->{fh};
51 if ( $self->{audit_file} && !$self->{audit_fh} ) {
52 my $flags = O_WRONLY | O_APPEND | O_CREAT;
55 sysopen( $fh, $self->{audit_file}, $flags )
56 or die "Cannot open audit file '$self->{audit_file}' for read/write: $!";
58 # Set the audit_fh to autoflush
63 $self->{audit_fh} = $fh;
71 unless ( $_[0]{base_db_obj} ) {
72 $_[0]{base_db_obj} = $_[1];
73 Scalar::Util::weaken( $_[0]{base_db_obj} );
80 # Adding O_BINARY does remove the need for the binmode below. However,
81 # I'm not going to remove it because I don't have the Win32 chops to be
82 # absolutely certain everything will be ok.
83 my $flags = O_RDWR | O_CREAT | O_BINARY;
86 sysopen( $fh, $self->{file}, $flags )
87 or die "DBM::Deep: Cannot sysopen file '$self->{file}': $!\n";
90 # Even though we use O_BINARY, better be safe than sorry.
93 if ($self->{autoflush}) {
123 # If db locking is set, flock() the db file. If called multiple
124 # times before unlock(), then the same number of unlocks() must
125 # be called before the lock is released.
129 my ($obj, $type) = @_;
130 $type = LOCK_EX unless defined $type;
132 if (!defined($self->{fh})) { return; }
134 if ($self->{locking}) {
135 if (!$self->{locked}) {
136 flock($self->{fh}, $type);
138 # refresh end counter in case file has changed size
139 my @stats = stat($self->{fh});
140 $self->{end} = $stats[7];
142 # double-check file inode, in case another process
143 # has optimize()d our file while we were waiting.
144 if ($stats[1] != $self->{inode}) {
149 $obj->{engine}->setup_fh( $obj );
151 flock($self->{fh}, $type); # re-lock
153 # This may not be necessary after re-opening
154 $self->{end} = (stat($self->{fh}))[7]; # re-end
166 # If db locking is set, unlock the db file. See note in lock()
167 # regarding calling lock() multiple times.
172 if (!defined($self->{fh})) { return; }
174 if ($self->{locking} && $self->{locked} > 0) {
176 if (!$self->{locked}) { flock($self->{fh}, LOCK_UN); }
184 sub set_transaction_offset {
186 $self->{transaction_offset} = shift;
189 sub begin_transaction {
192 my $fh = $self->{fh};
196 seek( $fh, $self->{transaction_offset}, SEEK_SET );
198 read( $fh, $buffer, 4 );
199 $buffer = unpack( 'N', $buffer );
202 next if $buffer & (1 << ($_ - 1));
203 $self->{transaction_id} = $_;
204 $buffer |= (1 << $_-1 );
208 seek( $fh, $self->{transaction_offset}, SEEK_SET );
209 print( $fh pack( 'N', $buffer ) );
213 return $self->{transaction_id};
216 sub end_transaction {
219 my $fh = $self->{fh};
223 seek( $fh, $self->{transaction_offset}, SEEK_SET );
225 read( $fh, $buffer, 4 );
226 $buffer = unpack( 'N', $buffer );
228 # Unset $self->{transaction_id} bit
230 seek( $fh, $self->{transaction_offset}, SEEK_SET );
231 print( $fh pack( 'N', $buffer ) );
235 $self->{transaction_id} = 0;
238 sub current_transactions {
241 my $fh = $self->{fh};
245 seek( $fh, $self->{transaction_offset}, SEEK_SET );
247 read( $fh, $buffer, 4 );
248 $buffer = unpack( 'N', $buffer );
254 if ( $buffer & (1 << ($_ - 1)) ) {
255 push @transactions, $_;
259 return grep { $_ != $self->{transaction_id} } @transactions;
262 sub transaction_id { return $_[0]->{transaction_id} }