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 { |
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 |
+ |
my $temp=join '::', $object->keys(); |
89 |
+ |
$temp=$object->data()."::".$temp; |
90 |
+ |
print $fh $temp."\n"; |
91 |
+ |
} |
92 |
+ |
close $fh; |
93 |
+ |
} |
94 |
+ |
|
95 |
+ |
sub restore { |
96 |
+ |
my $self=shift; |
97 |
+ |
my $filename=shift; |
98 |
+ |
my @arr; |
99 |
+ |
|
100 |
+ |
my $fh=FileHandle->new(); |
101 |
+ |
open ($fh, "<$filename") or die "Unable to open file $filename\n $!\n"; |
102 |
+ |
while ( <$fh> ) { |
103 |
+ |
next if /^#/; |
104 |
+ |
@arr=split ':', $_; |
105 |
+ |
$self->setdata(@arr); |
106 |
+ |
} |
107 |
+ |
close $fh; |
108 |
+ |
} |
109 |
+ |
|
110 |
|
# ------------------- Support Routines ------------ |
111 |
|
# returns list of array indices of items matching the keys |
112 |
|
|