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 { |
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}}; |
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 |
|
|