40 |
|
|
41 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
42 |
my $user = $Conf{SearchUser} || ''; |
my $user = $Conf{SearchUser} || ''; |
43 |
|
my $index_path = $Conf{HyperEstraierIndex}; |
44 |
|
$index_path = $TopDir . '/' . $index_path; |
45 |
|
$index_path =~ s#//#/#g; |
46 |
|
|
47 |
|
|
48 |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
49 |
|
|
68 |
$debug = $opt{v}; |
$debug = $opt{v}; |
69 |
} |
} |
70 |
|
|
71 |
## update index ## |
#---- subs ---- |
72 |
if ($opt{i}) { |
|
73 |
|
sub fmt_time { |
74 |
|
my $t = shift || return; |
75 |
|
my $out = ""; |
76 |
|
my ($ss,$mm,$hh) = gmtime($t); |
77 |
|
$out .= "${hh}h" if ($hh); |
78 |
|
$out .= sprintf("%02d:%02d", $mm,$ss); |
79 |
|
return $out; |
80 |
|
} |
81 |
|
|
82 |
my $index_dir = '/var/tmp/casket'; |
sub curr_time { |
83 |
|
return strftime($t_fmt,localtime()); |
84 |
|
} |
85 |
|
|
86 |
print "updating HyperEstraier index $index_dir..."; |
my $hest_db; |
87 |
|
|
88 |
use HyperEstraier; |
sub hest_update { |
89 |
my $db = HyperEstraier::Database->new(); |
|
90 |
$db->open($index_dir, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
my ($host_id, $share_id, $num) = @_; |
91 |
|
|
92 |
|
print curr_time," updating HyperEstraier: files"; |
93 |
|
|
94 |
|
my $t = time(); |
95 |
|
|
96 |
|
my $where = ''; |
97 |
|
if ($host_id && $share_id && $num) { |
98 |
|
$where = qq{ |
99 |
|
WHERE |
100 |
|
hosts.id = ? AND |
101 |
|
shares.id = ? AND |
102 |
|
files.backupnum = ? |
103 |
|
}; |
104 |
|
} |
105 |
|
|
106 |
my $sth = $dbh->prepare(qq{ |
my $sth = $dbh->prepare(qq{ |
107 |
SELECT |
SELECT |
108 |
files.id AS fid, |
files.id AS fid, |
109 |
hosts.name AS hname, |
hosts.name AS hname, |
110 |
shares.name AS sname, |
shares.name AS sname, |
111 |
shares.share AS sharename, |
-- shares.share AS sharename, |
112 |
files.backupNum AS backupNum, |
files.backupnum AS backupnum, |
113 |
files.name AS filename, |
-- files.name AS filename, |
114 |
files.path AS filepath, |
files.path AS filepath, |
115 |
files.date AS date, |
files.date AS date, |
116 |
files.type AS filetype, |
files.type AS filetype, |
117 |
files.size AS size, |
files.size AS size, |
118 |
files.shareid AS shareid |
files.shareid AS shareid, |
119 |
|
backups.date AS backup_date |
120 |
FROM files |
FROM files |
121 |
INNER JOIN shares ON files.shareID=shares.ID |
INNER JOIN shares ON files.shareID=shares.ID |
122 |
INNER JOIN hosts ON hosts.ID = shares.hostID |
INNER JOIN hosts ON hosts.ID = shares.hostID |
123 |
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID |
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID |
124 |
|
$where |
125 |
}); |
}); |
126 |
|
|
127 |
$sth->execute(); |
$sth->execute(@_); |
128 |
|
my $results = $sth->rows; |
129 |
|
|
130 |
my $dot = int($sth->rows / 15); |
if ($results == 0) { |
131 |
|
print " no files\n"; |
132 |
|
return; |
133 |
|
} |
134 |
|
|
135 |
print $sth->rows, " files ($dot/#) "; |
my $dot = int($results / 15) || 1; |
136 |
|
|
137 |
|
print " $results ($dot/#)"; |
138 |
|
|
139 |
sub fmt_date { |
sub fmt_date { |
140 |
my $t = shift || return; |
my $t = shift || return; |
143 |
return $iso; |
return $iso; |
144 |
} |
} |
145 |
|
|
146 |
my $i = 0; |
my $max = int($results / $dot); |
147 |
my $max = int($sth->rows / $dot); |
|
148 |
|
print " index $index_path..."; |
149 |
|
use HyperEstraier; |
150 |
|
my $db = HyperEstraier::Database->new(); |
151 |
|
|
152 |
|
# unless ($hest_db) { |
153 |
|
# print " open reader"; |
154 |
|
# $hest_db = HyperEstraier::Database->new(); |
155 |
|
# |
156 |
|
# } |
157 |
|
|
158 |
|
|
159 |
|
$db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
160 |
|
|
161 |
|
my $added = 0; |
162 |
|
|
163 |
while (my $row = $sth->fetchrow_hashref()) { |
while (my $row = $sth->fetchrow_hashref()) { |
164 |
|
|
165 |
|
my $fid = $row->{'fid'} || die "no fid?"; |
166 |
|
my $uri = 'file:///' . $fid; |
167 |
|
|
168 |
|
next if ($db->uri_to_id($uri)); |
169 |
|
|
170 |
# create a document object |
# create a document object |
171 |
my $doc = HyperEstraier::Document->new; |
my $doc = HyperEstraier::Document->new; |
172 |
|
|
173 |
# add attributes to the document object |
# add attributes to the document object |
174 |
$doc->add_attr('@uri', 'file:///' . $row->{'fid'}); |
$doc->add_attr('@uri', $uri); |
175 |
|
|
176 |
foreach my $c (qw/fid hname sname sharename backupNum filename filepath shareid/) { |
foreach my $c (@{ $sth->{NAME} }) { |
177 |
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
178 |
} |
} |
179 |
|
|
180 |
$doc->add_attr('date', fmt_date($row->{'date'})); |
#$doc->add_attr('@cdate', fmt_date($row->{'date'})); |
181 |
|
|
182 |
# add the body text to the document object |
# add the body text to the document object |
183 |
my $path = $row->{'filepath'}; |
my $path = $row->{'filepath'}; |
190 |
# register the document object to the database |
# register the document object to the database |
191 |
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
192 |
|
|
193 |
$i++; |
$added++; |
194 |
if ($i % $dot == 0) { |
if ($added % $dot == 0) { |
195 |
print "$max "; |
print "$max "; |
196 |
$max--; |
$max--; |
197 |
} |
} |
198 |
|
|
199 |
} |
} |
200 |
|
|
201 |
print "sync"; |
print "sync $added new files"; |
202 |
$db->sync(); |
$db->sync(); |
203 |
print " close\n"; |
print " close"; |
204 |
$db->close(); |
$db->close(); |
205 |
|
|
206 |
exit; |
my $dur = (time() - $t) || 1; |
207 |
|
printf(" [%.2f/s new %.2f/s dur: %s]\n", |
208 |
|
( $results / $dur ), |
209 |
|
( $added / $dur ), |
210 |
|
fmt_time($dur) |
211 |
|
); |
212 |
} |
} |
213 |
|
|
214 |
###################################create tables############################3 |
#---- /subs ---- |
215 |
|
|
216 |
|
|
217 |
|
## update index ## |
218 |
|
if ($opt{i} || ($index_path && ! -e $index_path)) { |
219 |
|
# update all |
220 |
|
print "force update of HyperEstraier index "; |
221 |
|
print "importing existing data" unless (-e $index_path); |
222 |
|
print "by -i flag" if ($opt{i}); |
223 |
|
print "\n"; |
224 |
|
hest_update(); |
225 |
|
} |
226 |
|
|
227 |
|
## create tables ## |
228 |
if ($opt{c}) { |
if ($opt{c}) { |
229 |
sub do_index { |
sub do_index { |
230 |
my $index = shift || return; |
my $index = shift || return; |
314 |
|
|
315 |
} |
} |
316 |
|
|
317 |
|
## delete data before inseting ## |
318 |
if ($opt{d}) { |
if ($opt{d}) { |
319 |
print "deleting "; |
print "deleting "; |
320 |
foreach my $table (qw(files dvds backups shares hosts)) { |
foreach my $table (qw(files dvds backups shares hosts)) { |
326 |
$dbh->commit; |
$dbh->commit; |
327 |
} |
} |
328 |
|
|
329 |
#################################INSERT VALUES############################# |
## insert new values ## |
330 |
|
|
331 |
# get hosts |
# get hosts |
332 |
$hosts = $bpc->HostInfoRead(); |
$hosts = $bpc->HostInfoRead(); |
360 |
VALUES (?,?,?,?,?,?,?) |
VALUES (?,?,?,?,?,?,?) |
361 |
}); |
}); |
362 |
|
|
|
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; |
|
|
} |
|
|
|
|
363 |
foreach my $host_key (keys %{$hosts}) { |
foreach my $host_key (keys %{$hosts}) { |
364 |
|
|
365 |
my $hostname = $hosts->{$host_key}->{'host'} || die "can't find host for $host_key"; |
my $hostname = $hosts->{$host_key}->{'host'} || die "can't find host for $host_key"; |
415 |
next if ($count > 0); |
next if ($count > 0); |
416 |
|
|
417 |
# dump some log |
# dump some log |
418 |
print strftime($t_fmt,localtime())," ", $share; |
print curr_time," ", $share; |
419 |
|
|
420 |
my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
421 |
|
|
438 |
( ($f+$d) / $dur ), |
( ($f+$d) / $dur ), |
439 |
fmt_time($dur) |
fmt_time($dur) |
440 |
); |
); |
441 |
|
|
442 |
|
hest_update($hostID, $shareID, $backupNum); |
443 |
} |
} |
444 |
|
|
445 |
} |
} |