/[BackupPC]/trunk/bin/BackupPC_updatedb
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 /trunk/bin/BackupPC_updatedb

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

revision 97 by dpavlin, Tue Aug 30 09:55:55 2005 UTC revision 98 by dpavlin, Tue Aug 30 14:19:54 2005 UTC
# Line 13  use File::Pid; Line 13  use File::Pid;
13  use POSIX qw/strftime/;  use POSIX qw/strftime/;
14    
15  use constant BPC_FTYPE_DIR => 5;  use constant BPC_FTYPE_DIR => 5;
16  use constant EST_SYNC_EVERY => 10000;  use constant EST_CHUNK => 10000;
17    
18  my $debug = 0;  my $debug = 0;
19  $|=1;  $|=1;
# Line 44  my $user = $Conf{SearchUser} || ''; Line 44  my $user = $Conf{SearchUser} || '';
44  my $index_path = $Conf{HyperEstraierIndex};  my $index_path = $Conf{HyperEstraierIndex};
45  $index_path = $TopDir . '/' . $index_path;  $index_path = $TopDir . '/' . $index_path;
46  $index_path =~ s#//#/#g;  $index_path =~ s#//#/#g;
47    if ($index_path) {
48            use HyperEstraier;
49    }
50    
51    
52  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
# Line 103  sub hest_update { Line 106  sub hest_update {
106    
107          my ($host_id, $share_id, $num) = @_;          my ($host_id, $share_id, $num) = @_;
108    
109          print curr_time," updating HyperEstraier: select files";          print curr_time," updating HyperEstraier:";
110    
111          my $t = time();          my $t = time();
112    
113          my $where = '';          my $offset = 0;
114          if ($host_id && $share_id && $num) {          my $added = 0;
                 $where = qq{  
                 WHERE  
                         hosts.id = ? AND  
                         shares.id = ? AND  
                         files.backupnum = ?  
                 };  
         }  
   
         my $sth = $dbh->prepare(qq{  
                 SELECT  
                         files.id                        AS fid,  
                         hosts.name                      AS hname,  
                         shares.name                     AS sname,  
                         -- shares.share                 AS sharename,  
                         files.backupnum                 AS backupnum,  
                         -- files.name                   AS filename,  
                         files.path                      AS filepath,  
                         files.date                      AS date,  
                         files.type                      AS type,  
                         files.size                      AS size,  
                         files.shareid                   AS shareid,  
                         backups.date                    AS backup_date  
                 FROM files  
                         INNER JOIN shares       ON files.shareID=shares.ID  
                         INNER JOIN hosts        ON hosts.ID = shares.hostID  
                         INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID  
                 $where  
         });  
   
         $sth->execute(@_);  
         my $results = $sth->rows;  
   
         if ($results == 0) {  
                 print " - no files, skipping\n";  
                 return;  
         }  
   
         my $dot = int($results / 15) || 1;  
