ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/HashDB.pm
Revision: 1.11
Committed: Fri Jan 13 18:48:29 2006 UTC (19 years, 4 months ago) by sashby
Content type: text/plain
Branch: MAIN
Changes since 1.10: +0 -1 lines
Log Message:
Added new container classes. More updates to XML based doc classes.

File Contents

# User Rev Content
1 sashby 1.9 =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 sashby 1.10 Shaun ASHBY
66 sashby 1.9
67     =cut
68    
69 williamc 1.1 package Utilities::HashDB;
70 williamc 1.3 use Utilities::DataItem;
71 williamc 1.4 use FileHandle;
72 williamc 1.1 require 5.001;
73    
74     sub new {
75     my $class=shift;
76     $self={};
77     bless $self, $class;
78 williamc 1.3 $self->{dataitems}=();
79 williamc 1.1 return $self;
80     }
81    
82     sub setdata {
83     my $self=shift;
84     my $data=shift;
85     my @keys=@_;
86    
87 williamc 1.3 push @{$self->{dataitems}}, Utilities::DataItem->new($data, @keys);
88 williamc 1.1 }
89    
90 williamc 1.6 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 williamc 1.1 sub items {
112     my $self=shift;
113 williamc 1.3 return $#{$self->{dataitems}};
114 williamc 1.1 }
115    
116 williamc 1.3 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 williamc 1.1
127 williamc 1.3 sub match {
128 williamc 1.1 my $self=shift;
129     my @keys=@_;
130 williamc 1.3
131 williamc 1.1 my @data=();
132     my @match=$self->_match(@keys);
133 williamc 1.3 foreach $i ( @match ) {
134     push @data, $self->{dataitems}[$i];
135 williamc 1.1 }
136     return @data;
137     }
138    
139 williamc 1.3 sub getdata {
140 williamc 1.1 my $self=shift;
141     my @keys=@_;
142 williamc 1.3
143     my @data=();
144 williamc 1.1 my @match=$self->_match(@keys);
145 williamc 1.3 my $i;
146 williamc 1.1 foreach $i ( @match ) {
147 williamc 1.3 push @data, ($self->{dataitems}[$i]->data());
148 williamc 1.1 }
149 williamc 1.3 return @data;
150 williamc 1.4 }
151    
152     sub store {
153     my $self=shift;
154     my $filename=shift;
155    
156     my $fh=FileHandle->new();
157 sashby 1.8 $fh->autoflush(1);
158 williamc 1.4 open ($fh, ">$filename") or die "Unable to open file $filename\n $!\n";
159     foreach $object ( @{$self->{dataitems}} ) {
160 williamc 1.6 $object->store($fh);
161 sashby 1.8 }
162 williamc 1.4 close $fh;
163 sashby 1.8 }
164 williamc 1.4
165     sub restore {
166     my $self=shift;
167     my $filename=shift;
168 williamc 1.5 my @arr=();
169     my $data;
170 williamc 1.4
171     my $fh=FileHandle->new();
172     open ($fh, "<$filename") or die "Unable to open file $filename\n $!\n";
173     while ( <$fh> ) {
174 williamc 1.6 push @{$self->{dataitems}}, Utilities::DataItem->restore($fh);
175 williamc 1.4 }
176     close $fh;
177 williamc 1.1 }
178    
179     # ------------------- Support Routines ------------
180     # returns list of array indices of items matching the keys
181 williamc 1.3
182 williamc 1.1 sub _match {
183     my $self=shift;
184     my @keys=@_;
185    
186 williamc 1.3 my @matches=();
187     my $data;
188     for ( $i=0; $i<=$#{$self->{dataitems}}; $i++ ) {
189     $data=$self->{dataitems}[$i];
190     if ( $data->match(@keys) ) {
191     push @matches, $i;
192 williamc 1.1 }
193     }
194 williamc 1.3 return @matches;
195 williamc 1.1 }