File Coverage

blib/lib/CDB_File/BiIndex/Generator.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package CDB_File::BiIndex::Generator;
2             $REVISION=q$Revision: 1.4 $ ; $VERSION = sprintf ( "%d.%02d", $REVISION =~ /(\d+).(\d+)/ );
3 1     1   3236 use CDB_File::Generator;
  0            
  0            
4             use strict;
5              
6             =head1 NAME
7              
8             CDB_File::BiIndex::Generator - build the cdbmake files for a bidirectional index
9              
10             =head1 SYNOPSIS
11              
12             use CDB_File::BiIndex::Generator;
13             $gen = new CDB_File::BiIndex::Generator "test";
14             $gen->add_relation("USA", "London");
15             $gen->add_relation("Malawi", "Lilongwe");
16             $gen->finish();
17              
18             =head1 DESCRIPTION
19              
20             A CDB_File::BiIndex is designed to keep pairs of values and be able to look
21             up bi-directional multivalued relations. This package is designed to
22             generate one of these indexes statically using CDB.
23              
24             At present this package is a complete hack designed simply to output
25             the two lists in the very specific case which I need.
26              
27             =head1 EXAMPLE
28              
29             Please see the example in the CDB_File::BiIndex documentation. This shows
30             both how to generate and use an index.
31              
32             =cut
33              
34             $CDB_File::BiIndex::Generator::verbose=0;
35              
36             =head1 METHODS
37              
38             =head2 new
39              
40             new (CLASS, database_filenamebase)
41             new (CLASS, first_database_filename, second_database_filename)
42              
43             New opens and sets up the databases ready for writing.
44              
45             =cut
46              
47             sub new ($$;$) {
48             my $class=shift;
49             my $ONE_TWO=shift;
50             my $TWO_ONE=shift;
51             unless ( defined $TWO_ONE) {
52             $TWO_ONE=$ONE_TWO . '.2-1';
53             $ONE_TWO=$ONE_TWO . '.1-2' ;
54             }
55             my $self={};
56             $self->{"one_two"} = new CDB_File::Generator $ONE_TWO;
57             $self->{"two_one"} = new CDB_File::Generator $TWO_ONE;
58             print STDERR "new CDB..Gen $TWO_ONE $ONE_TWO of first list\n"
59             if $CDB_File::BiIndex::Generator::verbose & 16;
60             return bless $self, $class;
61             }
62              
63             =head1 $gen->add_relation( first , second )
64              
65             Adds a key value pair to the database.
66              
67             =cut
68              
69             sub add_relation ($$$) {
70             my ($self, $first, $second)=@_;
71             $self->{"one_two"}->add($first, $second);
72             $self->{"two_one"}->add($second, $first);
73             }
74              
75              
76             =head1 $gen->finish()
77              
78             Writes the database so that it is ready for use by CDB_BiIndex.
79              
80             =cut
81              
82             sub finish ($) {
83             my ($self)=@_;
84             $self->{"one_two"}->finish();
85             $self->{"two_one"}->finish();
86             }
87              
88              
89             =head1 add_list_first / add_list_second ->(key, list_reference)
90              
91             These functions add an entire set of relations of one string to a list
92             of other strings
93              
94             C puts makes the key a key in the first index.
95             C puts the list in the first index and the key in the
96             second.
97              
98             =cut
99              
100              
101             sub add_list_first ($$$) {
102             my $self=shift;
103             my $first=shift;
104             print STDERR "adding to $first of first list\n"
105             if $CDB_File::BiIndex::Generator::verbose & 8;
106             my $seconds=shift;
107             _add_list( $self->{"one_two"}, $self->{"two_one"}, $first, $seconds);
108             }
109              
110             sub add_list_second ($$$) {
111             my $self=shift;
112             my $second=shift;
113             print STDERR "adding list to $second of second list\n"
114             if $CDB_File::BiIndex::Generator::verbose & 8;
115             my $firsts=shift;
116             _add_list( $self->{"two_one"}, $self->{"one_two"}, $second, $firsts);
117             }
118              
119             # internal function which implements the previous two user visible ones.
120              
121             sub _add_list ($$$$) {
122             my $one_two = shift;
123             my $two_one = shift;
124             my $first = shift;
125             my $seconds = shift;
126             my $second;
127             print STDERR "add $first all of $seconds\n"
128             if $CDB_File::BiIndex::Generator::verbose & 4;
129             foreach $second ( @$seconds ) {
130             print STDERR "adding item $first->$second\n"
131             if $CDB_File::BiIndex::Generator::verbose & 2;
132             $one_two->add($first, $second);
133             $two_one->add($second, $first);
134             }
135             }
136            
137             1;
138             # If require was a child, by now it would be so spoiled with people
139             #keeping it happy that it would be unbearable. Don't get me wrong, I'm
140             #not some kind of candidate for 'Victoran Parents' but there are
141             #limits..