lib/RDF/Service/Cache.pm untested

filter: Statementstmt 0.0% 0 / 281 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 94 MC/DCmcdc 0.0% 0 / 49 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 565 CC 50 SCAR 42.3
CC
50
Cov
0%
validate_type
182.0
validate_node
132.0
debug_start
90.0
CRAP 68.6
1 # $Id: Cache.pm,v 1.19 2000/12/21 22:04:18 aigan Exp $ -*-perl-*-
2  
3 package RDF::Service::Cache;
4  
5 #=====================================================================
6 #
7 # DESCRIPTION
8 # Exports access functions to cached data
9 #
10 # AUTHOR
11 # Jonas Liljegren <jonas@paranormal.se>
12 #
13 # COPYRIGHT
14 # Copyright (C) 2000 Jonas Liljegren. All Rights Reserved.
15 #
16 # This module is free software; you can redistribute it and/or
17 # modify it under the same terms as Perl itself.
18 #
19 #=====================================================================
20  
21 use strict;
22 use base 'Exporter';
23 use vars qw( $uri2id $id2uri $ids @EXPORT_OK %EXPORT_TAGS $create_cnt
24 $create_time $prefixlist $node %fc );
25 use RDF::Service::Constants qw( :all );
26 use Carp;
27  
28 our $DEBUG = 3;
29 our $Level = 0;
30 our @Level_stack = ();
31  
32 {
33 # If the hash and array gets to large, they should be tied to a
34 # dbm database.
35  
36 # These id's are internal and can be used for diffrent uri's if
37 # the server is restarted. They should not be used to store data
38 # in interfaces, such as the standard DBI interface.
39  
40 # %fc is the function counter. Used for debugging
41  
42 $uri2id = {};
43 $id2uri = [undef]; #First slot reserved
44  
45 $prefixlist = {};
46  
47 $ids =
48 {
49 '' => [],
50 };
51  
52 $create_time = 0;
53  
54 my @ALL = qw( uri2id id2uri save_ids interfaces get_unique_id
55 list_prefixes debug $Level $DEBUG debug_start
56 debug_end expire time_string validate_context
57 reset_level);
58  
59 @EXPORT_OK = ( @ALL );
60 %EXPORT_TAGS = ( 'all' => [@ALL],
61 );
62 }
63  
64 sub debug
65 {
66 my( $msg, $verbose ) = @_;
67 $verbose ||= 0;
68  
69 if( $verbose <= $DEBUG )
70 {
71 $msg =~ s/^/'| 'x$Level/gem;
72 warn( $msg );
73 }
74 }
75  
76 sub reset_level
77 {
78 $Level = 0;
79 @Level_stack = ();
80  
81 warn "\n**** Error Exception ****\n\n" if $DEBUG;
82 return "";
83 }
84  
85 sub debug_start
86 {
87 my( $call, $no, $res ) = @_;
88 die "Nesting too deep. Bailing out!\n" if $Level >= 50;
89 $Level++;
90 return unless $DEBUG;
91  
92  
93 $no = ' ' unless defined $no;
94 $fc{$call}++;
95 my $msg = '| 'x($Level-1);
96 $msg .= "/-- $no $call $fc{$call}\n";
97 warn $msg;
98 push @Level_stack, $call;
99 if( $res )
100 {
101 my $ids = $res->[NODE][IDS];
102 debug( $res->[NODE][URISTR]." IDS $res->[NODE][IDS]\n", 1);
103 debug( "W: $res->[WMODEL][NODE][URISTR] ".
104 "IDS $res->[WMODEL][NODE][IDS]\n", 2);
105 confess "IDS mismatch" if $ids ne $res->[WMODEL][NODE][IDS];
106 if( $res->[NODE][MODEL] )
107 {
108 debug( "M: $res->[NODE][MODEL][URISTR] ".
109 "IDS $res->[NODE][MODEL][IDS]\n", 2);
110 confess "IDS mismatch" if $ids ne $res->[NODE][MODEL][IDS];
111 }
112  
113 validate_context( $res ) if $DEBUG >= 4;
114 }
115 }
116  
117 sub validate_context
118 {
119 my( $context ) = @_;
120  
121 unless( ref $context eq 'RDF::Service::Context' )
122 {
123 confess "The res ($context) should be a context";
124 }
125  
126 unless( ref($context->[HISTORY]) eq 'ARRAY' )
127 {
128 confess "Malformed HISTORY: $context->[HISTORY]";
129 }
130  
131 $context->[WMODEL] or confess "WMODEL missing";
132 $context->[NODE] or confess "NODE missing";
133  
134 debug "*** Validating $context->[NODE][URISTR] ".
135 "IDS $context->[NODE][IDS]\n";
136  
137 validate_node( $context->[WMODEL],
138 {
139 expect =>
140 {
141 types => [NS_LS.'#Model'],
142 props => [NS_LS.'#updated'],
143 }
144 }
145 );
146  
147 validate_node( $context );
148  
149 debug "*** Done\n";
150 }
151  
152 sub validate_node
153 {
154 my( $self, $arg ) = @_;
155  
156 my $node = $self->[NODE];
157  
158 ref $node eq 'RDF::Service::Resource'
159 or confess "The node ($node) is not a Resource";
160  
161 my $uri = $node->[URISTR] or confess "URISTR missing";
162  
163 defined $node->[RUNLEVEL] or confess "RUNLEVEL undefined";
164 $node->[IDS] or confess "IDS missing" if $node->[RUNLEVEL];
165 $node->[ID] or confess "ID missing";
166 uri2id( $uri ) eq $node->[ID] or confess "Wrong ID";
167 id2uri( $node->[ID] ) eq $uri or confess "Wrong URI";
168  
169 validate_type($self,
170 { target => TYPE,
171 expect => $arg->{'expect'}{'types'},
172 }) if $node->[TYPE];
173 validate_type($self, {target => REV_TYPE} ) if $node->[REV_TYPE];
174  
175 if( $node->[MODEL] )
176 {
177 ref $node->[MODEL] eq 'RDF::Service::Resource'
178 or confess "Bad model ($node->[MODEL])";
179 my $model_res = $self->get_context_by_id( $node->[MODEL][ID] );
180 my $model = $model_res->[NODE];
181 if( $model->[URISTR] ne NS_LD.'#The_Base_Model' )
182 {
183 $model_res->could_be_a(NS_LS.'#Model')
184 or confess "The model ($model->[URISTR]) ".
185 "is not a Model";
186 $node->[MODEL][REV_MODEL]{$node->[ID]}
187 or confess "Res ($uri IDS $node->[IDS]) is missing from ".
188 "model $node->[MODEL][URISTR] IDS $node->[MODEL][IDS]";
189 }
190 }
191  
192 validate_pred( $self ) if @{$node->[REV_PRED]};
193 validate_prop( $self, {target => REV_SUBJ} ) if $node->[REV_SUBJ];
194 validate_prop( $self, {target => REV_OBJ } ) if $node->[REV_OBJ];
195 validate_arc( $self ) if $node->[PRED];
196 validate_model($self ) if $self->is_known_as_a( NS_LS.'#Model' );
197 }
198  
199 sub validate_type
200 {
201 my( $self, $arg ) = @_;
202  
203 my( $rel, $rev, $rel_all, $rel_name, $rev_name );
204 if( not defined $arg->{'target'} or $arg->{'target'} == TYPE )
205 {
206 $rel = $self->[NODE][TYPE];
207 $rel_name = 'TYPE';
208 $rev_name = 'REV_TYPE';
209 $rev = REV_TYPE;
210 $rel_all = TYPE_ALL;
211 }
212 elsif( $arg->{'target'} == REV_TYPE )
213 {
214 $rel = $self->[NODE][REV_TYPE];
215 $rel_name = 'REV_TYPE';
216 $rev_name = 'TYPE';
217 $rev = TYPE;
218 $rel_all = REV_TYPE_ALL;
219 }
220 else
221 {
222 confess "Wrong argument 'target': $arg->{'target'}";
223 }
224  
225 return if $self->[NODE][URISTR] eq NS_RDFS.'Resource';
226  
227 ref $rel eq 'HASH' or confess "Type ($rel) should be a hashref";
228  
229 my( %expect ) = map { uri2id($_), 0 } @{$arg->{'expect'}};
230 my $node_id = $self->[NODE][ID];
231  
232 debug "Validating $rel_name $self->[NODE][URISTR]\n", 6;
233 my $found_resource = 0;
234 foreach my $res_id ( keys %$rel )
235 {
236 $res_id =~ /^\d+$/
237 or confess "Res key ($res_id) should be a number";
238  
239 my $res = $self->get_context_by_id( $res_id );
240 my $uri = $res->[NODE][URISTR] or confess "Shit";
241  
242 if( $rev == REV_TYPE )
243 {
244 $res->could_be_a(NS_RDFS.'Class')
245 or confess "The type ($uri) is not a class";
246 }
247  
248 debug " Checking type $uri\n", 6;
249 if( $uri eq NS_RDFS.'Resource' )
250 {
251 debug " Setting found_resource to 1\n", 6;
252 $found_resource ++;
253 }
254  
255 ref $rel->{$res_id} eq "HASH"
256 or confess "Value for $uri should be a hashref";
257 keys( %{$rel->{$res_id}} )
258 or confess "$rel_name for $self->[NODE][URISTR] is $uri, ".
259 "but that's false. No model has that. --";
260  
261 foreach my $model_id ( keys %{$rel->{$res_id}} )
262 {
263 $model_id =~ /^\d+$/
264 or confess "The model key ($model_id)".
265 " for $uri should be a number";
266  
267 my $model_res = $self->get_context_by_id( $model_id );
268 my $model = $model_res->[NODE];
269  
270 if( $model->[URISTR] ne NS_LD.'#The_Base_Model' )
271 {
272 $model_res->could_be_a(NS_LS.'#Model')
273 or confess "The model ($model->[URISTR]) ".
274 "is not a Model";
275 }
276  
277 if( my $state = $rel->{$res_id}{$model_id} )
278 {
279 $state =~ /^[12]$/
280 or confess "The value of type ($uri) ".
281 "can only be 1 or 2";
282 }
283  
284 unless( $res->[NODE][$rev]{$node_id}{$model_id} )
285 {
286 unless( $uri eq NS_RDFS.'Resource' )
287 {
288 my $rel_uri = $self->[NODE][URISTR];
289 my $rev_uri = $uri;
290 my $model_uri = $model->[URISTR];
291 my $explain = "\nIn the model $model_uri ".
292 "(IDS : $model->[IDS])\n";
293 $explain .= "$rel_uri is $self->[NODE] ".
294 "with IDS $self->[NODE][IDS]\n";
295 $explain .= "$rev_uri is $res->[NODE] ".
296 "with IDS $res->[NODE][IDS]\n";
297 $explain .= "$rel_uri --$rel_name--> $rev_uri exists\n";
298 $explain .= "$rev_uri --$rev_name--> $rel_uri missing\n";
299 confess $explain ."--";
300 }
301 }
302 }
303  
304 $expect{$res_id}++;
305  
306 }
307  
308 if( $self->[NODE][$rel_all] )
309 {
310 if( $rev == REV_TYPE )
311 {
312 $found_resource or confess "A defined TYPE should ".
313 "at least include Resource:\n".
314 $self->types_as_string . "--";
315 }
316 foreach my $type_id ( keys %expect )
317 {
318 unless( $expect{$type_id} )
319 {
320 my $uri = id2uri( $type_id );
321 confess "Type $uri expected but not found ".
322 "for $self->[NODE][URISTR]:\n".
323 $self->types_as_string . "--";
324 }
325 }
326 }
327 }
328  
329 sub validate_pred
330 {
331 my( $self, $arg ) = @_;
332  
333 my $node = $self->[NODE];
334  
335 ref $node->[REV_PRED] eq "ARRAY"
336 or confess "REV_SUBJ should be an array ref";
337  
338 foreach my $arc_node ( @{$node->[REV_PRED]} )
339 {
340 my $arc = $self->get_context_by_id( $arc_node->[ID] );
341  
342 # warn "*** REV_PRED $arc_node->[URISTR]\n";
343  
344 $arc->could_be_a( NS_RDF.'Statement' )
345 or confess "The arc ($arc_node->[URISTR]) ".
346 "should be a Statement";
347  
348 $arc_node->[PRED][ID] == $node->[ID]
349 or confess "The REV_PRED was not met by ".
350 "the arc ($arc_node->[URISTR])";
351 }
352  
353 $self->could_be_a( NS_RDF.'Property' )
354 or confess "The node ($node->[URISTR]) should ".
355 "be a Property";
356  
357 }
358  
359 sub validate_prop
360 {
361 my( $self, $arg ) = @_;
362  
363 my( $rel, $rel_name, $rev );
364 if( not defined $arg->{'target'} or $arg->{'target'} == REV_SUBJ )
365 {
366 $rel = REV_SUBJ;
367 $rel_name = 'REV_SUBJ';
368 $rev = SUBJ;
369 }
370 elsif( $arg->{'target'} == REV_OBJ )
371 {
372 $rel = REV_OBJ;
373 $rel_name = 'REV_OBJ';
374 $rev = OBJ;
375 }
376 else
377 {
378 confess "Wrong argument 'target': $arg->{'target'}";
379 }
380  
381 my $node = $self->[NODE];
382 ref $node->[$rel] eq 'HASH'
383 or confess "Var ($node->[$rel]) should be a hashref";
384  
385 my( %expect ) = map { uri2id($_), 0 } @{$arg->{'expect'}};
386  
387 foreach my $pred_id ( keys %{$node->[$rel]} )
388 {
389 $pred_id =~ /^\d+$/
390 or confess "The Pred key ($pred_id) should be a number";
391  
392 my $pred = $self->get_context_by_id( $pred_id );
393 my $uri = $pred->[NODE][URISTR] or confess "Shit";
394  
395 $pred->could_be_a(NS_RDF.'Property')
396 or confess "The Res ($uri) should be a Property";
397  
398 ref $node->[$rel]{$pred_id} eq "ARRAY"
399 or confess "Value for $uri should be an array ref";
400 @{$node->[$rel]{$pred_id}}
401 or confess "$rel_name $uri should not be empty";
402  
403 foreach my $arc_node ( @{$node->[$rel]{$pred_id}} )
404 {
405 my $arc = $self->get_context_by_id( $arc_node->[ID] );
406  
407 $arc->could_be_a( NS_RDF.'Statement' )
408 or confess "The res ($arc_node->[URISTR] ".
409 "IDS $arc_node->[IDS]) ".
410 "should be a Statement";
411  
412 $arc_node->[$rev][ID] == $node->[ID]
413 or confess "The pred $pred->[NODE][URISTR] was ".
414 "not met by the arc ($arc_node->[URISTR])";
415 }
416  
417 $expect{$pred_id}++;
418 }
419  
420 foreach my $pred_id ( keys %expect )
421 {
422 unless( $expect{$pred_id} )
423 {
424 my $uri = id2uri( $pred_id );
425 confess "Pred $uri expected but not found";
426 }
427 }
428 }
429  
430 sub validate_arc
431 {
432 my( $self, $arg ) = @_;
433  
434 my $node = $self->[NODE];
435  
436 $self->could_be_a( NS_RDF.'Statement' )
437 or confess "The arc ($node->[URISTR] IDS $node->[IDS]) ".
438 "should be a Statement";
439  
440 my $explain = " P $node->[PRED][URISTR] IDS $node->[PRED][IDS]\n";
441 $explain .= " S $node->[SUBJ][URISTR] IDS $node->[SUBJ][IDS]\n";
442 $explain .= " O $node->[OBJ][URISTR] IDS $node->[OBJ][IDS]\n";
443 $explain .= " M $node->[MODEL][URISTR] IDS $node->[MODEL][IDS]\n";
444 $explain .= " A $node->[URISTR] IDS $node->[IDS]\n";
445 $explain .= " SOLID $node->[SOLID]\n";
446  
447 my $found = 0;
448 foreach my $arc_node ( @{$node->[PRED][REV_PRED]} )
449 {
450 $found ++ if $arc_node->[ID] == $node->[ID]
451 }
452 $found or confess "$explain The pred $node->[PRED][URISTR] ".
453 "did not meet up with REV_PRED";
454  
455 my $model_id = $node->[MODEL][ID];
456 my $pred_id = $node->[PRED][ID];
457  
458 # This only checks if there is any subj pointing with the same
459 # pred
460 #
461 $node->[SUBJ][REV_SUBJ]{$pred_id}
462 or confess "$explain SUBJ was not met by a REV_SUBJ";
463  
464 $node->[OBJ][REV_OBJ]{$pred_id}
465 or confess "$explain OBJ was not met by a REV_OBJ";
466 }
467  
468 sub validate_model
469 {
470 my( $self, $arg ) = @_;
471 #
472 # NB! The model include this node in REV_MODEL if it self or any
473 # of its types belongs to the model. But the node includes the
474 # model only if int's internal data belongs to that model.
475  
476 return;
477  
478 my $node = $self->[NODE];
479 foreach my $res_id ( keys %{$node->[REV_MODEL]} )
480 {
481 my $res = $self->get_context_by_id($res_id);
482 my $res_node = $res->[NODE];
483  
484 unless( $res_node->[MODEL] )
485 {
486 confess "Res ($res_node->[URISTR]) model ".
487 "should be defined ".
488 "and point to the model ($node->[URISTR])\n";
489 }
490  
491 unless( $res_node->[MODEL][ID] == $node->[ID] )
492 {
493 my $explain = "The res ($res_node->[URISTR]) should belong ".
494 "to this model\n";
495 $explain .= "$res_node->[URISTR] is $res_node ".
496 "with IDS $res_node->[IDS]\n";
497 $explain .= "Types for $node->[URISTR]:\n";
498 $explain .= $self->types_as_string;
499 confess $explain ."--";
500 }
501 }
502 }
503  
504 sub debug_end
505 {
506 my( $call, $no, $res ) = @_;
507 if( $DEBUG >= 4 and $res )
508 {
509 validate_context( $res );
510 }
511 $Level--;
512 return unless $DEBUG;
513  
514 $no = ' ' unless defined $no;
515  
516 my $in_call = pop @Level_stack;
517  
518 my $msg = '| 'x$Level;
519 $msg .= "\\__ $no $call\n";
520 warn $msg;
521  
522 if( $in_call ne $call )
523 {
524 warn "*** call mismatch ***\n";
525 confess "Call mismatch. End call should be '$in_call' ";
526 }
527 }
528  
529 sub uri2id
530 {# TODO: Define constnats for the most common nodes
531  
532  
533 # $_[0] is the uri. (How much faster is this?)
534  
535 confess unless defined $_[0];
536  
537 # Todo: Normalize the uri and consider aliases
538 #
539 my $id = $uri2id->{$_[0]};
540 return $id if defined $id;
541  
542 $id = $#$id2uri+1; #No threads here!
543  
544 $id2uri->[$id] = $_[0];
545 $uri2id->{$_[0]} = $id;
546  
547 return $id;
548 }
549  
550 sub id2uri
551 {
552 if( $DEBUG )
553 {
554 confess "Not a number ( $_[0] )" unless $_[0] =~ /^\d+$/;
555 }
556 return $id2uri->[$_[0]];
557 }
558  
559 sub time_string
560 {
561 # NB! Don't call this as &time_string, since that reuses the old
562 # @_
563  
564 use Time::Object;
565 return localtime( $_[0] || time )->strftime('%Y-%m-%d %H:%M:%S');
566 }
567  
568  
569 sub save_ids
570 {
571 # $_[0] is the new IDS
572 # $_[1] is a ref to array of interface objects
573 $ids->{$_[0]} = $_[1];
574 }
575  
576 sub interfaces
577 {
578 # Return ref to array of inteface object
579  
580 # carp "*** interfaces @{$ids->{$_[0]}} ***\n";
581 return $ids->{$_[0]} or die "IDS $_[0] does not exist\n";
582 }
583  
584 sub get_unique_id
585 {
586 # Return a unique id. This depends on
587 # usage in a ns owned by the server process. I.e: only one process
588 # allowed, unless combined with the PID.
589  
590 # Remember the number of objects created this second
591 #
592 my $time = time;
593 if( $time != $create_time )
594 {
595 $create_time = $time;
596 $create_cnt = 1;
597 }
598 else
599 {
600 $create_cnt++;
601 }
602  
603 # Normally not more than 1000 objects created per second
604 #
605 use POSIX qw( strftime );
606 return strftime( "%Y%m%dT%H%M%S", localtime($time)).
607 sprintf('-%.3d', $create_cnt);
608 }
609  
610 sub list_prefixes
611 {
612 my( $ids ) = @_;
613  
614 debug "Creating a prefixlist for IDS $ids\n", 2;
615  
616 return @{ $prefixlist->{$ids} ||= [sort {length($b) <=> length($a)}
617 map( keys %{$_->[MODULE_REG]},
618 @{interfaces($ids)}),'' ] };
619 }
620  
621 1;