115    
116          print " $results ($dot/#)";          print " opening index $index_path";
117            $hest_db = HyperEstraier::Database->new();
118            $hest_db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
119    
120            my $results = 0;
121    
122            do {
123    
124                    my $where = '';
125                    if ($host_id && $share_id && $num) {
126                            $where = qq{
127                            WHERE
128                                    hosts.id = ? AND
129                                    shares.id = ? AND
130                                    files.backupnum = ?
131                            };
132                    }
133    
134          sub fmt_date {                  my $limit = sprintf('LIMIT '.EST_CHUNK.' OFFSET %d', $offset);
                 my $t = shift || return;  
                 my $iso = BackupPC::Lib::timeStamp($t);  
                 $iso =~ s/\s/T/;  
                 return $iso;  
         }  
135    
136          my $max = int($results / $dot);                  my $sth = $dbh->prepare(qq{
137                            SELECT
138                                    files.id                        AS fid,
139                                    hosts.name                      AS hname,
140                                    shares.name                     AS sname,
141                                    -- shares.share                 AS sharename,
142                                    files.backupnum                 AS backupnum,
143                                    -- files.name                   AS filename,
144                                    files.path                      AS filepath,
145                                    files.date                      AS date,
146                                    files.type                      AS type,
147                                    files.size                      AS size,
148                                    files.shareid                   AS shareid,
149                                    backups.date                    AS backup_date
150                            FROM files
151                                    INNER JOIN shares       ON files.shareID=shares.ID
152                                    INNER JOIN hosts        ON hosts.ID = shares.hostID
153                                    INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID
154                            $where
155                            $limit
156                    });
157    
158                    $sth->execute(@_);
159                    $results = $sth->rows;
160    
161                    if ($results == 0) {
162                            print " - no more files\n";
163                            last;
164                    }
165    
166          print ", opening index $index_path...";                  sub fmt_date {
167          use HyperEstraier;                          my $t = shift || return;
168          my $db = HyperEstraier::Database->new();                          my $iso = BackupPC::Lib::timeStamp($t);
169                            $iso =~ s/\s/T/;
170                            return $iso;
171                    }
172    
173  #       unless ($hest_db) {                  while (my $row = $sth->fetchrow_hashref()) {
 #               print " open reader";  
 #               $hest_db = HyperEstraier::Database->new();  
 #  
 #       }  
174    
175                            my $fid = $row->{'fid'} || die "no fid?";
176                            my $uri = 'file:///' . $fid;
177    
178          $db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);                          my $id = $hest_db->uri_to_id($uri);
179                            next unless ($id == -1);
180    
181          my $added = 0;                          # create a document object
182                            my $doc = HyperEstraier::Document->new;
183    
184          while (my $row = $sth->fetchrow_hashref()) {                          # add attributes to the document object
185                            $doc->add_attr('@uri', $uri);
186    
187                  my $fid = $row->{'fid'} || die "no fid?";                          foreach my $c (@{ $sth->{NAME} }) {
188                  my $uri = 'file:///' . $fid;                                  $doc->add_attr($c, $row->{$c}) if ($row->{$c});
189                            }
190    
191                  my $id = $db->uri_to_id($uri);                          #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
                 next unless ($id == -1);  
192    
193                  # create a document object                          # add the body text to the document object
194                  my $doc = HyperEstraier::Document->new;                          my $path = $row->{'filepath'};
195                            $doc->add_text($path);
196                            $path =~ s/(.)/$1 /g;
197                            $doc->add_hidden_text($path);
198    
199                  # add attributes to the document object                          print STDERR $doc->dump_draft,"\n" if ($debug > 1);
                 $doc->add_attr('@uri', $uri);  
200    
201                  foreach my $c (@{ $sth->{NAME} }) {                          # register the document object to the database
202                          $doc->add_attr($c, $row->{$c}) if ($row->{$c});                          $hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
203                            $added++;
204                  }                  }
205    
206                  #$doc->add_attr('@cdate', fmt_date($row->{'date'}));                  print " $added";
207                    $hest_db->sync();
                 # add the body text to the document object  
                 my $path = $row->{'filepath'};  
                 $doc->add_text($path);  
                 $path =~ s/(.)/$1 /g;  
                 $doc->add_hidden_text($path);  
   
                 print STDERR $doc->dump_draft,"\n" if ($debug > 1);  
   
                 # register the document object to the database  
                 $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);  
   
                 $added++;  
                 if ($added % $dot == 0) {  
                         print "$max ";  
                         $max--;  
                 }  
208    
209                  if ($added % EST_SYNC_EVERY == 0) {                  $offset += EST_CHUNK;
                         print "sync ";  
                         $db->sync();  
                 }  
210    
211          }          } while ($results == EST_CHUNK);
212    
         print "sync $added new files";  
         $db->sync();  
213          print ", close";          print ", close";
214          $db->close();          $hest_db->close();
215    
216          my $dur = (time() - $t) || 1;          my $dur = (time() - $t) || 1;
217          printf(" [%.2f/s new %.2f/s dur: %s]\n",          printf(" [%.2f/s dur: %s]\n",
                 ( $results / $dur ),  
218                  ( $added / $dur ),                  ( $added / $dur ),
219                  fmt_time($dur)                  fmt_time($dur)
220          );          );

Legend:
Removed from v.97  
changed lines
  Added in v.98

  ViewVC Help
Powered by ViewVC 1.1.26