4 |
# Author : Ulrich Pfeifer |
# Author : Ulrich Pfeifer |
5 |
# Created On : Mon Aug 12 14:05:14 1996 |
# Created On : Mon Aug 12 14:05:14 1996 |
6 |
# Last Modified By: Ulrich Pfeifer |
# Last Modified By: Ulrich Pfeifer |
7 |
# Last Modified On: Sun Nov 22 18:44:43 1998 |
# Last Modified On: Sat Apr 27 17:46:53 2002 |
8 |
# Language : CPerl |
# Language : CPerl |
9 |
# Update Count : 65 |
# Update Count : 92 |
10 |
# Status : Unknown, Use with caution! |
# Status : Unknown, Use with caution! |
11 |
# |
# |
12 |
# Copyright (c) 1996-1997, Ulrich Pfeifer |
# Copyright (c) 1996-1997, Ulrich Pfeifer |
15 |
package WAIT::IndexScan; |
package WAIT::IndexScan; |
16 |
|
|
17 |
use strict; |
use strict; |
18 |
use DB_File; |
use BerkeleyDB; |
19 |
use Fcntl; |
use Fcntl; |
20 |
|
|
21 |
sub new { |
sub new { |
22 |
my $type = shift; |
my $type = shift; |
23 |
my $index = shift; |
my $index = shift; |
24 |
my $code = shift; |
my $code = shift; |
25 |
my ($first, $tid) = ('', ''); |
my $cursor; |
26 |
|
|
27 |
# find the first key |
# find the first key |
28 |
if ($index->{dbh}->seq($first, $tid, R_FIRST)) { |
unless ($cursor = $index->{dbh}->db_cursor()) { |
29 |
require Carp; |
require Carp; |
30 |
Carp::croak("Could not open scan"); |
Carp::croak("Could not open scan"); |
31 |
} |
} |
32 |
# Not sure about this. R_FIRST sets $tid to no-of-records? |
# Not sure about this. DB_FIRST sets $tid to no-of-records? |
33 |
# $index->{dbh}->seq($first, $tid, R_NEXT); |
# $index->{dbh}->seq($first, $tid, DB_NEXT); |
34 |
# register to avoid unnecessary position calls |
# register to avoid unnecessary position calls |
|
$index->{scans}++; |
|
35 |
|
|
36 |
bless {Index => $index, code => $code, |
bless {code => $code, cursor => $cursor, Index => $index}, |
37 |
nextk => $first, tid => $tid}, $type or ref($type); |
$type or ref($type); |
38 |
} |
} |
39 |
|
|
40 |
sub next { |
sub next { |
41 |
my $self = shift; |
my $self = shift; |
42 |
my $dbh = $self->{Index}->{dbh}; |
my $cursor = $self->{cursor}; |
43 |
my ($key, $tid, $ntid); |
my ($key, $tid) = ('', ''); |
44 |
|
|
45 |
if (defined $self->{nextk}) { |
$cursor->c_get($key, $tid, DB_NEXT) == 0 or return; |
46 |
unless ($dbh){ |
|
47 |
require Carp; |
my @tuple = split /$;/, $key; |
48 |
Carp::croak("Cannot scan closed index"); |
|
49 |
} |
my %tuple = (_id => $tid); |
50 |
$key = $self->{nextk}; |
for (@{$self->{Index}->{attr}}) { |
51 |
|
$tuple{$_} = shift @tuple; |
52 |
if ($self->{Index}->{scans} > 1) { |
} |
53 |
# Another scan is open. Reset the cursor |
|
54 |
$dbh->seq($key, $tid, R_CURSOR); |
|
55 |
} else { |
if ($self->{code}) { # test condition |
56 |
$tid = $self->{tid}; |
&{$self->{code}}(\%tuple)? %tuple : $self->next; |
|
} |
|
|
if ($dbh->seq($self->{nextk}, $self->{tid}, R_NEXT)) { |
|
|
# current tuple is last one |
|
|
delete $self->{nextk}; |
|
|
} |
|
|
|
|
|
my @tuple = split /$;/, $key; |
|
|
my %tuple = (_id => $tid); |
|
|
for (@{$self->{Index}->{attr}}) { |
|
|
$tuple{$_} = shift @tuple; |
|
|
} |
|
|
|
|
|
if ($self->{code}) { # test condition |
|
|
&{$self->{code}}(\%tuple)? %tuple : $self->next; |
|
|
} else { |
|
|
%tuple; |
|
|
} |
|
57 |
} else { |
} else { |
58 |
return; |
%tuple; |
59 |
} |
} |
60 |
} |
} |
61 |
|
|
62 |
sub close { undef $_[0]} # force DESTROY |
sub close { |
63 |
sub DESTROY { |
my $self = shift; |
64 |
shift->{Index}->{scans}--; |
delete $self->{cursor}; |
65 |
} |
} |
66 |
|
|
67 |
1; |
1; |