clean up offset calculation (now works with ISIS databases from isis.dll),
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 30 Dec 2004 17:16:34 +0000 (17:16 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 30 Dec 2004 17:16:34 +0000 (17:16 +0000)
don't re-fetch MFN if in memory allready,
dump debugging messages to STDERR

git-svn-id: file:///home/dpavlin/svn/Biblio-Isis/trunk@16 4670fa4d-42ec-0310-ab5b-a66af6943492

IsisDB.pm

index 87709db..dd38c82 100644 (file)
--- a/IsisDB.pm
+++ b/IsisDB.pm
@@ -7,7 +7,7 @@ use Data::Dumper;
 BEGIN {
        use Exporter ();
        use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
-       $VERSION     = 0.05;
+       $VERSION     = 0.06;
        @ISA         = qw (Exporter);
        #Give a hoot don't pollute, do not export more than needed by default
        @EXPORT      = qw ();
@@ -210,7 +210,7 @@ sub new {
                my $buff = shift || return;
                my @arr = unpack("ssssssllls", $buff);
 
-               print "unpack_cnt: ",join(" ",@arr),"\n" if ($self->{'debug'});
+               print STDERR "unpack_cnt: ",join(" ",@arr),"\n" if ($self->{'debug'});
 
                my $IDTYPE = shift @arr;
                foreach (@flds) {
@@ -227,7 +227,7 @@ sub new {
 
        close(fileCNT);
 
-       print Dumper($self),"\n" if ($self->{debug});
+       print STDERR Dumper($self),"\n" if ($self->{debug});
 
        # open files for later
        open($self->{'fileXRF'}, $self->{isisdb}.".XRF") || croak "can't open '$self->{isisdb}.XRF': $!";
@@ -258,12 +258,16 @@ sub fetch {
 
        my $mfn = shift || croak "fetch needs MFN as argument!";
 
-       print "fetch: $mfn\n" if ($self->{debug});
+       # is mfn allready in memory?
+       my $old_mfn = $self->{'current_mfn'} || -1;
+       return if ($mfn == $old_mfn);
+
+       print STDERR "## fetch: $mfn\n" if ($self->{debug});
 
        # XXX check this?
        my $mfnpos=($mfn+int(($mfn-1)/127))*4;
 
-       print "seeking to $mfnpos in file '$self->{isisdb}.XRF'\n" if ($self->{debug});
+       print STDERR "## seeking to $mfnpos in file '$self->{isisdb}.XRF'\n" if ($self->{debug});
        seek($self->{'fileXRF'},$mfnpos,0);
 
        my $buff;
@@ -275,32 +279,26 @@ sub fetch {
        my $XRFMFB = int($pointer/2048);
        my $XRFMFP = $pointer - ($XRFMFB*2048);
 
-       print "XRFMFB: $XRFMFB XRFMFP: $XRFMFP\n" if ($self->{debug});
 
-       # XXX fix this to be more readable!!
-       # e.g. (XRFMFB - 1) * 512 + XRFMFP
+       # (XRFMFB - 1) * 512 + XRFMFP
+       # why do i have to do XRFMFP % 1024 ?
 
-       my $offset = $pointer;
-       my $offset2=int($offset/2048)-1;
-       my $offset22=int($offset/4096);
-       my $offset3=$offset-($offset22*4096);
-       if ($offset3>512) {
-               $offset3=$offset3-2048;
-       }
-       my $offset4=($offset2*512)+$offset3;
+       my $blk_off = (($XRFMFB - 1) * 512) + ($XRFMFP % 1024);
 
-       print "$offset - $offset2 - $offset3 - $offset4\n" if ($self->{debug});
+       print STDERR "## pointer: $pointer XRFMFB: $XRFMFB XRFMFP: $XRFMFP offset: $blk_off\n" if ($self->{'debug'});
 
        # Get Record Information
 
-       seek($self->{'fileMST'},$offset4,0);
+       seek($self->{'fileMST'},$blk_off,0);
 
        read($self->{'fileMST'}, $buff, 4);
        my $value=unpack("l",$buff);
 
+       print STDERR "## offset for rowid $value is $blk_off (blk $XRFMFB off $XRFMFP)\n" if ($self->{debug});
+
        if ($value!=$mfn) {
-print ("Error: The MFN:".$mfn." is not found in MST(".$value.")");    
-               return -1;      # XXX deleted record?
+               carp "Error: MFN ".$mfn." not found in MST(".$value.")";    
+               #return;                # XXX deleted record?
        }
 
 #      $MFRL=$self->Read16($fileMST);
@@ -314,16 +312,19 @@ print ("Error: The MFN:".$mfn." is not found in MST(".$value.")");
 
        my ($MFRL,$MFBWB,$MFBWP,$BASE,$NVF,$STATUS) = unpack("slssss", $buff);
 
-       print "MFRL: $MFRL MFBWB: $MFBWB MFBWP: $MFBWP BASE: $BASE NVF: $NVF STATUS: $STATUS\n" if ($self->{debug});
+       print STDERR "## MFRL: $MFRL MFBWB: $MFBWB MFBWP: $MFBWP BASE: $BASE NVF: $NVF STATUS: $STATUS\n" if ($self->{debug});
 
        # delete old record
        delete $self->{record};
 
+       ## FIXME this is a bug
        if (! $self->{'include_deleted'} && $MFRL < 0) {
                print "## logically deleted record $mfn, skipping...\n" if ($self->{debug});
                return;
        }
 
+       warn "BASE is not 18+6*NVF" unless ($BASE == 18 + 6 * $NVF);
+
        # Get Directory Format
 
        my @FieldPOS;
@@ -332,7 +333,7 @@ print ("Error: The MFN:".$mfn." is not found in MST(".$value.")");
 
        read($self->{'fileMST'}, $buff, 6 * $NVF);
 
-       my $fld_len = 0;
+       my $rec_len = 0;
 
        for (my $i = 0 ; $i < $NVF ; $i++) {
 
@@ -342,7 +343,7 @@ print ("Error: The MFN:".$mfn." is not found in MST(".$value.")");
 
                my ($TAG,$POS,$LEN) = unpack("sss", substr($buff,$i * 6, 6));
 
-               print "TAG: $TAG POS: $POS LEN: $LEN\n" if ($self->{debug});
+               print STDERR "## TAG: $TAG POS: $POS LEN: $LEN\n" if ($self->{debug});
 
                # The TAG does not exists in .FDT so we set it to 0.
                #
@@ -358,12 +359,14 @@ print ("Error: The MFN:".$mfn." is not found in MST(".$value.")");
                push @FieldPOS,$POS;
                push @FieldLEN,$LEN;
 
-               $fld_len += $LEN;
+               $rec_len += $LEN;
        }
 
        # Get Variable Fields
 
-       read($self->{'fileMST'},$buff,$fld_len);
+       read($self->{'fileMST'},$buff,$rec_len);
+
+       print STDERR "## rec_len: $rec_len poc: ",tell($self->{'fileMST'})."\n" if ($self->{debug});
 
        for (my $i = 0 ; $i < $NVF ; $i++) {
                # skip zero-sized fields
@@ -373,6 +376,8 @@ print ("Error: The MFN:".$mfn." is not found in MST(".$value.")");
        }
        close(fileMST);
 
+       $self->{'current_mfn'} = $mfn;
+
        print Dumper($self),"\n" if ($self->{debug});
 
        return $self->{'record'};
@@ -467,7 +472,7 @@ sub to_hash {
        my $mfn = shift || confess "need mfn!";
 
        # init record to include MFN as field 000
-       my $rec = { '000' => $mfn };
+       my $rec = { '000' => [ $mfn ] };
 
        my $row = $self->fetch($mfn);