merge-xref.pl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/perl
  2. use lib 'lib';
  3. use integer;
  4. use DB_File;
  5. use strict;
  6. use LXR::Common;
  7. use LXR::Config;
  8. use File::Basename;
  9. my ($tree, @others) = @ARGV;
  10. # this deals with an implementation detail of LXR::*::init;
  11. $ENV{'SCRIPT_NAME'} = '/' . $tree . '/' . basename($0);
  12. my ($Conf, $HTTP, $Path, $head) = &init($0);
  13. my %treemap = %{$Conf->{'treehash'}};
  14. die "Could not find target $tree" unless defined $treemap{$tree};
  15. my ($dbdir, @trees);
  16. $dbdir = (dirname $Conf->dbdir) . '/';
  17. foreach my $othertree (@others) {
  18. unless (defined $treemap{$othertree}) {
  19. print "could not find $othertree\n";
  20. next;
  21. }
  22. push @trees, $othertree;
  23. }
  24. print "Merging: ";
  25. {
  26. local $, = ', ';
  27. print @trees;
  28. }
  29. print " into $tree\n";
  30. my (%index, %filelist, %index_in, %filelist_in, $fileno);
  31. my $hash_params = new DB_File::HASHINFO;
  32. $hash_params->{'cachesize'} = 30000;
  33. $fileno = 0;
  34. sub merge_tree
  35. {
  36. my ($tree, $treedir, $treesrcdir) = @_;
  37. my $treebase = $tree.'/';
  38. return unless (
  39. tie(%index_in,
  40. "DB_File",
  41. $treedir."/xref",
  42. O_RDONLY,
  43. 0664,
  44. $hash_params)
  45. );
  46. unless (
  47. tie(%filelist_in,
  48. "DB_File",
  49. $treedir."/fileidx",
  50. O_RDONLY,
  51. undef,
  52. $hash_params)
  53. ) {
  54. untie %index_in;
  55. return;
  56. }
  57. my @filelisting = keys %filelist_in;
  58. foreach my $key (@filelisting) {
  59. $filelist{$fileno + $key} = $treebase . $filelist_in{$key};
  60. }
  61. untie(%filelist_in);
  62. foreach my $key (keys %index_in) {
  63. my $val = $index_in{$key};
  64. my @ids = split /\t/, $val;
  65. $val = '';
  66. foreach my $ref (@ids) {
  67. if ($ref =~ /^(.)(\d+)(:[0-9,]+)/) {
  68. $val .= $1 . ($fileno + $2) . "$3\t";
  69. }
  70. }
  71. $index{$key} .= $val;
  72. }
  73. $fileno += scalar @filelisting;
  74. untie(%index_in);
  75. symlink($treesrcdir, $Conf->sourceroot.'/'.$tree);
  76. }
  77. # dumpdb should move...
  78. sub dumpdb {
  79. my ($file, $dbref, $statusmsg, $modulus) = @_;
  80. my %indb = %{$dbref};
  81. my %outdb;
  82. tie (%outdb, 'DB_File', $file, O_RDWR|O_CREAT, 0664, $DB_HASH)
  83. || die("Could not open '$file' for writing");
  84. my ($i, $k, $v) = (0);
  85. while (($k, $v) = each(%indb)) {
  86. $i++;
  87. delete($indb{$k});
  88. $outdb{$k} = $v;
  89. unless (!$modulus || ($i % $modulus)) {
  90. printf STDERR $statusmsg, $i, $k, $v;
  91. }
  92. }
  93. untie(%outdb);
  94. }
  95. my $fileidxname = $Conf->dbdir . "/fileidx.out.$$";
  96. tie (%filelist, 'DB_File', $fileidxname, O_RDWR|O_CREAT, 0660, $DB_HASH)
  97. || die("Could not open '$fileidxname' for writing");
  98. foreach $tree (@trees) {
  99. merge_tree($tree, $dbdir.$tree, $treemap{$tree});
  100. }
  101. $dbdir = $Conf->dbdir;
  102. my $xreffilename = "$dbdir/xref.out.$$";
  103. dumpdb($xreffilename, \%index, 'Dumping identifier %d [%s => %s] to '.$xreffilename."\n", 1);
  104. dbmclose(%filelist);
  105. rename($fileidxname, $dbdir . '/fileidx');
  106. rename($xreffilename, $dbdir . '/xref');