/[webpac2]/trunk/lib/WebPAC/Input/ISIS.pm
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/lib/WebPAC/Input/ISIS.pm

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

revision 9 by dpavlin, Sat Jul 16 17:14:43 2005 UTC revision 265 by dpavlin, Fri Dec 16 16:23:44 2005 UTC
# Line 62  from database (so you can skip beginning Line 62  from database (so you can skip beginning
62  If optional parametar C<limit_mfn> is set, it will read just 500 records  If optional parametar C<limit_mfn> is set, it will read just 500 records
63  from database in example above.  from database in example above.
64    
65  Returns number of last record read into memory (size of database, really).  Returns size of database, regardless of C<start_mfn> and C<limit_mfn>
66    parametars, see also C<$isis->size>.
67    
68  =cut  =cut
69    
# Line 75  sub open { Line 76  sub open {
76          $log->logcroak("need filename") if (! $arg->{'filename'});          $log->logcroak("need filename") if (! $arg->{'filename'});
77          my $code_page = $arg->{'code_page'} || '852';          my $code_page = $arg->{'code_page'} || '852';
78    
         $log->logdie("can't find database ",$arg->{'filename'}) unless (glob($arg->{'filename'}.'.*'));  
   
79          # store data in object          # store data in object
         $self->{'isis_filename'} = $arg->{'filename'};  
