/[wait]/trunk/script/index_ora
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/script/index_ora

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 62 by ulpfr, Fri Jan 4 15:11:54 2002 UTC revision 75 by laperla, Thu Mar 14 17:27:22 2002 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2  #                              -*- Mode: Perl -*-  #                              -*- Mode: Perl -*-
3  # $Basename$  # $Basename$
4  # $Revision: 1.3 $  # $Revision: 1.12 $
5  # Author          : Ulrich Pfeifer  # Author          : Ulrich Pfeifer
6  # Created On      : Mon Dec 31 13:57:11 2001  # Created On      : Mon Dec 31 13:57:11 2001
7  # Last Modified By: Ulrich Pfeifer  # Last Modified By: Ulrich Pfeifer
8  # Last Modified On: Fri Jan  4 15:59:20 2002  # Last Modified On: Fri Jan  4 15:59:20 2002
9  # Language        : CPerl  # Language        : CPerl
10  #  #
11  # (C) Copyright 2001, UUNET Deutschland GmbH, Germany  # (C) Copyright 2001, Ulrich Pfeifer
12  #  #
13    
14    use 5.007;
15    
16  use strict;  use strict;
17    use Devel::Peek qw(Dump);
18    
19  use File::Path;  use File::Path;
20  use DB_File;  use DB_File;
21  use Getopt::Long;  use Getopt::Long;
22  use Cwd;  use Cwd;
23    
24  require WAIT::Config;  BEGIN {require WAIT::Config;}
25  require WAIT::Database;  use WAIT::Database;
26  require WAIT::Parse::Ora;  use WAIT::Parse::Ora;
27  require WAIT::Document::Ora;  use WAIT::Document::Ora;
28  require WAIT::InvertedIndex;  use WAIT::InvertedIndex;
29    use Data::Dumper;
30    
31    
32  $DB_BTREE->{'cachesize'} = 200_000 ;  $DB_BTREE->{'cachesize'} = 200_000 ;
33    
34  my %OPT = (clean    => 0,  use lib "/usr/local/apache/lib";
35             database => 'DB',  use lib "/online/www/sites/ora/catalogsearch/run/lib";
36             dir      => $WAIT::Config->{WAIT_home} || '/tmp',  use oreilly_de_catalog::config;
37    use oreilly_de_catalog::wait_filter;
38    
39    my %OPT = (
40               database => 'oreilly_de_catalog',
41               dir      => oreilly_de_catalog::config::WAITDIR,
42             table    => 'ora',             table    => 'ora',
43            );            );
44    
45    my $droot = oreilly_de_catalog::config::CATALOG;
46    
47  GetOptions(\%OPT,  GetOptions(\%OPT,
            'clean!',  
48             'database=s',             'database=s',
49             'dir=s',             'dir=s',
50             'table=s',             'table=s',
51            ) || die "Usage: ...\n";            ) || die "Usage: ...\n";
52    
53  if ($OPT{clean} and -d "$OPT{dir}/$OPT{database}") {  my @localtime = localtime;
54    my $tmp = WAIT::Database->open(name        => $OPT{database},  $localtime[5] += 1900;
55                                   'directory' => $OPT{dir})  $localtime[4]++;
56      or die "Could not open table $OPT{table}: $@\n";  my $jobid = sprintf "%04s-%02s-%02s_%02s:%02s_%d", @localtime[5,4,3,2,1], $$;
57    my $tbl = $tmp->table(name => $OPT{table});  my $db = WAIT::Database->create(name      => "$OPT{database}-$jobid",
58    $tbl->drop if $tbl;                                  directory => $OPT{dir})
59    rmtree("$OPT{dir}/$OPT{database}/$OPT{table}", 1, 1)      or die "Could not create database $OPT{database}: $@\n";
     if -d "$OPT{dir}/$OPT{database}/$OPT{table}";  
   $tmp->close;  
 }  
   
 my $db;  
 unless (-d "$OPT{dir}/$OPT{database}") {  
   $db = WAIT::Database->create(name       => $OPT{database},  
                               'directory' => $OPT{dir})  
     or die "Could not open database $OPT{database}: $@\n";  
 }  
 else {  
   $db = WAIT::Database->open(name        => $OPT{database},  
                              'directory' => $OPT{dir})  
     or die "Could not open table $OPT{table}: $@\n";  
 }  
