1 |
#
|
2 |
# HashDB.pm
|
3 |
#
|
4 |
# Originally Written by Christopher Williams
|
5 |
#
|
6 |
# Description
|
7 |
#
|
8 |
# Interface
|
9 |
# ---------
|
10 |
# new() : A new HashDB object
|
11 |
# setdata(data, @keys) : set a data item to the given keys
|
12 |
# getdata(@keys) : return all data items that match the given keys
|
13 |
# deletedata(@keys) : detete all data items that match the given keys
|
14 |
# match(@keys) : return the full DataItem object refs that match keys
|
15 |
# items() : return the number of seperate items in the store
|
16 |
# store(filename) : dump to file
|
17 |
# restore(filename) : restore from file
|
18 |
# alias(\@refofkeys,\@aliaskeys) : attatch another set of keys to items that
|
19 |
# match the refopfkeys
|
20 |
# note that these are references to the arrays
|
21 |
# unalias(@aliaskeys) :remove an alias
|
22 |
|
23 |
package Utilities::HashDB;
|
24 |
use Utilities::DataItem;
|
25 |
use FileHandle;
|
26 |
require 5.001;
|
27 |
|
28 |
sub new {
|
29 |
my $class=shift;
|
30 |
$self={};
|
31 |
bless $self, $class;
|
32 |
$self->{dataitems}=();
|
33 |
return $self;
|
34 |
}
|
35 |
|
36 |
sub setdata {
|
37 |
my $self=shift;
|
38 |
my $data=shift;
|
39 |
my @keys=@_;
|
40 |
|
41 |
push @{$self->{dataitems}}, Utilities::DataItem->new($data, @keys);
|
42 |
}
|
43 |
|
44 |
sub alias {
|
45 |
my $self=shift;
|
46 |
my $keyref=shift;
|
47 |
my $aliaskeys=shift;
|
48 |
|
49 |
my @objs=$self->match(@{$keyref});
|
50 |
foreach $obj ( @objs ) {
|
51 |
$obj->alias(@{$aliaskeys});
|
52 |
}
|
53 |
}
|
54 |
|
55 |
sub unalias {
|
56 |
my $self=shift;
|
57 |
my @keys=@_;
|
58 |
|
59 |
my @objs=$self->match(@keys);
|
60 |
foreach $obj ( @objs ) {
|
61 |
$obj->unalias(@_);
|
62 |
}
|
63 |
}
|
64 |
|
65 |
sub items {
|
66 |
my $self=shift;
|
67 |
return $#{$self->{dataitems}};
|
68 |
}
|
69 |
|
70 |
sub deletedata {
|
71 |
my $self=shift;
|
72 |
my @keys=@_;
|
73 |
|
74 |
# first get all the keys we want to delete
|
75 |
my @match=$self->_match(@keys);
|
76 |
foreach $i ( @match ) {
|
77 |
splice (@{$self->{dataitems}}, $i, 1 );
|
78 |
}
|
79 |
}
|
80 |
|
81 |
sub match {
|
82 |
my $self=shift;
|
83 |
my @keys=@_;
|
84 |
|
85 |
my @data=();
|
86 |
my @match=$self->_match(@keys);
|
87 |
foreach $i ( @match ) {
|
88 |
push @data, $self->{dataitems}[$i];
|
89 |
}
|
90 |
return @data;
|
91 |
}
|
92 |
|
93 |
sub getdata {
|
94 |
my $self=shift;
|
95 |
my @keys=@_;
|
96 |
|
97 |
my @data=();
|
98 |
my @match=$self->_match(@keys);
|
99 |
my $i;
|
100 |
foreach $i ( @match ) {
|
101 |
push @data, ($self->{dataitems}[$i]->data());
|
102 |
}
|
103 |
return @data;
|
104 |
}
|
105 |
|
106 |
sub store {
|
107 |
my $self=shift;
|
108 |
my $filename=shift;
|
109 |
|
110 |
my $fh=FileHandle->new();
|
111 |
$fh->autoflush(1);
|
112 |
open ($fh, ">$filename") or die "Unable to open file $filename\n $!\n";
|
113 |
foreach $object ( @{$self->{dataitems}} ) {
|
114 |
print $fh "\n";
|
115 |
$object->store($fh);
|
116 |
}
|
117 |
close $fh;
|
118 |
}
|
119 |
|
120 |
sub restore {
|
121 |
my $self=shift;
|
122 |
my $filename=shift;
|
123 |
my @arr=();
|
124 |
my $data;
|
125 |
|
126 |
my $fh=FileHandle->new();
|
127 |
open ($fh, "<$filename") or die "Unable to open file $filename\n $!\n";
|
128 |
while ( <$fh> ) {
|
129 |
push @{$self->{dataitems}}, Utilities::DataItem->restore($fh);
|
130 |
}
|
131 |
close $fh;
|
132 |
}
|
133 |
|
134 |
# ------------------- Support Routines ------------
|
135 |
# returns list of array indices of items matching the keys
|
136 |
|
137 |
sub _match {
|
138 |
my $self=shift;
|
139 |
my @keys=@_;
|
140 |
|
141 |
my @matches=();
|
142 |
my $data;
|
143 |
for ( $i=0; $i<=$#{$self->{dataitems}}; $i++ ) {
|
144 |
$data=$self->{dataitems}[$i];
|
145 |
if ( $data->match(@keys) ) {
|
146 |
push @matches, $i;
|
147 |
}
|
148 |
}
|
149 |
return @matches;
|
150 |
}
|