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

# User Rev Content
1 williamc 1.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 williamc 1.3 # match(@keys) : return the full DataItem object refs that match keys
15 williamc 1.1 # items() : return the number of seperate items in the store
16 williamc 1.4 # store(filename) : dump to file
17     # restore(filename) : restore from file
18 williamc 1.6 # 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 williamc 1.1
23     package Utilities::HashDB;
24 williamc 1.3 use Utilities::DataItem;
25 williamc 1.4 use FileHandle;
26 williamc 1.1 require 5.001;
27    
28     sub new {
29     my $class=shift;
30     $self={};
31     bless $self, $class;
32 williamc 1.3 $self->{dataitems}=();
33 williamc 1.1 return $self;
34     }
35    
36     sub setdata {
37     my $self=shift;
38     my $data=shift;
39     my @keys=@_;
40    
41 williamc 1.3 push @{$self->{dataitems}}, Utilities::DataItem->new($data, @keys);
42 williamc 1.1 }
43    
44 williamc 1.6 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 williamc 1.1 sub items {
67     my $self=shift;
68 williamc 1.3 return $#{$self->{dataitems}};
69 williamc 1.1 }
70    
71 williamc 1.3 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 williamc 1.1
82 williamc 1.3 sub match {
83 williamc 1.1 my $self=shift;
84     my @keys=@_;
85 williamc 1.3
86 williamc 1.1 my @data=();
87     my @match=$self->_match(@keys);
88 williamc 1.3 foreach $i ( @match ) {
89     push @data, $self->{dataitems}[$i];
90 williamc 1.1 }
91     return @data;
92     }
93    
94 williamc 1.3 sub getdata {
95 williamc 1.1 my $self=shift;
96     my @keys=@_;
97 williamc 1.3
98     my @data=();
99 williamc 1.1 my @match=$self->_match(@keys);
100 williamc 1.3 my $i;
101 williamc 1.1 foreach $i ( @match ) {
102 williamc 1.3 push @data, ($self->{dataitems}[$i]->data());
103 williamc 1.1 }
104 williamc 1.3 return @data;
105 williamc 1.4 }
106    
107     sub store {
108     my $self=shift;
109     my $filename=shift;
110    
111     my $fh=FileHandle->new();
112 williamc 1.6 $fh->autoflush(1);
113 williamc 1.4 open ($fh, ">$filename") or die "Unable to open file $filename\n $!\n";
114     foreach $object ( @{$self->{dataitems}} ) {
115 williamc 1.6 print $fh "\n";
116     $object->store($fh);
117 williamc 1.4 }
118     close $fh;
119     }
120    
121     sub restore {
122     my $self=shift;
123     my $filename=shift;
124 williamc 1.5 my @arr=();
125     my $data;
126 williamc 1.4
127     my $fh=FileHandle->new();
128     open ($fh, "<$filename") or die "Unable to open file $filename\n $!\n";
129     while ( <$fh> ) {
130 williamc 1.6 push @{$self->{dataitems}}, Utilities::DataItem->restore($fh);
131 williamc 1.4 }
132     close $fh;
133 williamc 1.1 }
134    
135     # ------------------- Support Routines ------------
136     # returns list of array indices of items matching the keys
137 williamc 1.3
138 williamc 1.1 sub _match {
139     my $self=shift;
140     my @keys=@_;
141    
142 williamc 1.3 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 williamc 1.1 }
149     }
150 williamc 1.3 return @matches;
151 williamc 1.1 }