/[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.14 - (hide annotations)
Thu Dec 5 09:40:30 2002 UTC (21 years, 7 months ago) by jonen
Branch: MAIN
Changes since 1.13: +15 -3 lines
+ added option->{destroy} at getObject for unloading all instance

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

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