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 |
|
19 |
package Utilities::HashDB;
|
20 |
use Utilities::DataItem;
|
21 |
use FileHandle;
|
22 |
require 5.001;
|
23 |
|
24 |
sub new {
|
25 |
my $class=shift;
|
26 |
$self={};
|
27 |
bless $self, $class;
|
28 |
$self->{dataitems}=();
|
29 |
return $self;
|
30 |
}
|
31 |
|
32 |
sub setdata {
|
33 |
my $self=shift;
|
34 |
my $data=shift;
|
35 |
my @keys=@_;
|
36 |
|
37 |
push @{$self->{dataitems}}, Utilities::DataItem->new($data, @keys);
|
38 |
}
|
39 |
|
40 |
sub items {
|
41 |
my $self=shift;
|
42 |
return $#{$self->{dataitems}};
|
43 |
}
|
44 |
|
45 |
sub deletedata {
|
46 |
my $self=shift;
|
47 |
my @keys=@_;
|
48 |
|
49 |
# first get all the keys we want to delete
|
50 |
my @match=$self->_match(@keys);
|
51 |
foreach $i ( @match ) {
|
52 |
splice (@{$self->{dataitems}}, $i, 1 );
|
53 |
}
|
54 |
}
|
55 |
|
56 |
sub match {
|
57 |
my $self=shift;
|
58 |
my @keys=@_;
|
59 |
|
60 |
my @data=();
|
61 |
my @match=$self->_match(@keys);
|
62 |
foreach $i ( @match ) {
|
63 |
push @data, $self->{dataitems}[$i];
|
64 |
}
|
65 |
return @data;
|
66 |
}
|
67 |
|
68 |
sub getdata {
|
69 |
my $self=shift;
|
70 |
my @keys=@_;
|
71 |
|
72 |
my @data=();
|
73 |
my @match=$self->_match(@keys);
|
74 |
my $i;
|
75 |
foreach $i ( @match ) {
|
76 |
push @data, ($self->{dataitems}[$i]->data());
|
77 |
}
|
78 |
return @data;
|
79 |
}
|
80 |
|
81 |
sub store {
|
82 |
my $self=shift;
|
83 |
my $filename=shift;
|
84 |
|
85 |
my $fh=FileHandle->new();
|
86 |
open ($fh, ">$filename") or die "Unable to open file $filename\n $!\n";
|
87 |
foreach $object ( @{$self->{dataitems}} ) {
|
88 |
# print the keys first then the data item
|
89 |
foreach $key ( $object->keys() ) {
|
90 |
print $fh "#".$key."\n";
|
91 |
}
|
92 |
print $fh ">".$object->data()."\n";
|
93 |
}
|
94 |
close $fh;
|
95 |
}
|
96 |
|
97 |
sub restore {
|
98 |
my $self=shift;
|
99 |
my $filename=shift;
|
100 |
my @arr=();
|
101 |
my $data;
|
102 |
|
103 |
my $fh=FileHandle->new();
|
104 |
open ($fh, "<$filename") or die "Unable to open file $filename\n $!\n";
|
105 |
while ( <$fh> ) {
|
106 |
if ( $_=~/^#(.*)/ ) {
|
107 |
push @arr, $1;
|
108 |
}
|
109 |
if ( $_=~/^>(.*)/ ) {
|
110 |
$data=$1;
|
111 |
if ( $#arr >= 0 ) {
|
112 |
$self->setdata($data,@arr);
|
113 |
}
|
114 |
undef @arr;
|
115 |
}
|
116 |
}
|
117 |
close $fh;
|
118 |
}
|
119 |
|
120 |
# ------------------- Support Routines ------------
|
121 |
# returns list of array indices of items matching the keys
|
122 |
|
123 |
sub _match {
|
124 |
my $self=shift;
|
125 |
my @keys=@_;
|
126 |
|
127 |
my @matches=();
|
128 |
my $data;
|
129 |
for ( $i=0; $i<=$#{$self->{dataitems}}; $i++ ) {
|
130 |
$data=$self->{dataitems}[$i];
|
131 |
if ( $data->match(@keys) ) {
|
132 |
push @matches, $i;
|
133 |
}
|
134 |
}
|
135 |
return @matches;
|
136 |
}
|