/[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.17 by joko, Thu Dec 12 02:51:09 2002 UTC revision 1.30 by joko, Thu Mar 27 15:31:14 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.30  2003/03/27 15:31:14  joko
7    #  fixes to modules regarding new namespace(s) below Data::Mungle::*
8    #
9    #  Revision 1.29  2003/02/21 01:47:18  joko
10    #  purged old code
11    #  minor cosmetics
12    #
13    #  Revision 1.28  2003/02/20 20:20:26  joko
14    #  tried to get auto-disconnect working again - failed with that
15    #
16    #  Revision 1.27  2003/01/31 06:30:59  joko
17    #  + enabled 'sendQuery'
18    #
19    #  Revision 1.26  2003/01/30 22:29:47  joko
20    #  + fixed module usage (removed dependency on 'libp.pm')
21    #
22    #  Revision 1.25  2003/01/19 02:30:05  joko
23    #  + fix: modified call to '_initSchema'
24    #
25    #  Revision 1.24  2002/12/22 14:13:01  joko
26    #  + sub dropDb
27    #
28    #  Revision 1.23  2002/12/19 16:31:53  joko
29    #  +- renamed sub to 'rebuildDb'
30    #
31    #  Revision 1.22  2002/12/18 22:28:16  jonen
32    #  + added extended logging at 'getObjectByGuid()'
33    #
34    #  Revision 1.21  2002/12/16 22:20:49  jonen
35    #  + fixed bug at 'getObjectByGuid()'
36    #
37    #  Revision 1.20  2002/12/16 20:49:17  jonen
38    #  + added sub 'getObjectByGuid()'
39    #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'
40    #
41    #  Revision 1.19  2002/12/16 06:46:09  joko
42    #  + attempt to introduce a generic '_patchSchema' - cancelled!
43    #
44    #  Revision 1.18  2002/12/13 21:48:07  joko
45    #  + fix to 'sub sendQuery'
46    #
47  #  Revision 1.17  2002/12/12 02:51:09  joko  #  Revision 1.17  2002/12/12 02:51:09  joko
48  #  + cosmetics  #  + cosmetics
49  #  #
# Line 10  Line 51 
51  #  + fix: encapsulated object-loading inside an 'eval'  #  + fix: encapsulated object-loading inside an 'eval'
52  #  #
53  #  Revision 1.15  2002/12/05 13:55:21  joko  #  Revision 1.15  2002/12/05 13:55:21  joko
54  #  + now utilizing 'object2hash' instead of 'var_deref'  #  + now utilizing 'expand' instead of 'var_deref'
55  #  + played around with having fresh-objects - no progress....  #  + played around with having fresh-objects - no progress....
56  #  #
57  #  Revision 1.14  2002/12/05 09:40:30  jonen  #  Revision 1.14  2002/12/05 09:40:30  jonen
# Line 83  use warnings; Line 124  use warnings;
124  use base ("Data::Storage::Handler");  use base ("Data::Storage::Handler");
125  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
126    
127  use Tangram;  
128  use Data::Dumper;  use Data::Dumper;
129  use libp qw( getNewPerlObjectByPkgName );  use Tangram;
 use Data::Storage::Result::Tangram;  
 use Data::Compare::Struct qw( isEmpty );  
 use Data::Transform::Deep qw( object2hash );  
 use Data::Transform::Encode qw( var2utf8 );  
130    
131    use DesignPattern::Object;
132    use Data::Storage::Result::Tangram;
133    use Data::Mungle::Compare::Struct qw( isEmpty );
134    use Data::Mungle::Transform::Deep qw( expand );
135    
136  # get logger instance  # get logger instance
137  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
# Line 111  sub _initSchema { Line 152  sub _initSchema {
152    my $self = shift;    my $self = shift;
153    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
154    #if (!$schema_tangram) {    #if (!$schema_tangram) {
155      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} } );
156        my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } );
157      $schema_tangram = $obj->getSchema();      $schema_tangram = $obj->getSchema();
158    #}    #}
159    if (!$schema_tangram) {    if (!$schema_tangram) {
160      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
161      return 0;      return 0;
162    }    }
163      #$self->_patchSchema();
164    return 1;    return 1;
165  }  }
166    
167    sub _patchSchema {
168      my $self = shift;
169      foreach (keys %{$schema_tangram->{classes}}) {
170        next if $schema_tangram->{classes}->{$_}->{abstract};
171        #next if ($_ ne 'TsBankAccount');
172        #$_ ne 'AbstractAccount' &&
173        print "class: $_", "\n";
174    #print Dumper($schema_tangram->{classes}->{$_});
175        # create new string property named 'guid'
176        my $tstring = Tangram::String->new();
177        $tstring->{name} = $tstring->{col} = 'guid';
178        # inject property into schema
179        #$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring;
180        print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields});
181      }
182    }
183    
184  sub connect {  sub connect {
185    
186      my $self = shift;      my $self = shift;
187            
188      my $dsn = shift;      my $dsn = shift;
189    
190    #print Dumper($self);
191    #exit;
192    
193        # TODO: re-enable
194      $dsn ||= $self->{locator}->{dbi}->{dsn};      $dsn ||= $self->{locator}->{dbi}->{dsn};
       
195      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
196            
197      #my $storage = Tangram::Relational->connect( $schema, $dsn );      #my $storage = Tangram::Relational->connect( $schema, $dsn );
# Line 139  sub connect { Line 203  sub connect {
203  #      return;  #      return;
204  #    }  #    }
205    
206      return unless $self->_initSchema();      #return unless $self->_initSchema();
207        $self->_initSchema();
208    
209      # create the main tangram storage object      # create the main tangram storage object
210      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
# Line 266  sub configureCOREHANDLE { Line 331  sub configureCOREHANDLE {
331    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
332    
333    #my $subLayer = $self->_getSubLayerHandle();    #my $subLayer = $self->_getSubLayerHandle();
334      #print Dumper($self);
335      #exit;
336    
337    # apply configured modifications    # apply configured modifications
338      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
# Line 331  sub retreatSchema { Line 398  sub retreatSchema {
398    return $ok;    return $ok;
399  }  }
400    
401  sub rebuildDbAndSchema {  sub rebuildDb {
402    my $self = shift;    my $self = shift;
403    $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );    $logger->info( __PACKAGE__ . "->rebuildDb()" );
404    my @results;    my @results;
405    
406    # sum up results (bool (0/1)) in array    # sum up results (bool (0/1)) in array
# Line 373  sub getListFiltered { Line 440  sub getListFiltered {
440    my @results;    my @results;
441    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );
442    
443    #print Dumper($filters);  #print Dumper($filters);
444        
445    my @tfilters;    my @tfilters;
446        
# Line 412  sub getListFiltered { Line 479  sub getListFiltered {
479    # 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
480    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
481        
482      #print "eval: $evalstring", "\n";
483      
484    # get filtered list/set    # get filtered list/set
485    @results = eval($evalstring);    @results = eval($evalstring);
486    die $@ if $@;    die $@ if $@;
# Line 429  sub createCursor { Line 498  sub createCursor {
498    
499  sub createSet {  sub createSet {
500    my $self = shift;    my $self = shift;
501    #print "-" x 80, "\n";
502    #print Dumper(@_);
503    my @objects = @_;    my @objects = @_;
504    my $rh = Set::Object->new();    my $rh = Set::Object->new();
505    foreach (@objects) {    foreach (@objects) {
506      #print Dumper($_);      if (!isEmpty($_)) {
507      $rh->insert($_) if !isEmpty($_);  #print Dumper($_);
508          $rh->insert($_);
509        }
510    }    }
511    #print Dumper($rh->members());    #print Dumper($rh->members());
512    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
# Line 457  sub sendQuery { Line 530  sub sendQuery {
530      my $ident = $query->{criterias}->[0]->{val};      my $ident = $query->{criterias}->[0]->{val};
531  #print "load obj", "\n";  #print "load obj", "\n";
532      #return $self->createSet() if $ident == 5;      #return $self->createSet() if $ident == 5;
533        $self->{_COREHANDLE}->unload($ident);
534      my $object = $self->{_COREHANDLE}->load($ident);      my $object = $self->{_COREHANDLE}->load($ident);
535  #print "get id", "\n";  #print "get id", "\n";
536      my $oid = $self->{_COREHANDLE}->id($object);      my $oid = $self->{_COREHANDLE}->id($object);
537    #print Dumper($object);
538    #print "oid: $oid", "\n";
539      return $self->createSet($object);      return $self->createSet($object);
540      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
541    }    }
542    
543    die("This should not be reached for now - redirect to \$self->getListFiltered() here!");    my $list = $self->getListFiltered($query->{node}, $query->{criterias});
544      #return $self->createSet($object);
545      #return $self->createSet($list);
546      return $self->createSet(@$list);
547    
548      #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
549    
550    
   # TODO: do a common tangram query here  
