aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArne Georg Gleditsch <argggh@lxr.linpro.no>2007-11-27 01:10:58 +0100
committerArne Georg Gleditsch <argggh@lxr.linpro.no>2007-11-27 01:10:58 +0100
commit77f76725888920d10b527db5f291d704f0f5b26d (patch)
tree1c0e1874bcdc90352548f5125cbdb96643aa5002
parent5c682b22600bbd08888af07f93422c1cd733609d (diff)
Rework usage stats.
-rwxr-xr-xcgi-bin/lxr3
-rw-r--r--lib/LXRng/Context.pm1
-rw-r--r--lib/LXRng/Index/DBI.pm43
-rw-r--r--lib/LXRng/Index/Generic.pm7
-rw-r--r--lib/LXRng/Index/Pg.pm20
-rw-r--r--lib/LXRng/Index/PgBatch.pm5
6 files changed, 45 insertions, 34 deletions
diff --git a/cgi-bin/lxr b/cgi-bin/lxr
index 28d6635..375b640 100755
--- a/cgi-bin/lxr
+++ b/cgi-bin/lxr
@@ -331,8 +331,9 @@ sub search {
'idents' => \@cooked};
}
if ($type eq 'ident') {
+ my $usage = $context->config->{'usage'};
my ($symname, $symid, $ident, $refs) =
- $index->get_identifier_info($find, $rel_id);
+ $index->get_identifier_info($usage, $find, $rel_id);
$$ident[1] = $LXRng::Lang::deftypes{$$ident[1]};
$$ident[5] &&= $LXRng::Lang::deftypes{$$ident[5]};
diff --git a/lib/LXRng/Context.pm b/lib/LXRng/Context.pm
index dab03e3..3bc7a92 100644
--- a/lib/LXRng/Context.pm
+++ b/lib/LXRng/Context.pm
@@ -41,6 +41,7 @@ sub new {
unless ref($config[0]) eq 'HASH' and exists($config[0]{$tree});
$$self{'config'} = $config[0]{$tree};
+ $$self{'config'}{'usage'} ||= $$self{'config'}{'index'};
}
if (exists $$self{'params'}{'v'} and $$self{'params'}{'v'}) {
diff --git a/lib/LXRng/Index/DBI.pm b/lib/LXRng/Index/DBI.pm
index 932202c..2b796bd 100644
--- a/lib/LXRng/Index/DBI.pm
+++ b/lib/LXRng/Index/DBI.pm
@@ -334,19 +334,6 @@ sub _symbols_by_file {
return \%res;
}
-sub _add_usage {
- my ($self, $file_id, $line, $symbol_id) = @_;
-
- my $dbh = $self->dbh;
- my $pre = $self->prefix;
- my $sth = $$self{'sth'}{'_add_usage'} ||=
- $dbh->prepare(qq{insert into ${pre}usage(id_rfile, line, id_symbol)
- values (?, ?, ?)});
- $sth->execute($file_id, $line, $symbol_id);
-
- return 1;
-}
-
sub _usage_by_file {
my ($self, $rfile_id) = @_;
@@ -455,21 +442,28 @@ sub get_symbol_usage {
return undef unless $symid =~ /^\d+$/s;
my $sth =
$dbh->prepare(qq{
- select u.id_rfile, u.line
- from ${pre}usage u, ${pre}filereleases fr
+ select f.path, u.lines
+ from ${pre}usage u, ${pre}filereleases fr,
+ ${pre}files f, ${pre} revisions r
where u.id_symbol = $symid
and u.id_rfile = fr.id_rfile and fr.id_release = ?
+ and u.id_rfile = r.id and r.id_file = f.id
limit 1000});
$sth->execute($rel_id);
my $res = $sth->fetchall_arrayref();
$sth->finish();
- return $res;
+ my %rlines;
+ foreach my $r (@$res) {
+ $rlines{$$r[0]} = [$$r[1] =~ /(\d+),?/g];
+ }
+
+ return \%rlines;
}
sub get_identifier_info {
- my ($self, $ident, $rel_id) = @_;
+ my ($self, $usage, $ident, $rel_id) = @_;
my $dbh = $self->dbh;
my $pre = $self->prefix;
@@ -499,15 +493,16 @@ sub get_identifier_info {
$sth->fetchrow_array();
$sth->finish();
- my $refs = {$rfile_id => $path};
- $self->get_referring_files($rel_id, $rfile_id, $refs);
- my $usage = $self->get_symbol_usage($rel_id, $symid);
+ my $incs = {$rfile_id => $path};
+ $self->get_referring_files($rel_id, $rfile_id, $incs);
+ my %paths; @paths{values %$incs} = ();
+ my $refs = $usage->get_symbol_usage($rel_id, $symid);
my %reflines;
- foreach my $u (@$usage) {
- next unless $$refs{$$u[0]};
- $reflines{$$refs{$$u[0]}} ||= [];
- push(@{$reflines{$$refs{$$u[0]}}}, $$u[1]);
+ my ($p, $l);
+ while (($p, $l) = each %$refs) {
+ next unless exists $paths{$p};
+ $reflines{$p} = $l;
}
return ($symname, $symid,
diff --git a/lib/LXRng/Index/Generic.pm b/lib/LXRng/Index/Generic.pm
index cab2513..1cdaab5 100644
--- a/lib/LXRng/Index/Generic.pm
+++ b/lib/LXRng/Index/Generic.pm
@@ -144,13 +144,12 @@ sub symbols_by_file {
}
sub add_usage {
- my ($self, $file_id, $line, $symbol) = @_;
+ my ($self, $doc, $file_id, $sym_id, $lines) = @_;
- my $sym_id = $self->symbol_id($symbol, 1);
-
- return $self->_add_usage($file_id, $line, $sym_id);
+ return $self->_add_usage($file_id, $sym_id, $lines);
}
+# TODO: What functionality actually uses this? Can it be removed?
sub usage_by_file {
my ($self, $tree, $release, $path) = @_;
diff --git a/lib/LXRng/Index/Pg.pm b/lib/LXRng/Index/Pg.pm
index 1b905c0..a385767 100644
--- a/lib/LXRng/Index/Pg.pm
+++ b/lib/LXRng/Index/Pg.pm
@@ -154,9 +154,9 @@ sub init_db {
$dbh->do(qq{
create table ${pre}usage
(
- id_rfile int references ${pre}revisions(id) deferrable,
- id_symbol int references ${pre}symbols(id) deferrable,
- line int
+ id_rfile int,
+ id_symbol int,
+ lines int[]
)
}) or die($dbh->errstr);
@@ -407,6 +407,20 @@ sub _add_ident {
return $id;
}
+sub _add_usage {
+ my ($self, $file_id, $symbol_id, $lines) = @_;
+
+ my $dbh = $self->dbh;
+ my $pre = $self->prefix;
+ my $sth = $$self{'sth'}{'_add_usage'} ||=
+ $dbh->prepare(qq{insert into ${pre}usage(id_rfile, id_symbol, lines)
+ values (?, ?, ?)});
+ $sth->execute($file_id, $symbol_id, '{'.join(',', @$lines).'}');
+
+ return 1;
+}
+
+
sub DESTROY {
my ($self) = @_;
diff --git a/lib/LXRng/Index/PgBatch.pm b/lib/LXRng/Index/PgBatch.pm
index 19c9fa9..40654d2 100644
--- a/lib/LXRng/Index/PgBatch.pm
+++ b/lib/LXRng/Index/PgBatch.pm
@@ -146,9 +146,10 @@ sub _prime_symbol_cache {
}
sub _add_usage {
- my ($self, $file_id, $line, $symbol_id) = @_;
+ my ($self, $file_id, $symbol_id, $lines) = @_;
- push(@{$self->_cache('usage')}, "$file_id\t$symbol_id\t$line\n");
+ push(@{$self->_cache('usage')},
+ "$file_id\t$symbol_id\t\{".join(",", @$lines)."}\n");
return 1;
}