/usr/share/perl5/DBIx/FullTextSearch/Blob.pm is in libdbix-fulltextsearch-perl 0.73-10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | package DBIx::FullTextSearch::Blob;
use strict;
# Open in the backend just sets the object
sub open {
my ($class, $fts) = @_;
return bless { 'fts' => $fts }, $class;
}
# Create creates the table(s) according to the parameters
sub _create_tables {
my ($class, $fts) = @_;
my $CREATE_DATA = <<EOF;
create table $fts->{'data_table'} (
word varchar($fts->{'word_length'}) binary
default '' not null,
idx longblob default '' not null,
primary key (word)
)
EOF
my $dbh = $fts->{'dbh'};
$dbh->do($CREATE_DATA) or return $dbh->errstr;
push @{$fts->{'created_tables'}}, $fts->{'data_table'};
return;
}
sub add_document {
my ($self, $id, $words) = @_;
my $fts = $self->{'fts'};
my $dbh = $fts->{'dbh'};
my $data_table = $fts->{'data_table'};
my $update_sth = ( defined $self->{'adding_update_sth'}
? $self->{'adding_update_sth'}
: $self->{'adding_update_sth'} = $dbh->prepare(
"update $data_table set idx = concat(idx, ?)
where word = ?") );
my @insert_values;
my $packstring = $DBIx::FullTextSearch::BITS_TO_PACK{$fts->{'doc_id_bits'}}
. $DBIx::FullTextSearch::BITS_TO_PACK{$fts->{'count_bits'}};
my $num_words = 0;
for my $word ( keys %$words ) {
### print STDERR "$word($id) adding\n";
# here we will want to parametrize the bit size of the
# data
my $value = pack $packstring, $id, $words->{$word};
my $rows = $update_sth->execute($value, $word);
push @insert_values, $word, $value if $rows == 0;
$num_words += $words->{$word};
}
if(@insert_values){
my $sql_str = "insert into $data_table values ". join(',', ('(?, ?)') x (@insert_values/2));
$dbh->do($sql_str,{},@insert_values);
}
return $num_words;
}
sub delete_document {
my $self = shift;
for my $id (@_) { $self->update_document($id, {}); }
}
sub update_document {
my ($self, $id, $words) = @_;
my $fts = $self->{'fts'};
my $dbh = $fts->{'dbh'};
my $data_table = $fts->{'data_table'};
my $insert_sth = ( defined $self->{'insert_sth'}
? $self->{'insert_sth'}
: $self->{'insert_sth'} = $dbh->prepare("
insert into $data_table values (?, ?)") );
my $update_sth = ( defined $self->{'update_update_sth'}
? $self->{'update_update_sth'}
: $self->{'update_update_sth'} =
$dbh->prepare("update $data_table set idx =
concat(substring(idx, 1, ?), ?, substring(idx, ?))
where word = ?") );
my @insert_values;
$dbh->do("lock tables $data_table write");
my $select_sth = $dbh->prepare("select word from $data_table");
$select_sth->execute;
my $packstring = $DBIx::FullTextSearch::BITS_TO_PACK{$fts->{'doc_id_bits'}}
. $DBIx::FullTextSearch::BITS_TO_PACK{$fts->{'count_bits'}};
my ($packnulls) = pack $packstring, 0, 0;
my $packlength = length $packnulls;
my $num_words = 0;
while (my ($word) = $select_sth->fetchrow_array) {
my $value = (defined $words->{$word} ?
pack($packstring, $id, $words->{$word}) : '');
# the method find_position finds the position of the
# "record" for document $id with word $word; returned is
# the position in bytes and yes/no values specifying if
# the record is already present in the blob; if it is,
# we need to replace it, otherwise just insert.
my ($pos, $shift) = $self->find_position($word, $id);
if (not defined $pos) {
push @insert_values, $word, $value;
}
else {
my $spos = $pos + 1; # I'm not sure why this
$spos += $packlength if $shift;
$update_sth->execute($pos, $value, $spos, $word);
}
delete $words->{$word};
$num_words++ if defined $value;
}
for my $word ( keys %$words ) {
my $value = pack $packstring, $id, $words->{$word};
push @insert_values, $word, $value;
# $insert_sth->execute($word, $value);
$num_words++;
}
if(@insert_values){
my $sql_str = "insert into $data_table values ". join(',', ('(?, ?)') x (@insert_values/2));
$dbh->do($sql_str,{},@insert_values);
}
$dbh->do("unlock tables");
return $num_words;
}
sub find_position {
my ($self, $word, $id) = @_;
# here, with the calculation of where in the blob we have the
# docid and where the count of words and how long they are, we
# should really look at the parameters (num of bits of various
# structures and values) given to create
my $fts = $self->{'fts'};
my $dbh = $fts->{'dbh'};
my $data_table = $fts->{'data_table'};
# Sth to read the length of the blob holding the document/count info
my $get_length_sth = ( defined $self->{'get_length_sth'}
? $self->{'get_length_sth'}
: $self->{'get_length_sth'} = $dbh->prepare("select
length(idx) from $data_table where word = ?"));
my $length = $dbh->selectrow_array($get_length_sth, {}, $word);
my $packstring = $DBIx::FullTextSearch::BITS_TO_PACK{$fts->{'doc_id_bits'}}
. $DBIx::FullTextSearch::BITS_TO_PACK{$fts->{'count_bits'}};
my ($packnulls) = pack $packstring, 0, 0;
my $packlength = length $packnulls;
if (not defined $length) { return; }
$length = int($length/$packlength);
my ($bot, $top, $med, $val) = (0, $length);
if (not defined $fts->{'max_doc_id'})
{ $med = int(($top - $bot) / 2); }
else
{ $med = int($top * $id / $fts->{'max_doc_id'}); }
my $blob_direct_fetch = $fts->{'blob_direct_fetch'};
# we divide the interval
while ($bot != $top) {
$med = $top - 1 if $med >= $top;
$med = $bot if $med < $bot;
if ($top - $bot <= $blob_direct_fetch) {
my $get_interval_sth = (
defined $self->{'get_interval_sth'}
? $self->{'get_interval_sth'}
: $self->{'get_interval_sth'} = $dbh->prepare("select substring(idx,?,?) from $data_table where word = ?"));
my $alldata = $dbh->selectrow_array($get_interval_sth,
{},
$bot * $packlength + 1,
($top - $bot) * $packlength,
$word);
return unless defined $alldata;
my @docs;
my $i = 0;
while ($i < length $alldata) {
push @docs, unpack $packstring,
substr $alldata, $i, $packlength;
$i += $packlength;
}
for (my $i = 0; $i < @docs; $i += 2) {
if ($docs[$i] == $id) { return (($bot+($i/2))*$packlength, 1); }
if ($docs[$i] > $id) { return (($bot+($i/2))*$packlength, 0); }
}
return ($top * $packlength, 0);
}
($val) = $dbh->selectrow_array(
"select substring(idx, ?, 2) from $data_table
where word = ?", {}, ($med * $packlength) + 1, $word);
($val) = unpack $packstring, $val;
if (not defined $val) { return; }
if ($val == $id) { return ($med * $packlength, 1); }
elsif ($val < $id) { $bot = $med + 1; }
else { $top = $med; }
$med = int($med * $id / $val);
}
return ($bot * $packlength, 0);
}
sub contains_hashref {
my $self = shift;
my $fts = $self->{'fts'};
my $dbh = $fts->{'dbh'};
my $data_table = $fts->{'data_table'};
my $packstring = $DBIx::FullTextSearch::BITS_TO_PACK{$fts->{'doc_id_bits'}}
. $DBIx::FullTextSearch::BITS_TO_PACK{$fts->{'count_bits'}};
my ($packnulls) = pack $packstring, 0, 0;
my $packlength = length $packnulls;
my $sth = ( defined $self->{'get_idx_sth'} ?
$self->{'get_idx_sth'} :
$self->{'get_idx_sth'} =
$dbh->prepare(
"select idx from $data_table where word like ?"
));
my $out = {};
for my $word (@_) {
$sth->execute($word);
while (my ($blob) = $sth->fetchrow_array) {
next unless defined $blob;
my @data;
my $i = 0;
while ($i < length $blob) {
push @data, unpack $packstring,
substr $blob, $i, $packlength;
$i += $packlength;
}
while (@data) {
my $doc = shift @data;
my $count = shift @data;
unless (defined $out->{$doc}) { $out->{$doc} = 0; }
$out->{$doc} += $count;
}
}
$sth->finish;
}
$out;
}
*parse_and_index_data = \&DBIx::FullTextSearch::parse_and_index_data_count;
1;
|