/[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 140 - (show annotations)
Thu Oct 30 00:10:09 2003 UTC (20 years, 4 months ago) by dpavlin
File size: 5521 byte(s)
fix for total number of entries from index if using filter, renamed
check function to count and added limit

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 return $total;
124 }
125
126
127 sub fetch {
128 my $self = shift;
129
130 my $field = shift;
131 my $where = shift;
132
133 my $from_ord = shift || 0;
134 my $rows = shift || 10;
135
136 my @sql_args;
137
138 my $sql = "select item,ord from $field";
139
140 if ($where) {
141 my $sql2 = "select ord from $field where upper(item) like upper(?)||'%'";
142 my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
143
144 $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
145 if (my $row = $sth->fetchrow_hashref) {
146 $from_ord += $row->{ord} - 1;
147 } else {
148 # if no match is found when searching from beginning
149 # of word in index, try substring match anywhere
150 $sql2 = "select ord from $field where upper(item) like '%'||upper(?)||'%'";
151 $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
152 $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
153 if (my $row = $sth->fetchrow_hashref) {
154 $from_ord += $row->{ord} - 1;
155 }
156 }
157 }
158 $sql .= " order by ord limit $rows offset $from_ord";
159
160 my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
161 $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
162 my @arr;
163 while (my $row = $sth->fetchrow_hashref) {
164 $row->{item} = HTML::Entities::encode($row->{item},'<>&"');
165 push @arr,$row;
166 }
167 return @arr;
168 }
169
170 sub close {
171 my $self = shift;
172
173 return if (! $self->{dbh});
174
175 foreach my $table (keys %Table) {
176 $self->bench("Crating table $table");
177 $self->delete_and_create($table);
178
179 $self->{dbh}->begin_work || die $self->{dbh}->errstr();
180
181 $self->bench("Sorting ".$Table{$table}." items in $table");
182 my @keys = sort keys %{$c_table->{$table}};
183
184 $self->bench("Dumping data into $table");
185 my $sql = "insert into $table (ord,item,count) values (?,?,?)";
186 my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
187
188 my $ord = 0;
189 foreach my $key (@keys) {
190 $sth->execute(++$ord,
191 $c_table->{$table}->{$key},
192 $c_count->{$table}->{$key}
193 );
194 }
195
196 $self->{dbh}->commit || die $self->{dbh}->errstr();
197 }
198 $self->bench("disconnecting from database");
199
200 $self->{dbh}->disconnect;
201 undef $self->{dbh};
202 }
203
204 END {
205 $Count--;
206 print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0);
207 # FIX: debug output
208 # print STDERR "usage\ttable\n";
209 # foreach (keys %Table) {
210 # print STDERR $Table{$_},"\t$_\n";
211 # }
212 }
213
214 1;

Properties

Name Value
cvs2svn:cvs-rev 1.9

  ViewVC Help
Powered by ViewVC 1.1.26