/[webpac]/trunk/index_DBI_cache.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/index_DBI_cache.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 93 - (show annotations)
Sun Jul 13 14:44:03 2003 UTC (16 years, 5 months ago) by dpavlin
File size: 6155 byte(s)
fix OpenIsis 0.9.0 perl bug

1 #
2 # this file implements index functions using DBI
3 # and huge amounts of memory for cache speedup
4 #
5
6 package index_DBI;
7 use strict qw(vars);
8 use vars qw($Count);
9 use HTML::Entities;
10
11 use DBI;
12
13 my %Table; # index tables which where visited in this run
14 my %sth_cache; # cache prepared statements
15
16 # cache var
17 my $c_table;
18 my $c_count;
19
20 # bench time
21 my $t = time();
22
23 sub new {
24 my $class = shift;
25 my $self = {};
26 bless($self, $class);
27
28 my $dbd = shift || die "need dbi_dbd= in [global] section of configuration file";
29 my $dsn = shift || die "need dbi_dsn= in [global] section of configuration file";
30 my $user = shift || die "need dbi_user= in [global] section of configuration file";
31 my $passwd = shift || die "need dbi_passwd= in [global] section of configuration file";
32
33 $self->{dbh} = DBI->connect("DBI:$dbd:$dsn",$user,$passwd) || die $DBI::errstr;
34 # begin transaction
35 $self->{dbh}->begin_work || die $self->{dbh}->errstr();
36
37 $Count++;
38
39 return $self;
40 }
41
42 sub delete_and_create {
43 my $self = shift;
44
45 my $field = shift;
46
47 #print "#### delete_and_create($field)\n";
48
49 $self->{dbh}->commit;
50
51 my $sql = "select count(*) from $field";
52 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
53 # FIX: this is not a good way to check if table exists!
54 if ($sth->execute() && $sth->fetchrow_hashref) {
55 my $sql = "drop table $field";
56 $self->{dbh}->begin_work;
57 my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
58 $self->{dbh}->commit;
59 }
60 $sql = "create table $field (
61 item varchar(255),
62 ident varchar(255),
63 count int,
64 ord int,
65 primary key (item,ident)
66 )";
67
68 $self->{dbh}->begin_work;
69 $sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr();
70 $self->{dbh}->commit;
71
72 $self->{dbh}->begin_work;
73 }
74
75 sub insert {
76 my $self = shift;
77
78 my $field = shift;
79 my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
80 my $ident = shift || ''; # e.g. library id
81
82 if (! $index_data) {
83 print STDERR "\$index->insert() -- no value to insert\n";
84 return;
85 }
86
87 if (! $Table{$field}) {
88 $self->delete_and_create($field);
89
90 my $sql = "select item from $field where upper(item)=upper(?)";
91 $sth_cache{$field."select"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
92
93 $sql = "insert into $field (item,ident,count) values (?,?,?)";
94 $sth_cache{$field."insert"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
95
96 $sql = "update $field set count = count + 1 where item = ? and ident = ?";
97 $sth_cache{$field."update"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
98 }
99 $Table{$field}++;
100
101 #$sth_cache{$field."select"}->execute($index_data) || die "cache: $field select; ".$self->{dbh}->errstr();
102
103 # XXX for some strange reason, it seems that some entries in my
104 # database produce strings which start with null byte. I suspect
105 # this to be bug in OpenIsis 0.9.0.
106 # This should fix it..
107 $index_data =~ s/^[^\w]+//;
108 $index_data = substr($index_data,0,255);
109
110 my $uc = uc($index_data);
111 if (! $c_table->{$field}->{$ident}->{$uc}) {
112 $sth_cache{$field."insert"}->execute($index_data,$ident,0) || warn "cache: $field insert ($index_data,$ident); ".$self->{dbh}->errstr();
113 #print stderr "in index: $index_data\n";
114 $c_table->{$field}->{$ident}->{$uc} = $index_data;
115 $c_count->{$field}->{$ident}->{$uc} = 1;
116 } else {
117 $c_count->{$field}->{$ident}->{$uc}++;
118 }
119 }
120
121 sub check {
122 my $self = shift;
123
124 my $field = shift;
125
126 my $sql = "select count(*) from $field";
127
128 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
129 $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
130
131 my ($total) = $sth->fetchrow_array();
132
133 return $total;
134 }
135
136
137 sub fetch {
138 my $self = shift;
139
140 my $field = shift;
141 my $what = shift || 'item'; # 'item,ident'
142 my $where = shift;
143
144 my $from_ord = shift || 0;
145 my $rows = shift || 10;
146
147 my @sql_args;
148
149 my $sql = "select $what,ord from $field";
150
151 if ($where) {
152 my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";
153 my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
154
155 $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
156 if (my $row = $sth->fetchrow_hashref) {
157 $from_ord += $row->{ord} - 1;
158 }
159 }
160 $sql .= " order by ord limit $rows offset $from_ord";
161
162 my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
163 $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
164 my @arr;
165 while (my $row = $sth->fetchrow_hashref) {
166 $row->{item} = HTML::Entities::encode($row->{item},'<>&"');
167 push @arr,$row;
168 }
169 return @arr;
170 }
171
172 sub close {
173 my $self = shift;
174
175
176 # re-create ord column (sorted order) in table
177 sub create_ord {
178
179 my $table = shift;
180
181 $self->{dbh}->begin_work || die $self->{dbh}->errstr();
182
183 my $sql = "select oid from $table order by upper(item)";
184 my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
185 $sql = "update $table set ord=? where oid=?";
186 my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
187 $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
188 my $ord = 1;
189 while (my $row = $sth->fetchrow_hashref) {
190 $sth_update->execute($ord++,$row->{oid});
191 }
192
193 $self->{dbh}->commit || die $self->{dbh}->errstr();
194 }
195 #--- end of sub
196
197 if ($self->{dbh}) {
198
199 # commit
200 $self->{dbh}->commit || die $self->{dbh}->errstr();
201
202 foreach my $table (keys %Table) {
203 # FIX
204 print STDERR "last operation took ",time()-$t," seconds...\n";
205 $t=time();
206 print STDERR "creating ord for $table...\n";
207 create_ord($table);
208 undef $sth_cache{$table."select"};
209 undef $sth_cache{$table."insert"};
210 undef $sth_cache{$table."update"};
211 # XXX
212 # $sth_cache{$field."update"}->execute($index_data,$ident) || die "cache: $field update; ".$self->{dbh}->errstr();
213 }
214
215 $self->{dbh}->disconnect;
216 undef $self->{dbh};
217 }
218 }
219
220 END {
221 $Count--;
222 print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0);
223 # FIX: debug output
224 # print STDERR "usage\ttable\n";
225 # foreach (keys %Table) {
226 # print STDERR $Table{$_},"\t$_\n";
227 # }
228 }
229
230 1;

Properties

Name Value
cvs2svn:cvs-rev 1.5

  ViewVC Help
Powered by ViewVC 1.1.26