ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/HashDB.pm
Revision: 1.12
Committed: Thu Jan 19 17:26:29 2006 UTC (19 years, 3 months ago) by sashby
Content type: text/plain
Branch: MAIN
Changes since 1.11: +19 -5 lines
Log Message:
Gradually moving to XML parsing...

File Contents

# Content
1 =head1 NAME
2
3 Utilities::HashDB - A type of database object.
4
5 =head1 SYNOPSIS
6
7 my $obj = Utilities::HashDB->new();
8
9 =head1 METHODS
10
11 =over
12
13 =cut
14
15 =item C<new()>
16
17 Create a new HashDB object.
18
19 =item C<setdata(data, @keys)>
20
21 set a data item to the given keys.
22
23 =item C<getdata(@keys)>
24
25 return all data items that match the given keys.
26
27 =item C<deletedata(@keys)>
28
29 detete all data items that match the given keys.
30
31 =item C<match(@keys)>
32
33 return the full DataItem object refs that match keys.
34
35 =item C<items()>
36
37 return the number of seperate items in the store.
38
39 =item C<store(filename)>
40
41 dump to file.
42
43 =item C<restore(filename)>
44
45 restore from file.
46
47 =item C<alias(\@refofkeys,\@aliaskeys)>
48
49 attatch another set of keys to items that
50 match the refopfkeys (note that these are
51 references to the arrays).
52
53 =item unalias(@aliaskeys)
54
55 remove an alias.
56
57 =back
58
59 =head1 AUTHOR
60
61 Originally Written by Christopher Williams.
62
63 =head1 MAINTAINER
64
65 Shaun ASHBY
66
67 =cut
68
69 package Utilities::HashDB;
70 use Utilities::DataItem;
71 use FileHandle;
72 require 5.001;
73
74 sub new {
75 my $class=shift;
76 $self={};
77 bless $self, $class;
78 $self->{dataitems}=();
79 return $self;
80 }
81
82 sub setdata {
83 my $self=shift;
84 my $data=shift;
85 my @keys=@_;
86
87 push @{$self->{dataitems}}, Utilities::DataItem->new($data, @keys);
88 }
89
90 sub alias {
91 my $self=shift;
92 my $keyref=shift;
93 my $aliaskeys=shift;
94
95 my @objs=$self->match(@{$keyref});
96 foreach $obj ( @objs ) {
97 $obj->alias(@{$aliaskeys});
98 }
99 }
100
101 sub unalias {
102 my $self=shift;
103 my @keys=@_;
104
105 my @objs=$self->match(@keys);
106 foreach $obj ( @objs ) {
107 $obj->unalias(@_);
108 }
109 }
110
111 sub items {
112 my $self=shift;
113 return $#{$self->{dataitems}};
114 }
115
116 sub deletedata {
117 my $self=shift;
118 my @keys=@_;
119
120 # first get all the keys we want to delete
121 my @match=$self->_match(@keys);
122 foreach $i ( @match ) {
123 splice (@{$self->{dataitems}}, $i, 1 );
124 }
125 }
126
127 sub match {
128 my $self=shift;
129 my @keys=@_;
130
131 my @data=();
132 my @match=$self->_match(@keys);
133 foreach $i ( @match ) {
134 push @data, $self->{dataitems}[$i];
135 }
136 return @data;
137 }
138
139 sub getdata {
140 my $self=shift;
141 my @keys=@_;
142
143 my @data=();
144 my @match=$self->_match(@keys);
145 my $i;
146 foreach $i ( @match ) {
147 push @data, ($self->{dataitems}[$i]->data());
148 }
149 return @data;
150 }
151
152 sub store {
153 my $self=shift;
154 my $filename=shift;
155
156 my $fh=FileHandle->new();
157 $fh->autoflush(1);
158 open ($fh, ">$filename") or die "Unable to open file $filename\n $!\n";
159 foreach $object ( @{$self->{dataitems}} ) {
160 $object->store($fh);
161 }
162 close $fh;
163 }
164
165 sub restore {
166 my $self=shift;
167 my $filename=shift;
168 my @arr=();
169 my $data;
170 my $fh=FileHandle->new();
171 local *FH=$fh;
172
173 if ($fh->open("<$filename"))
174 {
175 # my $line;
176 # while ($line = $fh->getline())
177 while (defined $fh)
178 {
179 push @{$self->{dataitems}}, Utilities::DataItem->restore($fh);
180 }
181 }
182
183 # open ($fh, "<$filename") or die "Unable to open file $filename\n $!\n";
184
185 #while ( <$fh> ) {
186 # { print "...HashDB: in while loop","\n";
187 # push @{$self->{dataitems}}, Utilities::DataItem->restore($fh);
188 # }
189 close $fh;
190 #print "end restore","\n";
191 }
192
193 # ------------------- Support Routines ------------
194 # returns list of array indices of items matching the keys
195
196 sub _match {
197 my $self=shift;
198 my @keys=@_;
199
200 my @matches=();
201 my $data;
202 for ( $i=0; $i<=$#{$self->{dataitems}}; $i++ ) {
203 $data=$self->{dataitems}[$i];
204 if ( $data->match(@keys) ) {
205 push @matches, $i;
206 }
207 }
208 return @matches;
209 }