/[webpac]/trunk2/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

Diff of /trunk2/index_DBI_cache.pm

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

revision 83 by dpavlin, Fri Jul 11 20:14:19 2003 UTC revision 142 by dpavlin, Thu Oct 30 01:02:22 2003 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 index_DBI;
10  use strict qw(vars);  use strict qw(vars);
# Line 17  my %sth_cache; # cache prepared statemen Line 20  my %sth_cache; # cache prepared statemen
20  my $c_table;  my $c_table;
21  my $c_count;  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 {  sub new {
36          my $class = shift;          my $class = shift;
37          my $self = {};          my $self = {};
# Line 28  sub new { Line 43  sub new {
43          my $passwd = shift || die "need dbi_passwd= in [global] section of configuration file";          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;          $self->{dbh} = DBI->connect("DBI:$dbd:$dsn",$user,$passwd) || die $DBI::errstr;
         # begin transaction  
         $self->{dbh}->begin_work || die $self->{dbh}->errstr();  
   
46          $Count++;          $Count++;
47    
48            $self->bench("connected to $dbd as $user");
49    
50          return $self;          return $self;
51  }  }
52    
# Line 43  sub delete_and_create { Line 57  sub delete_and_create {
57    
58  #print "#### delete_and_create($field)\n";  #print "#### delete_and_create($field)\n";
59    
         $self->{dbh}->commit;  
   
60          my $sql = "select count(*) from $field";          my $sql = "select count(*) from $field";
61          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
62  # FIX: this is not a good way to check if table exists!  # FIX: this is not a good way to check if table exists!
63          if ($sth->execute() && $sth->fetchrow_hashref) {          if ($sth->execute() && $sth->fetchrow_hashref) {
64                  my $sql = "drop table $field";                  my $sql = "drop table $field";
                 $self->{dbh}->begin_work;  
65                  my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
                 $self->{dbh}->commit;  
66          }          }
67          $sql = "create table $field (          $sql = "create table $field (
68                          item varchar(255),                          item varchar(255),
                         ident varchar(255),  
69                          count int,                          count int,
70                          ord int,                          ord int,
71                          primary key (item,ident)                          primary key (item)
72                  )";                  )";
73    
74          $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;  
75  }  }
76    
77  sub insert {  sub insert {
# Line 81  sub insert { Line 86  sub insert {
86                  return;                  return;
87          }          }
88    
         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();  
         }  
89          $Table{$field}++;          $Table{$field}++;
90    
91          #$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();
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);          $index_data = substr($index_data,0,255);
99    
100          my $uc = uc($index_data);          my $uc = uc($index_data);
101          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();  
102  #print stderr "in index: $index_data\n";  #print stderr "in index: $index_data\n";
103                  $c_table->{$field}->{$ident}->{$uc} = $index_data;                  $c_table->{$field}->{$uc} = $index_data;
104                  $c_count->{$field}->{$ident}->{$uc} = 1;                  $c_count->{$field}->{$uc} = 1;
105          } else {          } else {
106                  $c_count->{$field}->{$ident}->{$uc}++;                  $c_count->{$field}->{$uc}++;
107          }          }
108  }  }
109    
110  sub check {  sub count {
111          my $self = shift;          my $self = shift;
112    
113          my $field = shift;          my $field = shift;
114            my $where = shift;
115    
116          my $sql = "select count(*) from $field";          my $sql = "select count(*) from $field where upper(item) like upper(?)||'%'";
117    
118          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
119          $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();          $sth->execute($where) || die "sql: $sql; ".$self->{dbh}->errstr();
120    
121          my ($total) = $sth->fetchrow_array();          my ($total) = $sth->fetchrow_array();
122    
123          return $total;          # no results, count all
124            if (! $total) {
125                    my $sql = "select count(*) from $field";
126    
127                    my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
128                    $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
129                    $total = $sth->fetchrow_array();
130    
131            }
132    
133            return $total || 1;
134  }  }
135    
136    
# Line 128  sub fetch { Line 138  sub fetch {
138          my $self = shift;          my $self = shift;
139    
140          my $field = shift;          my $field = shift;
         my $what = shift || 'item';     # 'item,ident'  
141          my $where = shift;          my $where = shift;
142    
143          my $from_ord = shift || 0;          my $from_ord = shift || 0;
# Line 136  sub fetch { Line 145  sub fetch {
145    
146          my @sql_args;          my @sql_args;
147    
148          my $sql = "select $what,ord from $field";          my $sql = "select item,ord from $field";
149    
150          if ($where) {          if ($where) {
151                  my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";                  my $sql2 = "select ord from $field where upper(item) like upper(?)||'%'";
152                  my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
153    
154                  $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();                  $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
155                  if (my $row = $sth->fetchrow_hashref) {                  if (my $row = $sth->fetchrow_hashref) {
156                          $from_ord += $row->{ord} - 1;                          $from_ord += $row->{ord} - 1;
157                    } else {
158                            # if no match is found when searching from beginning
159                            # of word in index, try substring match anywhere
160                            $sql2 = "select ord from $field where upper(item) like '%'||upper(?)||'%'";
161                            $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
162                            $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
163                            if (my $row = $sth->fetchrow_hashref) {
164                                    $from_ord += $row->{ord} - 1;
165                            }
166                  }                  }
167          }          }
168          $sql .= " order by ord limit $rows offset $from_ord";          $sql .= " order by ord limit $rows offset $from_ord";
# Line 162  sub fetch { Line 180  sub fetch {
180  sub close {  sub close {
181          my $self = shift;          my $self = shift;
182    
183            return if (! $self->{dbh});
184    
185          # re-create ord column (sorted order) in table          foreach my $table (keys %Table) {
186          sub create_ord {                  $self->bench("Crating table $table");
187                    $self->delete_and_create($table);
                 my $table = shift;  
188    
189                  $self->{dbh}->begin_work || die $self->{dbh}->errstr();                  $self->{dbh}->begin_work || die $self->{dbh}->errstr();
190    
191                  my $sql = "select oid from $table order by upper(item)";                  $self->bench("Sorting ".$Table{$table}." items in $table");
192                    my @keys = sort keys %{$c_table->{$table}};
193    
194                    $self->bench("Dumping data into $table");
195                    my $sql = "insert into $table (ord,item,count) values (?,?,?)";
196                  my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
197                  $sql = "update $table set ord=? where oid=?";  
198                  my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();                  my $ord = 0;
199                  $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();                  foreach my $key (@keys) {
200                  my $ord = 1;                          $sth->execute(++$ord,
201                  while (my $row = $sth->fetchrow_hashref) {                                  $c_table->{$table}->{$key},
202                          $sth_update->execute($ord++,$row->{oid});                                  $c_count->{$table}->{$key}
203                            );
204                  }                  }
205    
206                  $self->{dbh}->commit || die $self->{dbh}->errstr();                  $self->{dbh}->commit || die $self->{dbh}->errstr();
207          }          }
208          #--- end of sub          $self->bench("disconnecting from database");
   
         if ($self->{dbh}) {  
   
                 # commit  
                 $self->{dbh}->commit || die $self->{dbh}->errstr();  
   
                 foreach my $table (keys %Table) {  
 # FIX  
 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();  
                 }  
209    
210                  $self->{dbh}->disconnect;          $self->{dbh}->disconnect;
211                  undef $self->{dbh};          undef $self->{dbh};
         }  
212  }  }
213    
214  END {  END {

Legend:
Removed from v.83  
changed lines
  Added in v.142

  ViewVC Help
Powered by ViewVC 1.1.26