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',
49 ################################################## subroutine header end ##
55 bless ($self, $class);
57 croak "need marcdb parametar" unless ($self->{marcdb});
59 print STDERR "# opening ",$self->{marcdb},"\n" if ($self->{debug});
61 open($self->{fh}, $self->{marcdb}) || croak "can't open ",$self->{marcdb},": $!";
66 while (! eof($self->{fh})) {
69 # save record position
70 push @{$self->{fh_offset}}, tell($self->{fh});
73 my $len = read($self->{fh}, $leader, 24);
76 carp "short read of leader, aborting\n";
83 # 5 Status (n=new, c=corrected and d=deleted)
84 # 6 Type of Record (a=printed material)
85 # 7 Bibliographic Level (m=monograph)
87 # 10 Indictator count (2 for monographs)
88 # 11 Subfield code count (2 - 0x1F+subfield code itself)
89 # 12-16 Base address of data
90 # 17 Encoding level (blank=full level, 1=sublevel 1, 2=sublevel 2,
92 # 18 Descriptive Cataloguing Form (blank=record is full ISBD,
93 # n=record is in non-ISBD format, i=record is in
94 # an incomplete ISBD format)
96 # 20 Length of length field in directory (always 4 in UNIMARC)
97 # 21 Length of Starting Character Position in directory (always
99 # 22 Length of implementation defined portion in directory (always
103 # |0 45 89 |12 16|1n 450 |
104 # |xxxxxnam 22(.....) 45 <---
106 print STDERR "REC ",$self->{count},": $leader\n" if ($self->{debug});
108 # store leader for later
109 push @{$self->{leaders}}, $leader;
111 # skip to next record
112 my $o = substr($leader,0,5);
114 seek($self->{fh},$o-24,1) if ($o);
126 Return number of records in database
134 return $self->{count};
139 Fetch record from database
141 my $hash = $marc->fetch(42);
148 my $rec_nr = shift || return;
150 my $leader = $self->{leaders}->[$rec_nr - 1];
152 carp "can't find record $rec_nr";
155 my $offset = $self->{fh_offset}->[$rec_nr - 1];
156 unless (defined($offset)) {
157 carp "can't find offset for record $rec_nr";
161 my $reclen = substr($leader,0,5);
162 my $base_addr = substr($leader,12,5);
164 print STDERR "# $rec_nr leader: '$leader' reclen: $reclen base addr: $base_addr [dir: ",$base_addr - 24,"]\n" if ($self->{debug});
168 print STDERR "# seeking to $offset + 24\n" if ($self->{debug});
170 if ( ! seek($self->{fh}, $offset+24, 0) ) {
171 carp "can't seek to $offset: $!";
175 print STDERR "# reading ",$base_addr-24," bytes of dictionary\n" if ($self->{debug});
178 if( ! read($self->{fh},$directory,$base_addr-24) ) {
179 carp "can't read directory: $!";
182 print STDERR "# $rec_nr directory: [",length($directory),"] '$directory'\n" if ($self->{debug});
185 print STDERR "# reading ",$reclen-$base_addr," bytes of fields\n" if ($self->{debug});
188 if( ! read($self->{fh},$fields,$reclen-$base_addr) ) {
189 carp "can't read fields: $!";
192 print STDERR "# $rec_nr fields: '$fields'\n" if ($self->{debug});
197 while (!$skip && $directory =~ s/(\d{3})(\d{4})(\d{5})//) {
198 my ($tag,$len,$addr) = ($1,$2,$3);
200 if (($addr+$len) > length($fields)) {
201 print STDERR "WARNING: error in dictionary on record $rec_nr skipping...\n" if (! $self->{quiet});
207 my $f = substr($fields,$addr,$len);
208 print STDERR "tag/len/addr $tag [$len] $addr: '$f'\n" if ($self->{debug});
210 push @{ $row->{$tag} }, $f;
212 my $del = substr($fields,$addr+$len-1,1);
214 # check field delimiters...
215 if ($self->{assert} && $del ne chr(30)) {
216 print STDERR "WARNING: skipping record $rec_nr, can't find delimiter 30 got: '$del'\n" if (! $self->{quiet});
221 if ($self->{assert} && length($f) < 2) {
222 print STDERR "WARNING: skipping field $tag from record $rec_nr because it's too short!\n" if (! $self->{quiet});
234 Read record with specified MFN and convert it to hash
236 my $hash = $marc->to_hash($mfn);
238 It has ability to convert characters (using C<hash_filter>) from MARC
239 database before creating structures enabling character re-mapping or quick
242 This function returns hash which is like this:
249 'f' => 'Valdo D\'Arienzo',
250 'e' => 'tipografie e tipografi nel XVI secolo',
254 This method will also create additional field C<000> with MFN.
261 my $mfn = shift || confess "need mfn!";
263 # init record to include MFN as field 000
264 my $rec = { '000' => [ $mfn ] };
266 my $row = $self->fetch($mfn) || return;
268 foreach my $k (keys %{$row}) {
269 foreach my $l (@{$row->{$k}}) {
275 $l = $self->{'hash_filter'}->($l) if ($self->{'hash_filter'});
280 ($val->{'i1'},$val->{'i2'}) = ($1,$2) if ($l =~ s/^([01 #])([01 #])\x1F/\x1F/);
284 foreach my $t (split(/\x1F/,$l)) {
286 $val->{substr($t,0,1)} = substr($t,1);
292 push @{$rec->{$k}}, $val;
316 http://www.rot13.org/~dpavlin/
320 This program is free software; you can redistribute
321 it and/or modify it under the same terms as Perl itself.
323 The full text of the license can be found in the
324 LICENSE file included with this module.