/[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.14 by jonen, Thu Dec 5 09:40:30 2002 UTC
# Line 1  Line 1 
1  #################################  ############################################
2  #  #
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.14  2002/12/05 09:40:30  jonen
7    #  + added option->{destroy} at getObject for unloading all instance
8    #
9    #  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    #  Revision 1.12  2002/12/04 11:34:49  joko
15    #  - $schema_tangram doesn't have to be in class?
16    #
17    #  Revision 1.11  2002/12/04 08:54:08  jonen
18    #  + untested bugfix: undef($object) after transform to hash at getObjectAsHash
19    #
20    #  Revision 1.10  2002/12/03 15:53:23  joko
21    #  + small bugfix regarding object hierarchy
22    #
23    #  Revision 1.9  2002/12/03 05:29:40  joko
24    #  + sub getObject
25    #  + sub getObjectAsHash
26    #
27    #  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    #  Revision 1.7  2002/12/01 04:46:19  joko
31    #  + sub eraseAll
32    #
33    #  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  #  Revision 1.5  2002/11/17 06:35:18  joko  #  Revision 1.5  2002/11/17 06:35:18  joko
44  #  + locator metadata can now be reached via ->{locator}  #  + locator metadata can now be reached via ->{locator}
45  #  - getChildNodes is now wrapped via COREHANDLE  #  - getChildNodes is now wrapped via COREHANDLE
# Line 25  Line 62 
62  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko
63  #  + new  #  + new
64  #  #
65  #  ############################################
66  #################################  
67    
68  package Data::Storage::Handler::Tangram;  package Data::Storage::Handler::Tangram;
69    
70  use strict;  use strict;
71  use warnings;  use warnings;
72    
73    use base ("Data::Storage::Handler");
74  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
75    
76  use Tangram;  use Tangram;
77  use Data::Dumper;  use Data::Dumper;
78    use libp qw( getNewPerlObjectByPkgName );
79    use Data::Storage::Result::Tangram;
80    use Data::Compare::Struct qw( isEmpty );
81    use Data::Transform::Deep qw( var_deref );
82    use Data::Transform::Encode qw( var2utf8 );
83    
84    
85  # get logger instance  # get logger instance
86  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
87    
88    
89  our $metainfo = {  # this holds the complete instantiated schema from tangram
90    'disconnectMethod' => 'disconnect',  my $schema_tangram;
 };  
91    
92  sub getNewPerlObjectByPkgName {  sub getMetaInfo {
93    my $pkgname = shift;    my $self = shift;
94    my $args = shift;    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
95    $logger->debug( __PACKAGE__ . "->getNewPerlObjectByPkgName( pkgname $pkgname args $args )" );    return {
96    my $evstring = "use $pkgname;";      'disconnectMethod' => 'disconnect',
97    eval($evstring);    };
   $@ && $logger->error( __PACKAGE__ . ':' . __LINE__ . " Error in eval $evstring: " .  $@ );  
   return $pkgname->new($args);  
98  }  }
99    
100  sub _initSchema {  sub _initSchema {
101    my $self = shift;    my $self = shift;
102    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
103    #if (!$self->{schema_tangram}) {    #if (!$schema_tangram) {
104      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} } );
105      $self->{schema_tangram} = $obj->getSchema();      $schema_tangram = $obj->getSchema();
106    #}    #}
107    if (!$self->{schema_tangram}) {    if (!$schema_tangram) {
108      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
109      return 0;      return 0;
110    }    }
# Line 90  sub connect { Line 131  sub connect {
131    
132      return unless $self->_initSchema();      return unless $self->_initSchema();
133    
134        # create the main tangram storage object
135      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
136      $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn );      $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
137    
138    #print "connect", "\n";
139    #my $core = $self->{_COREHANDLE};
140    #print Dumper($core);
141        
142        # some attempts for configuring the wrapped underlying dbi.....
143      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
144      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();
145      #$self->_configureUnderlyingStorage;      #$self->_configureUnderlyingStorage;
146        
147        # ..... encapsulation wins!
148      $self->configureCOREHANDLE();      $self->configureCOREHANDLE();
149    
150    $self->{locator}->{status}->{connected} = 1;    $self->{locator}->{status}->{connected} = 1;
# Line 114  sub getChildNodes { Line 163  sub getChildNodes {
163    # 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
164    #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} });
165    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
166      
167      # todo: should we retrieve information from the schema here
168      # rather than poorly getting table names from underlying dbi?
169    my $storage = $self->_getSubLayerHandle();    my $storage = $self->_getSubLayerHandle();
170    @nodes = @{$storage->getChildNodes()};    @nodes = @{$storage->getChildNodes()};
171    #$storage->_configureCOREHANDLE();    #$storage->_configureCOREHANDLE();
# Line 121  sub getChildNodes { Line 173  sub getChildNodes {
173  #print Dumper($self);  #print Dumper($self);
174    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
175        
176    $storage->disconnect();    # TODO: REVIEW
177      #$storage->disconnect();
178        
179    $self->{meta}->{childnodes} = \@nodes;    $self->{meta}->{childnodes} = \@nodes;
180        
# Line 130  sub getChildNodes { Line 183  sub getChildNodes {
183  }  }
184    
185    
 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}};  
 }  
   
   
