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