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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by joko, Sun Nov 17 06:35:18 2002 UTC revision 1.17 by joko, Thu Dec 12 02:51:09 2002 UTC
# Line 1  Line 1 
1  #################################  ############################################
2  #  #
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.17  2002/12/12 02:51:09  joko
7    #  + cosmetics
8    #
9    #  Revision 1.16  2002/12/11 06:54:10  joko
10    #  + fix: encapsulated object-loading inside an 'eval'
11    #
12    #  Revision 1.15  2002/12/05 13:55:21  joko
13    #  + now utilizing 'object2hash' instead of 'var_deref'
14    #  + played around with having fresh-objects - no progress....
15    #
16    #  Revision 1.14  2002/12/05 09:40:30  jonen
17    #  + added option->{destroy} at getObject for unloading all instance
18    #
19    #  Revision 1.13  2002/12/05 07:59:04  joko
20    #  + now using Tie::SecureHash as a base for the COREHANDLE
21    #  + former public COREHANDLE becomes private _COREHANDLE now
22    #  + sub getCOREHANDLE
23    #
24    #  Revision 1.12  2002/12/04 11:34:49  joko
25    #  - $schema_tangram doesn't have to be in class?
26    #
27    #  Revision 1.11  2002/12/04 08:54:08  jonen
28    #  + untested bugfix: undef($object) after transform to hash at getObjectAsHash
29    #
30    #  Revision 1.10  2002/12/03 15:53:23  joko
31    #  + small bugfix regarding object hierarchy
32    #
33    #  Revision 1.9  2002/12/03 05:29:40  joko
34    #  + sub getObject
35    #  + sub getObjectAsHash
36    #
37    #  Revision 1.8  2002/12/01 22:25:51  joko
38    #  + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode
39    #
40    #  Revision 1.7  2002/12/01 04:46:19  joko
41    #  + sub eraseAll
42    #
43    #  Revision 1.6  2002/11/29 05:02:30  joko
44    #  - sub getNewPerlObjectByPkgName (moved to libp.pm)
45    #  + sub getMetaInfo
46    #  - sub existsChildNode (moved to Abstract.pm)
47    #  + sub getListUnfiltered
48    #  + sub getListFiltered
49    #  + sub createCursor
50    #  + sub createSet
51    #  + sub sendQuery
52    #
53  #  Revision 1.5  2002/11/17 06:35:18  joko  #  Revision 1.5  2002/11/17 06:35:18  joko
54  #  + locator metadata can now be reached via ->{locator}  #  + locator metadata can now be reached via ->{locator}
55  #  - getChildNodes is now wrapped via COREHANDLE  #  - getChildNodes is now wrapped via COREHANDLE
# Line 25  Line 72 
72  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko
73  #  + new  #  + new
74  #  #
75  #  ############################################
76  #################################  
77    
78  package Data::Storage::Handler::Tangram;  package Data::Storage::Handler::Tangram;
79    
80  use strict;  use strict;
81  use warnings;  use warnings;
82    
83    use base ("Data::Storage::Handler");
84  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
85    
86  use Tangram;  use Tangram;
87  use Data::Dumper;  use Data::Dumper;
88    use libp qw( getNewPerlObjectByPkgName );
89    use Data::Storage::Result::Tangram;
90    use Data::Compare::Struct qw( isEmpty );
91    use Data::Transform::Deep qw( object2hash );
92    use Data::Transform::Encode qw( var2utf8 );
93    
94    
95  # get logger instance  # get logger instance
96  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
97    
98    
99  our $metainfo = {  # this holds the complete instantiated schema from tangram
100    'disconnectMethod' => 'disconnect',  my $schema_tangram;
 };  
