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 |
|
|
17 |
|
package Utilities::HashDB; |
18 |
+ |
use Utilities::DataItem; |
19 |
|
require 5.001; |
20 |
|
|
21 |
|
sub new { |
22 |
|
my $class=shift; |
23 |
|
$self={}; |
24 |
|
bless $self, $class; |
25 |
< |
$self->{datahash}=(); |
24 |
< |
$self->{datakey}=(); |
25 |
> |
$self->{dataitems}=(); |
26 |
|
return $self; |
27 |
|
} |
28 |
|
|
31 |
|
my $data=shift; |
32 |
|
my @keys=@_; |
33 |
|
|
34 |
< |
push @{$self->{datakey}}, [ @keys ]; |
34 |
< |
push @{$self->{datahash}}, $data; |
34 |
> |
push @{$self->{dataitems}}, Utilities::DataItem->new($data, @keys); |
35 |
|
} |
36 |
|
|
37 |
|
sub items { |
38 |
|
my $self=shift; |
39 |
< |
return $#{$self->{datahash}}; |
39 |
> |
return $#{$self->{dataitems}}; |
40 |
|
} |
41 |
|
|
42 |
< |
|
43 |
< |
sub getdata { |
42 |
> |
sub deletedata { |
43 |
|
my $self=shift; |
44 |
|
my @keys=@_; |
45 |
|
|
46 |
+ |
# first get all the keys we want to delete |
47 |
+ |
my @match=$self->_match(@keys); |
48 |
+ |
foreach $i ( @match ) { |
49 |
+ |
splice (@{$self->{dataitems}}, $i, 1 ); |
50 |
+ |
} |
51 |
+ |
} |
52 |
+ |
|
53 |
+ |
sub match { |
54 |
+ |
my $self=shift; |
55 |
+ |
my @keys=@_; |
56 |
+ |
|
57 |
|
my @data=(); |
58 |
|
my @match=$self->_match(@keys); |
59 |
< |
foreach $d ( @match ) { |
60 |
< |
push @data, $self->{datahash}[$d]; |
59 |
> |
foreach $i ( @match ) { |
60 |
> |
push @data, $self->{dataitems}[$i]; |
61 |
|
} |
62 |
|
return @data; |
63 |
|
} |
64 |
|
|
65 |
< |
sub deletedata { |
65 |
> |
sub getdata { |
66 |
|
my $self=shift; |
67 |
|
my @keys=@_; |
68 |
< |
|
69 |
< |
# first get all the keys we want to delete |
68 |
> |
|
69 |
> |
my @data=(); |
70 |
|
my @match=$self->_match(@keys); |
71 |
+ |
my $i; |
72 |
|
foreach $i ( @match ) { |
73 |
< |
print $i; |
63 |
< |
splice ( @{$self->{datahash}}, $i, 1 ); |
64 |
< |
splice ( @{$self->{datakey}}, $i, 1 ); |
73 |
> |
push @data, ($self->{dataitems}[$i]->data()); |
74 |
|
} |
75 |
+ |
return @data; |
76 |
|
} |
77 |
|
|
78 |
|
# ------------------- Support Routines ------------ |
79 |
|
# returns list of array indices of items matching the keys |
80 |
+ |
|
81 |
|
sub _match { |
82 |
|
my $self=shift; |
83 |
|
my @keys=@_; |
84 |
|
|
85 |
< |
my $i; |
86 |
< |
my @matchold; |
87 |
< |
my @matchnew; |
88 |
< |
# At the start everything matches |
89 |
< |
for ( $i=0; $i<=$self->items(); $i++ ) { |
90 |
< |
if ( $#{$self->{datakey}[$i]} >= $#keys ) { |
80 |
< |
push @matchold, $i; |
81 |
< |
} |
82 |
< |
} |
83 |
< |
|
84 |
< |
# now test against keys |
85 |
< |
for ( $i=0; $i <= $#keys; $i++ ) { |
86 |
< |
undef @matchnew; |
87 |
< |
foreach $dn ( @matchold ){ |
88 |
< |
if ( $self->{datakey}[$dn][$i] eq $keys[$i] ) { |
89 |
< |
push @matchnew,$dn; |
90 |
< |
#print "match at $dn $keys[$i]\n"; |
91 |
< |
} |
85 |
> |
my @matches=(); |
86 |
> |
my $data; |
87 |
> |
for ( $i=0; $i<=$#{$self->{dataitems}}; $i++ ) { |
88 |
> |
$data=$self->{dataitems}[$i]; |
89 |
> |
if ( $data->match(@keys) ) { |
90 |
> |
push @matches, $i; |
91 |
|
} |
93 |
– |
@matchold=@matchnew; |
92 |
|
} |
93 |
< |
return @matchold; # list of array indecies |
93 |
> |
return @matches; |
94 |
|
} |
97 |
– |
|