| 1 | package CDB_File::BiIndex::Generator; | ||||||||||
| 2 | 1 | $REVISION=q$Revision: 1.4 $ ; $VERSION = sprintf ( "%d.%02d", $REVISION =~ /(\d+).(\d+)/ ); | |||||||||
| 3 | 1 | ▶ | use CDB_File::Generator; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 1 | $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 | 1 | ▶ | sub new ($$;$) { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 48 | 1 | my $class=shift; | |||||||||
| 49 | 1 | my $ONE_TWO=shift; | |||||||||
| 50 | 1 | my $TWO_ONE=shift; | |||||||||
| 51 | 1 | ▶ | unless ( defined $TWO_ONE) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 1 | $TWO_ONE=$ONE_TWO . '.2-1'; | |||||||||
| 53 | 1 | $ONE_TWO=$ONE_TWO . '.1-2' ; | |||||||||
| 54 | } | ||||||||||
| 55 | 1 | my $self={}; | |||||||||
| 56 | 1 | $self->{"one_two"} = new CDB_File::Generator $ONE_TWO; | |||||||||
| 57 | 1 | $self->{"two_one"} = new CDB_File::Generator $TWO_ONE; | |||||||||
| 58 | 1 | ▶ | print STDERR "new CDB..Gen $TWO_ONE $ONE_TWO of first list\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | if $CDB_File::BiIndex::Generator::verbose & 16; | ||||||||||
| 60 | 1 | 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 | 4 | ▶ | sub add_relation ($$$) { | ||||||||
|
Subroutine
add_relation: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 70 | 4 | my ($self, $first, $second)=@_; | |||||||||
| 71 | 4 | $self->{"one_two"}->add($first, $second); | |||||||||
| 72 | 4 | $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 | 1 | ▶ | sub finish ($) { | ||||||||
|
Subroutine
finish: called CC=1 SCAR=0.4
Pod
undocumented
| |||||||||||
| 83 | 1 | my ($self)=@_; | |||||||||
| 84 | 1 | $self->{"one_two"}->finish(); | |||||||||
| 85 | 0 | $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<add_list_first> puts makes the key a key in the first index. | ||||||||||
| 95 | C<add_list_second> puts the list in the first index and the key in the | ||||||||||
| 96 | second. | ||||||||||
| 97 | |||||||||||
| 98 | =cut | ||||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | 0 | ▶ | sub add_list_first ($$$) { | ||||||||
|
Subroutine
add_list_first: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 102 | 0 | my $self=shift; | |||||||||
| 103 | 0 | my $first=shift; | |||||||||
| 104 | 0 | ▶ | print STDERR "adding to $first of first list\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | if $CDB_File::BiIndex::Generator::verbose & 8; | ||||||||||
| 106 | 0 | my $seconds=shift; | |||||||||
| 107 | 0 | _add_list( $self->{"one_two"}, $self->{"two_one"}, $first, $seconds); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 0 | ▶ | sub add_list_second ($$$) { | ||||||||
|
Subroutine
add_list_second: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 111 | 0 | my $self=shift; | |||||||||
| 112 | 0 | my $second=shift; | |||||||||
| 113 | 0 | ▶ | print STDERR "adding list to $second of second list\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | if $CDB_File::BiIndex::Generator::verbose & 8; | ||||||||||
| 115 | 0 | my $firsts=shift; | |||||||||
| 116 | 0 | _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 | 0 | ▶ | sub _add_list ($$$$) { | ||||||||
|
Subroutine
_add_list: not called CC=4 SCAR=30.0
| |||||||||||
| 122 | 0 | my $one_two = shift; | |||||||||
| 123 | 0 | my $two_one = shift; | |||||||||
| 124 | 0 | my $first = shift; | |||||||||
| 125 | 0 | my $seconds = shift; | |||||||||
| 126 | 0 | my $second; | |||||||||
| 127 | 0 | ▶ | print STDERR "add $first all of $seconds\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | if $CDB_File::BiIndex::Generator::verbose & 4; | ||||||||||
| 129 | 0 | foreach $second ( @$seconds ) { | |||||||||
| 130 | 0 | ▶ | print STDERR "adding item $first->$second\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | if $CDB_File::BiIndex::Generator::verbose & 2; | ||||||||||
| 132 | 0 | $one_two->add($first, $second); | |||||||||
| 133 | 0 | $two_one->add($second, $first); | |||||||||
| 134 | } | ||||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 1 | 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.. | ||||||||||