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