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 => 100000; |
17 |
|
|
18 |
my $debug = 0; |
my $debug = 0; |
19 |
$|=1; |
$|=1; |
42 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
43 |
my $user = $Conf{SearchUser} || ''; |
my $user = $Conf{SearchUser} || ''; |
44 |
my $index_path = $Conf{HyperEstraierIndex}; |
my $index_path = $Conf{HyperEstraierIndex}; |
45 |
$index_path = $TopDir . '/' . $index_path; |
if ($index_path) { |
46 |
$index_path =~ s#//#/#g; |
$index_path = $TopDir . '/' . $index_path; |
47 |
|
$index_path =~ s#//#/#g; |
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 }); |
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; |
|
|
} |
|
115 |
|
|
116 |
my $dot = int($results / 15) || 1; |
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 |
|
my @data; |
128 |
|
if ($host_id && $share_id && $num) { |
129 |
|
$where = qq{ |
130 |
|
WHERE |
131 |
|
hosts.id = ? AND |
132 |
|
shares.id = ? AND |
133 |
|
files.backupnum = ? |
134 |
|
}; |
135 |
|
@data = ( $host_id, $share_id, $num ); |
136 |
|
} |
137 |
|
|
138 |
print " $results ($dot/#)"; |
my $limit = sprintf('LIMIT '.EST_CHUNK.' OFFSET %d', $offset); |
139 |
|
|
140 |
sub fmt_date { |
my $sth = $dbh->prepare(qq{ |
141 |
my $t = shift || return; |
SELECT |
142 |
my $iso = BackupPC::Lib::timeStamp($t); |
files.id AS fid, |
143 |
$iso =~ s/\s/T/; |
hosts.name AS hname, |
144 |
return $iso; |
shares.name AS sname, |
145 |
} |
-- shares.share AS sharename, |
146 |
|
files.backupnum AS backupnum, |
147 |
my $max = int($results / $dot); |
-- files.name AS filename, |
148 |
|
files.path AS filepath, |
149 |
|
files.date AS date, |
150 |
|
files.type AS type, |
151 |
|
files.size AS size, |
152 |
|
files.shareid AS shareid, |
153 |
|
backups.date AS backup_date |
154 |
|
FROM files |
155 |
|
INNER JOIN shares ON files.shareID=shares.ID |
156 |
|
INNER JOIN hosts ON hosts.ID = shares.hostID |
157 |
|
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID |
158 |
|
$where |
159 |
|
$limit |
160 |
|
}); |
161 |
|
|
162 |
|
$sth->execute(@data); |
163 |
|
$results = $sth->rows; |
164 |
|
|
165 |
|
if ($results == 0) { |
166 |
|
print " - no new files\n"; |
167 |
|
last; |
168 |
|
} |
169 |
|
|
170 |
print ", opening index $index_path..."; |
sub fmt_date { |
171 |
use HyperEstraier; |
my $t = shift || return; |
172 |
my $db = HyperEstraier::Database->new(); |
my $iso = BackupPC::Lib::timeStamp($t); |
173 |
|
$iso =~ s/\s/T/; |
174 |
|
return $iso; |
175 |
|
} |
176 |
|
|
177 |
# unless ($hest_db) { |
while (my $row = $sth->fetchrow_hashref()) { |
|
# print " open reader"; |
|
|
# $hest_db = HyperEstraier::Database->new(); |
|
|
# |
|
|
# } |
|
178 |
|
|
179 |
|
my $fid = $row->{'fid'} || die "no fid?"; |
180 |
|
my $uri = 'file:///' . $fid; |
181 |
|
|
182 |
$db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
my $id = $hest_db->uri_to_id($uri); |
183 |
|
next unless ($id == -1); |
184 |
|
|
185 |
my $added = 0; |
# create a document object |
186 |
|
my $doc = HyperEstraier::Document->new; |
187 |
|
|
188 |
while (my $row = $sth->fetchrow_hashref()) { |
# add attributes to the document object |
189 |
|
$doc->add_attr('@uri', $uri); |
190 |
|
|
191 |
my $fid = $row->{'fid'} || die "no fid?"; |
foreach my $c (@{ $sth->{NAME} }) { |
192 |
my $uri = 'file:///' . $fid; |
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
193 |
|
} |
194 |
|
|
195 |
my $id = $db->uri_to_id($uri); |
#$doc->add_attr('@cdate', fmt_date($row->{'date'})); |
|
next unless ($id == -1); |
|
196 |
|
|
197 |
# create a document object |
# add the body text to the document object |
198 |
my $doc = HyperEstraier::Document->new; |
my $path = $row->{'filepath'}; |
199 |
|
$doc->add_text($path); |
200 |
|
$path =~ s/(.)/$1 /g; |
201 |
|
$doc->add_hidden_text($path); |
202 |
|
|
203 |
# add attributes to the document object |
print STDERR $doc->dump_draft,"\n" if ($debug > 1); |
|
$doc->add_attr('@uri', $uri); |
|
204 |
|
|
205 |
foreach my $c (@{ $sth->{NAME} }) { |
# register the document object to the database |
206 |
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
$hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
207 |
|
$added++; |
208 |
} |
} |
209 |
|
|
210 |
#$doc->add_attr('@cdate', fmt_date($row->{'date'})); |
print " $added"; |
211 |
|
$hest_db->sync(); |
212 |
|
|
213 |
# 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--; |
|
|
} |
|
214 |
|
|
215 |
if ($added % EST_SYNC_EVERY == 0) { |
} while ($results == EST_CHUNK); |
|
print "sync "; |
|
|
$db->sync(); |
|
|
} |
|
|
|
|
|
} |
|
216 |
|
|
|
print "sync $added new files"; |
|
|
$db->sync(); |
|
217 |
print ", close"; |
print ", close"; |
218 |
$db->close(); |
$hest_db->close(); |
219 |
|
|
220 |
my $dur = (time() - $t) || 1; |
my $dur = (time() - $t) || 1; |
221 |
printf(" [%.2f/s new %.2f/s dur: %s]\n", |
printf(" [%.2f/s dur: %s]\n", |
|
( $results / $dur ), |
|
222 |
( $added / $dur ), |
( $added / $dur ), |
223 |
fmt_time($dur) |
fmt_time($dur) |
224 |
); |
); |
452 |
fmt_time($dur) |
fmt_time($dur) |
453 |
); |
); |
454 |
|
|
455 |
hest_update($hostID, $shareID, $backupNum); |
hest_update($hostID, $shareID, $backupNum) if ($nf + $nd > 0); |
456 |
} |
} |
457 |
|
|
458 |
} |
} |