/[cvs]/nfo/perl/libs/Data/Storage/Handler/Tangram.pm
ViewVC logotype

Annotation of /nfo/perl/libs/Data/Storage/Handler/Tangram.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (hide annotations)
Thu Dec 5 13:55:21 2002 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.14: +36 -9 lines
+ now utilizing 'object2hash' instead of 'var_deref'
+ played around with having fresh-objects - no progress....

1 joko 1.6 ############################################
2 cvsjoko 1.1 #
3 joko 1.15 # $Id: Tangram.pm,v 1.14 2002/12/05 09:40:30 jonen Exp $
4 joko 1.2 #
5     # $Log: Tangram.pm,v $
6 joko 1.15 # Revision 1.14 2002/12/05 09:40:30 jonen
7     # + added option->{destroy} at getObject for unloading all instance
8     #
9 jonen 1.14 # Revision 1.13 2002/12/05 07:59:04 joko
10     # + now using Tie::SecureHash as a base for the COREHANDLE
11     # + former public COREHANDLE becomes private _COREHANDLE now
12     # + sub getCOREHANDLE
13     #
14 joko 1.13 # Revision 1.12 2002/12/04 11:34:49 joko
15     # - $schema_tangram doesn't have to be in class?
16     #
17 joko 1.12 # Revision 1.11 2002/12/04 08:54:08 jonen
18     # + untested bugfix: undef($object) after transform to hash at getObjectAsHash
19     #
20 jonen 1.11 # Revision 1.10 2002/12/03 15:53:23 joko
21     # + small bugfix regarding object hierarchy
22     #
23 joko 1.10 # Revision 1.9 2002/12/03 05:29:40 joko
24     # + sub getObject
25     # + sub getObjectAsHash
26     #
27 joko 1.9 # Revision 1.8 2002/12/01 22:25:51 joko
28     # + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode
29     #
30 joko 1.8 # Revision 1.7 2002/12/01 04:46:19 joko
31     # + sub eraseAll
32     #
33 joko 1.7 # Revision 1.6 2002/11/29 05:02:30 joko
34     # - sub getNewPerlObjectByPkgName (moved to libp.pm)
35     # + sub getMetaInfo
36     # - sub existsChildNode (moved to Abstract.pm)
37     # + sub getListUnfiltered
38     # + sub getListFiltered
39     # + sub createCursor
40     # + sub createSet
41     # + sub sendQuery
42     #
43 joko 1.6 # Revision 1.5 2002/11/17 06:35:18 joko
44     # + locator metadata can now be reached via ->{locator}
45     # - getChildNodes is now wrapped via COREHANDLE
46     #
47 joko 1.5 # Revision 1.4 2002/10/25 11:44:44 joko
48     # + sub _initSchema
49     # + sub existsChildNode
50     # + sub testIntegrity
51     # + sub rebuildDbAndSchema
52     #
53 joko 1.4 # Revision 1.3 2002/10/17 03:56:55 joko
54     # + bugfix: trapped eval error
55     #
56 joko 1.3 # Revision 1.2 2002/10/17 00:10:05 joko
57     # + removed dependency from tsobj.pm, schema is now independent
58     # + sub getNewPerlObjectByPkgName
59     # + sub deploySchema
60     # + sub retreatSchema
61     #
62 joko 1.2 # Revision 1.1 2002/10/10 03:44:07 cvsjoko
63     # + new
64 cvsjoko 1.1 #
65 joko 1.6 ############################################
66    
67 cvsjoko 1.1
68     package Data::Storage::Handler::Tangram;
69    
70     use strict;
71     use warnings;
72    
73 joko 1.13 use base ("Data::Storage::Handler");
74 cvsjoko 1.1 use base ("Data::Storage::Handler::Abstract");
75    
76     use Tangram;
77     use Data::Dumper;
78 joko 1.6 use libp qw( getNewPerlObjectByPkgName );
79     use Data::Storage::Result::Tangram;
80     use Data::Compare::Struct qw( isEmpty );
81 joko 1.15 use Data::Transform::Deep qw( object2hash );
82 joko 1.9 use Data::Transform::Encode qw( var2utf8 );
83    
84 cvsjoko 1.1
85     # get logger instance
86     my $logger = Log::Dispatch::Config->instance;
87    
88    
89 joko 1.12 # this holds the complete instantiated schema from tangram
90     my $schema_tangram;
91    
92 joko 1.6 sub getMetaInfo {
93     my $self = shift;
94     $logger->debug( __PACKAGE__ . "->getMetaInfo()" );
95     return {
96     'disconnectMethod' => 'disconnect',
97     };
98 joko 1.2 }
99    
100 joko 1.4 sub _initSchema {
101     my $self = shift;
102     $logger->debug( __PACKAGE__ . "->_initSchema()" );
103 joko 1.12 #if (!$schema_tangram) {
104 joko 1.5 my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } );
105 joko 1.12 $schema_tangram = $obj->getSchema();
106 joko 1.4 #}
107 joko 1.12 if (!$schema_tangram) {
108 joko 1.4 $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
109     return 0;
110     }
111     return 1;
112     }
113    
114 cvsjoko 1.1 sub connect {
115    
116     my $self = shift;
117    
118     my $dsn = shift;
119 joko 1.5 $dsn ||= $self->{locator}->{dbi}->{dsn};
120 cvsjoko 1.1
121 joko 1.2 $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
122 cvsjoko 1.1
123     #my $storage = Tangram::Relational->connect( $schema, $dsn );
124     #my $storage = Tangram::mysql->connect( $schema, $dsn );
125     #$storage = Tangram::Relational->connect( Project->schema, $dsn );
126    
127     # if (!testDsn($dsn)) {
128     # croak("Database at \"$dsn\" is not available");
129     # return;
130     # }
131    
132 joko 1.4 return unless $self->_initSchema();
133    
134 joko 1.6 # create the main tangram storage object
135 joko 1.2 #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
136 joko 1.13 $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
137    
138     #print "connect", "\n";
139     #my $core = $self->{_COREHANDLE};
140     #print Dumper($core);
141 cvsjoko 1.1
142 joko 1.6 # some attempts for configuring the wrapped underlying dbi.....
143 cvsjoko 1.1 #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
144     #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();
145     #$self->_configureUnderlyingStorage;
146 joko 1.6
147     # ..... encapsulation wins!
148 cvsjoko 1.1 $self->configureCOREHANDLE();
149    
150 joko 1.5 $self->{locator}->{status}->{connected} = 1;
151    
152 joko 1.4 return 1;
153    
154 cvsjoko 1.1 }
155    
156     sub getChildNodes {
157    
158     my $self = shift;
159     my @nodes;
160    
161 joko 1.4 $logger->debug( __PACKAGE__ . "->getChildNodes()" );
162    
163 cvsjoko 1.1 # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE
164     #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
165     #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
166 joko 1.6
167     # todo: should we retrieve information from the schema here
168     # rather than poorly getting table names from underlying dbi?
169 cvsjoko 1.1 my $storage = $self->_getSubLayerHandle();
170 joko 1.5 @nodes = @{$storage->getChildNodes()};
171 cvsjoko 1.1 #$storage->_configureCOREHANDLE();
172 joko 1.5 #print "getchildnodes\n";
173     #print Dumper($self);
174 cvsjoko 1.1 #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
175    
176 joko 1.8 # TODO: REVIEW
177     #$storage->disconnect();
178 cvsjoko 1.1
179 joko 1.4 $self->{meta}->{childnodes} = \@nodes;
180    
181 cvsjoko 1.1 return \@nodes;
182    
183     }
184    
185    
186 joko 1.4 sub testIntegrity {
187    
188     my $self = shift;
189    
190     $logger->debug( __PACKAGE__ . "->testIntegrity()" );
191    
192     # 1st test: are there tables?
193     if (!$self->getChildNodes()) {
194     $logger->warning( __PACKAGE__ . "->testIntegrity no childnodes exist" );
195     return;
196     }
197    
198     # 2nd test: is there a table named "Tangram"?
199     if (!$self->existsChildNode("Tangram")) {
200     $logger->warning( __PACKAGE__ . "->testIntegrity childnode \"Tangram\" doesn't exist" );
201     return;
202     }
203    
204 joko 1.5 $self->{locator}->{status}->{integrity} = 1;
205 joko 1.4 return 1;
206    
207     }
208    
209    
210 cvsjoko 1.1 sub _getSubLayerHandle {
211    
212     my $self = shift;
213    
214 joko 1.4 $logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" );
215    
216 cvsjoko 1.1 #print Dumper($self);
217 joko 1.5
218 cvsjoko 1.1 # hack, make more generic!
219 joko 1.4 if (!$self->{dataStorageLayer}) {
220     $logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" );
221     #my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} );
222 joko 1.5 my $loc = Data::Storage::Locator->new( { type => "DBI", dbi => $self->{locator}->{dbi} } );
223 joko 1.4 $self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } );
224 cvsjoko 1.1 #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE();
225     #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE();
226     }
227    
228     #print Dumper($self->{STORAGE_UNDER_THE_HOOD});
229    
230 joko 1.4 return $self->{dataStorageLayer};
231 cvsjoko 1.1
232     }
233    
234     sub _configureUnderlyingStorage {
235    
236     my $self = shift;
237 joko 1.4
238     $logger->debug( __PACKAGE__ . "->_configureUnderlyingStorage" );
239    
240 cvsjoko 1.1 $self->_configureCOREHANDLE_DBI();
241     return;
242    
243     foreach my $key (keys %{$self->{dbi}}) {
244     my $val = $self->{dbi}->{$key};
245     print "entry: $key; $val", "\n";
246 joko 1.13 $self->{_COREHANDLE}->{db}->{$key} = $val;
247 cvsjoko 1.1 }
248     #print Dumper($self->{COREHANDLE}->{db});
249     }
250    
251    
252     sub configureCOREHANDLE {
253    
254     my $self = shift;
255    
256 joko 1.4 $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
257 cvsjoko 1.1
258     #my $subLayer = $self->_getSubLayerHandle();
259    
260     # apply configured modifications
261     if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
262 joko 1.13 $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
263 cvsjoko 1.1 }
264     if (exists $self->{dbi}->{RaiseError}) {
265 joko 1.13 $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
266 cvsjoko 1.1 }
267     if (exists $self->{dbi}->{PrintError}) {
268 joko 1.13 $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
269 cvsjoko 1.1 }
270     if (exists $self->{dbi}->{HandleError}) {
271 joko 1.13 $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
272 cvsjoko 1.1 }
273    
274 joko 1.2 }
275    
276     sub deploySchema {
277     my $self = shift;
278 joko 1.5 my $args = shift;
279    
280     my $dsn = $self->{locator}->{dbi}->{dsn};
281 joko 1.4
282     $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );
283    
284 joko 1.2 my $ok;
285 joko 1.8 if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
286 joko 1.4 return unless $self->_initSchema();
287 joko 1.12 $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
288 joko 1.2 $dbh->disconnect();
289     }
290     return $ok;
291     }
292    
293     sub retreatSchema {
294 joko 1.4
295 joko 1.2 my $self = shift;
296 joko 1.5 my $dsn = $self->{locator}->{dbi}->{dsn};
297 joko 1.4
298     $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );
299    
300 joko 1.2 my $ok;
301 joko 1.8 if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
302 joko 1.4
303     return unless $self->_initSchema();
304    
305     #use Data::Dumper; print Dumper($self);
306     $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
307    
308 joko 1.12 $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
309 joko 1.8
310     # answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because...
311     # - ... Tangram::Relational->retreat doesn't seem to return a valid status
312     # - possible improvement:
313     # - test this by checking for count of tables in database
314     # - problem with this: there may be some left not having been included to the schema
315     # - maybe better: use "->getChildNodes"?
316     $ok = 2;
317    
318 joko 1.2 $dbh->disconnect();
319 joko 1.8
320 joko 1.2 }
321     return $ok;
322 joko 1.4 }
323    
324     sub rebuildDbAndSchema {
325     my $self = shift;
326     $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );
327     my @results;
328    
329     # sum up results (bool (0/1)) in array
330     push @results, $self->retreatSchema();
331     push @results, $self->{dataStorageLayer}->dropDb();
332     push @results, $self->{dataStorageLayer}->createDb();
333     push @results, $self->deploySchema();
334    
335     # scan array for "bad ones"
336     my $res = 1;
337     map {
338     $res = 0 if (!$_);
339     } @results;
340    
341     return $res;
342 joko 1.6 }
343    
344     sub getListUnfiltered {
345     my $self = shift;
346     my $nodename = shift;
347     my @results;
348     $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
349     # get set of objects from odbms by object name
350 joko 1.13 my $object_set = $self->{_COREHANDLE}->remote($nodename);
351     @results = $self->{_COREHANDLE}->select($object_set);
352 joko 1.6 return \@results;
353     }
354    
355     sub getListFiltered {
356     my $self = shift;
357    
358     # redirect to unfiltered mode
359     #return $self->getListUnfiltered(@_);
360    
361     my $nodename = shift;
362     my $filters = shift;
363     my @results;
364     $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );
365    
366     #print Dumper($filters);
367    
368     my @tfilters;
369    
370     foreach my $filter (@$filters) {
371    
372     # get filter - TODO: for each filter
373     #my $filter = $filters->[0];
374    
375     # build filter
376     my $lexpr = $filter->{key};
377     #my $op = $filter->{op};
378     my $op = '=';
379     my $rexpr = $filter->{val};
380     my $tight = 100;
381    
382     # my $tfilter = Tangram::Filter->new(
383     # expr => "t1.$lexpr $op '$rexpr'",
384     # tight => $tight,
385     # objects => $objects,
386     # );
387    
388     # HACK: build eval-string (sorry) to get filtered list - please give advice here
389     push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
390    
391     }
392    
393     my $tfilter = join(' & ', @tfilters);
394    
395     # get set of objects from odbms by object name
396 joko 1.13 my $remote = $self->{_COREHANDLE}->remote($nodename);
397 joko 1.6
398     # was:
399     #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
400    
401     # is:
402     # HACK: build eval-string (sorry) to get filtered list - please give advice here
403 joko 1.13 my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
404 joko 1.6
405     # get filtered list/set
406     @results = eval($evalstring);
407     die $@ if $@;
408    
409     return \@results;
410     }
411    
412     sub createCursor {
413     my $self = shift;
414     my $node = shift;
415 joko 1.13 my $cmdHandle = $self->{_COREHANDLE}->cursor($node);
416 joko 1.6 my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
417     return $result;
418     }
419    
420     sub createSet {
421     my $self = shift;
422     my @objects = @_;
423     my $rh = Set::Object->new();
424     foreach (@objects) {
425     #print Dumper($_);
426     $rh->insert($_) if !isEmpty($_);
427     }
428     #print Dumper($rh->members());
429     my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
430     return $result;
431     }
432    
433     sub sendQuery {
434     my $self = shift;
435     my $query = shift;
436     #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
437     #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
438    
439     #print Dumper($query);
440    
441     # HACK: special case: querying by id does not translate into a common tangram query
442     # just load the object by given id(ent)
443     if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {
444     #print "LOAD!!!", "\n";
445     #exit;
446     #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
447     my $ident = $query->{criterias}->[0]->{val};
448     #print "load obj", "\n";
449     #return $self->createSet() if $ident == 5;
450 joko 1.13 my $object = $self->{_COREHANDLE}->load($ident);
451 joko 1.6 #print "get id", "\n";
452 joko 1.13 my $oid = $self->{_COREHANDLE}->id($object);
453 joko 1.6 return $self->createSet($object);
454     #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
455     }
456    
457     die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
458    
459     # TODO: do a common tangram query here
460    
461     my @crits;
462     foreach (@{$query->{criterias}}) {
463     my $op = '';
464     $op = '=' if lc $_->{op} eq 'eq';
465     push @crits, "$_->{key}$op'$_->{val}'";
466     }
467     my $subnodes = {};
468     map { $subnodes->{$_}++ } @{$query->{subnodes}};
469     # HACK: this is hardcoded ;( expand possibilities!
470     #my $crit = join(' AND ', @crits);
471     #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
472     #return $self->sendCommand($sql);
473     #my $h = $self->{COREHANDLE}->remote($query->{node});
474     #my $res = $self->{COREHANDLE}->select($h, $h->{);
475     return $self->createCursor($query->{node});
476 joko 1.7 }
477    
478     sub eraseAll {
479     my $self = shift;
480     my $classname = shift;
481 joko 1.13 my $remote = $self->{_COREHANDLE}->remote($classname);
482     my @objs = $self->{_COREHANDLE}->select($remote);
483     $self->{_COREHANDLE}->erase(@objs);
484 joko 1.8 }
485    
486     sub createDb {
487     my $self = shift;
488     my $storage = $self->_getSubLayerHandle();
489     return $storage->createDb();
490 joko 1.9 }
491    
492     sub getObject {
493     my $self = shift;
494     my $oid = shift;
495 jonen 1.14 my $options = shift;
496     # TODO: create a deep_unload method
497     # unload($oid) will only unload object, not deep object hashes
498     $self->{_COREHANDLE}->unload() if($options->{destroy});
499 joko 1.9 # TODO: review this
500     #if (!$self->{COREHANDLE}) { return; }
501 joko 1.13 return $self->{_COREHANDLE}->load($oid);
502 joko 1.9 }
503    
504     sub getObjectAsHash {
505     my $self = shift;
506     my $oid = shift;
507     my $options = shift;
508 jonen 1.14 my $obj = $self->getObject($oid, $options);
509 joko 1.15
510     # build options (a callback to unload autovivified objects) for 'expand'
511     # TODO: use $logger to write to debug here!
512     my $cb; # = sub {};
513     =pod
514     if ($options->{destroy}) {
515     $options->{cb}->{destroy} = sub {
516     print "================ DESTROY", "\n";
517     my $object = shift;
518     #print Dumper($object);
519     $self->{_COREHANDLE}->unload($object);
520     #undef($object);
521     };
522     }
523     =cut
524    
525     my $hash = object2hash($obj, $options);
526     #$options->{cb}->{destroy}->($obj);
527     #$self->{_COREHANDLE}->unload($obj);
528    
529     # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML
530     # now done in object2hash
531     #var2utf8($hash) if ($options->{utf8});
532    
533     # old (wrong) attempts to get rid of used instances, if requested
534     #$obj->clear_refs;
535     #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
536     #$obj->DESTROY;
537     #undef($obj) if($options->{destroy});
538    
539     return $hash;
540 joko 1.12 }
541    
542     sub getSchema {
543     return $schema_tangram;
544 joko 1.13 }
545    
546     sub getCOREHANDLE {
547     my $self = shift;
548     return $self->{_COREHANDLE};
549 cvsjoko 1.1 }
550    
551     1;

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed