Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I could store a hash in a db file with DB_File. It has been done since now I have a DB_file (*.db) in my hard disk which is about 4 GB, However, I can not re-access it.
$DB_BTREE->{'flags'} = R_DUP ; $x = tie %dictf, "DB_File", "f.db", O_RDWR|O_CREAT, 0666, $DB_BTREE + or die "Cannot open file f.db: $!\n"; my $number = keys %dictf; print "$number\n"; my $number = keys %dictf; print "$number\n"; $key = $value = 0 ; for ($status = $x->seq($key, $value, R_FIRST) ; $status == 0 ; $status = $x->seq($key, $value, R_NEXT) ) { print "$key -> $value\n"; }
and the result is:
0
while I have:
> ls -lh *.db -rw-r--r-- 1 xxxxx xxx 4.2G Dec 3 14:03 f.db
as my database. What is wrong in my code?

Replies are listed 'Best First'.
Re: re-tie the previous db
by flexvault (Monsignor) on Dec 04, 2011 at 13:34 UTC

    My documentation states that DB_File is limited to 4GB(2**32) bytes. Maybe you have passed that limit and the DB is imploding. Hope you have a copy with less than 4GB!

    Good Luck

    "Well done is better than well said." - Benjamin Franklin

      A single key or a single value can be 4GB. A single database can be up to 256 terabytes.

      http://docs.oracle.com/cd/E17076_02/html/gsg/C/databaseLimits.html

Re: re-tie the previous db
by Anonymous Monk on Dec 04, 2011 at 08:12 UTC
      That is probably just sloppy copy / pasting out of an editor which uses "+" as some line continuation mark.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      It is correct, and + is because of the webpage syntax.
      $DB_BTREE->{'flags'} = R_DUP ; $x = tie %dictf, "DB_File", "f.db", O_RDWR|O_CREAT, 0666, $DB_BTREE or + die "Cannot open file f.db: $!\n"; my $number = keys %dictf; print "$number\n"; my $number = keys %dictf; print "$number\n"; $key = $value = 0 ; for ($status = $x->seq($key, $value, R_FIRST) ; $status == 0 ; $status = $x->seq($key, $value, R_NEXT) ) { print "$key -> $value\n"; }
      However, I made it work with :
      $DB_BTREE->{'flags'} = R_DUP ; my $file="f.db"; my $x = tie %h, "DB_File", $file, O_RDWR|O_CREAT, 0666, $DB_BTREE or d +ie "Cannot open file $file: $!\n";

        It is correct, and + is because of the webpage syntax.

        No, in here, the + isn't red, its part of the code (your copy/paste)

        And, in here, the code is still incomplete.

        $ perl junk Can't locate object method "TIEHASH" via package "DB_File" at junk lin +e 2.

        See on posting complete code.

Re: re-tie the previous db
by Anonymous Monk on Dec 04, 2011 at 15:38 UTC

    This works

    #!/usr/bin/perl -- use strict; use warnings; use File::Temp (); use DB_File; Main( @ARGV ); exit( 0 ); sub Main { Shabba(); my $temp = File::Temp->new()->filename; my $btnf = DB_File::BTREEINFO->new; $btnf ->{'flags'} = R_DUP; HitAndQuit( $temp, $btnf , qw[ 1 2 1 4 ] ); HitAndQuit( $temp, $btnf , qw[ 2 6 2 8 ] ); HitAndQuit( $temp, $btnf , qw[ b o b s ] ); unlink $temp or die "unlink $temp: $!"; } sub HitAndQuit { my( $temp, $BTREEINFO , @rest ) = @_; #~ my $tree = tie my(%foo), 'DB_File', $temp, O_RDWR|O_CREAT, 0666, $BTREEINFO or die "Cannot tie ($temp): $!"; for( my $ix = 0; $ix < @rest ; $ix+=2 ){ $foo{ $rest[ $ix] } = $rest[ $ix+1 ]; } DDUP( tied %foo ); #~ untie $tree; } sub DDUP { my ( $x ) = @_; for ( my $st = $x->seq( my( $key, $value), R_FIRST ) ; $st == 0 ; $st = $x->seq( $key, $value, R_NEXT ) ) { print "{ $key }{ $value } "; } print "\n"; } sub Shabba { print <<"SHABBA"; ### $] #~ \$DB_File::db_ver $DB_File::db_ver #~ \$DB_File::db_version $DB_File::db_version #~ \$DB_File::VERSION $DB_File::VERSION ######## SHABBA } __END__ ### 5.014001 #~ $DB_File::db_ver 5.001025 #~ $DB_File::db_version 5.1 #~ $DB_File::VERSION 1.824 ######## { 1 }{ 2 } { 1 }{ 4 } { 1 }{ 2 } { 1 }{ 4 } { 2 }{ 6 } { 2 }{ 8 } { 1 }{ 2 } { 1 }{ 4 } { 2 }{ 6 } { 2 }{ 8 } { b }{ o } { b }{ s }