/[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

Diff of /trunk2/lib/WebPac/Index.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/index_DBI_cache.pm revision 87 by dpavlin, Fri Jul 11 20:58:45 2003 UTC trunk2/lib/WebPac/Index.pm revision 348 by dpavlin, Sun Jun 13 20:20:29 2004 UTC
# Line 2  Line 2 
2  # this file implements index functions using DBI  # this file implements index functions using DBI
3  # and huge amounts of memory for cache speedup  # 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;  package WebPac::Index;
10  use strict qw(vars);  use strict qw(vars);
11  use vars qw($Count);  use vars qw($Count);
12  use HTML::Entities;  use HTML::Entities;
13    use URI::Escape;
14    use locale;
15    use Carp;
16    
17  use DBI;  use DBI;
18    
# Line 17  my %sth_cache; # cache prepared statemen Line 23  my %sth_cache; # cache prepared statemen
23  my $c_table;  my $c_table;
24  my $c_count;  my $c_count;
25    
26    # bench time
27    my $bench_time = time();
28    
29    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  sub new {  sub new {
39          my $class = shift;          my $class = shift;
40          my $self = {};          my $self = {@_};
41          bless($self, $class);          bless($self, $class);
42    
43          my $dbd = shift || die "need dbi_dbd= in [global] section of configuration file";          croak "need dbd" if (! $self->{dbd});
44          my $dsn = shift || die "need dbi_dsn= in [global] section of configuration file";          croak "need dsn" if (! $self->{dsn});
45          my $user = shift || die "need dbi_user= in [global] section of configuration file";          croak "need user" if (! $self->{user});
46          my $passwd = shift || die "need dbi_passwd= in [global] section of configuration file";          croak "need passwd" if (! $self->{passwd});
   
         $self->{dbh} = DBI->connect("DBI:$dbd:$dsn",$user,$passwd) || die $DBI::errstr;  
         # begin transaction  
         $self->{dbh}->begin_work || die $self->{dbh}->errstr();  
47    
48            $self->{dbh} = DBI->connect("DBI:$self->{dbd}:$self->{dsn}",$self->{user},$self->{passwd}) || die $DBI::errstr;
49          $Count++;          $Count++;
50    
51            $self->bench("connected to $self->{dbd} as $self->{user}");
52    
53            # 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          return $self;          return $self;
61  }  }
62    
# Line 43  sub delete_and_create { Line 67  sub delete_and_create {
67    
68  #print "#### delete_and_create($field)\n";  #print "#### delete_and_create($field)\n";
69    
         $self->{dbh}->commit;  
   
70          my $sql = "select count(*) from $field";          my $sql = "select count(*) from $field";
71          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
72  # FIX: this is not a good way to check if table exists!  # FIX: this is not a good way to check if table exists!
73          if ($sth->execute() && $sth->fetchrow_hashref) {          if ($sth->execute() && $sth->fetchrow_hashref) {
74                  my $sql = "drop table $field";                  my $sql = "drop table $field";
75                  $self->{dbh}->begin_work;                  my $sth = $self->{dbh}->do($sql) || warn "SQL: $sql - ".$sth->errstr();
                 my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();  
                 $self->{dbh}->commit;  
76          }          }
77          $sql = "create table $field (          $sql = "create table $field (
78                          item varchar(255),                          item varchar(255),
79                          ident varchar(255),                          display text,
80                          count int,                          count int,
81                          ord int,                          ord int,
82                          primary key (item,ident)                          primary key (item)
83                  )";                  )";
84    
85          $self->{dbh}->begin_work;          $sth = $self->{dbh}->do($sql) || warn "SQL: $sql ".$self->{dbh}->errstr();
         $sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr();  
         $self->{dbh}->commit;  
   
         $self->{dbh}->begin_work;  
86  }  }
87    
88  sub insert {  sub insert {
# Line 74  sub insert { Line 90  sub insert {
90    
91          my $field = shift;          my $field = shift;
92          my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";          my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
93          my $ident = shift || '';        # e.g. library id          my $display = shift || $index_data;
94    
95          if (! $index_data) {          if (! $index_data) {
96                  print STDERR "\$index->insert() -- no value to insert\n";                  print STDERR "\$index->insert() -- no value to insert\n";
97                  return;                  return;
98          }          }
99    
         if (! $Table{$field}) {  
                 $self->delete_and_create($field);  
   
                 my $sql = "select item from $field where upper(item)=upper(?)";  
                 $sth_cache{$field."select"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();  
   
                 $sql = "insert into $field (item,ident,count) values (?,?,?)";  
                 $sth_cache{$field."insert"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();  
   
                 $sql = "update $field set count = count + 1 where item = ? and ident = ?";  
                 $sth_cache{$field."update"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();  
         }  
100          $Table{$field}++;          $Table{$field}++;
101    
102          #$sth_cache{$field."select"}->execute($index_data) || die "cache: $field select; ".$self->{dbh}->errstr();          #$sth_cache{$field."select"}->execute($index_data) || die "cache: $field select; ".$self->{dbh}->errstr();
103    
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          $index_data = substr($index_data,0,255);          $index_data = substr($index_data,0,255);
110    
111          my $uc = uc($index_data);          my $uc = uc($index_data);
112          if (! $c_table->{$field}->{$ident}->{$uc}) {          if (! $c_table->{$field}->{$uc}) {
                 $sth_cache{$field."insert"}->execute($index_data,$ident,0) || warn "cache: $field insert ($index_data,$ident); ".$self->{dbh}->errstr();  
113  #print stderr "in index: $index_data\n";  #print stderr "in index: $index_data\n";
114                  $c_table->{$field}->{$ident}->{$uc} = $index_data;                  $c_table->{$field}->{$uc} = $index_data;
115                  $c_count->{$field}->{$ident}->{$uc} = 1;                  $c_table->{$field}->{$uc}->{display} = $display;
116                    $c_count->{$field}->{$uc} = 1;
117          } else {          } else {
118                  $c_count->{$field}->{$ident}->{$uc}++;                  $c_count->{$field}->{$uc}++;
119          }          }
120  }  }
121    
122  sub check {  sub count {
123          my $self = shift;          my $self = shift;
124    
125          my $field = shift;          my $field = shift;
126            my $where = shift;
127    
128          my $sql = "select count(*) from $field";          my $sql = "select count(*) from $field where upper(item) like upper(?)||'%'";
129    
130          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
131          $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();          $sth->execute($where) || die "sql: $sql; ".$self->{dbh}->errstr();
132    
133          my ($total) = $sth->fetchrow_array();          my ($total) = $sth->fetchrow_array();
134    
135          return $total;          # 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  }  }
147    
148    
# Line 128  sub fetch { Line 150  sub fetch {
150          my $self = shift;          my $self = shift;
151    
152          my $field = shift;          my $field = shift;
         my $what = shift || 'item';     # 'item,ident'  
153          my $where = shift;          my $where = shift;
154    
155          my $from_ord = shift || 0;          my $from_ord = shift || 0;
# Line 136  sub fetch { Line 157  sub fetch {
157    
158          my @sql_args;          my @sql_args;
159    
160          my $sql = "select $what,ord from $field";          my $sql = "select item,display,ord from $field";
161    
162          if ($where) {          if ($where) {
163                  my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";                  my $sql2 = "select ord from $field where upper(item) like upper(?)||'%'";
164                  my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
165    
166                  $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();                  $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
167                  if (my $row = $sth->fetchrow_hashref) {                  if (my $row = $sth->fetchrow_hashref) {
168                          $from_ord += $row->{ord} - 1;                          $from_ord += $row->{ord} - 1;
169                    } else {
170                            # if no match is found when searching from beginning
171                            # of word in index, try substring match anywhere
172                            $sql2 = "select ord from $field where upper(item) like '% '||upper(?)||'%'";
173                            $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                  }                  }
179          }          }
180          $sql .= " order by ord limit $rows offset $from_ord";          $sql .= " order by ord limit $rows offset $from_ord";
# Line 153  sub fetch { Line 183  sub fetch {
183          $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();          $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
184          my @arr;          my @arr;
185          while (my $row = $sth->fetchrow_hashref) {          while (my $row = $sth->fetchrow_hashref) {
186                  $row->{item} = HTML::Entities::encode($row->{item},'<>&"');                  $row->{item} = HTML::Entities::encode($row->{item},' <>&"');
187                    $row->{display} = HTML::Entities::encode($row->{display},'<>&"');
188                  push @arr,$row;                  push @arr,$row;
189          }          }
190          return @arr;          return @arr;
# Line 162  sub fetch { Line 193  sub fetch {
193  sub close {  sub close {
194          my $self = shift;          my $self = shift;
195    
196            return if (! $self->{dbh});
197    
198          # re-create ord column (sorted order) in table          foreach my $table (keys %Table) {
199          sub create_ord {                  $self->bench("Crating table $table");
200                    $self->delete_and_create($table);
                 my $table = shift;  
201    
202                  $self->{dbh}->begin_work || die $self->{dbh}->errstr();                  $self->{dbh}->begin_work || die $self->{dbh}->errstr();
203    
204                  my $sql = "select oid from $table order by upper(item)";                  $self->bench("Sorting ".$Table{$table}." (with duplicates) items in $table");
205                    my @keys = sort keys %{$c_table->{$table}};
206    
207                    $self->bench("Dumping ".($#keys+1)." items into $table");
208                    my $sql = "insert into $table (ord,item,display,count) values (?,?,?,?)";
209                  my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
210                  $sql = "update $table set ord=? where oid=?";  
211                  my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();                  my $ord = 0;
212                  $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();                  foreach my $key (@keys) {
213                  my $ord = 1;                          $sth->execute(++$ord,
214                  while (my $row = $sth->fetchrow_hashref) {                                  $c_table->{$table}->{$key},
215                          $sth_update->execute($ord++,$row->{oid});                                  $c_table->{$table}->{$key}->{display},
216                                    $c_count->{$table}->{$key}
217                            );
218                  }                  }
219    
220                  $self->{dbh}->commit || die $self->{dbh}->errstr();                  $self->{dbh}->commit || die $self->{dbh}->errstr();
221          }          }
         #--- end of sub  
   
         if ($self->{dbh}) {  
222    
223                  # commit          if ($self->{dbd} =~ m/(Pg|SQLite)/) {
224                  $self->{dbh}->commit || die $self->{dbh}->errstr();                  $self->{dbh}->do(qq{vacuum}) || warn "vacumming failed. It shouldn't if you are using PostgreSQL or SQLite: ".$self->{dbh}->errstr();
225            }
226    
227                  foreach my $table (keys %Table) {          $self->bench("disconnecting from database");
 # FIX  
 print STDERR print scalar localtime()."\n";  
 print STDERR "creating ord for $table...\n";  
                         create_ord($table);  
                         undef $sth_cache{$table."select"};  
                         undef $sth_cache{$table."insert"};  
                         undef $sth_cache{$table."update"};  
 # XXX  
 #               $sth_cache{$field."update"}->execute($index_data,$ident) || die "cache: $field update; ".$self->{dbh}->errstr();  
                 }  
228    
229                  $self->{dbh}->disconnect;          $self->{dbh}->disconnect;
230                  undef $self->{dbh};          undef $self->{dbh};
         }  
231  }  }
232    
233  END {  END {

Legend:
Removed from v.87  
changed lines
  Added in v.348

  ViewVC Help
Powered by ViewVC 1.1.26