/[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.1 by cvsjoko, Thu Oct 10 03:44:07 2002 UTC revision 1.12 by joko, Wed Dec 4 11:34:49 2002 UTC
# Line 1  Line 1 
1  #################################  ############################################
2  #  #
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.12  2002/12/04 11:34:49  joko
7    #  - $schema_tangram doesn't have to be in class?
8    #
9    #  Revision 1.11  2002/12/04 08:54:08  jonen
10    #  + untested bugfix: undef($object) after transform to hash at getObjectAsHash
11    #
12    #  Revision 1.10  2002/12/03 15:53:23  joko
13    #  + small bugfix regarding object hierarchy
14    #
15    #  Revision 1.9  2002/12/03 05:29:40  joko
16    #  + sub getObject
17    #  + sub getObjectAsHash
18    #
19    #  Revision 1.8  2002/12/01 22:25:51  joko
20    #  + now utilizing metadata from storage locator when connecting to DBI in "raw"-mode
21    #
22    #  Revision 1.7  2002/12/01 04:46:19  joko
23    #  + sub eraseAll
24    #
25    #  Revision 1.6  2002/11/29 05:02:30  joko
26    #  - sub getNewPerlObjectByPkgName (moved to libp.pm)
27    #  + sub getMetaInfo
28    #  - sub existsChildNode (moved to Abstract.pm)
29    #  + sub getListUnfiltered
30    #  + sub getListFiltered
31    #  + sub createCursor
32    #  + sub createSet
33    #  + sub sendQuery
34    #
35    #  Revision 1.5  2002/11/17 06:35:18  joko
36    #  + locator metadata can now be reached via ->{locator}
37    #  - getChildNodes is now wrapped via COREHANDLE
38    #
39    #  Revision 1.4  2002/10/25 11:44:44  joko
40    #  + sub _initSchema
41    #  + sub existsChildNode
42    #  + sub testIntegrity
43    #  + sub rebuildDbAndSchema
44    #
45    #  Revision 1.3  2002/10/17 03:56:55  joko
46    #  + bugfix: trapped eval error
47    #
48    #  Revision 1.2  2002/10/17 00:10:05  joko
49    #  + removed dependency from tsobj.pm, schema is now independent
50    #  + sub getNewPerlObjectByPkgName
51    #  + sub deploySchema
52    #  + sub retreatSchema
53    #
54  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko  #  Revision 1.1  2002/10/10 03:44:07  cvsjoko
55  #  + new  #  + new
56  #  #
57  #  ############################################
58  #################################  
59    
60  package Data::Storage::Handler::Tangram;  package Data::Storage::Handler::Tangram;
61    
# Line 18  use base ("Data::Storage::Handler::Abstr Line 66  use base ("Data::Storage::Handler::Abstr
66    
67  use Tangram;  use Tangram;
68  use Data::Dumper;  use Data::Dumper;
69    use libp qw( getNewPerlObjectByPkgName );
70    use Data::Storage::Result::Tangram;
71    use Data::Compare::Struct qw( isEmpty );
72    use Data::Transform::Deep qw( var_deref );
73    use Data::Transform::Encode qw( var2utf8 );
74    
75    
76  # get logger instance  # get logger instance
77  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
78    
79    
80  our $metainfo = {  # this holds the complete instantiated schema from tangram
81    'disconnectMethod' => 'disconnect',  my $schema_tangram;
82  };  
83    sub getMetaInfo {
84      my $self = shift;
85      $logger->debug( __PACKAGE__ . "->getMetaInfo()"  );
86      return {
87        'disconnectMethod' => 'disconnect',
88      };
89    }
90    
91    sub _initSchema {
92      my $self = shift;
93      $logger->debug( __PACKAGE__ . "->_initSchema()" );
94      #if (!$schema_tangram) {
95        my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } );
96        $schema_tangram = $obj->getSchema();
97      #}
98      if (!$schema_tangram) {
99        $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );
100        return 0;
101      }
102      return 1;
103    }
104    
105  sub connect {  sub connect {
106    
107      my $self = shift;      my $self = shift;
108            
109      my $dsn = shift;      my $dsn = shift;
110      $dsn ||= $self->{dbi}->{dsn};      $dsn ||= $self->{locator}->{dbi}->{dsn};
111            
112      $logger->debug( __PACKAGE__ . "->connect($dsn)" );      $logger->debug( __PACKAGE__ . "->connect( dsn $dsn )" );
113            
114      #my $storage = Tangram::Relational->connect( $schema, $dsn );      #my $storage = Tangram::Relational->connect( $schema, $dsn );
115      #my $storage = Tangram::mysql->connect( $schema, $dsn );      #my $storage = Tangram::mysql->connect( $schema, $dsn );
# Line 45  sub connect { Line 120  sub connect {
120  #      return;  #      return;
121  #    }  #    }
122    
123        return unless $self->_initSchema();
124    
125      require 'OSA/tsobj.pm';      # create the main tangram storage object
126      tsobj::initSchema();      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
127      #sleep 1;      $self->{COREHANDLE} = Tangram::Relational->connect( $schema_tangram, $dsn );
     my $schema = tsobj::getSchema();  
     $self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );  
128        
129        # some attempts for configuring the wrapped underlying dbi.....
130      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();      #$self->{STORAGEHANDLE_UNDERLYING} = $self->getUnderlyingStorage();
131      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();      #$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE();
132      #$self->_configureUnderlyingStorage;      #$self->_configureUnderlyingStorage;
133        
134        # ..... encapsulation wins!
135      $self->configureCOREHANDLE();      $self->configureCOREHANDLE();
136    
137      $self->{locator}->{status}->{connected} = 1;
138    
139      return 1;
140    
141  }  }
142    
143  sub getChildNodes {  sub getChildNodes {
# Line 64  sub getChildNodes { Line 145  sub getChildNodes {
145    my $self = shift;    my $self = shift;
146    my @nodes;    my @nodes;
147    
148      $logger->debug( __PACKAGE__ . "->getChildNodes()" );
149    
150    # 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
151    #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} });
152    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
153      
154      # todo: should we retrieve information from the schema here
155      # rather than poorly getting table names from underlying dbi?
156    my $storage = $self->_getSubLayerHandle();    my $storage = $self->_getSubLayerHandle();
157      @nodes = @{$storage->getChildNodes()};
158    #$storage->_configureCOREHANDLE();    #$storage->_configureCOREHANDLE();
159    #print "getchildnodes\n";
160    #print Dumper($self);
161    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
   if (my $result = $storage->sendCommand( 'SHOW TABLES;' ) ) {  
     while ( my $row = $result->_getNextEntry() ) {  
       push @nodes, $row;  
     }  
   }  
162        
163    $storage->disconnect();    # TODO: REVIEW
164      #$storage->disconnect();
165      
166      $self->{meta}->{childnodes} = \@nodes;
167        
168    return \@nodes;    return \@nodes;
169    
170  }  }
171    
172    
173    sub testIntegrity {
174    
175      my $self = shift;
176    
177      $logger->debug( __PACKAGE__ . "->testIntegrity()" );
178    
179      # 1st test: are there tables?
180      if (!$self->getChildNodes()) {
181        $logger->warning( __PACKAGE__ . "->testIntegrity no childnodes exist" );
182        return;
183      }
184    
185      # 2nd test: is there a table named "Tangram"?
186      if (!$self->existsChildNode("Tangram")) {
187        $logger->warning( __PACKAGE__ . "->testIntegrity childnode \"Tangram\" doesn't exist" );
188        return;
189      }
190    
191      $self->{locator}->{status}->{integrity} = 1;
192      return 1;
193    
194    }
195    
196    
197  sub _getSubLayerHandle {  sub _getSubLayerHandle {
198        
199    my $self = shift;    my $self = shift;
200        
201    use Data::Dumper;    $logger->debug( __PACKAGE__ . "->_getSubLayerHandle()" );
202    
203    #print Dumper($self);    #print Dumper($self);
204      
205    # hack, make more generic!    # hack, make more generic!
206    if (!$self->{STORAGE_SUBLAYER}) {    if (!$self->{dataStorageLayer}) {
207      my $loc = new Data::Storage::Locator( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} );      $logger->debug( __PACKAGE__ . "->_getSubLayerHandle() creating new dataStorageLayer" );
208      $self->{STORAGE_SUBLAYER} = new Data::Storage( $loc, { protected => 1 } );      #my $loc = Data::Storage::Locator->new( type => "DBI", dbi => $self->{dbi}, COREHANDLE => $self->{COREHANDLE}->{db} );
209        my $loc = Data::Storage::Locator->new( { type => "DBI", dbi => $self->{locator}->{dbi} } );
210        $self->{dataStorageLayer} = Data::Storage->new( $loc, { protected => 1 } );
211      #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE();      #$self->{STORAGE_UNDER_THE_HOOD}->{STORAGEHANDLE}->_configureCOREHANDLE();
212      #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE();      #$self->{STORAGE_UNDER_THE_HOOD}->_configureCOREHANDLE();
213    }    }
214        
215    #print Dumper($self->{STORAGE_UNDER_THE_HOOD});    #print Dumper($self->{STORAGE_UNDER_THE_HOOD});
216        
217    return $self->{STORAGE_SUBLAYER};    return $self->{dataStorageLayer};
218        
219  }  }
220    
221  sub _configureUnderlyingStorage {  sub _configureUnderlyingStorage {
222        
223    my $self = shift;    my $self = shift;
224    
225      $logger->debug( __PACKAGE__ . "->_configureUnderlyingStorage" );
226    
227    $self->_configureCOREHANDLE_DBI();    $self->_configureCOREHANDLE_DBI();
228    return;    return;
229        
# Line 124  sub configureCOREHANDLE { Line 240  sub configureCOREHANDLE {
240    
241    my $self = shift;    my $self = shift;
242    
243    $logger->debug( __PACKAGE__ . "->_configureCOREHANDLE" );    $logger->debug( __PACKAGE__ . "->configureCOREHANDLE" );
244    
245    #my $subLayer = $self->_getSubLayerHandle();    #my $subLayer = $self->_getSubLayerHandle();
246    
# Line 144  sub configureCOREHANDLE { Line 260  sub configureCOREHANDLE {
260    
261  }  }
262    
263    sub deploySchema {
264      my $self = shift;
265      my $args = shift;
266      
267      my $dsn = $self->{locator}->{dbi}->{dsn};
268    
269      $logger->debug( __PACKAGE__ . "->deploySchema( dsn $dsn )" );
270    
271      my $ok;
272      if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
273        return unless $self->_initSchema();
274        $ok = Tangram::Relational->deploy($schema_tangram, $dbh );
275        $dbh->disconnect();
276      }
277      return $ok;
278    }
279    
280    sub retreatSchema {
281    
282      my $self = shift;
283      my $dsn = $self->{locator}->{dbi}->{dsn};
284    
285      $logger->debug( __PACKAGE__ . "->retreatSchema( dsn $dsn )" );
286    
287      my $ok;
288      if ( my $dbh = DBI->connect($dsn, '', '', $self->{locator}->{dbi} ) ) {
289    
290        return unless $self->_initSchema();
291        
292        #use Data::Dumper; print Dumper($self);
293        $self->{dataStorageLayer}->removeLogDispatchHandler("Tangram11");
294        
295        $ok = Tangram::Relational->retreat($schema_tangram, $dbh );
296    
297        # answer "$ok=2" means "maybe" for now - we have to patch this to a constant here because...
298        # - ... Tangram::Relational->retreat doesn't seem to return a valid status
299        # - possible improvement:
300        #   - test this by checking for count of tables in database
301        #   - problem with this: there may be some left not having been included to the schema
302        #   - maybe better: use "->getChildNodes"?
303        $ok = 2;
304    
305        $dbh->disconnect();
306    
307      }
308      return $ok;
309    }
310    
311    sub rebuildDbAndSchema {
312      my $self = shift;
313      $logger->info( __PACKAGE__ . "->rebuildDbAndSchema()" );
314      my @results;
315    
316      # sum up results (bool (0/1)) in array
317      push @results, $self->retreatSchema();
318      push @results, $self->{dataStorageLayer}->dropDb();
319      push @results, $self->{dataStorageLayer}->createDb();
320      push @results, $self->deploySchema();
321    
322      # scan array for "bad ones"
323      my $res = 1;
324      map {
325        $res = 0 if (!$_);
326      } @results;
327      
328      return $res;
329    }
330    
331    sub getListUnfiltered {
332      my $self = shift;
333      my $nodename = shift;
334      my @results;
335      $logger->debug( __PACKAGE__ . "->getListUnfiltered( nodename => '" . $nodename . "' )" );
336      # get set of objects from odbms by object name
337      my $object_set = $self->{COREHANDLE}->remote($nodename);
338      @results = $self->{COREHANDLE}->select($object_set);
339      return \@results;
340    }
341    
342    sub getListFiltered {
343      my $self = shift;
344    
345      # redirect to unfiltered mode
346      #return $self->getListUnfiltered(@_);
347    
348      my $nodename = shift;
349      my $filters = shift;
350      my @results;
351      $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );
352    
353      #print Dumper($filters);
354      
355      my @tfilters;
356      
357      foreach my $filter (@$filters) {
358      
359        # get filter - TODO: for each filter
360        #my $filter = $filters->[0];
361      
362        # build filter
363        my $lexpr = $filter->{key};
364        #my $op = $filter->{op};
365        my $op = '=';
366        my $rexpr = $filter->{val};
367        my $tight = 100;
368        
369      #  my $tfilter = Tangram::Filter->new(
370      #    expr => "t1.$lexpr $op '$rexpr'",
371      #    tight => $tight,
372      #    objects => $objects,
373      #  );
374      
375        # HACK: build eval-string (sorry) to get filtered list - please give advice here
376        push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
377    
378      }
379    
380      my $tfilter = join(' & ', @tfilters);
381    
382      # get set of objects from odbms by object name
383      my $remote = $self->{COREHANDLE}->remote($nodename);
384      
385      # was:
386      #@results = $self->{COREHANDLE}->select($object_set, $tfilter);
387    
388      # is:
389      # HACK: build eval-string (sorry) to get filtered list - please give advice here
390      my $evalstring = 'return $self->{COREHANDLE}->select($remote, ' . $tfilter . ');';
391      
392      # get filtered list/set
393      @results = eval($evalstring);
394      die $@ if $@;
395      
396      return \@results;
397    }
398    
399    sub createCursor {
400      my $self = shift;
401      my $node = shift;
402      my $cmdHandle = $self->{COREHANDLE}->cursor($node);
403      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $cmdHandle );
404      return $result;
405    }
406    
407    sub createSet {
408      my $self = shift;
409      my @objects = @_;
410      my $rh = Set::Object->new();
411      foreach (@objects) {
412        #print Dumper($_);
413        $rh->insert($_) if !isEmpty($_);
414      }
415      #print Dumper($rh->members());
416      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
417      return $result;
418    }
419    
420    sub sendQuery {
421      my $self = shift;
422      my $query = shift;
423      #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";
424      #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);
425    
426      #print Dumper($query);
427    
428      # HACK: special case: querying by id does not translate into a common tangram query
429      # just load the object by given id(ent)
430      if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {
431        #print "LOAD!!!", "\n";
432        #exit;
433        #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
434        my $ident = $query->{criterias}->[0]->{val};
435    #print "load obj", "\n";
436        #return $self->createSet() if $ident == 5;
437        my $object = $self->{COREHANDLE}->load($ident);
438    #print "get id", "\n";
439        my $oid = $self->{COREHANDLE}->id($object);
440        return $self->createSet($object);
441        #return $self->createSet( $self->{COREHANDLE}->load('300090018') );
442      }
443    
444      die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
445    
446      # TODO: do a common tangram query here
447    
448      my @crits;
449      foreach (@{$query->{criterias}}) {
450        my $op = '';
451        $op = '=' if lc $_->{op} eq 'eq';
452        push @crits, "$_->{key}$op'$_->{val}'";
453      }
454      my $subnodes = {};
455      map { $subnodes->{$_}++ } @{$query->{subnodes}};
456      # HACK: this is hardcoded ;(    expand possibilities!
457      #my $crit = join(' AND ', @crits);
458      #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
459      #return $self->sendCommand($sql);
460      #my $h = $self->{COREHANDLE}->remote($query->{node});
461      #my $res = $self->{COREHANDLE}->select($h, $h->{);
462      return $self->createCursor($query->{node});
463    }
464    
465    sub eraseAll {
466      my $self = shift;
467      my $classname = shift;
468      my $remote = $self->{COREHANDLE}->remote($classname);
469      my @objs = $self->{COREHANDLE}->select($remote);
470      $self->{COREHANDLE}->erase(@objs);
471    }
472    
473    sub createDb {
474      my $self = shift;
475      my $storage = $self->_getSubLayerHandle();
476      return $storage->createDb();
477    }
478    
479    sub getObject {
480      my $self = shift;
481      my $oid = shift;
482      # TODO: review this
483      #if (!$self->{COREHANDLE}) { return; }
484      return $self->{COREHANDLE}->load($oid);
485    }
486    
487    sub getObjectAsHash {
488      my $self = shift;
489      my $oid = shift;
490      my $options = shift;
491      my $obj = $self->getObject($oid);
492      my $deref = var_deref($obj);
493      var2utf8($deref) if ($options->{utf8});
494      undef($obj) if($options->{destroy});
495      return $deref;
496    }
497    
498    sub getSchema {
499      return $schema_tangram;
500    }
501    
502  1;  1;

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.12

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