9 use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
12 #Give a hoot don't pollute, do not export more than needed by default
20 MARC::Fast - Very fast implementation of MARC database reader
29 This is very fast alternative to C<MARC> and C<MARC::Record> modules.
31 It's is also very sutable for random access to MARC records (as opposed to
40 my $marc = new MARC::Fast(
41 marcdb => 'unimarc.iso',
46 my ($t, $record_number) = @_;
54 ################################################## subroutine header end ##
60 bless ($self, $class);
62 croak "need marcdb parametar" unless ($self->{marcdb});
64 print STDERR "# opening ",$self->{marcdb},"\n" if ($self->{debug});
66 open($self->{fh}, $self->{marcdb}) || croak "can't open ",$self->{marcdb},": $!";
71 while (! eof($self->{fh})) {
74 # save record position
75 push @{$self->{fh_offset}}, tell($self->{fh});
78 my $len = read($self->{fh}, $leader, 24);
81 carp "short read of leader, aborting\n";
88 # 5 Status (n=new, c=corrected and d=deleted)
89 # 6 Type of Record (a=printed material)
90 # 7 Bibliographic Level (m=monograph)
92 # 10 Indictator count (2 for monographs)
93 # 11 Subfield code count (2 - 0x1F+subfield code itself)
94 # 12-16 Base address of data
95 # 17 Encoding level (blank=full level, 1=sublevel 1, 2=sublevel 2,
97 # 18 Descriptive Cataloguing Form (blank=record is full ISBD,
98 # n=record is in non-ISBD format, i=record is in
99 # an incomplete ISBD format)
101 # 20 Length of length field in directory (always 4 in UNIMARC)
102 # 21 Length of Starting Character Position in directory (always
104 # 22 Length of implementation defined portion in directory (always
108 # |0 45 89 |12 16|1n 450 |
109 # |xxxxxnam 22(.....) 45 <---
111 print STDERR "REC ",$self->{count},": $leader\n" if ($self->{debug});
113 # store leader for later
114 push @{$self->{leaders}}, $leader;
116 # skip to next record
117 my $o = substr($leader,0,5);
119 seek($self->{fh},$o-24,1) if ($o);
131 Return number of records in database
139 return $self->{count};
144 Fetch record from database
146 my $hash = $marc->fetch(42);
153 my $rec_nr = shift || return;
155 my $leader = $self->{leaders}->[$rec_nr - 1];
157 carp "can't find record $rec_nr";
160 my $offset = $self->{fh_offset}->[$rec_nr - 1];
161 unless (defined($offset)) {
162 carp "can't find offset for record $rec_nr";
166 my $reclen = substr($leader,0,5);
167 my $base_addr = substr($leader,12,5);
169 print STDERR "# $rec_nr leader: '$leader' reclen: $reclen base addr: $base_addr [dir: ",$base_addr - 24,"]\n" if ($self->{debug});
173 print STDERR "# seeking to $offset + 24\n" if ($self->{debug});
175 if ( ! seek($self->{fh}, $offset+24, 0) ) {
176 carp "can't seek to $offset: $!";
180 print STDERR "# reading ",$base_addr-24," bytes of dictionary\n" if ($self->{debug});
183 if( ! read($self->{fh},$directory,$base_addr-24) ) {
184 carp "can't read directory: $!";
187 print STDERR "# $rec_nr directory: [",length($directory),"] '$directory'\n" if ($self->{debug});
190 print STDERR "# reading ",$reclen-$base_addr," bytes of fields\n" if ($self->{debug});
193 if( ! read($self->{fh},$fields,$reclen-$base_addr) ) {
194 carp "can't read fields: $!";
197 print STDERR "# $rec_nr fields: '$fields'\n" if ($self->{debug});
202 while (!$skip && $directory =~ s/(\d{3})(\d{4})(\d{5})//) {
203 my ($tag,$len,$addr) = ($1,$2,$3);
205 if (($addr+$len) > length($fields)) {
206 print STDERR "WARNING: error in dictionary on record $rec_nr skipping...\n" if (! $self->{quiet});
212 my $f = substr($fields,$addr,$len);
213 print STDERR "tag/len/addr $tag [$len] $addr: '$f'\n" if ($self->{debug});
215 push @{ $row->{$tag} }, $f;
217 my $del = substr($fields,$addr+$len-1,1);
219 # check field delimiters...
220 if ($self->{assert} && $del ne chr(30)) {
221 print STDERR "WARNING: skipping record $rec_nr, can't find delimiter 30 got: '$del'\n" if (! $self->{quiet});
226 if ($self->{assert} && length($f) < 2) {
227 print STDERR "WARNING: skipping field $tag from record $rec_nr because it's too short!\n" if (! $self->{quiet});
239 Read record with specified MFN and convert it to hash
241 my $hash = $marc->to_hash($mfn);
243 It has ability to convert characters (using C<hash_filter>) from MARC
244 database before creating structures enabling character re-mapping or quick
247 This function returns hash which is like this:
254 'f' => 'Valdo D\'Arienzo',
255 'e' => 'tipografie e tipografi nel XVI secolo',
259 This method will also create additional field C<000> with MFN.
266 my $mfn = shift || confess "need mfn!";
268 # init record to include MFN as field 000
269 my $rec = { '000' => [ $mfn ] };
271 my $row = $self->fetch($mfn) || return;
273 foreach my $rec_nr (keys %{$row}) {
274 foreach my $l (@{$row->{$rec_nr}}) {
280 $l = $self->{'hash_filter'}->($l, $rec_nr) if ($self->{'hash_filter'});
285 ($val->{'i1'},$val->{'i2'}) = ($1,$2) if ($l =~ s/^([01 #])([01 #])\x1F/\x1F/);
289 foreach my $t (split(/\x1F/,$l)) {
291 my $f = substr($t,0,1);
292 # repeatable subfileds. When we hit first one,
293 # store CURRENT (up to that) in first repetition
294 # of this record. Then, new record with same
295 # identifiers will be created.
297 push @{$rec->{$rec_nr}}, $val;
303 $val->{substr($t,0,1)} = substr($t,1);
309 push @{$rec->{$rec_nr}}, $val;
318 print $marc->to_ascii( 42 );
325 my $mfn = shift || confess "need mfn";
326 my $row = $self->fetch($mfn) || return;
330 foreach my $f (sort keys %{$row}) {
331 my $dump = join('', @{ $row->{$f} });
333 $dump =~ s/\x1f/\$/g;
334 $out .= "$f\t$dump\n";
348 http://www.rot13.org/~dpavlin/
352 This program is free software; you can redistribute
353 it and/or modify it under the same terms as Perl itself.
355 The full text of the license can be found in the
356 LICENSE file included with this module.
361 L<Biblio::Isis>, perl(1).