/[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 92 by dpavlin, Sun Aug 28 18:02:58 2005 UTC revision 99 by dpavlin, Tue Aug 30 14:45:33 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_CHUNK => 100000;
17    
18  my $debug = 0;  my $debug = 0;
19  $|=1;  $|=1;
# Line 43  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 85  sub curr_time { Line 89  sub curr_time {
89    
90  my $hest_db;  my $hest_db;
91    
92    sub signal {
93            my($sig) = @_;
94            if ($hest_db) {
95                    print "\nCaught a SIG$sig--syncing database and shutting down\n";
96                    $hest_db->sync();
97                    $hest_db->close();
98            }
99            exit(0);
100    }
101    
102    $SIG{'INT'}  = \&signal;
103    $SIG{'QUIT'} = \&signal;
104    
105  sub hest_update {  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            print " increment is " . EST_CHUNK . " files";
121    
122            my $results = 0;
123    
124            do {
125    
126                    my $where = '';
127                    if ($host_id && $share_id && $num) {
128                            $where = qq{
129                            WHERE
130                                    hosts.id = ? AND
131                                    shares.id = ? AND
132                                    files.backupnum = ?
133                            };
134                    }
135    
136          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;  
         }  
137    
138          my $max = int($results / $dot);                  my $sth = $dbh->prepare(qq{
139                            SELECT
140                                    files.id                        AS fid,
141                                    hosts.name                      AS hname,
142                                    shares.name                     AS sname,
143                                    -- shares.share                 AS sharename,
144                                    files.backupnum                 AS backupnum,
145                                    -- files.name                   AS filename,
146                                    files.path                      AS filepath,
147                                    files.date                      AS date,
148                                    files.type                      AS type,
149                                    files.size                      AS size,
150                                    files.shareid                   AS shareid,
151                                    backups.date                    AS backup_date
152                            FROM files
153                                    INNER JOIN shares       ON files.shareID=shares.ID
154                                    INNER JOIN hosts        ON hosts.ID = shares.hostID
155                                    INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID
156                            $where
157                            $limit
158                    });
159    
160                    $sth->execute(@_);
161                    $results = $sth->rows;
162    
163                    if ($results == 0) {
164                            print " - no more files\n";
165                            last;
166                    }
167    
168          print ", opening index $index_path...";                  sub fmt_date {
169          use HyperEstraier;                          my $t = shift || return;
170          my $db = HyperEstraier::Database->new();                          my $iso = BackupPC::Lib::timeStamp($t);
171                            $iso =~ s/\s/T/;
172                            return $iso;
173                    }
174    
175  #       unless ($hest_db) {                  while (my $row = $sth->fetchrow_hashref()) {
 #               print " open reader";  
 #               $hest_db = HyperEstraier::Database->new();  
 #  
 #       }  
176    
177                            my $fid = $row->{'fid'} || die "no fid?";
178                            my $uri = 'file:///' . $fid;
179    
180          $db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);                          my $id = $hest_db->uri_to_id($uri);
181                            next unless ($id == -1);
182    
183          my $added = 0;                          # create a document object
184                            my $doc = HyperEstraier::Document->new;
185    
186          while (my $row = $sth->fetchrow_hashref()) {                          # add attributes to the document object
187                            $doc->add_attr('@uri', $uri);
188    
189                  my $fid = $row->{'fid'} || die "no fid?";                          foreach my $c (@{ $sth->{NAME} }) {
190                  my $uri = 'file:///' . $fid;                                  $doc->add_attr($c, $row->{$c}) if ($row->{$c});
191                            }
192    
193                  my $id = $db->uri_to_id($uri);                          #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
                 next unless ($id == -1);  
194    
195                  # create a document object                          # add the body text to the document object
196                  my $doc = HyperEstraier::Document->new;                          my $path = $row->{'filepath'};
197                            $doc->add_text($path);
198                            $path =~ s/(.)/$1 /g;
199                            $doc->add_hidden_text($path);
200    
201                  # add attributes to the document object                          print STDERR $doc->dump_draft,"\n" if ($debug > 1);
                 $doc->add_attr('@uri', $uri);  
202    
203                  foreach my $c (@{ $sth->{NAME} }) {                          # register the document object to the database
204                          $doc->add_attr($c, $row->{$c}) if ($row->{$c});                          $hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
205                            $added++;
206                  }                  }
207    
208                  #$doc->add_attr('@cdate', fmt_date($row->{'date'}));                  print " $added";
209                    $hest_db->sync();
210    
211                  # add the body text to the document object                  $offset += EST_CHUNK;
                 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--;  
                 }  
212    
213          }          } while ($results == EST_CHUNK);
214    
         print "sync $added new files";  
         $db->sync();  
215          print ", close";          print ", close";
216          $db->close();          $hest_db->close();
217    
218          my $dur = (time() - $t) || 1;          my $dur = (time() - $t) || 1;
219          printf(" [%.2f/s new %.2f/s dur: %s]\n",          printf(" [%.2f/s dur: %s]\n",
                 ( $results / $dur ),  
220                  ( $added / $dur ),                  ( $added / $dur ),
221                  fmt_time($dur)                  fmt_time($dur)
222          );          );
# Line 216  sub hest_update { Line 226  sub hest_update {
226    
227    
228  ## update index ##  ## update index ##
229  if ($opt{i} || ($index_path && ! -e $index_path)) {  if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) {
230          # update all          # update all
231          print "force update of HyperEstraier index ";          print "force update of HyperEstraier index ";
232          print "importing existing data" unless (-e $index_path);          print "importing existing data" unless (-e $index_path);

Legend:
Removed from v.92  
changed lines
  Added in v.99

  ViewVC Help
Powered by ViewVC 1.1.26