blib/lib/NTuples.pm

filter: Statementstmt 84.3% 86 / 102 Branchbran 54.5% 12 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 9 / 11 Podpod 0.0% 0 / 10 totaltotal 73.7% 107 / 145 CC 21 SCAR 15.5
CC
21
Cov
79%
debug_display
12.0
update_row
6.0
insert_data
5.0
CRAP 4.7
↑ Summary
1 # NTuples
2 # Specialized intra-memory RDBMS
3 # Copyright (c) 2005, 2006, 2007 Charles Morris
4 # All rights reserved.
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the same terms as Perl itself.
8 #
9 # This module is the core association engine for a few projects
10 # based on fast access to network configuration information.
11 #
12 # There are generic wrappers available to you for use in your
13 # own projects. See the NTuples::wrap::* packages.
14 #
15 # For more information on my projects, check for news at:
16 # http://www.cs.odu.edu/~cmorris/
17 #
18 # Who says project creep should be avoided?
19  
20  
21 package NTuples;
22  
23 1 our @DATA;
24 1 our @FMT;
25 1 our %colname_to_colnum;
26 1 our %addr_to_loc;
27  
28 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 $VERSION = '0.11';
30 }
31  
32 sub debug_display
33 0 ▶{
Subroutine debug_display: not called CC=3 SCAR=24.8
Pod undocumented
34 0 my $rownum = 0;
35 0 foreach my $row ( @DATA )
36 {
37 0 my $colnum = 0;
38 0 foreach my $val ( @{$row} )
39 {
40 0 print "$val:[$rownum][$colnum] ";
41 0 $colnum++;
42 }
43 0 $rownum++;
44 0 print "\n";
45 }
46 }
47  
48 #---------- Constructor ----------#
49 #static method
50 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
51 1 my ($pkg) = shift;
52  
53 1 my $instance = bless( {}, $pkg );
54  
55 1 return $instance;
56 }
57  
58 ##########################
59 #--- object functions ---#
60  
61 # new_format()
62 # static method
63 # register format for any current or added data tuples.
64 sub new_format
65 1 ▶{
Subroutine new_format: called CC=3 SCAR=11.1
Pod undocumented
66 1 my ($instance) = shift;
67 1 ▶ warn "expecting a ". __PACKAGE__ ."\n" unless $instance->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $instance->isa(__PACKAGE__) 0 1 1
68  
69 1 @FMT = @_;
70  
71 1 my $i = 0;
72 1 my $x;
73 map{
74 1 $x = $_;
75 3 ▶ if($x){$colname_to_colnum{$x} = $i;}
Branch: 1 branch
BranchTrueFalseTotal
if ($x) 3 0 3
76 3 $i++;
77 } @FMT;
78  
79 1 return $instance;
80 }
81  
82  
83 # new_data()
84 # static method
85 # register data (overwriting old data)
86 sub new_data
87 1 ▶{
Subroutine new_data: called CC=5 SCAR=16.1
Pod undocumented
88 1 my ($instance) = shift;
89 1 ▶ warn "expecting a ". __PACKAGE__ ."\n" unless $instance->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $instance->isa(__PACKAGE__) 0 1 1
90  
91 1 @DATA = @_;
92  
93 # probeably be made alot more efficient.
94 # any patches are welcome :)
95  
96 #use Time::HiRes;
97 #my $start = Time::HiRes::time();
98 #$last = $start;
99  
100 1 my $rownum = 0;
101 1 foreach my $row (@DATA)
102 {
103 13 my $colnum = 0;
104 13 foreach my $field (@{$row})
105 {
106 39 my $i = 0;
107  
108 39 foreach my $colname (@FMT)
109 {
110 #debugged, $field needs to be encapsulated with ''s.
111 117 $fieldrep = $field;
112 117 $fieldrep =~ s/'/\\\'/g;
113 # $code = '$lh_'. $FMT[$colnum] .'{\''. $fieldrep .'\'}{'. $colname .'} = '.
114 # '\$DATA[$rownum][$colname_to_colnum{$colname}];';
115  
116 $lh{ $FMT[$colnum] }{ $fieldrep }{ $colname } =
117 117 \$DATA[$rownum][$colname_to_colnum{$colname}];
118  
119 # eval "$code"; warn "$code: $@" if $@;
120 }
121  
122 39 $addr_to_loc{\$DATA[$rownum][$colnum]} = [ $rownum, $colnum ];
123  
124 39 $colnum++;
125 }
126 13 $rownum++;
127 }
128  
129 #$finish = Time::HiRes::time();
130 #print $finish - $start ."\n";
131  
132 1 return $instance;
133 }
134  
135  
136 # insert_data()
137 # static method
138 # register an array of data (pushes to the end of current data)
139 # INSERT INTO NTuples VALUES @data
140 sub insert_data
141 1 ▶{
Subroutine insert_data: called CC=5 SCAR=16.1
Pod undocumented
142 1 my ($instance) = shift;
143 1 ▶ warn "expecting a ". __PACKAGE__ ."\n" unless $instance->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $instance->isa(__PACKAGE__) 0 1 1
144  
145 1 push(@DATA, @_);
146  
147 # we REALLLY dont need to remap the entire LoL........
148 # but thats how its happening right now until I have time to sit down and figure it out
149  
150 1 my $rownum = 0;
151 1 foreach my $row (@DATA)
152 {
153 14 my $colnum = 0;
154 14 foreach my $field (@{$row})
155 {
156 42 my $i = 0;
157 42 foreach my $colname (@FMT)
158 {
159 # $code = '$lh_'. $FMT[$colnum] .'{'. $field .'}{'. $colname .'} = '.
160 # '\$DATA[$rownum][$colname_to_colnum{$colname}];';
161  
162 $lh{ $FMT[$colnum] }{ $field }{ $colname } =
163 126 \$DATA[$rownum][$colname_to_colnum{$colname}];
164  
165 # eval "$code"; warn "$code: $@" if $@;
166 }
167  
168 42 $addr_to_loc{\$DATA[$rownum][$colnum]} = [ $rownum, $colnum ];
169  
170 42 $colnum++;
171 }
172 14 $rownum++;
173 }
174  
175 1 return $instance;
176 }
177  
178  
179 # select_row()
180 # SELECT * FROM NTuples WHERE $keyname = $key
181 sub select_row
182 2 ▶{
Subroutine select_row: called CC=2 SCAR=7.0
Pod undocumented
183 2 my ($instance, $keyname, $key) = @_;
184 2 ▶ warn "expecting a ". __PACKAGE__ ."\n" unless $instance->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $instance->isa(__PACKAGE__) 0 2 2
185  
186  
187 # This is faster (just an educated guess) but it returns crazy ordered results
188 # (ordered by the value of the hashed key.. I think)
189 # my %result;
190 # my $code = '%result = %{$lh_'. $keyname .'{'. $key .'}};';
191 # eval "$code"; warn $@ if $@;
192 # my @ret;
193 # map { push(@ret, ${$_}) } values %result;
194  
195 # So we do this instead.
196  
197 2 my @ret;
198 2 map { push(@ret, NTuples::select_value($instance, $keyname, $key, $_)) } @FMT;
199
200 2 return @ret;
201 }
202  
203  
204 # update_row()
205 # static method
206 # updates row resolved by $keyname{$key}
207 # UPDATE NTuples SET * = @row WHERE $keyname = $key
208 sub update_row
209 0 ▶{
Subroutine update_row: not called CC=2 SCAR=17.9
Pod undocumented
210 0 my ($instance, $keyname, $key, @row) = @_;
211 0 ▶ warn "expecting a ". __PACKAGE__ ."\n" unless $instance->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $instance->isa(__PACKAGE__) 0 0 0
212  
213 0 my $i = 0;
214 0 map { NTuples::update_value($instance, $keyname, $key, @FMT[$i], $_); $i++ } @row;
215  
216 0 return $instance;
217 }
218  
219  
220 # select_value()
221 # static method
222 # resolves value $valname from row resolved by $keyname{$key}
223 # SELECT $valname FROM NTuples WHERE $keyname = $key
224 sub select_value
225 14 ▶{
Subroutine select_value: called CC=2 SCAR=7.0
Pod undocumented
226 14 my ($instance, $keyname, $key, $valname) = @_;
227 14 ▶ warn "expecting a ". __PACKAGE__ ."\n" unless $instance->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $instance->isa(__PACKAGE__) 0 14 14
228  
229 14 my $result;
230 # my $code = '$result = $lh_'. $keyname .'{'. $key .'}{'. $valname .'};';
231 # eval "$code"; warn "$code: $@" if $@;
232  
233 14 $result = $lh{ $keyname }{ $key }{ $valname };
234  
235 14 return ${$result};
236 }
237  
238  
239 # update_value()
240 # static method
241 # updates value of $valname in row resolved by $keyname{$key} to $val
242 # UPDATE NTuples SET $valname = $val WHERE $keyname = $key
243 sub update_value
244 1 ▶{
Subroutine update_value: called CC=2 SCAR=6.9
Pod undocumented
245 1 my ($instance, $keyname, $key, $valname, $val) = @_;
246 1 ▶ warn "expecting a ". __PACKAGE__ ."\n" unless $instance->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $instance->isa(__PACKAGE__) 0 1 1
247  
248 #Step 1) Buffer old value
249 #Step 2) Alter actual value
250 #Step 3) Copy association based on $valname from $oldval to $val
251 #Step 4) Remove old association based on $oldval
252  
253 #ok, so now its at least only doing one scalar assignment and one eval{}
254 # my $code = '
255 # $oldvalue = ${$lh_'. $keyname .'{'. $key .'}{'. $valname .'}};
256 # ${$lh_'. $keyname .'{'. $key .'}{'. $valname .'}} = $val;
257 # $lh_'. $valname .'{'. $val .'} = $lh_'. $valname .'{$oldvalue};
258 # delete $lh_'. $valname .'{$oldvalue};
259 # ';
260 #codes and evaluates (using test.pl)
261 # $oldvalue = ${$lh_username{sys}{uid}};
262 # ${$lh_username{sys}{uid}} = $val;
263 # $lh_uid{17} = $lh_uid{$oldvalue};
264 # delete $lh_uid{$oldvalue};
265  
266 # print "eval: $code\n";
267 # eval "$code"; warn "$code: $@" if $@;
268
269 1 $oldvalue = ${ $lh{ $keyname }{ $key }{ $valname } };
270 #print "$oldvalue = \${ \$lh{ $keyname }{ $key }{ $valname } }\n";
271
272 1 ${ $lh{ $keyname }{ $key }{ $valname } } = $val;
273 #print "\${ \$lh{ $keyname }{ $key }{ $valname } } = $val\n";
274
275 1 $lh{ $valname }{ $val } = $lh{ $valname }{ $oldvalue };
276 #print "\$lh{ $valname }{ $val } = \$lh{ $valname }{ $oldvalue }\n";
277
278 1 delete $lh{ $valname }{ $oldvalue };
279  
280 1 return $instance;
281 }
282  
283  
284 # delete_row()
285 # static method
286 # deletes row resolved by $keyname{$key}
287 # DELETE FROM NTuples WHERE $keyname = $key
288 sub delete_row
289 2 ▶{
Subroutine delete_row: called CC=5 SCAR=16.1
Pod undocumented
290 2 my ($instance, $keyname, $key) = @_;
291 2 ▶ warn "expecting a ". __PACKAGE__ ."\n" unless $instance->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $instance->isa(__PACKAGE__) 0 2 2
292  
293 2 my $result;
294 # my $code = '$result = $lh_'. $keyname .'{'. $key .'}{'. $keyname .'};';
295 # print "eval: $code\n";
296 # eval "$code"; warn "$code: $@" if $@;
297  
298 2 $result = $lh{ $keyname }{ $key }{ $keyname };
299  
300 2 ▶ if( $result )
Branch: 1 branch
BranchTrueFalseTotal
if ($result) 1 1 2
301 {
302 1 splice( @DATA, @{$addr_to_loc{$result}}[0], 1 );
303
304 1 my $i = 0;
305 1 my @row = NTuples::select_row($instance, $keyname, $key);
306
307 1 foreach my $colname (@FMT)
308 {
309 # $code = 'delete $lh_'. $colname .'{'. $row[$i] .'};'; #if exists will not work with delete()
310 #eval "$code"; warn "$code: $@" if $@;
311  
312 3 delete $lh{$colname}{$row[$i]};
313  
314 3 $i++;
315 }
316 }
317  
318 2 ▶return (defined($result)? '1' : '0');
Branch: 1 branch
BranchTrueFalseTotal
defined $result ? : 1 1 2
319 }
320  
321 1 1;
322  
323 __END__
↑ Summary