ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/HashDB.pm
Revision: 1.4
Committed: Thu Oct 21 09:59:44 1999 UTC (25 years, 6 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.3: +32 -0 lines
Log Message:
Add persistency

File Contents

# Content
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 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
113 sub _match {
114 my $self=shift;
115 my @keys=@_;
116
117 my @matches=();
118 my $data;
119 for ( $i=0; $i<=$#{$self->{dataitems}}; $i++ ) {
120 $data=$self->{dataitems}[$i];
121 if ( $data->match(@keys) ) {
122 push @matches, $i;
123 }
124 }
125 return @matches;
126 }