186  sub testIntegrity {  sub testIntegrity {
187    
188    my $self = shift;    my $self = shift;
# Line 200  sub _configureUnderlyingStorage { Line 243  sub _configureUnderlyingStorage {
243    foreach my $key (keys %{$self->{dbi}}) {    foreach my $key (keys %{$self->{dbi}}) {
244      my $val = $self->{dbi}->{$key};      my $val = $self->{dbi}->{$key};
245      print "entry: $key; $val", "\n";      print "entry: $key; $val", "\n";
246      $self->{COREHANDLE}->{db}->{$key} = $val;      $self->{_COREHANDLE}->{db}->{$key} = $val;
247    }    }
248    #print Dumper($self->{COREHANDLE}->{db});    #print Dumper($self->{COREHANDLE}->{db});
249  }  }
# Line 216  sub configureCOREHANDLE { Line 259  sub configureCOREHANDLE {
259    
260    # apply configured modifications    # apply configured modifications
261      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
262        $self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
263      }      }
264      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{dbi}->{RaiseError}) {
265        $self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
266      }      }
267      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{dbi}->{PrintError}) {
268        $self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
269      }      }
270      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{dbi}->{HandleError}) {
271        $self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
272      }      }
273    
274  }  }
# Line 235  sub deploySchema { Line 278  sub deploySchema {
278    my $args = shift;    my $args = shift;
279        
280    my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
   #my $dsn = $self->{dbi}->{dsn};  
281    
282    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );
283    
284    my $ok;    my $ok;
285    # 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,  
                                                     } ) ) {  
   
286      return unless $self->_initSchema();      return unless $self->_initSchema();
287            $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
     $ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh );  
288      $dbh->disconnect();      $dbh->disconnect();
289    }    }
290    return $ok;    return $ok;
# Line 257  sub retreatSchema { Line 294  sub retreatSchema {
294    
295    my $self = shift;    my $self = shift;
296    my $dsn = $self->{locator}->{dbi}->{dsn};    my $dsn = $self->{locator}->{dbi}->{dsn};
   #my $dsn = $self->{dbi}->{dsn};  
297    
298    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );    $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );
299    
300    my $ok;    my $ok;
301    if ( my $dbh = DBI->connect($dsn, '', '', {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
                                                       #PrintError => 0,  
                                                       #RaiseError => 0,  
                                                     } ) ) {  
302    
303      return unless $self->_initSchema();      return unless $self->_initSchema();
304            
305      #use Data::Dumper; print Dumper($self);      #use Data::Dumper; print Dumper($self);
306      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
307            
308      $ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh );      $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
309      $ok = 2;    # answer is "maybe" for now since Tangram::Relational->retreat doesn't seem to return a valid status  
310                      # 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...
311                      #          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
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      $dbh->disconnect();      $dbh->disconnect();
319    
320    }    }
321    return $ok;    return $ok;
322  }  }
# Line 301  sub rebuildDbAndSchema { Line 341  sub rebuildDbAndSchema {
341    return $res;    return $res;
342  }  }
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      my $object_set = $self->{_COREHANDLE}->remote($nodename);
351      @results = $self->{_COREHANDLE}->select($object_set);
352      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      my $remote = $self->{_COREHANDLE}->remote($nodename);
397      
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      my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
404      
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      my $cmdHandle = $self->{_COREHANDLE}->cursor($node);
416      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        my $object = $self->{_COREHANDLE}->load($ident);
451    #print "get id", "\n";
452        my $oid = $self->{_COREHANDLE}->id($object);
453        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    }
477    
478    sub eraseAll {
479      my $self = shift;
480      my $classname = shift;
481      my $remote = $self->{_COREHANDLE}->remote($classname);
482      my @objs = $self->{_COREHANDLE}->select($remote);
483      $self->{_COREHANDLE}->erase(@objs);
484    }
485    
486    sub createDb {
487      my $self = shift;
488      my $storage = $self->_getSubLayerHandle();
489      return $storage->createDb();
490    }
491    
492    sub getObject {
493      my $self = shift;
494      my $oid = shift;
495      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      # TODO: review this
500      #if (!$self->{COREHANDLE}) { return; }
501      return $self->{_COREHANDLE}->load($oid);
502    }
503    
504    sub getObjectAsHash {
505      my $self = shift;
506      my $oid = shift;
507      my $options = shift;
508      my $obj = $self->getObject($oid, $options);
509      my $deref = var_deref($obj);
510      var2utf8($deref) if ($options->{utf8});
511      #$obj->clear_refs;
512      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
513      #$obj->DESTROY;
514      #undef($obj) if($options->{destroy});
515      return $deref;
516    }
517    
518    sub getSchema {
519      return $schema_tangram;
520    }
521    
522    sub getCOREHANDLE {
523      my $self = shift;
524      return $self->{_COREHANDLE};
525    }
526    
527  1;  1;

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

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