ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/HashDB.pm
Revision: 1.10
Committed: Wed Aug 17 11:11:48 2005 UTC (19 years, 9 months ago) by sashby
Content type: text/plain
Branch: MAIN
CVS Tags: V1_0_4p1, V1_0_2, V1_0_2_p1
Branch point for: v103_branch
Changes since 1.9: +1 -7 lines
Log Message:
Added more POD doc.

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 print $fh "\n";
161 $object->store($fh);
162 }
163 close $fh;
164 }
165
166 sub restore {
167 my $self=shift;
168 my $filename=shift;
169 my @arr=();
170 my $data;
171
172 my $fh=FileHandle->new();
173 open ($fh, "<$filename") or die "Unable to open file $filename\n $!\n";
174 while ( <$fh> ) {
175 push @{$self->{dataitems}}, Utilities::DataItem->restore($fh);
176 }
177 close $fh;
178 }
179
180 # ------------------- Support Routines ------------
181 # returns list of array indices of items matching the keys
182
183 sub _match {
184 my $self=shift;
185 my @keys=@_;
186
187 my @matches=();
188 my $data;
189 for ( $i=0; $i<=$#{$self->{dataitems}}; $i++ ) {
190 $data=$self->{dataitems}[$i];
191 if ( $data->match(@keys) ) {
192 push @matches, $i;
193 }
194 }
195 return @matches;
196 }