/[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 95 by dpavlin, Sun Jul 13 21:54: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 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 172  sub fetch { Line 161  sub fetch {
161  sub close {  sub close {
162          my $self = shift;          my $self = shift;
163    
164            return if (! $self->{dbh});
165    
166          # re-create ord column (sorted order) in table          foreach my $table (keys %Table) {
167          sub create_ord {                  $self->bench("Crating table $table");
168                    $self->delete_and_create($table);
                 my $table = shift;  
169    
170                  $self->{dbh}->begin_work || die $self->{dbh}->errstr();                  $self->{dbh}->begin_work || die $self->{dbh}->errstr();
171    
172                  my $sql = "select oid from $table order by upper(item)";                  $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();                  my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
178                  $sql = "update $table set ord=? where oid=?";  
179                  my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();                  my $ord = 0;
180                  $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();                  foreach my $key (@keys) {
181                  my $ord = 1;                          $sth->execute(++$ord,
182                  while (my $row = $sth->fetchrow_hashref) {                                  $c_table->{$table}->{$key},
183                          $sth_update->execute($ord++,$row->{oid});                                  $c_count->{$table}->{$key}
184                            );
185                  }                  }
186    
187                  $self->{dbh}->commit || die $self->{dbh}->errstr();                  $self->{dbh}->commit || die $self->{dbh}->errstr();
188          }          }
189          #--- end of sub          $self->bench("disconnecting from database");
   
         if ($self->{dbh}) {  
190    
191                  # commit          $self->{dbh}->disconnect;
192                  $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};  
         }  
193  }  }
194    
195  END {  END {

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

  ViewVC Help
Powered by ViewVC 1.1.26