/[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.11 by jonen, Wed Dec 4 08:54:08 2002 UTC revision 1.18 by joko, Fri Dec 13 21:48:07 2002 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.18  2002/12/13 21:48:07  joko
7    #  + fix to 'sub sendQuery'
8    #
9    #  Revision 1.17  2002/12/12 02:51:09  joko
10    #  + cosmetics
11    #
12    #  Revision 1.16  2002/12/11 06:54:10  joko
13    #  + fix: encapsulated object-loading inside an 'eval'
14    #
15    #  Revision 1.15  2002/12/05 13:55:21  joko
16    #  + now utilizing 'object2hash' instead of 'var_deref'
17    #  + played around with having fresh-objects - no progress....
18    #
19    #  Revision 1.14  2002/12/05 09:40:30  jonen
20    #  + added option->{destroy} at getObject for unloading all instance
21    #
22    #  Revision 1.13  2002/12/05 07:59:04  joko
23    #  + now using Tie::SecureHash as a base for the COREHANDLE
24    #  + former public COREHANDLE becomes private _COREHANDLE now
25    #  + sub getCOREHANDLE
26    #
27    #  Revision 1.12  2002/12/04 11:34:49  joko
28    #  - $schema_tangram doesn't have to be in class?
29    #
30  #  Revision 1.11  2002/12/04 08:54:08  jonen  #  Revision 1.11  2002/12/04 08:54:08  jonen
31  #  + untested bugfix: undef($object) after transform to hash at getObjectAsHash  #  + untested bugfix: undef($object) after transform to hash at getObjectAsHash
32  #  #
# Line 59  package Data::Storage::Handler::Tangram; Line 83  package Data::Storage::Handler::Tangram;
83  use strict;  use strict;
84  use warnings;  use warnings;
85    
86    use base ("Data::Storage::Handler");
87  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
88    
89  use Tangram;  use Tangram;
# Line 66  use Data::Dumper; Line 91  use Data::Dumper;
91  use libp qw( getNewPerlObjectByPkgName );  use libp qw( getNewPerlObjectByPkgName );
92  use Data::Storage::Result::Tangram;  use Data::Storage::Result::Tangram;
93  use Data::Compare::Struct qw( isEmpty );  use Data::Compare::Struct qw( isEmpty );
94  use Data::Transform::Deep qw( var_deref );  use Data::Transform::Deep qw( object2hash );
95  use Data::Transform::Encode qw( var2utf8 );  use Data::Transform::Encode qw( var2utf8 );
96    
97    
# Line 74  use Data::Transform::Encode qw( var2utf8 Line 99  use Data::Transform::Encode qw( var2utf8
99  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
100    
101    
102    # this holds the complete instantiated schema from tangram
103    my $schema_tangram;
104    
105  sub getMetaInfo {  sub getMetaInfo {
106    my $self = shift;    my $self = shift;
107    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
# Line 85  sub getMetaInfo { Line 113  sub getMetaInfo {
113  sub _initSchema {  sub _initSchema {
114    my $self = shift;    my $self = shift;
115    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
116    #if (!$self->{schema_tangram}) {    #if (!$schema_tangram) {
117      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} } );
118      $self->{schema_tangram} = $obj->getSchema();      $schema_tangram = $obj->getSchema();
119    #}    #}
120    if (!$self->{schema_tangram}) {    if (!$schema_tangram) {
121      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
122      return 0;      return 0;
123    }    }
# Line 118  sub connect { Line 146  sub connect {
146    
147      # create the main tangram storage object      # create the main tangram storage object
148      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
149      $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn );      $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
150    
151    #print "connect", "\n";
152    #my $core = $self->{_COREHANDLE};
153    #print Dumper($core);
154        
155      # some attempts for configuring the wrapped underlying dbi.....      # some attempts for configuring the wrapped underlying dbi.....
156      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
# Line 224  sub _configureUnderlyingStorage { Line 256  sub _configureUnderlyingStorage {
256    foreach my $key (keys %{$self->{dbi}}) {    foreach my $key (keys %{$self->{dbi}}) {
257      my $val = $self->{dbi}->{$key};      my $val = $self->{dbi}->{$key};
258      print "entry: $key; $val", "\n";      print "entry: $key; $val", "\n";
259      $self->{COREHANDLE}->{db}->{$key} = $val;      $self->{_COREHANDLE}->{db}->{$key} = $val;
260    }    }
261    #print Dumper($self->{COREHANDLE}->{db});    #print Dumper($self->{COREHANDLE}->{db});
262  }  }
# Line 240  sub configureCOREHANDLE { Line 272  sub configureCOREHANDLE {
272    
273    # apply configured modifications    # apply configured modifications
274      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
275        $self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
276      }      }
277      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{dbi}->{RaiseError}) {
278        $self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
279      }      }
280      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{dbi}->{PrintError}) {
281        $self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
282      }      }
283      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{dbi}->{HandleError}) {
284        $self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
285      }      }
286    
287  }  }
# Line 265  sub deploySchema { Line 297  sub deploySchema {
297    my $ok;    my $ok;
298    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
299      return unless $self->_initSchema();      return unless $self->_initSchema();
300      $ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh );      $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
301      $dbh->disconnect();      $dbh->disconnect();
302    }    }
303    return $ok;    return $ok;
# Line 286  sub retreatSchema { Line 318  sub retreatSchema {
318      #use Data::Dumper; print Dumper($self);      #use Data::Dumper; print Dumper($self);
319      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
320            
321      $ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh );      $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
322    
323      # answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because...      # answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because...
324      # - ... Tangram::Relational->retreat doesn't seem to return a valid status      # - ... Tangram::Relational->retreat doesn't seem to return a valid status
# Line 328  sub getListUnfiltered { Line 360  sub getListUnfiltered {
360    my @results;    my @results;
361    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
362    # get set of objects from odbms by object name    # get set of objects from odbms by object name
363    my $object_set = $self->{COREHANDLE}->remote($nodename);    my $object_set = $self->{_COREHANDLE}->remote($nodename);
364    @results = $self->{COREHANDLE}->select($object_set);    @results = $self->{_COREHANDLE}->select($object_set);
365    return \@results;    return \@results;
366  }  }
367    
# Line 374  sub getListFiltered { Line 406  sub getListFiltered {
406    my $tfilter = join(' & ', @tfilters);    my $tfilter = join(' & ', @tfilters);
407    
408    # get set of objects from odbms by object name    # get set of objects from odbms by object name
409    my $remote = $self->{COREHANDLE}->remote($nodename);    my $remote = $self->{_COREHANDLE}->remote($nodename);
410        
411    # was:    # was:
412    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
413    
414    # is:    # is:
415    # HACK: build eval-string (sorry) to get filtered list - please give advice here    # HACK: build eval-string (sorry) to get filtered list - please give advice here
416    my $evalstring = 'return $self->{COREHANDLE}->select($remote, ' . $tfilter . ');';    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
417        
418    # get filtered list/set    # get filtered list/set
419    @results = eval($evalstring);    @results = eval($evalstring);
# Line 393  sub getListFiltered { Line 425  sub getListFiltered {
425  sub createCursor {  sub createCursor {
426    my $self = shift;    my $self = shift;
427    my $node = shift;    my $node = shift;
428    my $cmdHandle = $self->{COREHANDLE}->cursor($node);    my $cmdHandle = $self->{_COREHANDLE}->cursor($node);
429    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
430    return $result;    return $result;
431  }  }
432    
433  sub createSet {  sub createSet {
434    my $self = shift;    my $self = shift;
435    #print "-" x 80, "\n";
436    #print Dumper(@_);
437    my @objects = @_;    my @objects = @_;
438    my $rh = Set::Object->new();    my $rh = Set::Object->new();
439    foreach (@objects) {    foreach (@objects) {
440      #print Dumper($_);      if (!isEmpty($_)) {
441      $rh->insert($_) if !isEmpty($_);  #print Dumper($_);
442          $rh->insert($_);
443        }
444    }    }
445    #print Dumper($rh->members());    #print Dumper($rh->members());
446    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
# Line 428  sub sendQuery { Line 464  sub sendQuery {
464      my $ident = $query->{criterias}->[0]->{val};      my $ident = $query->{criterias}->[0]->{val};
465  #print "load obj", "\n";  #print "load obj", "\n";
466      #return $self->createSet() if $ident == 5;      #return $self->createSet() if $ident == 5;
467      my $object = $self->{COREHANDLE}->load($ident);      $self->{_COREHANDLE}->unload($ident);
468        my $object = $self->{_COREHANDLE}->load($ident);
469  #print "get id", "\n";  #print "get id", "\n";
470      my $oid = $self->{COREHANDLE}->id($object);      my $oid = $self->{_COREHANDLE}->id($object);
471    #print Dumper($object);
472    #print "oid: $oid", "\n";
473      return $self->createSet($object);      return $self->createSet($object);
474      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
475    }    }
# Line 459  sub sendQuery { Line 498  sub sendQuery {
498  sub eraseAll {  sub eraseAll {
499    my $self = shift;    my $self = shift;
500    my $classname = shift;    my $classname = shift;
501    my $remote = $self->{COREHANDLE}->remote($classname);    my $remote = $self->{_COREHANDLE}->remote($classname);
502    my @objs = $self->{COREHANDLE}->select($remote);    my @objs = $self->{_COREHANDLE}->select($remote);
503    $self->{COREHANDLE}->erase(@objs);    $self->{_COREHANDLE}->erase(@objs);
504  }  }
505    
506  sub createDb {  sub createDb {
# Line 473  sub createDb { Line 512  sub createDb {
512  sub getObject {  sub getObject {
513    my $self = shift;    my $self = shift;
514    my $oid = shift;    my $oid = shift;
515      my $options = shift;
516    
517      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
518      # unload($oid) will only unload object, not deep object hashes
519      $self->{_COREHANDLE}->unload() if ($options->{destroy});
520    
521    # TODO: review this    # TODO: review this
522    #if (!$self->{COREHANDLE}) { return; }    #if (!$self->{COREHANDLE}) { return; }
523    return $self->{COREHANDLE}->load($oid);  
524      # TODO: review this
525      my $object = eval('$self->{_COREHANDLE}->load($oid);');
526      print $@, "\n" if $@;
527    
528      return $object if $object;
529  }  }
530    
531  sub getObjectAsHash {  sub getObjectAsHash {
532    my $self = shift;    my $self = shift;
533    my $oid = shift;    my $oid = shift;
534    my $options = shift;    my $options = shift;
535    my $obj = $self->getObject($oid);    my $obj = $self->getObject($oid, $options);
536    my $deref = var_deref($obj);    
537    var2utf8($deref) if ($options->{utf8});    # build options (a callback to unload autovivified objects) for 'expand'
538    undef($obj) if($options->{destroy});    # TODO: use $logger to write to debug here!
539    return $deref;    my $cb; # = sub {};
540    =pod
541      if ($options->{destroy}) {
542        $options->{cb}->{destroy} = sub {
543          print "================ DESTROY", "\n";
544          my $object = shift;
545          #print Dumper($object);
546          $self->{_COREHANDLE}->unload($object);
547          #undef($object);
548        };
549      }
550    =cut
551    
552      my $hash = object2hash($obj, $options);
553      #$options->{cb}->{destroy}->($obj);
554      #$self->{_COREHANDLE}->unload($obj);
555      
556      # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML
557      # now done in object2hash
558      #var2utf8($hash) if ($options->{utf8});
559      
560      # old (wrong) attempts to get rid of used instances, if requested
561        #$obj->clear_refs;
562        #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
563        #$obj->DESTROY;
564        #undef($obj) if($options->{destroy});
565      
566      return $hash;
567    }
568    
569    sub getSchema {
570      return $schema_tangram;
571    }
572    
573    sub getCOREHANDLE {
574      my $self = shift;
575      return $self->{_COREHANDLE};
576  }  }
577    
578  1;  1;

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.18

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