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

revision 93 by dpavlin, Sun Jul 13 14:44:03 2003 UTC revision 127 by dpavlin, Thu Sep 4 14:56:41 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 18  my $c_table; Line 21  my $c_table;
21  my $c_count;  my $c_count;
22    
23  # bench time  # bench time
24  my $t = time();  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;
# Line 31  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 46  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 84  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();
# Line 108  sub insert { Line 98  sub insert {
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    
# Line 149  sub fetch { Line 138  sub fetch {
138          my $sql = "select $what,ord from $field";          my $sql = "select $what,ord from $field";
139    
140          if ($where) {          if ($where) {
141                  my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";                  my $sql2 = "select ord from $field where upper($what) like upper(?)||'%'";
142                  my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
143    
144                  $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();                  $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
145                  if (my $row = $sth->fetchrow_hashref) {                  if (my $row = $sth->fetchrow_hashref) {
146                          $from_ord += $row->{ord} - 1;                          $from_ord += $row->{ord} - 1;
147                    } 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                  }                  }
157          }          }
158          $sql .= " order by ord limit $rows offset $from_ord";          $sql .= " order by ord limit $rows offset $from_ord";
# Line 172  sub fetch { Line 170  sub fetch {
170  sub close {  sub close {
171          my $self = shift;          my $self = shift;
172    
173            return if (! $self->{dbh});
174    
175          # re-create ord column (sorted order) in table          foreach my $table (keys %Table) {
176          sub create_ord {                  $self->bench("Crating table $table");
177                    $self->delete_and_create($table);
                 my $table = shift;  
178    
179                  $self->{dbh}->begin_work || die $self->{dbh}->errstr();                  $self->{dbh}->begin_work || die $self->{dbh}->errstr();
180    
181                  my $sql = "select oid from $table order by upper(item)";                  $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                  my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
187                  $sql = "update $table set ord=? where oid=?";  
188                  my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();                  my $ord = 0;
189                  $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();                  foreach my $key (@keys) {
190                  my $ord = 1;                          $sth->execute(++$ord,
191                  while (my $row = $sth->fetchrow_hashref) {                                  $c_table->{$table}->{$key},
192                          $sth_update->execute($ord++,$row->{oid});                                  $c_count->{$table}->{$key}
193                            );
194                  }                  }
195    
196                  $self->{dbh}->commit || die $self->{dbh}->errstr();                  $self->{dbh}->commit || die $self->{dbh}->errstr();
197          }          }
198          #--- end of sub          $self->bench("disconnecting from database");
   
         if ($self->{dbh}) {  
199    
200                  # commit          $self->{dbh}->disconnect;
201                  $self->{dbh}->commit || die $self->{dbh}->errstr();          undef $self->{dbh};
   
                 foreach my $table (keys %Table) {  
 # FIX  
 print STDERR "last operation took ",time()-$t," seconds...\n";  
 $t=time();  
 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();  
                 }  
   
                 $self->{dbh}->disconnect;  
                 undef $self->{dbh};  
         }  
202  }  }
203    
204  END {  END {

Legend:
Removed from v.93  
changed lines
  Added in v.127

  ViewVC Help
Powered by ViewVC 1.1.26