[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Problem adding records to DB_File database



Folks,

I found a problem with DB_File.pm.  I am not sure if it is something that I
am doing incorrectly or their is a bug in DB_File.pm.  Please take a look
and see if the problem can be reproduced.  When I run the scripts below on
UNIX with Perl 5.003, the problem does not exist.

Problem Description: I create a database file initially with 100 records.
Next, I run the script again, this time I insert an additional 100 records.
When I fetch all of the keys from the database, there are only 131 keys in
the database.  Next, I print all of the keys from the database into a text
file.  When I look at the text file, some of the keys are missing.

The keys are numeric, created sequentially starting at 0 and ending at 99.
On the second insertion, the keys start at 100 and end at 199.  The values
are a character string that is 2048 characters long.

When you look at the list of keys, there are gaps in the sequential list.

Details about my setup: Powerbook 540c, 20MB RAM, MacOS 7.5.5, big version
of MacPerl 5.1.0r2.

Here is the script that creates the database.  To insert the additional 100
records, alter the 'for' loop values.

================================
#!/usr/local/bin/perl5 -w

use Carp;
use DB_File;
use Fcntl;

use strict;

my $i;
my $value = 'a';

# Create a large (2KB) value for each record

for ($i = 0; $i < 2047; $i++) {
    $value = $value . 'a';
}

my %myFile;

tie (%myFile, "DB_File", 'testDBFile', O_RDWR|O_CREAT, 0755, $DB_HASH)
        || carp "Error creating/opening testDBFile: $!\n";

# Populate the Database, initially 0 to 99 keys
# For additional records, change the values to:
#    for ($i = 100; $i < 200; $i++) {

for ($i = 0; $i < 100; $i++) {
    $myFile{$i} = $value;
}

untie %myFile;
======================================

To check the number of records and print a list of the keys, run this script:

======================================
#!/usr/local/bin/perl5 -w

use Carp;
use DB_File;
use Fcntl;

use strict;

my %myFile;

tie (%myFile, "DB_File", 'testDBFile', O_RDWR, 0755, $DB_HASH)
        || carp "Error creating/opening testDBFile: $!\n";

# Get list of keys from database and print number of keys in database

my @keys = keys %myFile;

print $#keys + 1, " DB Keys\n";

untie %myFile;

# Sort key list and write to a file

my @sortKeys;
@sortKeys = sort numeric @keys;

open (KEYLIST, '>KeyList');

my $i;

foreach $i (@sortKeys) {
    print KEYLIST  $i, "\n";
}

close KEYLIST;

sub numeric { $a <=> $b; }
======================================

Please let me know if it is something that I am doing incorrectly.   If
not, let me know of a fix or work around.

Thanks,
Keith






___________________________________________________________________
   "Providing Interactive Solutions for Business and Education"
Keith D. Fetterman                           Phone:  (206) 842-7164
Good Northwest Corporation                   FAX:    (206) 842-7291
321 High School Rd. N.E.; Suite 325          e-mail: kdf@gnw.com
Bainbridge Island, WA 98110                  http://www.gnw.com/