DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (495129e490bc)

VCS Links

Line Code
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
#!/usr/bin/perl

use lib 'lib';
use integer;
use DB_File;
use strict;
use LXR::Common;
use LXR::Config;
use File::Basename;

my ($tree, @others) = @ARGV;
# this deals with an implementation detail of LXR::*::init;
$ENV{'SCRIPT_NAME'} = '/' . $tree . '/' . basename($0);
my ($Conf, $HTTP, $Path, $head) = &init($0);

my %treemap = %{$Conf->{'treehash'}};
die "Could not find target $tree" unless defined $treemap{$tree};

my ($dbdir, @trees);
$dbdir = (dirname $Conf->dbdir) . '/';
foreach my $othertree (@others) {
  unless (defined $treemap{$othertree}) {
    print "could not find $othertree\n";
    next;
  }
  push @trees, $othertree;
}

print "Merging: ";
{
  local $, = ', ';
  print @trees;
}
print " into $tree\n";

my (%index, %filelist, %index_in, %filelist_in, $fileno);
my $hash_params = new DB_File::HASHINFO;
$hash_params->{'cachesize'} = 30000;

$fileno = 0;

sub merge_tree
{
  my ($tree, $treedir, $treesrcdir) = @_;
  my $treebase = $tree.'/';
  return unless (
    tie(%index_in,
         "DB_File",
         $treedir."/xref",
         O_RDONLY,
         0664,
         $hash_params)
  );
  unless (
    tie(%filelist_in,
        "DB_File",
        $treedir."/fileidx",
        O_RDONLY,
        undef,
        $hash_params)
  ) {
    untie %index_in;
    return;
  }
  my @filelisting = keys %filelist_in;
  foreach my $key (@filelisting) {
    $filelist{$fileno + $key} = $treebase . $filelist_in{$key};
  }
  untie(%filelist_in);
  foreach my $key (keys %index_in) {
    my $val = $index_in{$key};
    my @ids = split /\t/, $val;
    $val = '';
    foreach my $ref (@ids) {
      if ($ref =~ /^(.)(\d+)(:[0-9,]+)/) {
        $val .= $1 . ($fileno + $2) . "$3\t";
      }
    }
    $index{$key} .= $val;
  }
  $fileno += scalar @filelisting;
  untie(%index_in);
  symlink($treesrcdir, $Conf->sourceroot.'/'.$tree);
}

# dumpdb should move...
sub dumpdb {
  my ($file, $dbref, $statusmsg, $modulus) = @_;
  my %indb = %{$dbref};
  my %outdb;
  tie (%outdb, 'DB_File', $file, O_RDWR|O_CREAT, 0664, $DB_HASH)
      || die("Could not open '$file' for writing");

  my ($i, $k, $v) = (0);
  while (($k, $v) = each(%indb)) {
    $i++;
    delete($indb{$k});
    $outdb{$k} = $v;
    unless (!$modulus || ($i % $modulus)) {
      printf STDERR $statusmsg, $i, $k, $v;
    }
  }

  untie(%outdb);
}

my $fileidxname = $Conf->dbdir . "/fileidx.out.$$";
tie (%filelist, 'DB_File', $fileidxname, O_RDWR|O_CREAT, 0660, $DB_HASH)
    || die("Could not open '$fileidxname' for writing");

foreach $tree (@trees) {
  merge_tree($tree, $dbdir.$tree, $treemap{$tree});
}

$dbdir = $Conf->dbdir;
my $xreffilename = "$dbdir/xref.out.$$";
dumpdb($xreffilename, \%index, 'Dumping identifier %d [%s => %s] to '.$xreffilename."\n", 1);
dbmclose(%filelist);
rename($fileidxname, $dbdir . '/fileidx');
rename($xreffilename, $dbdir . '/xref');