/[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.8 by joko, Sun Dec 1 22:25:51 2002 UTC revision 1.29 by joko, Fri Feb 21 01:47:18 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.29  2003/02/21 01:47:18  joko
7    #  purged old code
8    #  minor cosmetics
9    #
10    #  Revision 1.28  2003/02/20 20:20:26  joko
11    #  tried to get auto-disconnect working again - failed with that
12    #
13    #  Revision 1.27  2003/01/31 06:30:59  joko
14    #  + enabled 'sendQuery'
15    #
16    #  Revision 1.26  2003/01/30 22:29:47  joko
17    #  + fixed module usage (removed dependency on 'libp.pm')
18    #
19    #  Revision 1.25  2003/01/19 02:30:05  joko
20    #  + fix: modified call to '_initSchema'
21    #
22    #  Revision 1.24  2002/12/22 14:13:01  joko
23    #  + sub dropDb
24    #
25    #  Revision 1.23  2002/12/19 16:31:53  joko
26    #  +- renamed sub to 'rebuildDb'
27    #
28    #  Revision 1.22  2002/12/18 22:28:16  jonen
29    #  + added extended logging at 'getObjectByGuid()'
30    #
31    #  Revision 1.21  2002/12/16 22:20:49  jonen
32    #  + fixed bug at 'getObjectByGuid()'
33    #
34    #  Revision 1.20  2002/12/16 20:49:17  jonen
35    #  + added sub 'getObjectByGuid()'
36    #  + added functionality to use 'getObjectByGuid' at 'getObjectAsHash()'
37    #
38    #  Revision 1.19  2002/12/16 06:46:09  joko
39    #  + attempt to introduce a generic '_patchSchema' - cancelled!
40    #
41    #  Revision 1.18  2002/12/13 21:48:07  joko
42    #  + fix to 'sub sendQuery'
43    #
44    #  Revision 1.17  2002/12/12 02:51:09  joko
45    #  + cosmetics
46    #
47    #  Revision 1.16  2002/12/11 06:54:10  joko
48    #  + fix: encapsulated object-loading inside an 'eval'
49    #
50    #  Revision 1.15  2002/12/05 13:55:21  joko
51    #  + now utilizing 'expand' instead of 'var_deref'
52    #  + played around with having fresh-objects - no progress....
53    #
54    #  Revision 1.14  2002/12/05 09:40:30  jonen
55    #  + added option->{destroy} at getObject for unloading all instance
56    #
57    #  Revision 1.13  2002/12/05 07:59:04  joko
58    #  + now using Tie::SecureHash as a base for the COREHANDLE
59    #  + former public COREHANDLE becomes private _COREHANDLE now
60    #  + sub getCOREHANDLE
61    #
62    #  Revision 1.12  2002/12/04 11:34:49  joko
63    #  - $schema_tangram doesn't have to be in class?
64    #
65    #  Revision 1.11  2002/12/04 08:54:08  jonen
66    #  + untested bugfix: undef($object) after transform to hash at getObjectAsHash
67    #
68    #  Revision 1.10  2002/12/03 15:53:23  joko
69    #  + small bugfix regarding object hierarchy
70    #
71    #  Revision 1.9  2002/12/03 05:29:40  joko
72    #  + sub getObject
73    #  + sub getObjectAsHash
74    #
75  #  Revision 1.8  2002/12/01 22:25:51  joko  #  Revision 1.8  2002/12/01 22:25:51  joko
76  #  + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode  #  + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode
77  #  #
# Line 49  package Data::Storage::Handler::Tangram; Line 118  package Data::Storage::Handler::Tangram;
118  use strict;  use strict;
119  use warnings;  use warnings;
120    
121    use base ("Data::Storage::Handler");
122  use base ("Data::Storage::Handler::Abstract");  use base ("Data::Storage::Handler::Abstract");
123    
124  use Tangram;  
125  use Data::Dumper;  use Data::Dumper;
126  use libp qw( getNewPerlObjectByPkgName );  use Tangram;
127    
128    use DesignPattern::Object;
129  use Data::Storage::Result::Tangram;  use Data::Storage::Result::Tangram;
130  use Data::Compare::Struct qw( isEmpty );  use Data::Compare::Struct qw( isEmpty );
131    use Data::Transform::Deep qw( expand );
132    
133  # get logger instance  # get logger instance
134  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
135    
136    
137    # this holds the complete instantiated schema from tangram
138    my $schema_tangram;
139    
140  sub getMetaInfo {  sub getMetaInfo {
141    my $self = shift;    my $self = shift;
142    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );    $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
# Line 72  sub getMetaInfo { Line 148  sub getMetaInfo {
148  sub _initSchema {  sub _initSchema {
149    my $self = shift;    my $self = shift;
150    $logger->debug( __PACKAGE__ . "->_initSchema()" );    $logger->debug( __PACKAGE__ . "->_initSchema()" );
151    #if (!$self->{schema_tangram}) {    #if (!$schema_tangram) {
152      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} } );
153      $self->{schema_tangram} = $obj->getSchema();      my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } );
154        $schema_tangram = $obj->getSchema();
155    #}    #}
156    if (!$self->{schema_tangram}) {    if (!$schema_tangram) {
157      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
158      return 0;      return 0;
159    }    }
160      #$self->_patchSchema();
161    return 1;    return 1;
162  }  }
163    
164    sub _patchSchema {
165      my $self = shift;
166      foreach (keys %{$schema_tangram->{classes}}) {
167        next if $schema_tangram->{classes}->{$_}->{abstract};
168        #next if ($_ ne 'TsBankAccount');
169        #$_ ne 'AbstractAccount' &&
170        print "class: $_", "\n";
171    #print Dumper($schema_tangram->{classes}->{$_});
172        # create new string property named 'guid'
173        my $tstring = Tangram::String->new();
174        $tstring->{name} = $tstring->{col} = 'guid';
175        # inject property into schema
176        #$schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields}->{string}->{$tstring->{name}} = $tstring;
177        print Dumper($schema_tangram->{classes}->{$_}->{root}->{SPECS}->[0]->{fields});
178      }
179    }
180    
181  sub connect {  sub connect {
182    
183      my $self = shift;      my $self = shift;
184            
185      my $dsn = shift;      my $dsn = shift;
186    
187    #print Dumper($self);
188    #exit;
189    
190        # TODO: re-enable
191      $dsn ||= $self->{locator}->{dbi}->{dsn};      $dsn ||= $self->{locator}->{dbi}->{dsn};
       
192      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
193            
194      #my $storage = Tangram::Relational->connect( $schema, $dsn );      #my $storage = Tangram::Relational->connect( $schema, $dsn );
# Line 101  sub connect { Line 200  sub connect {
200  #      return;  #      return;
201  #    }  #    }
202    
203      return unless $self->_initSchema();      #return unless $self->_initSchema();
204        $self->_initSchema();
205    
206      # create the main tangram storage object      # create the main tangram storage object
207      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
208      $self->{COREHANDLE} = Tangram::Relational->connect( $self->{schema_tangram}, $dsn );      $self->{_COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
209    
210    #print "connect", "\n";
211    #my $core = $self->{_COREHANDLE};
212    #print Dumper($core);
213        
214      # some attempts for configuring the wrapped underlying dbi.....      # some attempts for configuring the wrapped underlying dbi.....
215      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
# Line 211  sub _configureUnderlyingStorage { Line 315  sub _configureUnderlyingStorage {
315    foreach my $key (keys %{$self->{dbi}}) {    foreach my $key (keys %{$self->{dbi}}) {
316      my $val = $self->{dbi}->{$key};      my $val = $self->{dbi}->{$key};
317      print "entry: $key; $val", "\n";      print "entry: $key; $val", "\n";
318      $self->{COREHANDLE}->{db}->{$key} = $val;      $self->{_COREHANDLE}->{db}->{$key} = $val;
319    }    }
320    #print Dumper($self->{COREHANDLE}->{db});    #print Dumper($self->{COREHANDLE}->{db});
321  }  }
# Line 224  sub configureCOREHANDLE { Line 328  sub configureCOREHANDLE {
328    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
329    
330    #my $subLayer = $self->_getSubLayerHandle();    #my $subLayer = $self->_getSubLayerHandle();
331      #print Dumper($self);
332      #exit;
333    
334    # apply configured modifications    # apply configured modifications
335      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {      if (exists $self->{dbi}->{trace_level} && exists $self->{dbi}->{trace_file}) {
336        $self->{COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});        $self->{_COREHANDLE}->{db}->trace($self->{dbi}->{trace_level}, $self->{dbi}->{trace_file});
337      }      }
338      if (exists $self->{dbi}->{RaiseError}) {      if (exists $self->{dbi}->{RaiseError}) {
339        $self->{COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};        $self->{_COREHANDLE}->{db}->{RaiseError} = $self->{dbi}->{RaiseError};
340      }      }
341      if (exists $self->{dbi}->{PrintError}) {      if (exists $self->{dbi}->{PrintError}) {
342        $self->{COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};        $self->{_COREHANDLE}->{db}->{PrintError} = $self->{dbi}->{PrintError};
343      }      }
344      if (exists $self->{dbi}->{HandleError}) {      if (exists $self->{dbi}->{HandleError}) {
345        $self->{COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};        $self->{_COREHANDLE}->{db}->{HandleError} = $self->{dbi}->{HandleError};
346      }      }
347    
348  }  }
# Line 252  sub deploySchema { Line 358  sub deploySchema {
358    my $ok;    my $ok;
359    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {    if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
360      return unless $self->_initSchema();      return unless $self->_initSchema();
361      $ok = Tangram::Relational->deploy($self->{schema_tangram}, $dbh );      $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
362      $dbh->disconnect();      $dbh->disconnect();
363    }    }
364    return $ok;    return $ok;
# Line 273  sub retreatSchema { Line 379  sub retreatSchema {
379      #use Data::Dumper; print Dumper($self);      #use Data::Dumper; print Dumper($self);
380      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");      $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
381            
382      $ok = Tangram::Relational->retreat($self->{schema_tangram}, $dbh );      $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
383    
384      # 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...
385      # - ... Tangram::Relational->retreat doesn't seem to return a valid status      # - ... Tangram::Relational->retreat doesn't seem to return a valid status
# Line 289  sub retreatSchema { Line 395  sub retreatSchema {
395    return $ok;    return $ok;
396  }  }
397    
398  sub rebuildDbAndSchema {  sub rebuildDb {
399    my $self = shift;    my $self = shift;
400    $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );    $logger->info( __PACKAGE__ . "->rebuildDb()" );
401    my @results;    my @results;
402    
403    # sum up results (bool (0/1)) in array    # sum up results (bool (0/1)) in array
# Line 315  sub getListUnfiltered { Line 421  sub getListUnfiltered {
421    my @results;    my @results;
422    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
423    # get set of objects from odbms by object name    # get set of objects from odbms by object name
424    my $object_set = $self->{COREHANDLE}->remote($nodename);    my $object_set = $self->{_COREHANDLE}->remote($nodename);
425    @results = $self->{COREHANDLE}->select($object_set);    @results = $self->{_COREHANDLE}->select($object_set);
426    return \@results;    return \@results;
427  }  }
428    
# Line 331  sub getListFiltered { Line 437  sub getListFiltered {
437    my @results;    my @results;
438    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );
439    
440    #print Dumper($filters);  #print Dumper($filters);
441        
442    my @tfilters;    my @tfilters;
443        
# Line 361  sub getListFiltered { Line 467  sub getListFiltered {
467    my $tfilter = join(' & ', @tfilters);    my $tfilter = join(' & ', @tfilters);
468    
469    # get set of objects from odbms by object name    # get set of objects from odbms by object name
470    my $remote = $self->{COREHANDLE}->remote($nodename);    my $remote = $self->{_COREHANDLE}->remote($nodename);
471        
472    # was:    # was:
473    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
474    
475    # is:    # is:
476    # 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
477    my $evalstring = 'return $self->{COREHANDLE}->select($remote, ' . $tfilter . ');';    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';
478      
479      #print "eval: $evalstring", "\n";
480        
481    # get filtered list/set    # get filtered list/set
482    @results = eval($evalstring);    @results = eval($evalstring);
# Line 380  sub getListFiltered { Line 488  sub getListFiltered {
488  sub createCursor {  sub createCursor {
489    my $self = shift;    my $self = shift;
490    my $node = shift;    my $node = shift;
491    my $cmdHandle = $self->{COREHANDLE}->cursor($node);    my $cmdHandle = $self->{_COREHANDLE}->cursor($node);
492    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
493    return $result;    return $result;
494  }  }
495    
496  sub createSet {  sub createSet {
497    my $self = shift;    my $self = shift;
498    #print "-" x 80, "\n";
499    #print Dumper(@_);
500    my @objects = @_;    my @objects = @_;
501    my $rh = Set::Object->new();    my $rh = Set::Object->new();
502    foreach (@objects) {    foreach (@objects) {
503      #print Dumper($_);      if (!isEmpty($_)) {
504      $rh->insert($_) if !isEmpty($_);  #print Dumper($_);
505          $rh->insert($_);
506        }
507    }    }
508    #print Dumper($rh->members());    #print Dumper($rh->members());
509    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );    my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
# Line 415  sub sendQuery { Line 527  sub sendQuery {
527      my $ident = $query->{criterias}->[0]->{val};      my $ident = $query->{criterias}->[0]->{val};
528  #print "load obj", "\n";  #print "load obj", "\n";
529      #return $self->createSet() if $ident == 5;      #return $self->createSet() if $ident == 5;
530      my $object = $self->{COREHANDLE}->load($ident);      $self->{_COREHANDLE}->unload($ident);
531        my $object = $self->{_COREHANDLE}->load($ident);
532  #print "get id", "\n";  #print "get id", "\n";
533      my $oid = $self->{COREHANDLE}->id($object);      my $oid = $self->{_COREHANDLE}->id($object);
534    #print Dumper($object);
535    #print "oid: $oid", "\n";
536      return $self->createSet($object);      return $self->createSet($object);
537      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
538    }    }
539    
540    die("This should not be reached for now - redirect to \$self->getListFiltered() here!");    my $list = $self->getListFiltered($query->{node}, $query->{criterias});
541      #return $self->createSet($object);
542      #return $self->createSet($list);
543      return $self->createSet(@$list);
544    
545      #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
546    
   # TODO: do a common tangram query here  
