ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/HashDB.pm
(Generate patch)

Comparing COMP/SCRAM/src/Utilities/HashDB.pm (file contents):
Revision 1.1 by williamc, Thu Sep 16 14:54:21 1999 UTC vs.
Revision 1.7.4.1 by williamc, Fri Aug 4 08:27:56 2000 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines