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: Sat Apr 27 17:46:53 2002 |
8 |
# Language : CPerl |
9 |
# Update Count : 92 |
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 BerkeleyDB; |
19 |
use Fcntl; |
20 |
|
21 |
sub new { |
22 |
my $type = shift; |
23 |
my $index = shift; |
24 |
my $code = shift; |
25 |
my $cursor; |
26 |
|
27 |
# find the first key |
28 |
unless ($cursor = $index->{dbh}->db_cursor()) { |
29 |
require Carp; |
30 |
Carp::croak("Could not open scan"); |
31 |
} |
32 |
# Not sure about this. DB_FIRST sets $tid to no-of-records? |
33 |
# $index->{dbh}->seq($first, $tid, DB_NEXT); |
34 |
# register to avoid unnecessary position calls |
35 |
|
36 |
bless {code => $code, cursor => $cursor, Index => $index}, |
37 |
$type or ref($type); |
38 |
} |
39 |
|
40 |
sub next { |
41 |
my $self = shift; |
42 |
my $cursor = $self->{cursor}; |
43 |
my ($key, $tid) = ('', ''); |
44 |
|
45 |
$cursor->c_get($key, $tid, DB_NEXT) == 0 or return; |
46 |
|
47 |
my @tuple = split /$;/, $key; |
48 |
|
49 |
my %tuple = (_id => $tid); |
50 |
for (@{$self->{Index}->{attr}}) { |
51 |
$tuple{$_} = shift @tuple; |
52 |
} |
53 |
|
54 |
|
55 |
if ($self->{code}) { # test condition |
56 |
&{$self->{code}}(\%tuple)? %tuple : $self->next; |
57 |
} else { |
58 |
%tuple; |
59 |
} |
60 |
} |
61 |
|
62 |
sub close { |
63 |
my $self = shift; |
64 |
delete $self->{cursor}; |
65 |
} |
66 |
|
67 |
1; |