547    
548    
549    
550      # try a generic tangram query here
551      # TODO: try to place an oql on top of that (search.cpan.org!)
552    my @crits;    my @crits;
553    foreach (@{$query->{criterias}}) {    foreach (@{$query->{criterias}}) {
554      my $op = '';      my $op = '';
# Line 446  sub sendQuery { Line 569  sub sendQuery {
569  sub eraseAll {  sub eraseAll {
570    my $self = shift;    my $self = shift;
571    my $classname = shift;    my $classname = shift;
572    my $remote = $self->{storage}->remote($classname);    my $remote = $self->{_COREHANDLE}->remote($classname);
573    my @objs = $self->{storage}->select($remote);    my @objs = $self->{_COREHANDLE}->select($remote);
574    $self->{COREHANDLE}->erase(@objs);    $self->{_COREHANDLE}->erase(@objs);
575  }  }
576    
577  sub createDb {  sub createDb {
# Line 457  sub createDb { Line 580  sub createDb {
580    return $storage->createDb();    return $storage->createDb();
581  }  }
582    
583    sub getObject {
584      my $self = shift;
585      my $oid = shift;
586      my $options = shift;
587    
588      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
589      # unload($oid) will only unload object, not deep object hashes
590      $self->{_COREHANDLE}->unload() if ($options->{destroy});
591    
592      # TODO: review this
593      #if (!$self->{COREHANDLE}) { return; }
594    
595      # TODO: review this
596      my $object = eval('$self->{_COREHANDLE}->load($oid);');
597      print $@, "\n" if $@;
598    
599      return $object if $object;
600    }
601    
602    sub getObjectByGuid {
603      my $self = shift;
604      my $guid = shift;
605      my $options = shift;
606      
607      # Guid and Classname is needed
608      if(!$guid || !$options->{classname}) {
609       $logger->error( __PACKAGE__ . "->getObjectByGuid: No 'guid' OR no Classname in options hash was given but needed!" );
610        return;
611      }
612      
613      # TODO: create a deep_unload method (currently _all_ objects are unloaded)
614      # unload($oid) will only unload object, not deep object hashes
615      $self->{_COREHANDLE}->unload() if ($options->{destroy});
616    
617      # search for object with given Classname and Guid
618      my $obj_tmp = $self->{_COREHANDLE}->remote($options->{classname});
619      my @result = $self->{_COREHANDLE}->select($obj_tmp, $obj_tmp->{guid} eq $guid);
620      
621      # we searched for global unique identifer of some object,
622      # so I think we can trust there would be only one result
623      if($result[0]) {
624        return $result[0];
625      } else {
626        $logger->error( __PACKAGE__ . "->getObjectByGuid: No Object with Classname $options->{classname} and GUID $guid found!" );
627        return;
628      }
629      
630    }
631    
632    sub getObjectAsHash {
633      my $self = shift;
634      my $oid = shift;
635      my $options = shift;  
636      my $obj;
637      
638      if($options->{guid}) {
639        $obj = $self->getObjectByGuid($oid, $options);
640      } else {
641        $obj = $self->getObject($oid, $options);
642      }
643      
644      # build options (a callback to unload autovivified objects) for 'expand'
645      # TODO: use $logger to write to debug here!
646      my $cb; # = sub {};
647    
648      # deactivated way to get rid of used instances, if requested
649    =pod
650        if ($options->{destroy}) {
651          $options->{cb}->{destroy} = sub {
652            print "================ DESTROY", "\n";
653            my $object = shift;
654            #print Dumper($object);
655            $self->{_COREHANDLE}->unload($object);
656            #undef($object);
657          };
658        }
659    =cut
660    
661      my $hash = expand($obj, $options);
662    
663      # old (unsuccessful) attempts to get rid of used instances, if requested
664    
665        # V1:
666        #$options->{cb}->{destroy}->($obj);
667        #$self->{_COREHANDLE}->unload($obj);
668      
669        # V2:
670        #$obj->clear_refs;
671        #$self->{COREHANDLE}->unload($obj) if($options->{destroy});
672        #$obj->DESTROY;
673        #undef($obj) if($options->{destroy});
674      
675      return $hash;
676    }
677    
678    sub getSchema {
679      return $schema_tangram;
680    }
681    
682    sub getCOREHANDLE {
683      my $self = shift;
684      return $self->{_COREHANDLE};
685    }
686    
687    sub dropDb {
688      my $self = shift;
689      my $storage = $self->_getSubLayerHandle();
690      return $storage->dropDb();
691    }
692    
693    sub testAvailability {
694      my $self = shift;
695      my $storage = $self->_getSubLayerHandle();
696      return $storage->testAvailability();
697    }
698    
699    sub disconnect2 {
700      my $self = shift;
701      my $storage = $self->_getSubLayerHandle();
702      print "DISC!", "\n";
703    
704      my $storage_ll = $storage->{_COREHANDLE};
705      $storage_ll->disconnect();
706      
707      print Dumper($storage);
708      exit;
709      
710      #$self->{_COREHANDLE}
711      #$storage->disconnect();
712      $self->{dataStorageLayer}->disconnect();
713    }
714    
715  1;  1;
716    __END__

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.29

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