60    
61  my $layout = new WAIT::Parse::Ora;  my $layout = new WAIT::Parse::Ora;
62    
63  my $stem  = ['isotr', 'isolc', 'split2', 'stop', 'Stem'];  my $stem  = ['OR_tr_20020124', 'OR_lc_20020125', 'split2', 'stop', 'Stem'];
64  my $text  = [{  # my $text  = ['OR_tr_20020124', 'split2', 'OR_minus_20020311', 'OR_lc_20020125', 'split2', 'stop'];
65                'prefix'    => ['isotr', 'isolc'],  my $text  = ['OR_tr_20020124', 'split', 'OR_minus_20020311', 'OR_lc_20020125'];
66                'intervall' => ['isotr', 'isolc'],  my $wplus = ['split', 'OR_lc_20020125', 'OR_mixedonly_20020221'];
67               },  my $sound = ['OR_tr_20020124', 'OR_lc_20020125', 'split2', 'Soundex'];
68               'isotr', 'isolc', 'split2', 'stop'];  my $trigr = ['OR_lc_20020125', 'OR_trigrams_20020125'];
69  my $sound = ['isotr', 'isolc', 'split2', 'Soundex'],;  # split6 is better than split13 or split10: it allows them to enter
70    # shorter sequences when searching.
71    my $isbn  = ['split6', 'OR_isbn_20020127'];
72    
73  my $cwd = cwd;  my $cwd = cwd;
74    
75  my %D;  my %D;
76  my $access = tie %D, 'WAIT::Document::Ora', @ARGV,  my $access = tie %D, 'WAIT::Document::Ora', $droot,
77    or die "Couldn't tie to file: $!\n";    or die "Couldn't tie to dir $droot: $!\n";
78    
79  my $tb = $db->create_table(name     => $OPT{table},  my $tb = $db->create_table(name     => $OPT{table},
80                             attr     => ['author', 'isbn', 'title',                             attr     => ['author', 'isbn', 'title',
# Line 85  my $tb = $db->create_table(name     => $ Line 83  my $tb = $db->create_table(name     => $
83                             access   => $access,                             access   => $access,
84                             invindex =>                             invindex =>
85                             [                             [
86                              'title'  => $stem,                              'aboutauthor'  => $text,
87                              'about'  => $stem,                              'aboutauthor'  => $wplus,
88                              'text'   => $text,                              'abouttranslator'  => $text,
89                                'abouttranslator'  => $wplus,
90                                'abstract' => $text,
91                                'abstract' => $wplus,
92                              'author' => $text,                              'author' => $text,
93                              'author' => $sound,                              'colophon' => $text,
94                              'isbn'   => $text,                              'colophon' => $wplus,
95                                'desc'   => $text,
96                                'desc'   => $wplus,
97                                'inx'   => $text,
98                                'inx'   => $wplus,
99                                'isbn'   => $isbn,
100                                'subtitle'  => $text,
101                                'subtitle'  => $wplus,
102                                'title'  => $text,
103                                'title'  => $wplus,
104                                'title_orig'  => $text,
105                                'title_orig'  => $wplus,
106                                'toc'   => $text,
107                                'toc'   => $wplus,
108                                'translator'  => $text,
109                                'translator'  => $wplus,
110                             ]                             ]
111                            );                            );
112  die "Couldn't create table $OPT{table}: $@\n" unless $tb;  die "Couldn't create table $OPT{table}: $@\n" unless $tb;
113    
114  my ($did, $value);  my ($did, $value);
115  while (($did, $value) = each %D) {  binmode STDOUT, ":utf8";
116    my $ALL;
117    my $traceALL = 0; # expensive
118    my $done = 0;
119    my $todo = keys %D;
120    my $lasttimeround = my $starttime = time;
121    
122    DOC: while (($did, $value) = each %D) {
123      # next unless $did eq "jscook";
124    my $record   = $layout->split($value);    my $record   = $layout->split($value);
125    my $headline = $record->{title};    my $headline = $record->{title};
126    $headline =~ s/\s+/ /sg;    $headline =~ s/\s+/ /sg;
127    printf "%15s %s\n", $record->{isbn}, substr($headline,0,60);    # printf "%15s %s\n", $record->{isbn}, substr($headline,0,60);
128      printf "%15s %s...\n", $did, substr($headline,0,60);
129    $tb->insert('docid'  => $did,    $tb->insert('docid'  => $did,
130                headline => $headline,                headline => $headline,
131                %{$record});                %{$record});
132      $done++;
133      my $spenttime = time - $starttime;
134      my $averagetime = $spenttime/$done;
135      my $left = $todo-$done;
136      printf("%2d secs, %3d done, %3d left, %4d s done, %5.1f s avg, %4d s left\n",
137                   time - $lasttimeround,
138                   $done,
139                   $left,
140                   $spenttime,
141                   $averagetime,
142                   $left*$averagetime,
143                  );
144      $lasttimeround = time;
145      if ($traceALL) { # costs a lot when reaching the 100th file or so
146        $ALL->{$did} = $record;
147        open F, ">:utf8", "$OPT{dir}/$OPT{database}-$jobid/debug.dump" or die;
148        print F Data::Dumper::Dumper($ALL);
149        close F  or die "Couldn't close debug.dump: $!";;
150      }
151  }  }
152    undef $ALL;
153  $tb->set(top=>1);  $tb->set(top=>1);
154  $tb->close();  
155  $db->close();  my $tritb = $db->create_table(
156                                  name => "$OPT{table}_fallback",
157                                  attr => [qw(docid headline)], # name
158                                                                # "headline"
159                                                                # only for
160                                                                # sman
161                                  invindex => [ headline => $trigr ],
162                                 );
163    my %dict;
164    for my $f ($tb->fields) {
165      my(@idx) = @{$tb->table->{inverted}{$f} || []};
166      for my $idx (@idx) {
167        my $name = $idx->name;
168        next if $name =~ /(_|\b)(mixedonly|Stem|Soundex)(\b|_)/;
169                  # irrelevant for alternatives
170        my @keys = $idx->keys;
171        @dict{@keys} = ();
172      }
173    }
174    my @dictkeys = grep s/^p//, keys %dict;
175    my $maxdebug = 5;
176    for my $headline (@dictkeys) {
177      if ($maxdebug && $headline =~ /[^\040-\177]/) {
178        Dump $headline;
179        $maxdebug--;
180      }
181      # printf "%s\n", substr($headline,0,60);
182      $tritb->insert(docid => $headline, headline => $headline);
183    }
184    $tritb->set(top=>1);
185    $tritb->close or die "Couldn't close table: $!";
186    $tb->close() or die "Couldn't close table: $!";
187    $db->close() or die "Couldn't close database: $!";
188    
189    # Atomically relinking symlink: now we have a new database with a very
190    # long name "$OPT{database}-$jobid" (e.g.
191    # oreilly_de_catalog-2002-01-28_16:12_16467) and we want that database
192    # to be accessible with the oreilly_de_catalog name.
193    
194    use File::Spec;
195    chdir $OPT{dir} or die;
196    my $dir    = "$OPT{database}-$jobid";
197    my $slwant = File::Spec->catdir($OPT{dir}, $OPT{database});
198    my $sltmp  = File::Spec->catdir($OPT{dir}, "$OPT{database}-$$");
199    unlink $sltmp; # may fail
200    symlink $dir, $sltmp or die "Couldn't symlink $dir, $sltmp: $!";
201    rename $sltmp, $slwant or die "Couldn't rename $sltmp, $slwant: $!";
202    warn "$slwant now points to $dir";
203    system("chmod 777 $slwant/*/read")==0 or die;
204    
205    opendir DIR, "." or die "Could not opendir .: $!";
206    for my $dirent (readdir DIR) {
207      next if $dirent =~ /^\./;
208      next unless $dirent =~ /^$OPT{database}(.*)/;
209      my $ext = $1 or next;
210      next unless -M $dirent > 4;
211      warn "removing old index $dirent";
212      File::Path::rmtree($dirent);
213    }
214    closedir DIR;
215    
216  $WAIT::Config = $WAIT::Config; # make perl -w happy  $WAIT::Config = $WAIT::Config; # make perl -w happy
217    
# Line 124  index_ora - generate an WAIT index for O Line 228  index_ora - generate an WAIT index for O
228  =head1 SYNOPSIS  =head1 SYNOPSIS
229    
230  B<index_ora>  B<index_ora>
 [B<-clean>] [B<-noclean>]  
231  [B<-database> I<dbname>]  [B<-database> I<dbname>]
232  [B<-dir> I<directory>]  [B<-dir> I<directory>]
233  [B<-table> I<table name>]  [B<-table> I<table name>]
# Line 136  I<directory> Line 239  I<directory>
239    
240  =over 5  =over 5
241    
 =item B<-clean> / B<-noclean>  
   
 Clean the table before indexing. Default is B<off>.  
   
242  =item B<-database> I<dbname>  =item B<-database> I<dbname>
243    
244  Specify database name. Default is F<DB>.  Specify database name. Default is F<DB>.

Legend:
Removed from v.62  
changed lines
  Added in v.75

  ViewVC Help
Powered by ViewVC 1.1.26