551    
552    
553      # try a generic tangram query here
554      # TODO: try to place an oql on top of that (search.cpan.org!)
555    my @crits;    my @crits;
556    foreach (@{$query->{criterias}}) {    foreach (@{$query->{criterias}}) {
557      my $op = '';      my $op = '';
# Line 518  sub getObject { Line 602  sub getObject {
602    return $object if $object;    return $object if $object;
603  }  }
604    
605    sub getObjectByGuid {
606      my $self = shift;
607      my $guid = shift;
608      my $options = shift;
609      
610      # Guid and Classname is needed
611      if(!$guid || !$options->{classname}) {
612       $logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" );
613        return;
614      }
615      
616      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
617      # unload($oid) will only unload object, not deep object hashes
618      $self->{_COREHANDLE}->unload() if ($options->{destroy});
619    
620      # search for object with given Classname and Guid
621      my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname});
622      my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);
623      
624      # we searched for global unique identifer of some object,
625      # so I think we can trust there would be only one result
626      if($result[0]) {
627        return $result[0];
628      } else {
629        $logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" );
630        return;
631      }
632      
633    }
634    
635  sub getObjectAsHash {  sub getObjectAsHash {
636    my $self = shift;    my $self = shift;
637    my $oid = shift;    my $oid = shift;
638    my $options = shift;    my $options = shift;  
639    my $obj = $self->getObject($oid, $options);    my $obj;
640      
641      if($options->{guid}) {
642        $obj = $self->getObjectByGuid($oid, $options);
643      } else {
644        $obj = $self->getObject($oid, $options);
645      }
646        
647    # build options (a callback to unload autovivified objects) for 'expand'    # build options (a callback to unload autovivified objects) for 'expand'
648    # TODO: use $logger to write to debug here!    # TODO: use $logger to write to debug here!
649    my $cb; # = sub {};    my $cb; # = sub {};
650    
651      # deactivated way to get rid of used instances, if requested
652  =pod  =pod
653    if ($options->{destroy}) {      if ($options->{destroy}) {
654      $options->{cb}->{destroy} = sub {        $options->{cb}->{destroy} = sub {
655        print "================ DESTROY", "\n";          print "================ DESTROY", "\n";
656        my $object = shift;          my $object = shift;
657        #print Dumper($object);          #print Dumper($object);
658        $self->{_COREHANDLE}->unload($object);          $self->{_COREHANDLE}->unload($object);
659        #undef($object);          #undef($object);
660      };        };
661    }      }
662  =cut  =cut
663    
664    my $hash = object2hash($obj, $options);    my $hash = expand($obj, $options);
665    #$options->{cb}->{destroy}->($obj);  
666    #$self->{_COREHANDLE}->unload($obj);    # old (unsuccessful) attempts to get rid of used instances, if requested
667      
668    # convert values in hash to utf8 to be ready for (e.g.) encapsulation in XML      # V1:
669    # now done in object2hash      #$options->{cb}->{destroy}->($obj);
670    #var2utf8($hash) if ($options->{utf8});      #$self->{_COREHANDLE}->unload($obj);
671        
672    # old (wrong) attempts to get rid of used instances, if requested      # V2:
673      #$obj->clear_refs;      #$obj->clear_refs;
674      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});      #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
675      #$obj->DESTROY;      #$obj->DESTROY;
# Line 565  sub getCOREHANDLE { Line 687  sub getCOREHANDLE {
687    return $self->{_COREHANDLE};    return $self->{_COREHANDLE};
688  }  }
689    
690    sub dropDb {
691      my $self = shift;
692      my $storage = $self->_getSubLayerHandle();
693      return $storage->dropDb();
694    }
695    
696    sub testAvailability {
697      my $self = shift;
698      my $storage = $self->_getSubLayerHandle();
699      return $storage->testAvailability();
700    }
701    
702    sub disconnect2 {
703      my $self = shift;
704      my $storage = $self->_getSubLayerHandle();
705      print "DISC!", "\n";
706    
707      my $storage_ll = $storage->{_COREHANDLE};
708      $storage_ll->disconnect();
709      
710      print Dumper($storage);
711      exit;
712      
713      #$self->{_COREHANDLE}
714      #$storage->disconnect();
715      $self->{dataStorageLayer}->disconnect();
716    }
717    
718  1;  1;
719    __END__

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

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