fix offset to skip records just like SQL databases do, and not position us on it
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 20 Sep 2009 21:38:15 +0000 (21:38 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 20 Sep 2009 21:38:15 +0000 (21:38 +0000)
git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@1304 07558da8-63fa-0310-ba24-9fe276d99e06

lib/WebPAC/Input.pm
t/2-input.t

index 94afb8b..ecc6773 100644 (file)
@@ -137,7 +137,7 @@ This function will read whole database in memory and produce lookups.
 
 By default, C<input_encoding> is assumed to be C<cp852>.
 
-C<offset> is optional parametar to position at some offset before reading from database.
+C<offset> is optional parametar to skip records at beginning.
 
 C<limit> is optional parametar to read just C<limit> records from database
 
@@ -264,10 +264,10 @@ sub open {
        my $to_rec = $size;
 
        if (my $s = $self->{offset}) {
-               $log->debug("skipping to MFN $s");
-               $from_rec = $s;
+               $log->debug("offset $s records");
+               $from_rec = $s + 1;
        } else {
-               $self->{offset} = $from_rec;
+               $self->{offset} = $from_rec - 1;
        }
 
        if ($self->{limit}) {
@@ -277,7 +277,7 @@ sub open {
        }
 
        # store size for later
-       $self->{size} = ($to_rec - $from_rec) ? ($to_rec - $from_rec + 1) : 0;
+       $self->{size} = $to_rec - $from_rec + 1;
 
        my $strict_encoding = $arg->{strict_encoding} || $self->{strict_encoding}; ## FIXME should be 1 really
 
@@ -415,7 +415,7 @@ sub fetch {
        $log->logconfess("it seems that you didn't load database!") unless ($self->{pos});
 
        if ($self->{pos} == -1) {
-               $self->{pos} = $self->{offset};
+               $self->{pos} = $self->{offset} + 1;
        } else {
                $self->{pos}++;
        }
index 53ca2eb..052b586 100755 (executable)
@@ -94,14 +94,14 @@ sub test_start_limit($$$$) {
        diag "offset $s, limit: $l, expected: $e";
 
        ok($s = $input->open( path => "$abs_path/winisis/BIBL", offset => $s, limit => $l, debug => $debug ), "open winisis");
-       cmp_ok($s, '==', $size, "db size from open = $size");
+       cmp_ok($s, '==', $size, "db size");
        cmp_ok($input->size, '==', $e, "input->size = $e");
 }
 
 test_start_limit($input, 1, 3, 3);
 test_start_limit($input, $size, 3, 0);
-test_start_limit($input, 3, $size, $size - 2);
-test_start_limit($input, 1, $size + 2, $size);
+test_start_limit($input, 3, $size, $size - 3);
+test_start_limit($input, 1, $size, $size - 1);
 
 ok(my $s = $input->stats, "$module stats");
 diag "stats:\n$s" if ($debug);