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