/[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 142 - (show annotations)
Thu Oct 30 01:02:22 2003 UTC (20 years, 4 months ago) by dpavlin
File size: 5786 byte(s)
if no entries found, count should return number of all entries

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

Properties

Name Value
cvs2svn:cvs-rev 1.10

  ViewVC Help
Powered by ViewVC 1.1.26