1 package DBM::Deep::Engine;
8 use Fcntl qw( :DEFAULT :flock :seek );
12 # * All the local($/,$\); are to protect read() and print() from -l.
13 # * To add to bucket_size, make sure you modify the following:
16 # - add_bucket() - where the buckets are printed
19 # Setup file and tag signatures. These should never change.
21 sub SIG_FILE () { 'DPDB' }
22 sub SIG_HEADER () { 'h' }
23 sub SIG_INTERNAL () { 'i' }
24 sub SIG_HASH () { 'H' }
25 sub SIG_ARRAY () { 'A' }
26 sub SIG_NULL () { 'N' }
27 sub SIG_DATA () { 'D' }
28 sub SIG_INDEX () { 'I' }
29 sub SIG_BLIST () { 'B' }
30 sub SIG_FREE () { 'F' }
43 digest => \&Digest::MD5::md5,
47 # Maximum number of buckets per list before another level of indexing is
48 # done. Increase this value for slightly greater speed, but larger database
49 # files. DO NOT decrease this value below 16, due to risk of recursive
58 if ( defined $args->{pack_size} ) {
59 if ( lc $args->{pack_size} eq 'small' ) {
60 $args->{long_size} = 2;
61 $args->{long_pack} = 'n';
63 elsif ( lc $args->{pack_size} eq 'medium' ) {
64 $args->{long_size} = 4;
65 $args->{long_pack} = 'N';
67 elsif ( lc $args->{pack_size} eq 'large' ) {
68 $args->{long_size} = 8;
69 $args->{long_pack} = 'Q';
72 die "Unknown pack_size value: '$args->{pack_size}'\n";
76 # Grab the parameters we want to use
77 foreach my $param ( keys %$self ) {
78 next unless exists $args->{$param};
79 $self->{$param} = $args->{$param};
81 Scalar::Util::weaken( $self->{obj} ) if $self->{obj};
83 if ( $self->{max_buckets} < 16 ) {
84 warn "Floor of max_buckets is 16. Setting it to 16 from '$self->{max_buckets}'\n";
85 $self->{max_buckets} = 16;
91 sub _fileobj { return $_[0]{fileobj} }
92 sub _fh { return $_[0]->_fileobj->{fh} }
97 #XXX Does this need to be updated with different hashing algorithms?
98 $self->{index_size} = (2**8) * $self->{long_size};
99 $self->{bucket_size} = $self->{hash_size} + $self->{long_size} * 3;
100 $self->{bucket_list_size} = $self->{max_buckets} * $self->{bucket_size};
105 sub write_file_header {
112 my $loc = $self->_request_space( length( SIG_FILE ) + 21 );
113 seek($fh, $loc + $self->_fileobj->{file_offset}, SEEK_SET);
117 pack('N', 1), # header version
118 pack('N', 12), # header size
119 pack('N', 0), # currently running transaction IDs
120 pack('n', $self->{long_size}),
121 pack('A', $self->{long_pack}),
122 pack('n', $self->{data_size}),
123 pack('A', $self->{data_pack}),
124 pack('n', $self->{max_buckets}),
127 $self->_fileobj->set_transaction_offset( 13 );
132 sub read_file_header {
139 seek($fh, 0 + $self->_fileobj->{file_offset}, SEEK_SET);
141 my $bytes_read = read( $fh, $buffer, length(SIG_FILE) + 9 );
143 return unless $bytes_read;
145 my ($file_signature, $sig_header, $header_version, $size) = unpack(
149 unless ( $file_signature eq SIG_FILE ) {
150 $self->_fileobj->close;
151 $self->_throw_error( "Signature not found -- file is not a Deep DB" );
154 unless ( $sig_header eq SIG_HEADER ) {
155 $self->_fileobj->close;
156 $self->_throw_error( "Old file version found." );
160 $bytes_read += read( $fh, $buffer2, $size );
161 my ($running_transactions, @values) = unpack( 'N n A n A n', $buffer2 );
163 $self->_fileobj->set_transaction_offset( 13 );
165 if ( @values < 5 || grep { !defined } @values ) {
166 $self->_fileobj->close;
167 $self->_throw_error("Corrupted file - bad header");
170 #XXX Add warnings if values weren't set right
171 @{$self}{qw(long_size long_pack data_size data_pack max_buckets)} = @values;
185 #XXX The duplication of calculate_sizes needs to go away
186 unless ( $obj->{base_offset} ) {
187 my $bytes_read = $self->read_file_header;
189 $self->calculate_sizes;
192 # File is empty -- write header and master index
195 if ( my $afh = $self->_fileobj->{audit_fh} ) {
196 flock( $afh, LOCK_EX );
197 print( $afh "# Database created on " . localtime(time) . "\n" );
198 flock( $afh, LOCK_UN );
201 $self->write_file_header;
203 $obj->{base_offset} = $self->_request_space( $self->tag_size( $self->{index_size} ) );
206 $obj->_base_offset, $obj->_type,
207 chr(0)x$self->{index_size},
210 # Flush the filehandle
211 my $old_fh = select $fh;
212 my $old_af = $|; $| = 1; $| = $old_af;
216 $obj->{base_offset} = $bytes_read;
219 # Get our type from master index header
221 my $tag = $self->load_tag($obj->_base_offset);
224 $self->_throw_error("Corrupted file, no master index record");
227 unless ($obj->_type eq $tag->{signature}) {
229 $self->_throw_error("File type mismatch");
234 $self->calculate_sizes;
237 #XXX We have to make sure we don't mess up when autoflush isn't turned on
238 unless ( $self->_fileobj->{inode} ) {
239 my @stats = stat($fh);
240 $self->_fileobj->{inode} = $stats[1];
241 $self->_fileobj->{end} = $stats[7];
252 return SIG_SIZE + $self->{data_size} + $size;
257 # Given offset, signature and content, create tag and write to disk
260 my ($offset, $sig, $content) = @_;
261 my $size = length( $content );
267 if ( defined $offset ) {
268 seek($fh, $offset + $self->_fileobj->{file_offset}, SEEK_SET);
271 print( $fh $sig . pack($self->{data_pack}, $size) . $content );
273 return unless defined $offset;
278 offset => $offset + SIG_SIZE + $self->{data_size},
285 # Given offset, load single tag and return signature, size and data
292 # print join(':',map{$_||''}caller(1)), $/;
296 seek($fh, $offset + $self->_fileobj->{file_offset}, SEEK_SET);
298 #XXX I'm not sure this check will work if autoflush isn't enabled ...
302 read( $fh, $b, SIG_SIZE + $self->{data_size} );
303 my ($sig, $size) = unpack( "A $self->{data_pack}", $b );
306 read( $fh, $buffer, $size);
311 offset => $offset + SIG_SIZE + $self->{data_size},
316 sub _get_dbm_object {
321 if ($item->isa( 'DBM::Deep' )) {
328 my $r = Scalar::Util::reftype( $item ) || '';
329 if ( $r eq 'HASH' ) {
332 my $obj = tied(%$item);
333 if ($obj->isa( 'DBM::Deep' )) {
340 elsif ( $r eq 'ARRAY' ) {
343 my $obj = tied(@$item);
344 if ($obj->isa( 'DBM::Deep' )) {
357 my ($value, $key) = @_;
359 my $is_dbm_deep = eval {
360 local $SIG{'__DIE__'};
361 $value->isa( 'DBM::Deep' );
364 my $len = SIG_SIZE + $self->{data_size}
365 + $self->{data_size} + length( $key );
367 if ( $is_dbm_deep && $value->_fileobj eq $self->_fileobj ) {
368 return $len + $self->{long_size};
371 my $r = Scalar::Util::reftype( $value ) || '';
372 if ( $self->_fileobj->{autobless} ) {
373 # This is for the bit saying whether or not this thing is blessed.
377 unless ( $r eq 'HASH' || $r eq 'ARRAY' ) {
378 if ( defined $value ) {
379 $len += length( $value );
384 $len += $self->{index_size};
386 # if autobless is enabled, must also take into consideration
387 # the class name as it is stored after the key.
388 if ( $self->_fileobj->{autobless} ) {
389 my $c = Scalar::Util::blessed($value);
390 if ( defined $c && !$is_dbm_deep ) {
391 $len += $self->{data_size} + length($c);
400 # Adds one key/value pair to bucket list, given offset, MD5 digest of key,
401 # plain (undigested) key and value.
404 my ($tag, $md5, $plain_key, $value, $deleted, $orig_key) = @_;
409 # This verifies that only supported values will be stored.
411 my $r = Scalar::Util::reftype( $value );
414 last if $r eq 'HASH';
415 last if $r eq 'ARRAY';
418 "Storage of variables of type '$r' is not supported."
425 my $root = $self->_fileobj;
428 my $actual_length = $self->_length_needed( $value, $plain_key );
430 #ACID - This is a mutation. Must only find the exact transaction
431 my ($subloc, $offset, $size,$is_deleted) = $self->_find_in_buckets( $tag, $md5, 1 );
434 if ( $self->_fileobj->transaction_id == 0 ) {
435 @transactions = $self->_fileobj->current_transactions;
438 # $self->_release_space( $size, $subloc );
439 # Updating a known md5
440 #XXX This needs updating to use _release_space
444 if ($actual_length <= $size) {
448 $location = $self->_request_space( $actual_length );
451 $tag->{offset} + $offset
452 + $self->{hash_size} + $root->{file_offset},
455 print( $fh pack($self->{long_pack}, $location ) );
456 print( $fh pack($self->{long_pack}, $actual_length ) );
457 print( $fh pack('n n', $root->transaction_id, $deleted ) );
461 elsif ( defined $offset ) {
462 $location = $self->_request_space( $actual_length );
464 seek( $fh, $tag->{offset} + $offset + $root->{file_offset}, SEEK_SET );
465 print( $fh $md5 . pack($self->{long_pack}, $location ) );
466 print( $fh pack($self->{long_pack}, $actual_length ) );
467 print( $fh pack('n n', $root->transaction_id, $deleted ) );
469 for ( @transactions ) {
470 my $tag2 = $self->load_tag( $tag->{offset} - SIG_SIZE - $self->{data_size} );
471 $self->_fileobj->{transaction_id} = $_;
472 $self->add_bucket( $tag2, $md5, '', '', 1, $orig_key );
473 $self->_fileobj->{transaction_id} = 0;
476 # If bucket didn't fit into list, split into a new index level
477 # split_index() will do the _request_space() call
479 $location = $self->split_index( $md5, $tag );
482 $self->write_value( $location, $plain_key, $value, $orig_key );
489 my ($location, $key, $value, $orig_key) = @_;
494 my $root = $self->_fileobj;
496 my $dbm_deep_obj = _get_dbm_object( $value );
497 if ( $dbm_deep_obj && $dbm_deep_obj->_fileobj ne $self->_fileobj ) {
498 $self->_throw_error( "Cannot cross-reference. Use export() instead" );
501 seek($fh, $location + $root->{file_offset}, SEEK_SET);
504 # Write signature based on content type, set content length and write
507 my $r = Scalar::Util::reftype( $value ) || '';
508 if ( $dbm_deep_obj ) {
509 $self->write_tag( undef, SIG_INTERNAL,pack($self->{long_pack}, $dbm_deep_obj->_base_offset) );
511 elsif ($r eq 'HASH') {
512 if ( !$dbm_deep_obj && tied %{$value} ) {
513 $self->_throw_error( "Cannot store something that is tied" );
515 $self->write_tag( undef, SIG_HASH, chr(0)x$self->{index_size} );
517 elsif ($r eq 'ARRAY') {
518 if ( !$dbm_deep_obj && tied @{$value} ) {
519 $self->_throw_error( "Cannot store something that is tied" );
521 $self->write_tag( undef, SIG_ARRAY, chr(0)x$self->{index_size} );
523 elsif (!defined($value)) {
524 $self->write_tag( undef, SIG_NULL, '' );
527 $self->write_tag( undef, SIG_DATA, $value );
531 # Plain key is stored AFTER value, as keys are typically fetched less often.
533 print( $fh pack($self->{data_pack}, length($key)) . $key );
535 # Internal references don't care about autobless
536 return 1 if $dbm_deep_obj;
539 # If value is blessed, preserve class name
541 if ( $root->{autobless} ) {
542 my $c = Scalar::Util::blessed($value);
543 if ( defined $c && !$dbm_deep_obj ) {
545 print( $fh pack($self->{data_pack}, length($c)) . $c );
553 # Tie the passed in reference so that changes to it are reflected in the
554 # datafile. The use of $location as the base_offset will act as the
555 # the linkage between parent and child.
557 # The overall assignment is a hack around the fact that just tying doesn't
558 # store the values. This may not be the wrong thing to do.
562 tie %$value, 'DBM::Deep', {
563 base_offset => $location,
565 parent => $self->{obj},
566 parent_key => $orig_key,
570 elsif ($r eq 'ARRAY') {
572 tie @$value, 'DBM::Deep', {
573 base_offset => $location,
575 parent => $self->{obj},
576 parent_key => $orig_key,
586 my ($md5, $tag) = @_;
591 my $root = $self->_fileobj;
593 my $loc = $self->_request_space(
594 $self->tag_size( $self->{index_size} ),
597 seek($fh, $tag->{ref_loc} + $root->{file_offset}, SEEK_SET);
598 print( $fh pack($self->{long_pack}, $loc) );
600 my $index_tag = $self->write_tag(
602 chr(0)x$self->{index_size},
605 my $newtag_loc = $self->_request_space(
606 $self->tag_size( $self->{bucket_list_size} ),
609 my $keys = $tag->{content}
610 . $md5 . pack($self->{long_pack}, $newtag_loc)
611 . pack($self->{long_pack}, 0) # size
612 . pack($self->{long_pack}, 0); # transaction ID
616 for (my $i = 0; $i <= $self->{max_buckets}; $i++) {
617 my ($key, $old_subloc, $size) = $self->_get_key_subloc( $keys, $i );
619 die "[INTERNAL ERROR]: No key in split_index()\n" unless $key;
620 die "[INTERNAL ERROR]: No subloc in split_index()\n" unless $old_subloc;
622 my $num = ord(substr($key, $tag->{ch} + 1, 1));
625 seek($fh, $newloc[$num] + $root->{file_offset}, SEEK_SET);
627 read( $fh, $subkeys, $self->{bucket_list_size});
629 # This is looking for the first empty spot
630 my ($subloc, $offset, $size) = $self->_find_in_buckets(
631 { content => $subkeys }, '',
634 seek($fh, $newloc[$num] + $offset + $root->{file_offset}, SEEK_SET);
635 print( $fh $key . pack($self->{long_pack}, $old_subloc) );
640 seek($fh, $index_tag->{offset} + ($num * $self->{long_size}) + $root->{file_offset}, SEEK_SET);
642 my $loc = $self->_request_space(
643 $self->tag_size( $self->{bucket_list_size} ),
646 print( $fh pack($self->{long_pack}, $loc) );
648 my $blist_tag = $self->write_tag(
650 chr(0)x$self->{bucket_list_size},
653 seek($fh, $blist_tag->{offset} + $root->{file_offset}, SEEK_SET);
654 print( $fh $key . pack($self->{long_pack}, $old_subloc) );
656 $newloc[$num] = $blist_tag->{offset};
659 $self->_release_space(
660 $self->tag_size( $self->{bucket_list_size} ),
661 $tag->{offset} - SIG_SIZE - $self->{data_size},
669 my ($subloc, $orig_key) = @_;
676 # Found match -- seek to offset and read signature
679 seek($fh, $subloc + $self->_fileobj->{file_offset}, SEEK_SET);
680 read( $fh, $signature, SIG_SIZE);
683 # If value is a hash or array, return new DBM::Deep object with correct offset
685 if (($signature eq SIG_HASH) || ($signature eq SIG_ARRAY)) {
686 my $new_obj = DBM::Deep->new({
688 base_offset => $subloc,
689 fileobj => $self->_fileobj,
690 parent => $self->{obj},
691 parent_key => $orig_key,
694 if ($new_obj->_fileobj->{autobless}) {
696 # Skip over value and plain key to see if object needs
699 seek($fh, $self->{data_size} + $self->{index_size}, SEEK_CUR);
702 read( $fh, $size, $self->{data_size});
703 $size = unpack($self->{data_pack}, $size);
704 if ($size) { seek($fh, $size, SEEK_CUR); }
707 read( $fh, $bless_bit, 1);
708 if (ord($bless_bit)) {
710 # Yes, object needs to be re-blessed
713 read( $fh, $size, $self->{data_size});
714 $size = unpack($self->{data_pack}, $size);
715 if ($size) { read( $fh, $class_name, $size); }
716 if ($class_name) { $new_obj = bless( $new_obj, $class_name ); }
722 elsif ( $signature eq SIG_INTERNAL ) {
724 read( $fh, $size, $self->{data_size});
725 $size = unpack($self->{data_pack}, $size);
729 read( $fh, $new_loc, $size );
730 $new_loc = unpack( $self->{long_pack}, $new_loc );
732 return $self->read_from_loc( $new_loc, $orig_key );
739 # Otherwise return actual value
741 elsif ( $signature eq SIG_DATA ) {
743 read( $fh, $size, $self->{data_size});
744 $size = unpack($self->{data_pack}, $size);
747 if ($size) { read( $fh, $value, $size); }
752 # Key exists, but content is null
757 sub get_bucket_value {
759 # Fetch single value given tag and MD5 digested key.
762 my ($tag, $md5, $orig_key) = @_;
764 #ACID - This is a read. Can find exact or HEAD
765 my ($subloc, $offset, $size,$is_deleted) = $self->_find_in_buckets( $tag, $md5 );
766 if ( $subloc && !$is_deleted ) {
767 return $self->read_from_loc( $subloc, $orig_key );
774 # Delete single key/value pair given tag and MD5 digested key.
777 my ($tag, $md5, $orig_key) = @_;
781 #ACID - This is a mutation. Must only find the exact transaction
782 my ($subloc, $offset, $size) = $self->_find_in_buckets( $tag, $md5, 1 );
783 #XXX This needs _release_space()
786 seek($fh, $tag->{offset} + $offset + $self->_fileobj->{file_offset}, SEEK_SET);
787 print( $fh substr($tag->{content}, $offset + $self->{bucket_size} ) );
788 print( $fh chr(0) x $self->{bucket_size} );
797 # Check existence of single key given tag and MD5 digested key.
800 my ($tag, $md5) = @_;
802 #ACID - This is a read. Can find exact or HEAD
803 my ($subloc, $offset, $size, $is_deleted) = $self->_find_in_buckets( $tag, $md5 );
804 return ($subloc && !$is_deleted) && 1;
807 sub find_bucket_list {
809 # Locate offset for bucket list, given digested key
812 my ($offset, $md5, $args) = @_;
813 $args = {} unless $args;
818 # Locate offset for bucket list using digest index system
820 my $tag = $self->load_tag( $offset )
821 or $self->_throw_error( "INTERNAL ERROR - Cannot find tag" );
824 while ($tag->{signature} ne SIG_BLIST) {
825 my $num = ord substr($md5, $ch, 1);
827 my $ref_loc = $tag->{offset} + ($num * $self->{long_size});
828 $tag = $self->index_lookup( $tag, $num );
831 return if !$args->{create};
833 my $loc = $self->_request_space(
834 $self->tag_size( $self->{bucket_list_size} ),
838 seek($fh, $ref_loc + $self->_fileobj->{file_offset}, SEEK_SET);
839 print( $fh pack($self->{long_pack}, $loc) );
841 $tag = $self->write_tag(
843 chr(0)x$self->{bucket_list_size},
846 $tag->{ref_loc} = $ref_loc;
853 $tag->{ref_loc} = $ref_loc;
861 # Given index tag, lookup single entry in index and return .
864 my ($tag, $index) = @_;
866 my $location = unpack(
870 $index * $self->{long_size},
875 if (!$location) { return; }
877 return $self->load_tag( $location );
882 # Scan index and recursively step into deeper levels, looking for next key.
885 my ($obj, $offset, $ch, $force_return_next) = @_;
889 my $tag = $self->load_tag( $offset );
893 if ($tag->{signature} ne SIG_BLIST) {
894 my $content = $tag->{content};
895 my $start = $obj->{return_next} ? 0 : ord(substr($obj->{prev_md5}, $ch, 1));
897 for (my $idx = $start; $idx < (2**8); $idx++) {
902 $idx * $self->{long_size},
908 my $result = $self->traverse_index(
909 $obj, $subloc, $ch + 1, $force_return_next,
912 if (defined($result)) { return $result; }
916 $obj->{return_next} = 1;
920 my $keys = $tag->{content};
921 if ($force_return_next) { $obj->{return_next} = 1; }
924 # Iterate through buckets, looking for a key match
926 for (my $i = 0; $i < $self->{max_buckets}; $i++) {
927 my ($key, $subloc) = $self->_get_key_subloc( $keys, $i );
929 # End of bucket list -- return to outer loop
931 $obj->{return_next} = 1;
934 # Located previous key -- return next one found
935 elsif ($key eq $obj->{prev_md5}) {
936 $obj->{return_next} = 1;
939 # Seek to bucket location and skip over signature
940 elsif ($obj->{return_next}) {
941 seek($fh, $subloc + $self->_fileobj->{file_offset}, SEEK_SET);
943 # Skip over value to get to plain key
945 read( $fh, $sig, SIG_SIZE );
948 read( $fh, $size, $self->{data_size});
949 $size = unpack($self->{data_pack}, $size);
950 if ($size) { seek($fh, $size, SEEK_CUR); }
952 # Read in plain key and return as scalar
954 read( $fh, $size, $self->{data_size});
955 $size = unpack($self->{data_pack}, $size);
956 if ($size) { read( $fh, $plain_key, $size); }
962 $obj->{return_next} = 1;
963 } # tag is a bucket list
970 # Locate next key, given digested previous one
975 $obj->{prev_md5} = $_[1] ? $_[1] : undef;
976 $obj->{return_next} = 0;
979 # If the previous key was not specifed, start at the top and
980 # return the first one found.
982 if (!$obj->{prev_md5}) {
983 $obj->{prev_md5} = chr(0) x $self->{hash_size};
984 $obj->{return_next} = 1;
987 return $self->traverse_index( $obj, $obj->_base_offset, 0 );
992 sub _get_key_subloc {
994 my ($keys, $idx) = @_;
996 my ($key, $subloc, $size, $transaction_id, $is_deleted) = unpack(
997 # This is 'a', not 'A'. Please read the pack() documentation for the
998 # difference between the two and why it's important.
999 "a$self->{hash_size} $self->{long_pack}2 n2",
1002 ($idx * $self->{bucket_size}),
1003 $self->{bucket_size},
1007 return ($key, $subloc, $size, $transaction_id, $is_deleted);
1010 sub _find_in_buckets {
1012 my ($tag, $md5, $exact) = @_;
1014 my $trans_id = $self->_fileobj->transaction_id;
1019 for ( my $i = 0; $i < $self->{max_buckets}; $i++ ) {
1020 my ($key, $subloc, $size, $transaction_id, $is_deleted) = $self->_get_key_subloc(
1021 $tag->{content}, $i,
1024 my @rv = ($subloc, $i * $self->{bucket_size}, $size, $is_deleted);
1026 unless ( $subloc ) {
1027 if ( !$exact && @zero and $trans_id ) {
1028 @rv = ($zero[2], $zero[0] * $self->{bucket_size},$zero[3],$is_deleted);
1033 next BUCKET if $key ne $md5;
1035 # Save off the HEAD in case we need it.
1036 @zero = ($i,$key,$subloc,$size,$transaction_id,$is_deleted) if $transaction_id == 0;
1038 next BUCKET if $transaction_id != $trans_id;
1046 sub _request_space {
1050 my $loc = $self->_fileobj->{end};
1051 $self->_fileobj->{end} += $size;
1056 sub _release_space {
1058 my ($size, $loc) = @_;
1064 my $fh = $self->_fh;
1065 seek( $fh, $loc + $self->_fileobj->{file_offset}, SEEK_SET );
1067 . pack($self->{long_pack}, $size )
1068 . pack($self->{long_pack}, $next_loc )
1075 die "DBM::Deep: $_[1]\n";
1081 # This will be added in later, after more refactoring is done. This is an early
1082 # attempt at refactoring on the physical level instead of the virtual level.
1085 my ($spot, $amount, $unpack) = @_;
1089 my $fh = $self->_fh;
1090 seek( $fh, $spot + $self->_fileobj->{file_offset}, SEEK_SET );
1093 my $bytes_read = read( $fh, $buffer, $amount );
1096 $buffer = unpack( $unpack, $buffer );
1100 return ($buffer, $bytes_read);
1109 my ($spot, $data) = @_;
1113 my $fh = $self->_fh;
1114 seek( $fh, $spot, SEEK_SET );
1120 sub get_file_version {
1125 my $fh = $self->_fh;
1127 seek( $fh, 13 + $self->_fileobj->{file_offset}, SEEK_SET );
1129 my $bytes_read = read( $fh, $buffer, 4 );
1130 unless ( $bytes_read == 4 ) {
1131 $self->_throw_error( "Cannot read file version" );
1134 return unpack( 'N', $buffer );
1137 sub write_file_version {
1139 my ($new_version) = @_;
1143 my $fh = $self->_fh;
1145 seek( $fh, 13 + $self->_fileobj->{file_offset}, SEEK_SET );
1146 print( $fh pack( 'N', $new_version ) );