11 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
12 |
use File::Pid; |
use File::Pid; |
13 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
14 |
|
use BackupPC::SearchLib; |
15 |
|
|
16 |
use constant BPC_FTYPE_DIR => 5; |
use constant BPC_FTYPE_DIR => 5; |
17 |
|
use constant EST_CHUNK => 100000; |
18 |
|
|
19 |
my $debug = 0; |
my $debug = 0; |
20 |
$|=1; |
$|=1; |
42 |
|
|
43 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
44 |
my $user = $Conf{SearchUser} || ''; |
my $user = $Conf{SearchUser} || ''; |
|
my $index_path = $Conf{HyperEstraierIndex}; |
|
|
$index_path = $TopDir . '/' . $index_path; |
|
|
$index_path =~ s#//#/#g; |
|
45 |
|
|
46 |
|
my $use_hest = $Conf{HyperEstraierIndex}; |
47 |
|
my ($index_path, $index_node_url) = BackupPC::SearchLib::getHyperEstraier_url($use_hest); |
48 |
|
|
49 |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
50 |
|
|
85 |
} |
} |
86 |
|
|
87 |
my $hest_db; |
my $hest_db; |
88 |
|
my $hest_node; |
89 |
|
|
90 |
sub hest_update { |
sub signal { |
91 |
|
my($sig) = @_; |
92 |
my ($host_id, $share_id, $num) = @_; |
if ($hest_db) { |
93 |
|
print "\nCaught a SIG$sig--syncing database and shutting down\n"; |
94 |
print curr_time," updating HyperEstraier: select files"; |
$hest_db->sync(); |
95 |
|
$hest_db->close(); |
|
my $t = time(); |
|
|
|
|
|
my $where = ''; |
|
|
if ($host_id && $share_id && $num) { |
|
|
$where = qq{ |
|
|
WHERE |
|
|
hosts.id = ? AND |
|
|
shares.id = ? AND |
|
|
files.backupnum = ? |
|
|
}; |
|
96 |
} |
} |
97 |
|
exit(0); |
98 |
|
} |
99 |
|
|
100 |
my $sth = $dbh->prepare(qq{ |
$SIG{'INT'} = \&signal; |
101 |
SELECT |
$SIG{'QUIT'} = \&signal; |
|
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 |
|
|
}); |
|
102 |
|
|
103 |
$sth->execute(@_); |
sub hest_update { |
104 |
my $results = $sth->rows; |
|
105 |
|
my ($host_id, $share_id, $num) = @_; |
106 |
|
|
107 |
if ($results == 0) { |
unless ($use_hest) { |
108 |
print " - no files, skipping\n"; |
print STDERR "HyperEstraier support not enabled in configuration\n"; |
109 |
return; |
return; |
110 |
} |
} |
111 |
|
|
112 |
my $dot = int($results / 15) || 1; |
print curr_time," updating HyperEstraier:"; |
113 |
|
|
114 |
print " $results ($dot/#)"; |
my $t = time(); |
|
|
|
|
sub fmt_date { |
|
|
my $t = shift || return; |
|
|
my $iso = BackupPC::Lib::timeStamp($t); |
|
|
$iso =~ s/\s/T/; |
|
|
return $iso; |
|
|
} |
|
115 |
|
|
116 |
my $max = int($results / $dot); |
my $offset = 0; |
117 |
|
my $added = 0; |
118 |
|
|
119 |
print ", opening index $index_path..."; |
print " opening index"; |
120 |
use HyperEstraier; |
if ($index_path) { |
121 |
my $db = HyperEstraier::Database->new(); |
$hest_db = HyperEstraier::Database->new(); |
122 |
|
$hest_db->open($TopDir . $index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
123 |
|
print "$index_path directly"; |
124 |
|
} elsif ($index_node_url) { |
125 |
|
$hest_node ||= HyperEstraier::Node->new($index_node_url); |
126 |
|
$hest_node->set_auth('admin', 'admin'); |
127 |
|
print "$index_node_url via node URL"; |
128 |
|
} else { |
129 |
|
die "don't know how to use HyperEstraier Index $use_hest"; |
130 |
|
} |
131 |
|
print " increment is " . EST_CHUNK . " files:"; |
132 |
|
|
133 |
# unless ($hest_db) { |
my $results = 0; |
|
# print " open reader"; |
|
|
# $hest_db = HyperEstraier::Database->new(); |
|
|
# |
|
|
# } |
|
134 |
|
|
135 |
|
do { |
136 |
|
|
137 |
$db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
my $where = ''; |
138 |
|
my @data; |
139 |
|
if ($host_id && $share_id && $num) { |
140 |
|
$where = qq{ |
141 |
|
WHERE |
142 |
|
hosts.id = ? AND |
143 |
|
shares.id = ? AND |
144 |
|
files.backupnum = ? |
145 |
|
}; |
146 |
|
@data = ( $host_id, $share_id, $num ); |
147 |
|
} |
148 |
|
|
149 |
my $added = 0; |
my $limit = sprintf('LIMIT '.EST_CHUNK.' OFFSET %d', $offset); |
150 |
|
|
151 |
while (my $row = $sth->fetchrow_hashref()) { |
my $sth = $dbh->prepare(qq{ |
152 |
|
SELECT |
153 |
|
files.id AS fid, |
154 |
|
hosts.name AS hname, |
155 |
|
shares.name AS sname, |
156 |
|
-- shares.share AS sharename, |
157 |
|
files.backupnum AS backupnum, |
158 |
|
-- files.name AS filename, |
159 |
|
files.path AS filepath, |
160 |
|
files.date AS date, |
161 |
|
files.type AS type, |
162 |
|
files.size AS size, |
163 |
|
files.shareid AS shareid, |
164 |
|
backups.date AS backup_date |
165 |
|
FROM files |
166 |
|
INNER JOIN shares ON files.shareID=shares.ID |
167 |
|
INNER JOIN hosts ON hosts.ID = shares.hostID |
168 |
|
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID |
169 |
|
$where |
170 |
|
$limit |
171 |
|
}); |
172 |
|
|
173 |
|
$sth->execute(@data); |
174 |
|
$results = $sth->rows; |
175 |
|
|
176 |
|
if ($results == 0) { |
177 |
|
print " - no new files\n"; |
178 |
|
last; |
179 |
|
} |
180 |
|
|
181 |
my $fid = $row->{'fid'} || die "no fid?"; |
sub fmt_date { |
182 |
my $uri = 'file:///' . $fid; |
my $t = shift || return; |
183 |
|
my $iso = BackupPC::Lib::timeStamp($t); |
184 |
|
$iso =~ s/\s/T/; |
185 |
|
return $iso; |
186 |
|
} |
187 |
|
|
188 |
my $id = $db->uri_to_id($uri); |
while (my $row = $sth->fetchrow_hashref()) { |
|
next unless ($id == -1); |
|
189 |
|
|
190 |
# create a document object |
my $fid = $row->{'fid'} || die "no fid?"; |
191 |
my $doc = HyperEstraier::Document->new; |
my $uri = 'file:///' . $fid; |
192 |
|
|
193 |
# add attributes to the document object |
my $id = ($hest_db || $hest_node)->uri_to_id($uri); |
194 |
$doc->add_attr('@uri', $uri); |
next unless ($id == -1); |
195 |
|
|
196 |
foreach my $c (@{ $sth->{NAME} }) { |
# create a document object |
197 |
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
my $doc = HyperEstraier::Document->new; |
198 |
|
|
199 |
|
# add attributes to the document object |
200 |
|
$doc->add_attr('@uri', $uri); |
201 |
|
|
202 |
|
foreach my $c (@{ $sth->{NAME} }) { |
203 |
|
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
204 |
|
} |
205 |
|
|
206 |
|
#$doc->add_attr('@cdate', fmt_date($row->{'date'})); |
207 |
|
|
208 |
|
# add the body text to the document object |
209 |
|
my $path = $row->{'filepath'}; |
210 |
|
$doc->add_text($path); |
211 |
|
$path =~ s/(.)/$1 /g; |
212 |
|
$doc->add_hidden_text($path); |
213 |
|
|
214 |
|
print STDERR $doc->dump_draft,"\n" if ($debug > 1); |
215 |
|
|
216 |
|
# register the document object to the database |
217 |
|
if ($hest_db) { |
218 |
|
$hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
219 |
|
} elsif ($hest_node) { |
220 |
|
$hest_node->put_doc($doc); |
221 |
|
} else { |
222 |
|
die "not supported"; |
223 |
|
} |
224 |
|
$added++; |
225 |
} |
} |
226 |
|
|
227 |
#$doc->add_attr('@cdate', fmt_date($row->{'date'})); |
print " $added"; |
228 |
|
$hest_db->sync() if ($index_path); |
229 |
|
|
230 |
# 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--; |
|
|
} |
|
231 |
|
|
232 |
} |
} while ($results == EST_CHUNK); |
233 |
|
|
234 |
print "sync $added new files"; |
if ($index_path) { |
235 |
$db->sync(); |
print ", close"; |
236 |
print ", close"; |
$hest_db->close(); |
237 |
$db->close(); |
} |
238 |
|
|
239 |
my $dur = (time() - $t) || 1; |
my $dur = (time() - $t) || 1; |
240 |
printf(" [%.2f/s new %.2f/s dur: %s]\n", |
printf(" [%.2f/s dur: %s]\n", |
|
( $results / $dur ), |
|
241 |
( $added / $dur ), |
( $added / $dur ), |
242 |
fmt_time($dur) |
fmt_time($dur) |
243 |
); |
); |
471 |
fmt_time($dur) |
fmt_time($dur) |
472 |
); |
); |
473 |
|
|
474 |
hest_update($hostID, $shareID, $backupNum); |
hest_update($hostID, $shareID, $backupNum) if ($nf + $nd > 0); |
475 |
} |
} |
476 |
|
|
477 |
} |
} |