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

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

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