1 |
# -*- Mode: Cperl -*- |
2 |
# Index.pm -- |
3 |
# ITIID : $ITI$ $Header $__Header$ |
4 |
# Author : Ulrich Pfeifer |
5 |
# Created On : Thu Aug 8 13:05:10 1996 |
6 |
# Last Modified By: Ulrich Pfeifer |
7 |
# Last Modified On: Sun Nov 22 18:44:43 1998 |
8 |
# Language : CPerl |
9 |
# Update Count : 107 |
10 |
# Status : Unknown, Use with caution! |
11 |
# |
12 |
# Copyright (c) 1996-1997, Ulrich Pfeifer |
13 |
# |
14 |
|
15 |
package WAIT::Index; |
16 |
use WAIT::IndexScan; |
17 |
use strict; |
18 |
use DB_File; |
19 |
use Fcntl; |
20 |
|
21 |
sub new { |
22 |
my $type = shift; |
23 |
my %parm = @_; |
24 |
my $self = {}; |
25 |
|
26 |
unless ($self->{file} = $parm{file}) { |
27 |
require Carp; |
28 |
Carp::croak("No file specified"); |
29 |
} |
30 |
unless ($self->{attr} = $parm{attr}) { |
31 |
require Carp; |
32 |
Carp::croak("No attributes specified"); |
33 |
} |
34 |
bless $self, ref($type) || $type; |
35 |
} |
36 |
|
37 |
sub drop { |
38 |
my $self = shift; |
39 |
if ((caller)[0] eq 'WAIT::Table') { # Table knows about this |
40 |
my $file = $self->{file}; |
41 |
! (!-e $file or unlink $file); |
42 |
} else { # notify our database |
43 |
require Carp; |
44 |
Carp::croak(ref($self)."::drop called directly"); |
45 |
} |
46 |
} |
47 |
|
48 |
sub open { |
49 |
my $self = shift; |
50 |
my $file = $self->{file}; |
51 |
|
52 |
if (exists $self->{dbh}) { |
53 |
$self->{dbh}; |
54 |
} else { |
55 |
$self->{dbh} = tie(%{$self->{db}}, 'DB_File', $file, |
56 |
$self->{mode}, 0664, $DB_BTREE); |
57 |
} |
58 |
} |
59 |
|
60 |
sub insert { |
61 |
my $self = shift; |
62 |
my $key = shift; |
63 |
my %parm = @_; |
64 |
|
65 |
defined $self->{db} or $self->open; |
66 |
|
67 |
my $tuple = join($;, map($parm{$_}, @{$self->{attr}})); |
68 |
|
69 |
if (exists $self->{db}->{$tuple}) { |
70 |
# duplicate entry |
71 |
return undef; |
72 |
} |
73 |
$self->{db}->{$tuple} = $key; |
74 |
} |
75 |
|
76 |
sub have { |
77 |
my $self = shift; |
78 |
my %parm = @_; |
79 |
|
80 |
defined $self->{db} or $self->open; |
81 |
|
82 |
my $tuple = join($;, map($parm{$_}, @{$self->{attr}})); |
83 |
|
84 |
exists $self->{db}->{$tuple} && $self->{db}->{$tuple}; |
85 |
} |
86 |
|
87 |
sub fetch { |
88 |
my $self = shift; |
89 |
my %parm = @_; |
90 |
my @keys = @{$self->{attr}->[0]}; |
91 |
|
92 |
defined $self->{db} or $self->open; |
93 |
|
94 |
my $key = join($;, map($parm{$_}, @keys)); |
95 |
$self->{db}->{$key}; |
96 |
} |
97 |
|
98 |
sub delete { |
99 |
my $self = shift; |
100 |
my $key = shift; |
101 |
my %parm = @_; |
102 |
|
103 |
defined $self->{db} or $self->open; |
104 |
|
105 |
my $tuple = join($;, map($parm{$_}, @{$self->{attr}})); |
106 |
|
107 |
delete $self->{db}->{$tuple}; |
108 |
} |
109 |
|
110 |
sub sync { |
111 |
my $self = shift; |
112 |
$self->{dbh}->sync if $self->{dbh}; |
113 |
} |
114 |
|
115 |
sub close { |
116 |
my $self = shift; |
117 |
|
118 |
delete $self->{scans} if defined $self->{scans}; |
119 |
|
120 |
if ($self->{dbh}) { |
121 |
delete $self->{dbh}; |
122 |
untie %{$self->{db}}; |
123 |
delete $self->{db}; |
124 |
} |
125 |
} |
126 |
|
127 |
#sub DESTROY { $_[0]->close } |
128 |
|
129 |
sub open_scan { |
130 |
my $self = shift; |
131 |
my $code = shift; |
132 |
|
133 |
$self->{dbh} or $self->open; |
134 |
new WAIT::IndexScan $self, $code; |
135 |
} |
136 |
|
137 |
1; |