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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.10

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