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.2 by williamc, Fri Sep 17 15:40:26 1999 UTC vs.
Revision 1.6 by williamc, Tue Feb 8 13:53:22 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 >        print "$obj @_\n";
52 >          $obj->alias(@{$aliaskeys});
53 >        }
54   }
55  
56 + sub unalias {
57 +        my $self=shift;
58 +        my @keys=@_;
59  
60 < sub getdata {
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 $d ( @match ) {
89 <          push @data, $self->{datahash}[$d];
88 >        foreach $i ( @match ) {
89 >          push @data, $self->{dataitems}[$i];
90          }
91          return @data;
92   }
93  
94 < sub deletedata {
94 > sub getdata {
95          my $self=shift;
96          my @keys=@_;
97 <
98 <        # first get all the keys we want to delete
97 >        
98 >        my @data=();
99          my @match=$self->_match(@keys);
100 +        my $i;
101          foreach $i ( @match ) {
102 <          splice ( @{$self->{datahash}}, $i, 1 );
103 <          splice ( @{$self->{datakey}}, $i, 1 );
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 $i;
143 <        my @matchold;
144 <        my @matchnew;
145 <        # At the start everything matches
146 <        for ( $i=0; $i<=$self->items(); $i++ ) {
147 <          if ( $#{$self->{datakey}[$i]} >= $#keys ) {
79 <            push @matchold, $i;
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 <
83 <        # now test against keys
84 <        for ( $i=0; $i <= $#keys; $i++ ) {
85 <          undef @matchnew;
86 <          foreach $dn ( @matchold ){
87 <            if ( $self->{datakey}[$dn][$i] eq $keys[$i] ) {
88 <                push @matchnew,$dn;
89 <            }
90 <          }
91 <          @matchold=@matchnew;
92 <        }
93 <        return @matchold; # list of array indecies
150 >        return @matches;
151   }
95

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines