/[wait]/trunk/lib/WAIT/Index.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/WAIT/Index.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 116 - (show annotations)
Wed Jul 14 09:48:26 2004 UTC (19 years, 9 months ago) by dpavlin
File size: 4088 byte(s)
more fixes, more debug

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 Carp;
21 use vars qw($VERSION);
22
23 $VERSION = "2.000"; # Table.pm tests if we are loaded by checking $VERSION
24
25 sub new {
26 my $type = shift;
27 my %parm = @_;
28 my $self = {};
29
30 for my $x (qw(path attr env subname maindbfile tablename)) {
31 unless ($self->{$x} = $parm{$x}) {
32 confess("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 $path = $self->{path};
52 ! (!-e $path or unlink $path);
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 $path = $self->{path};
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 $path set to 'writing'";
70 } else {
71 $flags = DB_RDONLY;
72 # warn "Flags on index $path set to 'readonly'";
73 }
74 $self->{dbh} = tie(%{$self->{db}}, 'BerkeleyDB::Btree',
75 # Filename => $path,
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 #warn "DEBUG: sync dbh[$self->{dbh}]";
140 if ($self->{dbh}) {
141 unless (defined($self->{dbh}->db_sync)) {
142 carp "sync failed: $BerkeleyDB::Error";
143 }
144 } else {
145 confess "sync called without dbh";
146 }
147 }
148
149 sub close {
150 my $self = shift;
151
152 delete $self->{scans} if defined $self->{scans};
153
154 delete $self->{env};
155 if ($self->{dbh}) {
156 delete $self->{dbh};
157 delete $self->{_attr};
158
159 warn "DEBUG: close index (still possible bug)";
160 #use Devel::Peek;
161 #print Dump($self->{dbh});
162
163 untie %{$self->{db}};
164 for my $att (qw(db path maindbfile)) {
165 delete $self->{$att};
166 }
167 }
168 }
169
170 #sub DESTROY { $_[0]->close }
171
172 sub open_scan {
173 my $self = shift;
174 my $code = shift;
175
176 $self->{dbh} or $self->open;
177 new WAIT::IndexScan $self, $code;
178 }
179
180 1;

Properties

Name Value
cvs2svn:cvs-rev 1.4

  ViewVC Help
Powered by ViewVC 1.1.26