1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
package MARC::Fast;
use strict;
use Carp;
use Data::Dump qw/dump/;
BEGIN {
use Exporter ();
use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = 0.12;
@ISA = qw (Exporter);
#Give a hoot don't pollute, do not export more than needed by default
@EXPORT = qw ();
@EXPORT_OK = qw ();
%EXPORT_TAGS = ();
}
=head1 NAME
MARC::Fast - Very fast implementation of MARC database reader
=head1 SYNOPSIS
use MARC::Fast;
my $marc = new MARC::Fast(
marcdb => 'unimarc.iso',
);
foreach my $mfn ( 1 .. $marc->count ) {
print $marc->to_ascii( $mfn );
}
For longer example with command line options look at L<scripts/dump_fastmarc.pl>
=head1 DESCRIPTION
This is very fast alternative to C<MARC> and C<MARC::Record> modules.
It's is also very subtable for random access to MARC records (as opposed to
sequential one).
=head1 METHODS
=head2 new
Read MARC database
my $marc = new MARC::Fast(
marcdb => 'unimarc.iso',
quiet => 0,
debug => 0,
assert => 0,
hash_filter => sub {
my ($t, $record_number) = @_;
$t =~ s/foo/bar/;
return $t;
},
);
=cut
################################################## subroutine header end ##
sub new {
my $class = shift;
my $self = {@_};
bless ($self, $class);
croak "need marcdb parametar" unless ($self->{marcdb});
print STDERR "# opening ",$self->{marcdb},"\n" if ($self->{debug});
open($self->{fh}, $self->{marcdb}) || croak "can't open ",$self->{marcdb},": $!";
binmode($self->{fh});
$self->{count} = 0;
while (! eof($self->{fh})) {
$self->{count}++;
# save record position
push @{$self->{fh_offset}}, tell($self->{fh});
my $leader;
my $len = read($self->{fh}, $leader, 24);
if ($len < 24) {
warn "short read of leader, aborting\n";
$self->{count}--;
last;
}
# Byte Name
# ---- ----
# 0-4 Record Length
# 5 Status (n=new, c=corrected and d=deleted)
# 6 Type of Record (a=printed material)
# 7 Bibliographic Level (m=monograph)
# 8-9 Blanks
# 10 Indictator count (2 for monographs)
# 11 Subfield code count (2 - 0x1F+subfield code itself)
# 12-16 Base address of data
# 17 Encoding level (blank=full level, 1=sublevel 1, 2=sublevel 2,
# 3=sublevel 3)
# 18 Descriptive Cataloguing Form (blank=record is full ISBD,
# n=record is in non-ISBD format, i=record is in
# an incomplete ISBD format)
# 19 Blank
# 20 Length of length field in directory (always 4 in UNIMARC)
# 21 Length of Starting Character Position in directory (always
# 5 in UNIMARC)
# 22 Length of implementation defined portion in directory (always
# 0 in UNIMARC)
# 23 Blank
#
# |0 45 89 |12 16|1n 450 |
# |xxxxxnam 22(.....) 45 <---
print STDERR "REC ",$self->{count},": $leader\n" if ($self->{debug});
# store leader for later
push @{$self->{leader}}, $leader;
# skip to next record
my $o = substr($leader,0,5);
warn "# in record ", $self->{count}," record length isn't number but: ",dump($o),"\n" unless $o =~ m/^\d+$/;
if ($o > 24) {
seek($self->{fh},$o-24,1) if ($o);
} else {
last;
}
}
return $self;
}
=head2 count
Return number of records in database
print $marc->count;
=cut
sub count {
my $self = shift;
return $self->{count};
}
=head2 fetch
Fetch record from database
my $hash = $marc->fetch(42);
First record number is C<1>
=cut
sub fetch {
my $self = shift;
my $rec_nr = shift;
if ( ! $rec_nr ) {
$self->{last_leader} = undef;
return;
}
my $leader = $self->{leader}->[$rec_nr - 1];
$self->{last_leader} = $leader;
unless ($leader) {
carp "can't find record $rec_nr";
return;
};
my $offset = $self->{fh_offset}->[$rec_nr - 1];
unless (defined($offset)) {
carp "can't find offset for record $rec_nr";
return;
};
my $reclen = substr($leader,0,5);
my $base_addr = substr($leader,12,5);
print STDERR "# $rec_nr leader: '$leader' reclen: $reclen base addr: $base_addr [dir: ",$base_addr - 24,"]\n" if ($self->{debug});
my $skip = 0;
print STDERR "# seeking to $offset + 24\n" if ($self->{debug});
if ( ! seek($self->{fh}, $offset+24, 0) ) {
carp "can't seek to $offset: $!";
return;
}
print STDERR "# reading ",$base_addr-24," bytes of dictionary\n" if ($self->{debug});
my $directory;
if( ! read($self->{fh},$directory,$base_addr-24) ) {
carp "can't read directory: $!";
$skip = 1;
} else {
print STDERR "# $rec_nr directory: [",length($directory),"] '$directory'\n" if ($self->{debug});
}
print STDERR "# reading ",$reclen-$base_addr," bytes of fields\n" if ($self->{debug});
my $fields;
if( ! read($self->{fh},$fields,$reclen-$base_addr) ) {
carp "can't read fields: $!";
$skip = 1;
} else {
print STDERR "# $rec_nr fields: '$fields'\n" if ($self->{debug});
}
my $row;
while (!$skip && $directory =~ s/(\d{3})(\d{4})(\d{5})//) {
my ($tag,$len,$addr) = ($1,$2,$3);
if (($addr+$len) > length($fields)) {
print STDERR "WARNING: error in dictionary on record $rec_nr skipping...\n" if (! $self->{quiet});
$skip = 1;
next;
}
# take field
my $f = substr($fields,$addr,$len);
print STDERR "tag/len/addr $tag [$len] $addr: '$f'\n" if ($self->{debug});
push @{ $row->{$tag} }, $f;
my $del = substr($fields,$addr+$len-1,1);
# check field delimiters...
if ($self->{assert} && $del ne chr(30)) {
print STDERR "WARNING: skipping record $rec_nr, can't find delimiter 30 got: '$del'\n" if (! $self->{quiet});
$skip = 1;
next;
}
if ($self->{assert} && length($f) < 2) {
print STDERR "WARNING: skipping field $tag from record $rec_nr because it's too short!\n" if (! $self->{quiet});
next;
}
}
return $row;
}
=head2 last_leader
Returns leader of last record L<fetch>ed
print $marc->last_leader;
Added in version 0.08 of this module, so if you need it use:
use MARC::Fast 0.08;
to be sure that it's supported.
=cut
sub last_leader {
my $self = shift;
return $self->{last_leader};
}
=head2 to_hash
Read record with specified MFN and convert it to hash
my $hash = $marc->to_hash( $mfn, include_subfields => 1,
hash_filter => sub { my ($l,$tag) = @_; return $l; }
);
It has ability to convert characters (using C<hash_filter>) from MARC
database before creating structures enabling character re-mapping or quick
fix-up of data. If you specified C<hash_filter> both in C<new> and C<to_hash>
only the one from C<to_hash> will be used.
This function returns hash which is like this:
'200' => [
{
'i1' => '1',
'i2' => ' '
'a' => 'Goa',
'f' => 'Valdo D\'Arienzo',
'e' => 'tipografie e tipografi nel XVI secolo',
}
],
This method will also create additional field C<000> with MFN.
=cut
sub to_hash {
my $self = shift;
my $mfn = shift || confess "need mfn!";
my $args = {@_};
my $filter_coderef = $args->{'hash_filter'} || $self->{'hash_filter'};
# init record to include MFN as field 000
my $rec = { '000' => [ $mfn ] };
my $row = $self->fetch($mfn) || return;
foreach my $tag (keys %{$row}) {
foreach my $l (@{$row->{$tag}}) {
# remove end marker
$l =~ s/\x1E$//;
# filter output
$l = $filter_coderef->($l, $tag) if $filter_coderef;
my $val;
# has identifiers?
($val->{'i1'},$val->{'i2'}) = ($1,$2) if ($l =~ s/^([01 #])([01 #])\x1F/\x1F/);
my $sf_usage;
my @subfields;
# has subfields?
if ($l =~ m/\x1F/) {
foreach my $t (split(/\x1F/,$l)) {
next if (! $t);
my $f = substr($t,0,1);
my $v = substr($t,1);
push @subfields, ( $f, $sf_usage->{$f}++ || 0 );
# repeatable subfiled -- convert it to array
if ( defined $val->{$f} ) {
if ( ref($val->{$f}) ne 'ARRAY' ) {
$val->{$f} = [ $val->{$f}, $v ];
} else {
push @{$val->{$f}}, $v;
}
} else {
$val->{$f} = $v;
}
}
$val->{subfields} = [ @subfields ] if $args->{include_subfields};
} else {
$val = $l;
}
push @{$rec->{$tag}}, $val;
}
}
return $rec;
}
=head2 to_ascii
print $marc->to_ascii( 42 );
=cut
sub to_ascii {
my $self = shift;
my $mfn = shift || confess "need mfn";
my $row = $self->fetch($mfn) || return;
my $out;
foreach my $f (sort keys %{$row}) {
my $dump = join('', @{ $row->{$f} });
$dump =~ s/\x1e$//;
$dump =~ s/\x1f/\$/g;
$out .= "$f\t$dump\n";
}
return $out;
}
1;
__END__
=head1 UTF-8 ENCODING
This module does nothing with encoding. But, since MARC format is byte
oriented even when using UTF-8 which has variable number of bytes for each
character, file is opened in binary mode.
As a result, all scalars recturned to perl don't have utf-8 flag. Solution is
to use C<hash_filter> and L<Encode> to decode utf-8 encoding like this:
use Encode;
my $marc = new MARC::Fast(
marcdb => 'utf8.marc',
hash_filter => sub {
Encode::decode( 'utf-8', $_[0] );
},
);
This will affect C<to_hash>, but C<fetch> will still return binary representation
since it doesn't support C<hash_filter>.
=head1 AUTHOR
Dobrica Pavlinusic
CPAN ID: DPAVLIN
dpavlin@rot13.org
http://www.rot13.org/~dpavlin/
=head1 COPYRIGHT
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=head1 SEE ALSO
L<Biblio::Isis>, perl(1).
=cut
|