ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/HashDB.pm
Revision: 1.14
Committed: Fri Jan 14 17:36:43 2011 UTC (14 years, 3 months ago) by muzaffar
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.13: +0 -0 lines
State: FILE REMOVED
Log Message:
merged SCRAM_V2 branch in to head

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.004;
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 use FileHandle;
157 my $fh=FileHandle->new();
158 $fh->autoflush(1);
159
160 open ($fh, "> $filename") or die "Unable to open file $filename\n $!\n";
161 foreach $object ( @{$self->{dataitems}} ) {
162 print $fh "\n";
163 $object->store($fh);
164 }
165 close $fh;
166 }
167
168 sub restore {
169 my $self=shift;
170 my $filename=shift;
171 my @arr=();
172 my $data;
173 # Make $_ local so it's writable, otherwise you get errors like this:
174 # Modification of a read-only value attempted at
175 # /afs/cern.ch/user/s/sashby/w2/SCRAM/V1_0_3/src/Utilities/HashDB.pm line 178.
176 local $_;
177
178 my $fh=FileHandle->new();
179 open ($fh, "< $filename") or die "Unable to open file $filename\n $!\n";
180 while (<$fh>)
181 {
182 push @{$self->{dataitems}}, Utilities::DataItem->restore($fh);
183 }
184
185 close $fh;
186 }
187
188 # ------------------- Support Routines ------------
189 # returns list of array indices of items matching the keys
190
191 sub _match {
192 my $self=shift;
193 my @keys=@_;
194
195 my @matches=();
196 my $data;
197 for ( $i=0; $i<=$#{$self->{dataitems}}; $i++ ) {
198 $data=$self->{dataitems}[$i];
199 if ( $data->match(@keys) ) {
200 push @matches, $i;
201 }
202 }
203 return @matches;
204 }