Multi for comma (,) separated paths to isis databases multi
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 6 May 2020 10:18:18 +0000 (12:18 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 6 May 2020 10:18:18 +0000 (12:18 +0200)
lib/Biblio/Isis/Multi.pm [new file with mode: 0644]

diff --git a/lib/Biblio/Isis/Multi.pm b/lib/Biblio/Isis/Multi.pm
new file mode 100644 (file)
index 0000000..16d5a2d
--- /dev/null
@@ -0,0 +1,127 @@
+package Biblio::Isis::Multi;
+use strict;
+
+use Carp;
+use Biblio::Isis;
+use Data::Dump qw(dump);
+
+BEGIN {
+       use Exporter ();
+       use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
+       $VERSION     = 0.24;
+       @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
+
+Biblio::IsisMulti - Read CDS/ISIS, WinISIS and IsisMarc database
+
+=head2 new
+
+Open ISIS database
+
+ my $isis = new Biblio::Isis::Multi(
+       isisdb => './data/something,./data/something-else',
+       read_fdt => 1,
+       include_deleted => 1,
+       hash_filter => sub {
+               my ($v,$field_number) = @_;
+               $v =~ s#foo#bar#g;
+       },
+       debug => 1,
+       join_subfields_with => ' ; ',
+ );
+
+=cut
+
+sub new {
+       my $class = shift;
+       my $self = {@_};
+       bless($self, $class);
+
+       my $isisdb_dir = $self->{isisdb};
+
+       croak "new needs database name (isisdb) as argument!" unless $isisdb_dir;
+       if ( $isisdb_dir !~ m/,/ ) {
+               warn "# $isisdb_dir doesn't have , read normally";
+               return new Biblio::Isis(@_);
+       }
+
+       my @dirs = split(/,/, $isisdb_dir);
+       warn "## dirs = ",dump( @dirs );
+
+       foreach my $dir ( @dirs ) {
+               my $args = {@_};
+               $args->{isisdb} = "$dir/";
+               warn "## args = ",dump($args);
+               my $i = new Biblio::Isis(%$args);
+               push @{ $self->{_multi} }, $i;
+               push @{ $self->{_count} }, $i->count;
+       }
+
+       warn "# self = ",dump($self);
+
+       $self ? return $self : return undef;
+}
+
+=head2 count
+
+Return number of records in database
+
+  print $isis->count;
+
+=cut
+
+sub count {
+       my $self = shift;
+
+       my $sum = 0;
+       $sum += $_ foreach @{ $self->{_count} };
+       return $sum;
+}
+
+=head2 to_hash
+
+Read record with specified MFN and convert it to hash
+
+  my $hash = $isis->to_hash($mfn);
+
+=cut
+
+sub to_hash {
+       my $self = shift;
+
+
+       my $mfn = shift || confess "need mfn!";
+
+       my $part = 0;
+       my $offset = 0;
+       foreach my $count ( @{ $self->{_count} } ) {
+               if ( $mfn <= $count ) {
+                       my $hash = $self->{_multi}->[$part]->to_hash( $mfn );
+                       $hash->{'000'}->[0] += $offset;
+                       return $hash;
+               }
+               $mfn = $mfn - $count;
+               $part++;
+               $offset = $offset + $count;
+       }
+
+       confess "can't find $mfn in ",dump($self);
+}
+
+
+=head1 AUTHOR
+
+       Dobrica Pavlinusic
+       CPAN ID: DPAVLIN
+       dpavlin@rot13.org
+       http://www.rot13.org/~dpavlin/
+
+=cut
+1;