101    
102  sub getNewPerlObjectByPkgName {  sub getMetaInfo {
103    my $pkgname = shift;    my $self = shift;
104    my $args = shift;    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
105    $logger->debug( __PACKAGE__ . "->getNewPerlObjectByPkgName( pkgname $pkgname args $args )" );    return {
106    my $evstring = "use $pkgname;";      'disconnectMethod' => 'disconnect',
107    eval($evstring);    };
   $@ && $logger->error( __PACKAGE__ . ':' . __LINE__ . " Error in eval $evstring: " .  $@ );  
   return $pkgname->new($args);  
108  }  }
109    
110  sub _initSchema {  sub _initSchema {
111    my $self = shift;    my $self = shift;
112    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
113    #if (!$self->{schema_tangram}) {    #if (!$schema_tangram) {
114      my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } );      my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } );
115      $self->{schema_tangram} = $obj->getSchema();      $schema_tangram = $obj->getSchema();
116    #}    #}
117    if (!$self->{schema_tangram}) {    if (!$schema_tangram) {
118      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
119      return 0;      return 0;
120    }    }
# Line 90  sub connect { Line 141  sub connect {
141    
142      return unless $self->_initSchema();      return unless $self->_initSchema();
143    
144        # create the main tangram storage object
145      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
146      $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn );      $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
147    
148    #print "connect", "\n";
149    #my $core = $self->{_COREHANDLE};
150    #print Dumper($core);
151        
152        # some attempts for configuring the wrapped underlying dbi.....
153      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
154      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();
155      #$self->_configureUnderlyingStorage;      #$self->_configureUnderlyingStorage;
156        
157        # ..... encapsulation wins!
158      $self->configureCOREHANDLE();      $self->configureCOREHANDLE();
159    
160    $self->{locator}->{status}->{connected} = 1;    $self->{locator}->{status}->{connected} = 1;
# Line 114  sub getChildNodes { Line 173  sub getChildNodes {
173    # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE    # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE
174    #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });    #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
175    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
176      
177      # todo: should we retrieve information from the schema here
178      # rather than poorly getting table names from underlying dbi?
179    my $storage = $self->_getSubLayerHandle();    my $storage = $self->_getSubLayerHandle();
180    @nodes = @{$storage->getChildNodes()};    @nodes = @{$storage->getChildNodes()};
181    #$storage->_configureCOREHANDLE();    #$storage->_configureCOREHANDLE();
# Line 121  sub getChildNodes { Line 183  sub getChildNodes {
183  #print Dumper($self);  #print Dumper($self);
184    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
185        
186    $storage->disconnect();    # TODO: REVIEW
187      #$storage->disconnect();
188        
189    $self->{meta}->{childnodes} = \@nodes;    $self->{meta}->{childnodes} = \@nodes;
190        
# Line 130  sub getChildNodes { Line 193  sub getChildNodes {
193  }  }
194    
195    
 sub existsChildNode {  
   my $self = shift;  
   my $nodename = shift;  
   $logger->debug( __PACKAGE__ . "->getChildNode( nodename $nodename )" );  
   $self->getChildNodes() unless $self->{meta}->{childnodes};  
   #print Dumper($self->{meta}->{childnodes});  
   return grep $nodename, @{$self->{meta}->{childnodes}};  
 }  
   
   
196  sub testIntegrity {  sub testIntegrity {
197    
198    my $self = shift;    my $self = shift;
# Line 200  sub _configureUnderlyingStorage { Line 253  sub _configureUnderlyingStorage {
253    foreach my $key (keys %{$self->{dbi}}) {    foreach my $key (keys %{$self->{dbi}}) {
254      my $val = $self->{dbi}->{$key};      my $val = $self->{dbi}->{$key};
255      print "entry: $key; $val", "\n";      print "entry: $key; $val", "\n";
256      $self->{COREHANDLE}->{db}->{$key} = $val;      $self->{_COREHANDLE}->{db}->{$key} = $val;
257    }    }
258    #print Dumper($self->{COREHANDLE}->{db});    #print Dumper($self->{COREHANDLE}->{db});
259  }  }
# Line 216  sub configureCOREHANDLE { Line 269  sub configureCOREHANDLE {
269    
270    # apply configured modifications    # apply configured modifications
271      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
272        $self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
273      }      }
274      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{dbi}->{RaiseError}) {
275        $self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
276      }      }
277      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{dbi}->{PrintError}) {
278        $self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
279      }      }
280      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{dbi}->{HandleError}) {
281        $self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
282      }      }
283    
284  }  }
# Line 235  sub deploySchema { Line 288  sub deploySchema {
288    my $args = shift;    my $args = shift;
289        
290    my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
   #my $dsn = $self->{dbi}->{dsn};  