80          $self->{'isis_code_page'} = $code_page;          $self->{'isis_code_page'} = $code_page;
81            foreach my $v (qw/isis_filename start_mfn limit_mfn/) {
82          #$self->{'isis_code_page'} = $code_page;                  $self->{$v} = $arg->{$v} if ($arg->{$v});
83            }
84    
85          # create Text::Iconv object          # create Text::Iconv object
86          my $cp = Text::Iconv->new($code_page,$self->{'code_page'});          my $cp = Text::Iconv->new($code_page,$self->{'code_page'});
# Line 89  sub open { Line 88  sub open {
88          $log->info("reading ISIS database '",$arg->{'filename'},"'");          $log->info("reading ISIS database '",$arg->{'filename'},"'");
89          $log->debug("isis code page: $code_page");          $log->debug("isis code page: $code_page");
90    
91          my ($isis_db,$maxmfn);          my ($isis_db,$db_size);
92    
93          if ($have_openisis) {          if ($have_openisis) {
94                  $log->debug("using OpenIsis perl bindings");                  $log->debug("using OpenIsis perl bindings");
95                  $isis_db = OpenIsis::open($arg->{'filename'});                  $isis_db = OpenIsis::open($arg->{'filename'});
96                  $maxmfn = OpenIsis::maxRowid( $isis_db ) || 1;                  $db_size = OpenIsis::maxRowid( $isis_db ) || 1;
97          } elsif ($have_biblio_isis) {          } elsif ($have_biblio_isis) {
98                  $log->debug("using Biblio::Isis");                  $log->debug("using Biblio::Isis");
99                  use Biblio::Isis;                  use Biblio::Isis;
# Line 106  sub open { Line 105  sub open {
105                                  $l = $cp->convert($l);                                  $l = $cp->convert($l);
106                                  return $l;                                  return $l;
107                          },                          },
108                  );                  ) or $log->logdie("can't find database ",$arg->{'filename'});
109                  $maxmfn = $isis_db->count;  
110                    $db_size = $isis_db->count;
111    
112                  unless ($maxmfn) {                  unless ($db_size) {
113                          $log->logwarn("no records in database ", $arg->{'filename'}, ", skipping...");                          $log->logwarn("no records in database ", $arg->{'filename'}, ", skipping...");
114                          return;                          return;
115                  }                  }
# Line 120  sub open { Line 120  sub open {
120    
121    
122          my $startmfn = 1;          my $startmfn = 1;
123            my $maxmfn = $db_size;
124    
125          if (my $s = $self->{'start_mfn'}) {          if (my $s = $self->{'start_mfn'}) {
126                  $log->info("skipping to MFN $s");                  $log->info("skipping to MFN $s");
# Line 128  sub open { Line 129  sub open {
129                  $self->{'start_mfn'} = $startmfn;                  $self->{'start_mfn'} = $startmfn;
130          }          }
131    
132          $maxmfn = $startmfn + $self->{limit_mfn} if ($self->{limit_mfn});          if ($self->{limit_mfn}) {
133                    $log->info("limiting to ",$self->{limit_mfn}," records");
134                    $maxmfn = $startmfn + $self->{limit_mfn} - 1;
135                    $maxmfn = $db_size if ($maxmfn > $db_size);
136            }
137    
138            # store size for later
139            $self->{'size'} = ($maxmfn - $startmfn) ? ($maxmfn - $startmfn + 1) : 0;
140    
141          $log->info("processing ",($maxmfn-$startmfn)." records using ",( $have_openisis ? 'OpenIsis' : 'Biblio::Isis'));          $log->info("processing ",($maxmfn-$startmfn)." records using ",( $have_openisis ? 'OpenIsis' : 'Biblio::Isis'));
142    
# Line 172  sub open { Line 180  sub open {
180                          $log->logdie("hum? implementation missing?");                          $log->logdie("hum? implementation missing?");
181                  }                  }
182    
183                  $log->confess("record $mfn empty?") unless ($rec);                  if (! $rec) {
184                            $log->warn("record $mfn empty? skipping...");
185                            next;
186                    }
187    
188                  # store                  # store
189                  if ($self->{'low_mem'}) {                  if ($self->{'low_mem'}) {
# Line 182  sub open { Line 193  sub open {
193                  }                  }
194    
195                  # create lookup                  # create lookup
196                  $self->{'lookup'}->add( $rec ) if ($self->{'lookup'} && can($self->{'lookup'}->add));                  $self->{'lookup'}->add( $rec ) if ($rec && $self->{'lookup'});
197    
198                  $self->progress_bar($mfn,$maxmfn);                  $self->progress_bar($mfn,$maxmfn);
199    
# Line 194  sub open { Line 205  sub open {
205          $log->debug("max mfn: $maxmfn");          $log->debug("max mfn: $maxmfn");
206    
207          # store max mfn and return it.          # store max mfn and return it.
208          return $self->{'max_mfn'} = $maxmfn;          $self->{'max_mfn'} = $maxmfn;
209    
210            return $db_size;
211  }  }
212    
213  =head2 fetch_rec  =head2 fetch
214    
215  Fetch next record from database. It will also displays progress bar.  Fetch next record from database. It will also displays progress bar.
216    
217   my $rec = $webpac->fetch_rec;   my $rec = $isis->fetch;
218    
219  You should rearly have the need to call this function directly. Instead use  Record from this function should probably go to C<data_structure> for
220  C<fetch_data_structure> which returns normalised data.  normalisation.
221    
222  =cut  =cut
223    
224  sub fetch_rec {  sub fetch {
225          my $self = shift;          my $self = shift;
226    
227          my $log = $self->_get_logger();          my $log = $self->_get_logger();
# Line 231  sub fetch_rec { Line 244  sub fetch_rec {
244    
245          $self->progress_bar($mfn,$self->{'max_mfn'});          $self->progress_bar($mfn,$self->{'max_mfn'});
246    
247            my $rec;
248    
249          if ($self->{'low_mem'}) {          if ($self->{'low_mem'}) {
250                  return $self->{'db'}->get($mfn);                  $rec = $self->{'db'}->get($mfn);
251          } else {          } else {
252                  return $self->{'data'}->{$mfn};                  $rec = $self->{'data'}->{$mfn};
253          }          }
254    
255            $rec ||= 0E0;
256  }  }
257    
258  =head2 fetch_data_structure  =head2 pos
259    
260    Returns current record number (MFN).
261    
262  Fetch data structure of next record from database.   print $isis->pos;
263    
264   my @ds = $webpac->fetch_data_structure;  First record in database has position 1.
265    
266  =cut  =cut
267    
268  sub fetch_data_structure {  sub pos {
269          my $self = shift;          my $self = shift;
270            return $self->{'current_mfn'};
271    }
272    
273    
274    =head2 size
275    
276          return $self->data_structure(  Returns number of records in database
277                  $self->fetch_rec(@_)  
278          );   print $isis->size;
279    
280    Result from this function can be used to loop through all records
281    
282     foreach my $mfn ( 1 ... $isis->size ) { ... }
283    
284    because it takes into account C<start_mfn> and C<limit_mfn>.
285    
286    =cut
287    
288    sub size {
289            my $self = shift;
290            return $self->{'size'};
291  }  }
292    
293  =head2 mfn  =head2 seek
294    
295  Returns current record number (MFN).  Seek to specified MFN in file.
296    
297     $isis->seek(42);
298    
299   print $webpac->mfn;  First record in database has position 1.
300    
301  =cut  =cut
302    
303  sub mfn {  sub seek {
304          my $self = shift;          my $self = shift;
305          return $self->{'current_mfn'};          my $pos = shift || return;
306    
307            my $log = $self->_get_logger();
308    
309            if ($pos < 1) {
310                    $log->warn("seek before first record");
311                    $pos = 1;
312            } elsif ($pos > $self->{'max_mfn'}) {
313                    $log->warn("seek beyond last record");
314                    $pos = $self->{'max_mfn'};
315            }
316    
317            return $self->{'current_mfn'} = (($pos - 1) || -1);
318  }  }
319    
320  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.9  
changed lines
  Added in v.265

  ViewVC Help
Powered by ViewVC 1.1.26