ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/HashDB.pm
Revision: 1.6
Committed: Tue Feb 8 13:53:22 2000 UTC (25 years, 3 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.5: +30 -15 lines
Log Message:
Add aliasing mechanism and make dataItem store its own data

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 # alias(\@refofkeys,\@aliaskeys) : attatch another set of keys to items that
19 # match the refopfkeys
20 # note that these are references to the arrays
21 # unalias(@aliaskeys) :remove an alias
22
23 package Utilities::HashDB;
24 use Utilities::DataItem;
25 use FileHandle;
26 require 5.001;
27
28 sub new {
29 my $class=shift;
30 $self={};
31 bless $self, $class;
32 $self->{dataitems}=();
33 return $self;
34 }
35
36 sub setdata {
37 my $self=shift;
38 my $data=shift;
39 my @keys=@_;
40
41 push @{$self->{dataitems}}, Utilities::DataItem->new($data, @keys);
42 }
43
44 sub alias {
45 my $self=shift;
46 my $keyref=shift;
47 my $aliaskeys=shift;
48
49 my @objs=$self->match(@{$keyref});
50 foreach $obj ( @objs ) {
51 print "$obj @_\n";
52 $obj->alias(@{$aliaskeys});
53 }
54 }
55
56 sub unalias {
57 my $self=shift;
58 my @keys=@_;
59
60 my @objs=$self->match(@keys);
61 foreach $obj ( @objs ) {
62 $obj->unalias(@_);
63 }
64 }
65
66 sub items {
67 my $self=shift;
68 return $#{$self->{dataitems}};
69 }
70
71 sub deletedata {
72 my $self=shift;
73 my @keys=@_;
74
75 # first get all the keys we want to delete
76 my @match=$self->_match(@keys);
77 foreach $i ( @match ) {
78 splice (@{$self->{dataitems}}, $i, 1 );
79 }
80 }
81
82 sub match {
83 my $self=shift;
84 my @keys=@_;
85
86 my @data=();
87 my @match=$self->_match(@keys);
88 foreach $i ( @match ) {
89 push @data, $self->{dataitems}[$i];
90 }
91 return @data;
92 }
93
94 sub getdata {
95 my $self=shift;
96 my @keys=@_;
97
98 my @data=();
99 my @match=$self->_match(@keys);
100 my $i;
101 foreach $i ( @match ) {
102 push @data, ($self->{dataitems}[$i]->data());
103 }
104 return @data;
105 }
106
107 sub store {
108 my $self=shift;
109 my $filename=shift;
110
111 my $fh=FileHandle->new();
112 $fh->autoflush(1);
113 open ($fh, ">$filename") or die "Unable to open file $filename\n $!\n";
114 foreach $object ( @{$self->{dataitems}} ) {
115 print $fh "\n";
116 $object->store($fh);
117 }
118 close $fh;
119 }
120
121 sub restore {
122 my $self=shift;
123 my $filename=shift;
124 my @arr=();
125 my $data;
126
127 my $fh=FileHandle->new();
128 open ($fh, "<$filename") or die "Unable to open file $filename\n $!\n";
129 while ( <$fh> ) {
130 push @{$self->{dataitems}}, Utilities::DataItem->restore($fh);
131 }
132 close $fh;
133 }
134
135 # ------------------- Support Routines ------------
136 # returns list of array indices of items matching the keys
137
138 sub _match {
139 my $self=shift;
140 my @keys=@_;
141
142 my @matches=();
143 my $data;
144 for ( $i=0; $i<=$#{$self->{dataitems}}; $i++ ) {
145 $data=$self->{dataitems}[$i];
146 if ( $data->match(@keys) ) {
147 push @matches, $i;
148 }
149 }
150 return @matches;
151 }