291    
292    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );
293    
294    my $ok;    my $ok;
295    # TODO: is this DBI->connect okay here like it is? regarding errors.....???    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
   if ( my $dbh = DBI->connect($dsn, '', '', {  
                                                       PrintError => 0,  
                                                     } ) ) {  
   
296      return unless $self->_initSchema();      return unless $self->_initSchema();
297            $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
     $ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh );  
298      $dbh->disconnect();      $dbh->disconnect();
299    }    }
300    return $ok;    return $ok;
# Line 257  sub retreatSchema { Line 304  sub retreatSchema {
304    
305    my $self = shift;    my $self = shift;
306    my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
   #my $dsn = $self->{dbi}->{dsn};  
307    
308    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );
309    
310    my $ok;    my $ok;
311    if ( my $dbh = DBI->connect($dsn, '', '', {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
                                                       #PrintError => 0,  
                                                       #RaiseError => 0,  
                                                     } ) ) {  
312    
313      return unless $self->_initSchema();      return unless $self->_initSchema();
314            
315      #use Data::Dumper; print Dumper($self);      #use Data::Dumper; print Dumper($self);
316      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
317            
318      $ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh );      $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
319      $ok = 2;    # answer is "maybe" for now since Tangram::Relational->retreat doesn't seem to return a valid status  
320                      # idea: test this by checking for count of tables in database -      # answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because...
321                      #          problem with this: there may be some left not having been included to the schema      # - ... Tangram::Relational->retreat doesn't seem to return a valid status
322        # - possible improvement:
323        #   - test this by checking for count of tables in database
324        #   - problem with this: there may be some left not having been included to the schema
325        #   - maybe better: use "->getChildNodes"?
326        $ok = 2;
327    
328      $dbh->disconnect();      $dbh->disconnect();
329    
330    }    }
331    return $ok;    return $ok;
332  }  }
# Line 301  sub rebuildDbAndSchema { Line 351  sub rebuildDbAndSchema {
351    return $res;    return $res;
352  }  }
353    
354    sub getListUnfiltered {
355      my $self = shift;
356      my $nodename = shift;
357      my @results;
358      $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
359      # get set of objects from odbms by object name
360      my $object_set = $self->{_COREHANDLE}->remote($nodename);
361      @results = $self->{_COREHANDLE}->select($object_set);
362      return \@results;
363    }
364    
365    sub getListFiltered {
366      my $self = shift;
367    
368      # redirect to unfiltered mode
369      #return $self->getListUnfiltered(@_);
370    
371      my $nodename = shift;
372      my $filters = shift;
373      my @results;
374      $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );
375    
376      #print Dumper($filters);
377      
378      my @tfilters;
379      
380      foreach my $filter (@$filters) {
381      
382        # get filter - TODO: for each filter
383        #my $filter = $filters->[0];
384      
385        # build filter
386        my $lexpr = $filter->{key};
387        #my $op = $filter->{op};
388        my $op = '=';
389        my $rexpr = $filter->{val};
390        my $tight = 100;
391        
392      #  my $tfilter = Tangram::Filter->new(
393      #    expr => "t1.$lexpr $op '$rexpr'",
394      #    tight => $tight,
395      #    objects => $objects,
396      #  );
397      
398        # HACK: build eval-string (sorry) to get filtered list - please give advice here
399        push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
400    
401      }
402    
403      my $tfilter = join(' & ', @tfilters);
404    
405      # get set of objects from odbms by object name
406      my $remote = $self->{_COREHANDLE}->remote($nodename);
407      
408      # was:
409      #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
410    
411      # is:
412      # HACK: build eval-string (sorry) to get filtered list - please give advice here
413      my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
414      
415      # get filtered list/set
416      @results = eval($evalstring);
417      die $@ if $@;
418      
419      return \@results;
420    }
421    
422    sub createCursor {
423      my $self = shift;
424      my $node = shift;
425      my $cmdHandle = $self->{_COREHANDLE}->cursor($node);
426      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
427      return $result;
428    }
429    
430    sub createSet {
431      my $self = shift;
432      my @objects = @_;
433      my $rh = Set::Object->new();
434      foreach (@objects) {
435        #print Dumper($_);
436        $rh->insert($_) if !isEmpty($_);
437      }
438      #print Dumper($rh->members());
439      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
440      return $result;
441    }
442    
443    sub sendQuery {
444      my $self = shift;
445      my $query = shift;
446      #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
447      #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
448    
449      #print Dumper($query);
450    
451      # HACK: special case: querying by id does not translate into a common tangram query
452      # just load the object by given id(ent)
453      if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {
454        #print "LOAD!!!", "\n";
455        #exit;
456        #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
457        my $ident = $query->{criterias}->[0]->{val};
458    #print "load obj", "\n";
459        #return $self->createSet() if $ident == 5;
460        my $object = $self->{_COREHANDLE}->load($ident);
461    #print "get id", "\n";
462        my $oid = $self->{_COREHANDLE}->id($object);
463        return $self->createSet($object);
464        #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
465      }
466    
467      die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
468    
469      # TODO: do a common tangram query here
470    
471      my @crits;
472      foreach (@{$query->{criterias}}) {
473        my $op = '';
474        $op = '=' if lc $_->{op} eq 'eq';
475        push @crits, "$_->{key}$op'$_->{val}'";
476      }
477      my $subnodes = {};
478      map { $subnodes->{$_}++ } @{$query->{subnodes}};
479      # HACK: this is hardcoded ;(    expand possibilities!
480      #my $crit = join(' AND ', @crits);
481      #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
482      #return $self->sendCommand($sql);
483      #my $h = $self->{COREHANDLE}->remote($query->{node});
484      #my $res = $self->{COREHANDLE}->select($h, $h->{);
485      return $self->createCursor($query->{node});
486    }
487    
488    sub eraseAll {
489      my $self = shift;
490      my $classname = shift;
491      my $remote = $self->{_COREHANDLE}->remote($classname);
492      my @objs = $self->{_COREHANDLE}->select($remote);
493      $self->{_COREHANDLE}->erase(@objs);
494    }
495    
496    sub createDb {
497      my $self = shift;
498      my $storage = $self->_getSubLayerHandle();
499      return $storage->createDb();
500    }
501    
502    sub getObject {
503      my $self = shift;
504      my $oid = shift;
505      my $options = shift;
506    
507      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
508      # unload($oid) will only unload object, not deep object hashes
509      $self->{_COREHANDLE}->unload() if ($options->{destroy});
510    
511      # TODO: review this
512      #if (!$self->{COREHANDLE}) { return; }
513    
514      # TODO: review this
515      my $object = eval('$self->{_COREHANDLE}->load($oid);');
516      print $@, "\n" if $@;
517    
518      return $object if $object;
519    }
520    
521    sub getObjectAsHash {
522      my $self = shift;
523      my $oid = shift;
524      my $options = shift;
525      my $obj = $self->getObject($oid, $options);
526      
527      # build options (a callback to unload autovivified objects) for 'expand'
528      # TODO: use $logger to write to debug here!
529      my $cb; # = sub {};
530    =pod
531      if ($options->{destroy}) {
532        $options->{cb}->{destroy} = sub {
533          print "================ DESTROY", "\n";
534          my $object = shift;
535          #print Dumper($object);
536          $self->{_COREHANDLE}->unload($object);
537          #undef($object);
538        };
539      }
540    =cut
541    
542      my $hash = object2hash($obj, $options);
543      #$options->{cb}->{destroy}->($obj);
544      #$self->{_COREHANDLE}->unload($obj);
545      
546      # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML
547      # now done in object2hash
548      #var2utf8($hash) if ($options->{utf8});
549      
550      # old (wrong) attempts to get rid of used instances, if requested
551        #$obj->clear_refs;
552        #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
553        #$obj->DESTROY;
554        #undef($obj) if($options->{destroy});
555      
556      return $hash;
557    }
558    
559    sub getSchema {
560      return $schema_tangram;
561    }
562    
563    sub getCOREHANDLE {
564      my $self = shift;
565      return $self->{_COREHANDLE};
566    }
567    
568  1;  1;

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.17

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