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 BerkeleyDB; |
19 |
use Fcntl; |
20 |
use vars qw($VERSION); |
21 |
|
22 |
$VERSION = "2.000"; # Table.pm tests if we are loaded by checking $VERSION |
23 |
|
24 |
sub new { |
25 |
my $type = shift; |
26 |
my %parm = @_; |
27 |
my $self = {}; |
28 |
|
29 |
for my $x (qw(file attr env subname maindbfile tablename)) { |
30 |
unless ($self->{$x} = $parm{$x}) { |
31 |
require Carp; |
32 |
Carp::croak("No $x specified"); |
33 |
} |
34 |
} |
35 |
bless $self, ref($type) || $type; |
36 |
} |
37 |
|
38 |
for my $accessor (qw(maindbfile tablename subname)) { |
39 |
no strict 'refs'; |
40 |
*{$accessor} = sub { |
41 |
my($self) = @_; |
42 |
return $self->{$accessor} if $self->{$accessor}; |
43 |
require Carp; |
44 |
Carp::confess("accessor $accessor not there"); |
45 |
} |
46 |
} |
47 |
|
48 |
sub drop { |
49 |
my $self = shift; |
50 |
if ((caller)[0] eq 'WAIT::Table') { # Table knows about this |
51 |
my $file = $self->{file}; |
52 |
! (!-e $file or unlink $file); |
53 |
} else { # notify our database |
54 |
require Carp; |
55 |
Carp::croak(ref($self)."::drop called directly"); |
56 |
} |
57 |
} |
58 |
|
59 |
sub open { |
60 |
my $self = shift; |
61 |
my $file = $self->{file}; |
62 |
|
63 |
if (exists $self->{dbh}) { |
64 |
$self->{dbh}; |
65 |
} else { |
66 |
my $flags; |
67 |
if ($self->{mode} & O_RDWR) { |
68 |
$flags = DB_CREATE; # | DB_INIT_MPOOL | DB_INIT_CDB; |
69 |
# warn "Flags on index $file set to 'writing'"; |
70 |
} else { |
71 |
$flags = DB_RDONLY; |
72 |
# warn "Flags on index $file set to 'readonly'"; |
73 |
} |
74 |
$self->{dbh} = tie(%{$self->{db}}, 'BerkeleyDB::Btree', |
75 |
# Filename => $file, |
76 |
Filename => $self->maindbfile, |
77 |
$self->{env} ? (Env => $self->{env}) : (), |
78 |
Subname => join("/",$self->tablename,$self->subname), |
79 |
Mode => 0664, |
80 |
Flags => $flags, |
81 |
$WAIT::Database::Cachesize?(Cachesize => $WAIT::Database::Cachesize):(), |
82 |
$WAIT::Database::Pagesize?(Pagesize => $WAIT::Database::Pagesize):(), |
83 |
) or die $BerkeleyDB::Error; |
84 |
} |
85 |
} |
86 |
|
87 |
sub insert { |
88 |
my $self = shift; |
89 |
my $key = shift; |
90 |
my %parm = @_; |
91 |
|
92 |
defined $self->{db} or $self->open; |
93 |
|
94 |
my $tuple = join($;, map($parm{$_}, @{$self->{attr}})); |
95 |
|
96 |
if (exists $self->{db}->{$tuple}) { |
97 |
# duplicate entry |
98 |
return undef; |
99 |
} |
100 |
$self->{db}->{$tuple} = $key; |
101 |
} |
102 |
|
103 |
sub have { |
104 |
my $self = shift; |
105 |
my %parm = @_; |
106 |
|
107 |
defined $self->{db} or $self->open; |
108 |
|
109 |
my $tuple = join($;, map($parm{$_}, @{$self->{attr}})); |
110 |
|
111 |
$self->{db}->{$tuple}; |
112 |
} |
113 |
|
114 |
sub fetch { |
115 |
my $self = shift; |
116 |
my %parm = @_; |
117 |
my @keys = @{$self->{attr}->[0]}; |
118 |
|
119 |
defined $self->{db} or $self->open; |
120 |
|
121 |
my $key = join($;, map($parm{$_}, @keys)); |
122 |
$self->{db}->{$key}; |
123 |
} |
124 |
|
125 |
sub delete { |
126 |
my $self = shift; |
127 |
my $key = shift; |
128 |
my %parm = @_; |
129 |
|
130 |
defined $self->{db} or $self->open; |
131 |
|
132 |
my $tuple = join($;, map($parm{$_}||"", @{$self->{attr}})); |
133 |
|
134 |
delete $self->{db}->{$tuple}; |
135 |
} |
136 |
|
137 |
sub sync { |
138 |
my $self = shift; |
139 |
$self->{dbh}->sync if $self->{dbh}; |
140 |
} |
141 |
|
142 |
sub close { |
143 |
my $self = shift; |
144 |
|
145 |
delete $self->{scans} if defined $self->{scans}; |
146 |
|
147 |
delete $self->{env}; |
148 |
if ($self->{dbh}) { |
149 |
delete $self->{dbh}; |
150 |
untie %{$self->{db}}; |
151 |
for my $att (qw(db file maindbfile)) { |
152 |
delete $self->{$att}; |
153 |
} |
154 |
} |
155 |
} |
156 |
|
157 |
#sub DESTROY { $_[0]->close } |
158 |
|
159 |
sub open_scan { |
160 |
my $self = shift; |
161 |
my $code = shift; |
162 |
|
163 |
$self->{dbh} or $self->open; |
164 |
new WAIT::IndexScan $self, $code; |
165 |
} |
166 |
|
167 |
1; |