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; |
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; |
46 |
|
$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 }); |
53 |
|
|
87 |
return strftime($t_fmt,localtime()); |
return strftime($t_fmt,localtime()); |
88 |
} |
} |
89 |
|
|
90 |
#---- /subs ---- |
my $hest_db; |
91 |
|
|
92 |
## update index ## |
sub signal { |
93 |
if ($opt{i}) { |
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 |
print curr_time," updating HyperEstraier: files"; |
sub hest_update { |
106 |
|
|
107 |
|
my ($host_id, $share_id, $num) = @_; |
108 |
|
|
109 |
|
print curr_time," updating HyperEstraier:"; |
110 |
|
|
111 |
my $t = time(); |
my $t = time(); |
|
|
|
|
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 filetype, |
|
|
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 |
|
|
}); |
|
112 |
|
|
113 |
$sth->execute(); |
my $offset = 0; |
114 |
my $results = $sth->rows; |
my $added = 0; |
115 |
|
|
116 |
my $dot = int($results / 15); |
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 |
print " $results ($dot/#)"; |
my $limit = sprintf('LIMIT '.EST_CHUNK.' OFFSET %d', $offset); |
137 |
|
|
138 |
sub fmt_date { |
my $sth = $dbh->prepare(qq{ |
139 |
my $t = shift || return; |
SELECT |
140 |
my $iso = BackupPC::Lib::timeStamp($t); |
files.id AS fid, |
141 |
$iso =~ s/\s/T/; |
hosts.name AS hname, |
142 |
return $iso; |
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 |
my $i = 0; |
sub fmt_date { |
169 |
my $max = int($results / $dot); |
my $t = shift || return; |
170 |
|
my $iso = BackupPC::Lib::timeStamp($t); |
171 |
|
$iso =~ s/\s/T/; |
172 |
|
return $iso; |
173 |
|
} |
174 |
|
|
175 |
$index_path = $TopDir . '/' . $index_path; |
while (my $row = $sth->fetchrow_hashref()) { |
|
$index_path =~ s#//#/#g; |
|
176 |
|
|
177 |
print " index $index_path..."; |
my $fid = $row->{'fid'} || die "no fid?"; |
178 |
use HyperEstraier; |
my $uri = 'file:///' . $fid; |
|
my $db = HyperEstraier::Database->new(); |
|
|
$db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
|
179 |
|
|
180 |
|
my $id = $hest_db->uri_to_id($uri); |
181 |
|
next unless ($id == -1); |
182 |
|
|
183 |
while (my $row = $sth->fetchrow_hashref()) { |
# create a document object |
184 |
|
my $doc = HyperEstraier::Document->new; |
185 |
|
|
186 |
# create a document object |
# add attributes to the document object |
187 |
my $doc = HyperEstraier::Document->new; |
$doc->add_attr('@uri', $uri); |
188 |
|
|
189 |
# add attributes to the document object |
foreach my $c (@{ $sth->{NAME} }) { |
190 |
$doc->add_attr('@uri', 'file:///' . $row->{'fid'}); |
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
191 |
|
} |
192 |
|
|
193 |
foreach my $c (@{ $sth->{NAME} }) { |
#$doc->add_attr('@cdate', fmt_date($row->{'date'})); |
194 |
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
|
195 |
} |
# add the body text to the document object |
196 |
|
my $path = $row->{'filepath'}; |
197 |
|
$doc->add_text($path); |
198 |
|
$path =~ s/(.)/$1 /g; |
199 |
|
$doc->add_hidden_text($path); |
200 |
|
|
201 |
#$doc->add_attr('@cdate', fmt_date($row->{'date'})); |
print STDERR $doc->dump_draft,"\n" if ($debug > 1); |
202 |
|
|
203 |
# add the body text to the document object |
# register the document object to the database |
204 |
my $path = $row->{'filepath'}; |
$hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
205 |
$doc->add_text($path); |
$added++; |
|
$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); |
|
|
|
|
|
$i++; |
|
|
if ($i % $dot == 0) { |
|
|
print "$max "; |
|
|
$max--; |
|
206 |
} |
} |
207 |
|
|
208 |
} |
print " $added"; |
209 |
|
$hest_db->sync(); |
210 |
|
|
211 |
|
$offset += EST_CHUNK; |
212 |
|
|
213 |
|
} while ($results == EST_CHUNK); |
214 |
|
|
215 |
print "sync"; |
print ", close"; |
216 |
$db->sync(); |
$hest_db->close(); |
|
print " close"; |
|
|
$db->close(); |
|
217 |
|
|
218 |
my $dur = (time() - $t) || 1; |
my $dur = (time() - $t) || 1; |
219 |
printf(" [%.2f/s dur: %s]\n", |
printf(" [%.2f/s dur: %s]\n", |
220 |
( $results / $dur ), |
( $added / $dur ), |
221 |
fmt_time($dur) |
fmt_time($dur) |
222 |
); |
); |
|
|
|
|
exit; |
|
223 |
} |
} |
224 |
|
|
225 |
###################################create tables############################3 |
#---- /subs ---- |
226 |
|
|
227 |
|
|
228 |
|
## update index ## |
229 |
|
if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) { |
230 |
|
# update all |
231 |
|
print "force update of HyperEstraier index "; |
232 |
|
print "importing existing data" unless (-e $index_path); |
233 |
|
print "by -i flag" if ($opt{i}); |
234 |
|
print "\n"; |
235 |
|
hest_update(); |
236 |
|
} |
237 |
|
|
238 |
|
## create tables ## |
239 |
if ($opt{c}) { |
if ($opt{c}) { |
240 |
sub do_index { |
sub do_index { |
241 |
my $index = shift || return; |
my $index = shift || return; |
325 |
|
|
326 |
} |
} |
327 |
|
|
328 |
|
## delete data before inseting ## |
329 |
if ($opt{d}) { |
if ($opt{d}) { |
330 |
print "deleting "; |
print "deleting "; |
331 |
foreach my $table (qw(files dvds backups shares hosts)) { |
foreach my $table (qw(files dvds backups shares hosts)) { |
337 |
$dbh->commit; |
$dbh->commit; |
338 |
} |
} |
339 |
|
|
340 |
#################################INSERT VALUES############################# |
## insert new values ## |
341 |
|
|
342 |
# get hosts |
# get hosts |
343 |
$hosts = $bpc->HostInfoRead(); |
$hosts = $bpc->HostInfoRead(); |
449 |
( ($f+$d) / $dur ), |
( ($f+$d) / $dur ), |
450 |
fmt_time($dur) |
fmt_time($dur) |
451 |
); |
); |
452 |
|
|
453 |
|
hest_update($hostID, $shareID, $backupNum); |
454 |
} |
} |
455 |
|
|
456 |
} |
} |