/[Search-Estraier]/trunk/scripts/dbi-indexer.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/scripts/dbi-indexer.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 183 - (hide annotations)
Thu Aug 31 14:43:06 2006 UTC (17 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 2173 byte(s)
separate authorisation for estraier (estuser,estpasswd) and database (dbuser,dbpasswd)

1 dpavlin 131 #!/usr/bin/perl -w
2    
3     use strict;
4 dpavlin 144 use Search::Estraier 0.06;
5 dpavlin 131 use DBI;
6     use Data::Dumper;
7     use Encode qw/from_to/;
8 dpavlin 146 use Time::HiRes qw/time/;
9 dpavlin 159 use Getopt::Long;
10 dpavlin 131
11     =head1 NAME
12    
13     dbi-indexer.pl - example indexer of DBI sources for Search::Estraier
14    
15     =cut
16    
17 dpavlin 144 my $c = {
18     node_url => 'http://localhost:1978/node/dbi',
19     dbi => 'Pg:dbname=azop',
20     sql => qq{
21     select * from history_collection_view_cache
22     },
23 dpavlin 147 pk_col => '_id',
24 dpavlin 145 db_encoding => 'iso-8859-2',
25 dpavlin 144 debug => 0,
26 dpavlin 159 user => 'admin',
27     passwd => 'admin',
28 dpavlin 131 };
29    
30 dpavlin 183 GetOptions($c, qw/node_url=s sql=s pk_col=s eb_encoding=s debug+ estuser=s estpasswd=s dbuser=s dbpasswd=s/);
31 dpavlin 159
32     warn "# c: ", Dumper($c) if ($c->{debug});
33    
34 dpavlin 131 # create and configure node
35     my $node = new Search::Estraier::Node(
36 dpavlin 144 url => $c->{node_url},
37 dpavlin 183 user => $c->{estuser},
38     passwd => $c->{estpasswd},
39 dpavlin 131 croak_on_error => 1,
40 dpavlin 144 create => 1,
41 dpavlin 159 debug => $c->{debug} >= 4 ? 1 : 0,
42 dpavlin 131 );
43    
44     # create DBI connection
45 dpavlin 183 my $dbh = DBI->connect("DBI:$c->{dbi}", $c->{dbuser}, $c->{dbpasswd}) || die $DBI::errstr;
46 dpavlin 131
47 dpavlin 144 my $sth = $dbh->prepare($c->{sql}) || die $dbh->errstr();
48 dpavlin 131 $sth->execute() || die $sth->errstr();
49    
50 dpavlin 144 warn "# columns: ",join(",",@{ $sth->{NAME} }),"\n" if ($c->{debug});
51 dpavlin 131
52     my $total = $sth->rows;
53     my $i = 1;
54    
55 dpavlin 146 my $t = time();
56 dpavlin 147 my $pk_col = $c->{pk_col} || 'id';
57 dpavlin 146
58 dpavlin 131 while (my $row = $sth->fetchrow_hashref() ) {
59    
60 dpavlin 159 warn "# row: ",Dumper($row) if ($c->{debug} >= 3);
61 dpavlin 131
62     # create document
63     my $doc = new Search::Estraier::Document;
64    
65 dpavlin 147 if (my $id = $row->{$pk_col}) {
66     $doc->add_attr('@uri', $id);
67     } else {
68     die "can't find pk_col column '$pk_col' in results\n";
69     }
70 dpavlin 131
71     printf "%4d ",$i;
72    
73     while (my ($col,$val) = each %{$row}) {
74    
75     if ($val) {
76     # change encoding?
77 dpavlin 145 from_to($val, ($c->{db_encoding} || 'ISO-8859-1'), 'UTF-8');
78 dpavlin 131
79     # add attributes (make column usable from attribute search)
80     $doc->add_attr($col, $val);
81    
82     # add body text to document (make it searchable using full-text index)
83     $doc->add_text($val);
84    
85     print "R";
86     } else {
87     print ".";
88     }
89    
90     }
91    
92 dpavlin 159 warn "# doc draft: ",$doc->dump_draft, "\n" if ($c->{debug} >= 2);
93 dpavlin 131
94     die "error: ", $node->status,"\n" unless (eval { $node->put_doc($doc) });
95 dpavlin 146
96     printf (" %d%% %.1f/s\n", int(( $i++ / $total) * 100), ( $i / (time() - $t) ) );
97    
98 dpavlin 131 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26