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 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 |
my $sql2 = " select ord from $field where upper($what) 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 |
} |
148 |
} |
149 |
$sql .= " order by ord limit $rows offset $from_ord"; |
150 |
|
151 |
my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr(); |
152 |
$sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr(); |
153 |
my @arr; |
154 |
while (my $row = $sth->fetchrow_hashref) { |
155 |
$row->{item} = HTML::Entities::encode($row->{item},'<>&"'); |
156 |
push @arr,$row; |
157 |
} |
158 |
return @arr; |
159 |
} |
160 |
|
161 |
sub close { |
162 |
my $self = shift; |
163 |
|
164 |
return if (! $self->{dbh}); |
165 |
|
166 |
foreach my $table (keys %Table) { |
167 |
$self->bench("Crating table $table"); |
168 |
$self->delete_and_create($table); |
169 |
|
170 |
$self->{dbh}->begin_work || die $self->{dbh}->errstr(); |
171 |
|
172 |
$self->bench("Sorting ".$Table{$table}." items in $table"); |
173 |
my @keys = sort keys %{$c_table->{$table}}; |
174 |
|
175 |
$self->bench("Dumping data into $table"); |
176 |
my $sql = "insert into $table (ord,item,count) values (?,?,?)"; |
177 |
my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr(); |
178 |
|
179 |
my $ord = 0; |
180 |
foreach my $key (@keys) { |
181 |
$sth->execute(++$ord, |
182 |
$c_table->{$table}->{$key}, |
183 |
$c_count->{$table}->{$key} |
184 |
); |
185 |
} |
186 |
|
187 |
$self->{dbh}->commit || die $self->{dbh}->errstr(); |
188 |
} |
189 |
$self->bench("disconnecting from database"); |
190 |
|
191 |
$self->{dbh}->disconnect; |
192 |
undef $self->{dbh}; |
193 |
} |
194 |
|
195 |
END { |
196 |
$Count--; |
197 |
print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0); |
198 |
# FIX: debug output |
199 |
# print STDERR "usage\ttable\n"; |
200 |
# foreach (keys %Table) { |
201 |
# print STDERR $Table{$_},"\t$_\n"; |
202 |
# } |
203 |
} |
204 |
|
205 |
1; |