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

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

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