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: Sat Apr 27 18:06:47 2002 |
8 |
# Language : CPerl |
9 |
# Update Count : 128 |
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 = "1.801"; # 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 |
unless ($self->{file} = $parm{file}) { |
30 |
require Carp; |
31 |
Carp::croak("No file specified"); |
32 |
} |
33 |
unless ($self->{name} = $parm{name}) { |
34 |
require Carp; |
35 |
Carp::croak("No name specified"); |
36 |
} |
37 |
unless ($self->{attr} = $parm{attr}) { |
38 |
require Carp; |
39 |
Carp::croak("No attributes specified"); |
40 |
} |
41 |
bless $self, ref($type) || $type; |
42 |
} |
43 |
|
44 |
sub drop { |
45 |
my $self = shift; |
46 |
if ((caller)[0] eq 'WAIT::Table') { # Table knows about this |
47 |
my $file = $self->{file}; |
48 |
! (!-e $file or unlink $file); |
49 |
} else { # notify our database |
50 |
require Carp; |
51 |
Carp::croak(ref($self)."::drop called directly"); |
52 |
} |
53 |
} |
54 |
|
55 |
sub open { |
56 |
my $self = shift; |
57 |
my $file = $self->{file}; |
58 |
|
59 |
if ($self->{dbh}) { |
60 |
$self->{dbh}; |
61 |
} else { |
62 |
my $dbmode = ($self->{mode} & O_CREAT) ? DB_CREATE : 0; |
63 |
$self->{dbh} = tie(%{$self->{db}}, 'BerkeleyDB::Btree', |
64 |
-Filename => $self->{file}, |
65 |
-Subname => 'records', |
66 |
-Flags => $dbmode, |
67 |
-Mode => 0664); |
68 |
} |
69 |
} |
70 |
|
71 |
sub insert { |
72 |
my $self = shift; |
73 |
my $key = shift; |
74 |
my %parm = @_; |
75 |
|
76 |
defined $self->{db} or $self->open; |
77 |
|
78 |
my $tuple = join($;, map($parm{$_}, @{$self->{attr}})); |
79 |
|
80 |
if (exists $self->{db}->{$tuple}) { |
81 |
# duplicate entry |
82 |
return undef; |
83 |
} |
84 |
print STDERR "$tuple => $key\n"; |
85 |
$self->{db}->{$tuple} = $key; |
86 |
} |
87 |
|
88 |
sub have { |
89 |
my $self = shift; |
90 |
my %parm = @_; |
91 |
|
92 |
defined $self->{db} or $self->open; |
93 |
|
94 |
my $tuple = join($;, map($parm{$_}, @{$self->{attr}})); |
95 |
|
96 |
print STDERR "$tuple <= ", $self->{db}->{$tuple}, "\n"; |
97 |
$self->{db}->{$tuple}; |
98 |
} |
99 |
|
100 |
sub fetch { |
101 |
my $self = shift; |
102 |
my %parm = @_; |
103 |
my @keys = @{$self->{attr}->[0]}; |
104 |
|
105 |
defined $self->{db} or $self->open; |
106 |
|
107 |
my $key = join($;, map($parm{$_}, @keys)); |
108 |
$self->{db}->{$key}; |
109 |
} |
110 |
|
111 |
sub delete { |
112 |
my $self = shift; |
113 |
my $key = shift; |
114 |
my %parm = @_; |
115 |
|
116 |
defined $self->{db} or $self->open; |
117 |
|
118 |
my $tuple = join($;, map($parm{$_}||"", @{$self->{attr}})); |
119 |
|
120 |
delete $self->{db}->{$tuple}; |
121 |
} |
122 |
|
123 |
sub sync { |
124 |
my $self = shift; |
125 |
$self->{dbh}->db_sync if $self->{dbh}; |
126 |
} |
127 |
|
128 |
sub close { |
129 |
my $self = shift; |
130 |
|
131 |
delete $self->{scans} if defined $self->{scans}; |
132 |
|
133 |
if ($self->{dbh}) { |
134 |
delete $self->{dbh}; |
135 |
delete $self->{db}; |
136 |
#untie %{$self->{db}}; |
137 |
} |
138 |
} |
139 |
|
140 |
#sub DESTROY { $_[0]->close } |
141 |
|
142 |
sub open_scan { |
143 |
my $self = shift; |
144 |
my $code = shift; |
145 |
|
146 |
$self->{dbh} or $self->open; |
147 |
new WAIT::IndexScan $self, $code; |
148 |
} |
149 |
|
150 |
1; |