+if ($opt{v}) {
+ print "Debug level at $opt{v}\n";
+ $debug = $opt{v};
+} elsif ($opt{f}) {
+ print "WARNING: disabling full-text index update. You need to re-run $0 -j !\n";
+ $index_node_url = undef;
+}
+
+#---- subs ----
+
+sub fmt_time {
+ my $t = shift || return;
+ my $out = "";
+ my ($ss,$mm,$hh) = gmtime($t);
+ $out .= "${hh}h" if ($hh);
+ $out .= sprintf("%02d:%02d", $mm,$ss);
+ return $out;
+}
+
+sub curr_time {
+ return strftime($t_fmt,localtime());
+}
+
+my $hest_db;
+my $hest_node;
+
+sub signal {
+ my($sig) = @_;
+ if ($hest_db) {
+ print "\nCaught a SIG$sig--syncing database and shutting down\n";
+ $hest_db->sync();
+ $hest_db->close();
+ }
+ exit(0);
+}
+
+$SIG{'INT'} = \&signal;
+$SIG{'QUIT'} = \&signal;
+
+sub hest_update {
+
+ my ($host_id, $share_id, $num) = @_;
+
+ my $skip_check = $opt{j} && print STDERR "Skipping check for existing files -- this should be used only with initital import\n";
+
+ unless (defined($index_node_url)) {
+ print STDERR "HyperEstraier support not enabled in configuration\n";
+ $index_node_url = 0;
+ return;
+ }
+
+ print curr_time," updating Hyper Estraier:";
+
+ my $t = time();
+
+ my $offset = 0;
+ my $added = 0;
+
+ print " opening index $index_node_url";
+ if ($index_node_url) {
+ $hest_node ||= Search::Estraier::Node->new($index_node_url);
+ $hest_node->set_auth('admin', 'admin');
+ print " via node URL";
+ } else {
+ die "don't know how to use Hyper Estraier Index $index_node_url";
+ }
+
+ my $results = 0;
+
+ do {
+
+ my $where = '';
+ my @data;
+ if (defined($host_id) && defined($share_id) && defined($num)) {
+ $where = qq{
+ WHERE
+ hosts.id = ? AND
+ shares.id = ? AND
+ files.backupnum = ?
+ };
+ @data = ( $host_id, $share_id, $num );
+ }
+
+ my $limit = sprintf('LIMIT '.EST_CHUNK.' OFFSET %d', $offset);
+
+ 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
+ $limit
+ });
+
+ $sth->execute(@data);
+ $results = $sth->rows;
+
+ if ($results == 0) {
+ print " - no new files\n";
+ last;
+ } else {
+ print " - $results files: ";
+ }
+
+ sub fmt_date {
+ my $t = shift || return;
+ my $iso = BackupPC::Lib::timeStamp($t);
+ $iso =~ s/\s/T/;
+ return $iso;
+ }
+
+ while (my $row = $sth->fetchrow_hashref()) {
+
+ my $fid = $row->{'fid'} || die "no fid?";
+ my $uri = 'file:///' . $fid;
+
+ unless ($skip_check) {
+ my $id = ($hest_db || $hest_node)->uri_to_id($uri);
+ next unless ($id == -1);
+ }
+
+ # create a document object
+ my $doc = Search::Estraier::Document->new;
+
+ # add attributes to the document object
+ $doc->add_attr('@uri', $uri);
+
+ foreach my $c (@{ $sth->{NAME} }) {
+ print STDERR "attr $c = $row->{$c}\n" if ($debug > 2);
+ $doc->add_attr($c, $row->{$c}) if (defined($row->{$c}));
+ }
+
+ #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
+
+ # 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
+ if ($hest_node) {
+ $hest_node->put_doc($doc);
+ } else {
+ die "not supported";
+ }
+ $added++;
+ }
+
+ print " $added";
+
+ $offset += EST_CHUNK;
+
+ } while ($results == EST_CHUNK);
+
+ my $dur = (time() - $t) || 1;
+ printf(" [%.2f/s dur: %s]\n",
+ ( $added / $dur ),
+ fmt_time($dur)
+ );
+}
+
+#---- /subs ----
+