1 |
dpavlin |
60 |
# |
2 |
|
|
# this file implements index functions using DBI |
3 |
|
|
# and huge amounts of memory for cache speedup |
4 |
|
|
# |
5 |
dpavlin |
94 |
# this version doesn't support ident (which sould be location in |
6 |
|
|
# library). But, that functionality is not used anyway... |
7 |
|
|
# |
8 |
dpavlin |
60 |
|
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 |
dpavlin |
88 |
# bench time |
24 |
dpavlin |
94 |
my $bench_time = time(); |
25 |
dpavlin |
88 |
|
26 |
dpavlin |
94 |
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 |
dpavlin |
60 |
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 |
dpavlin |
94 |
$self->bench("connected to $dbd as $user"); |
49 |
|
|
|
50 |
dpavlin |
60 |
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 |
dpavlin |
94 |
primary key (item) |
72 |
dpavlin |
60 |
)"; |
73 |
|
|
|
74 |
dpavlin |
94 |
$sth = $self->{dbh}->do($sql) || warn "SQL: $sql ".$self->{dbh}->errstr(); |
75 |
dpavlin |
60 |
} |
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 |
dpavlin |
93 |
|
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 |
dpavlin |
60 |
$index_data = substr($index_data,0,255); |
99 |
dpavlin |
93 |
|
100 |
dpavlin |
60 |
my $uc = uc($index_data); |
101 |
dpavlin |
94 |
if (! $c_table->{$field}->{$uc}) { |
102 |
dpavlin |
60 |
#print stderr "in index: $index_data\n"; |
103 |
dpavlin |
94 |
$c_table->{$field}->{$uc} = $index_data; |
104 |
|
|
$c_count->{$field}->{$uc} = 1; |
105 |
dpavlin |
60 |
} else { |
106 |
dpavlin |
94 |
$c_count->{$field}->{$uc}++; |
107 |
dpavlin |
60 |
} |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
sub check { |
111 |
|
|
my $self = shift; |
112 |
|
|
|
113 |
|
|
my $field = shift; |
114 |
|
|
|
115 |
|
|
my $sql = "select count(*) from $field"; |
116 |
|
|
|
117 |
|
|
my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); |
118 |
|
|
$sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr(); |
119 |
|
|
|
120 |
|
|
my ($total) = $sth->fetchrow_array(); |
121 |
|
|
|
122 |
|
|
return $total; |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
|
126 |
|
|
sub fetch { |
127 |
|
|
my $self = shift; |
128 |
|
|
|
129 |
|
|
my $field = shift; |
130 |
|
|
my $what = shift || 'item'; # 'item,ident' |
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 $what,ord from $field"; |
139 |
|
|
|
140 |
|
|
if ($where) { |
141 |
dpavlin |
127 |
my $sql2 = "select ord from $field where upper($what) like upper(?)||'%'"; |
142 |
dpavlin |
60 |
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 |
dpavlin |
127 |
} 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($what) 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 |
dpavlin |
60 |
} |
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 |
dpavlin |
94 |
return if (! $self->{dbh}); |
174 |
dpavlin |
60 |
|
175 |
dpavlin |
94 |
foreach my $table (keys %Table) { |
176 |
|
|
$self->bench("Crating table $table"); |
177 |
|
|
$self->delete_and_create($table); |
178 |
dpavlin |
60 |
|
179 |
|
|
$self->{dbh}->begin_work || die $self->{dbh}->errstr(); |
180 |
|
|
|
181 |
dpavlin |
94 |
$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 |
dpavlin |
60 |
my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr(); |
187 |
dpavlin |
94 |
|
188 |
|
|
my $ord = 0; |
189 |
|
|
foreach my $key (@keys) { |
190 |
dpavlin |
95 |
$sth->execute(++$ord, |
191 |
dpavlin |
94 |
$c_table->{$table}->{$key}, |
192 |
|
|
$c_count->{$table}->{$key} |
193 |
|
|
); |
194 |
dpavlin |
60 |
} |
195 |
|
|
|
196 |
|
|
$self->{dbh}->commit || die $self->{dbh}->errstr(); |
197 |
|
|
} |
198 |
dpavlin |
94 |
$self->bench("disconnecting from database"); |
199 |
dpavlin |
60 |
|
200 |
dpavlin |
94 |
$self->{dbh}->disconnect; |
201 |
|
|
undef $self->{dbh}; |
202 |
dpavlin |
60 |
} |
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; |