/[webpac]/trunk2/lib/WebPAC/Index.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

Annotation of /trunk2/lib/WebPAC/Index.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 354 - (hide annotations)
Wed Jun 16 11:31:42 2004 UTC (16 years, 4 months ago) by dpavlin
File size: 6279 byte(s)
WebPac -> WebPAC

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

Properties

Name Value
cvs2svn:cvs-rev 1.17

  ViewVC Help
Powered by ViewVC 1.1.26