/[cvs]/nfo/perl/libs/Data/Storage/Container.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Storage/Container.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by joko, Sun Dec 1 07:08:35 2002 UTC revision 1.10 by joko, Fri Jun 6 03:26:24 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.10  2003/06/06 03:26:24  joko
7    #  + sub existsStorage
8    #
9    #  Revision 1.9  2003/03/27 15:31:08  joko
10    #  fixes to modules regarding new namespace(s) below Data::Mungle::*
11    #
12    #  Revision 1.8  2003/02/18 19:19:47  joko
13    #  + modified locator handling
14    #
15    #  Revision 1.7  2003/01/30 22:21:52  joko
16    #  + changed 'connect'-behaviour back to old state
17    #  + renamed log-output (now using 'initializing' instead of 'booting' - corresponding to 'initStorage')
18    #
19    #  Revision 1.6  2003/01/30 21:44:00  joko
20    #  + temporary fix: (FIXME) now connecting to storage on storagehandle-instantiation
21    #
22    #  Revision 1.5  2003/01/19 02:39:57  joko
23    #  + moved 'deep_copy' from module 'libp' to module 'Data.Transform.Deep'
24    #  + preserved order for hashes '$self->{config}' and '$self->{locator}' by using Tie::IxHash
25    #
26    #  Revision 1.4  2002/12/04 07:38:07  jonen
27    #  + deep copy
28    #
29    #  Revision 1.3  2002/12/01 22:18:28  joko
30    #  - no interactive implicit deploy
31    #  + only test integrity if storage available
32    #
33  #  Revision 1.2  2002/12/01 07:08:35  joko  #  Revision 1.2  2002/12/01 07:08:35  joko
34  #  + needs to "use Data::Storage;"  #  + needs to "use Data::Storage;"
35  #  #
# Line 24  use warnings; Line 51  use warnings;
51  # get logger instance  # get logger instance
52  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
53    
54  use libp qw( deep_copy );  
55    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   main
56    use Tie::IxHash;
57  use Data::Dumper;  use Data::Dumper;
58    
59  use Data::Storage;  use Data::Storage;
60  #use Data::Storage::Locator;  use Data::Storage::Locator;
61    use Data::Mungle::Transform::Deep qw( deep_copy );
62    use Data::Mungle::Compare::Struct qw( isEmpty );
63    
64  sub new {  sub new {
65    my $invocant = shift;    my $invocant = shift;
# Line 39  sub new { Line 71  sub new {
71    $logger->debug( __PACKAGE__ . "->new( @args )" );    $logger->debug( __PACKAGE__ . "->new( @args )" );
72    
73    my $self = { @_ };    my $self = { @_ };
74    return bless $self, $class;    bless $self, $class;
75      
76      # preserve order of configuration variables
77      #$self->{config} = {};
78      tie %{$self->{config}}, 'Tie::IxHash';
79      tie %{$self->{locator}}, 'Tie::IxHash';
80      
81      return $self;
82  }  }
83    
84  sub addConfig {  sub addConfig {
# Line 68  sub addStorage { Line 107  sub addStorage {
107    $self->{storage}->{$name} = $storage;    $self->{storage}->{$name} = $storage;
108  }  }
109    
110    sub existsStorage {
111      my $self = shift;
112      my $name = shift;
113      return exists $self->{storage}->{$name};
114    }
115    
116  sub initLocator {  sub initLocator {
117    my $self = shift;    my $self = shift;
118    my $name = shift;    my $name = shift;
119    my $db_config = $self->{config}->{$name};    my $db_config = $self->{config}->{$name};
120      $db_config ||= {};
121    
122    $logger->debug( __PACKAGE__ . "->initLocator( db_config $db_config )" );    $logger->debug( __PACKAGE__ . "->initLocator( name='$name' )" );
123    
124    my $cfg_locator = {};    my $cfg_locator = {};
125        
# Line 83  sub initLocator { Line 129  sub initLocator {
129      $cfg_locator->{$_} = $default->{$_};      $cfg_locator->{$_} = $default->{$_};
130    }    }
131        
   # name it  
   $cfg_locator->{name} = $name;  
     
132    # merge in specific settings    # merge in specific settings
133    foreach (keys %$db_config) {    my $specific = deep_copy($db_config);
134      $cfg_locator->{$_} = $db_config->{$_};    foreach (keys %$specific) {
135        $cfg_locator->{$_} = $specific->{$_};
136    }    }
137        
138    # HACK: transfer dsn from main to dbi settings    # HACK: transfer dsn from main to dbi settings
# Line 100  sub initLocator { Line 144  sub initLocator {
144    # HACK: set errorhandler if dbi settings are present    # HACK: set errorhandler if dbi settings are present
145    $cfg_locator->{dbi}->{HandleError} = \&_dbErrorHandler if $cfg_locator->{dbi};    $cfg_locator->{dbi}->{HandleError} = \&_dbErrorHandler if $cfg_locator->{dbi};
146    
147      # trace
148        #print "locator:", "\n";
149        #print Dumper($cfg_locator);
150    
151      # check locator metadata
152      if (isEmpty($cfg_locator)) {
153        $logger->warning( __PACKAGE__ . "->initLocator( name='$name' ): Metadata was empty. Please check configuration." );
154        return;
155      }
156    
157      # name it
158      $cfg_locator->{name} = $name;
159    
160    # create new locator object    # create new locator object
161    $self->{locator}->{$name} = Data::Storage::Locator->new( $cfg_locator );    $self->{locator}->{$name} = Data::Storage::Locator->new( $cfg_locator );
162    
# Line 108  sub initLocator { Line 165  sub initLocator {
165  sub initLocators {  sub initLocators {
166    my $self = shift;    my $self = shift;
167    foreach (keys %{$self->{config}}) {    foreach (keys %{$self->{config}}) {
168        #print $_, "\n";
169      $self->initLocator($_, $self->{config}->{$_}) if !/^_/;      $self->initLocator($_, $self->{config}->{$_}) if !/^_/;
170    }    }
171    #print "locs: ", Dumper($self->{locator});    #print "locs: ", Dumper($self->{locator});
# Line 123  sub initStorage { Line 181  sub initStorage {
181    my $self = shift;    my $self = shift;
182    my $name = shift;    my $name = shift;
183    
184      return if $self->existsStorage($name);
185    
186    my $locator = $self->getLocator($name);    my $locator = $self->getLocator($name);
187    $logger->info( __PACKAGE__ . " is booting storage declared by locator \"$name\"" );    $logger->info( __PACKAGE__ . " is initializing storage declared by locator \"$name\"" );
188    
189    my $storage = Data::Storage->new($locator);    my $storage = Data::Storage->new($locator);
190      
191      # TODO: do below (after 'testAvailability' and 'testIntegrity') again!!!
192      #$storage->connect();
193    
194    my $log_prefix = __PACKAGE__ . "->initStorage: ";    my $log_prefix = __PACKAGE__ . "->initStorage: ";
195    $log_prefix .= "dsn=\"$self->{locator}->{$name}->{dsn}\"" if $self->{locator}->{$name}->{dsn};    $log_prefix .= "dsn=\"$self->{locator}->{$name}->{dsn}\"" if $self->{locator}->{$name}->{dsn};
196    
197    # should we test availability of the storage before using it?    # should we test availability of the storage before using it?
198    if ($locator->{test_availability}) {    if ($locator->{test_availability}) {
199        $locator->{status}->{availability} = $storage->testAvailability();
200      if ( !$storage->testAvailability() ) {      if ( !$storage->testAvailability() ) {
201        $logger->error( "$log_prefix is not available" );        $logger->error( "$log_prefix: testAvailability failed" );
       return;  
202      }      }
203    }    }
204                        
205    # should we test integrity of the storage before using it?    # should we test integrity of the storage before using it?
206    if ($locator->{test_integrity}) {    if ($locator->{status}->{availability} && $locator->{test_integrity}) {
207      #return unless $storage->testIntegrity();      #return unless $storage->testIntegrity();
208      $locator->{status}->{integrity} = $storage->testIntegrity();      $locator->{status}->{integrity} = $storage->testIntegrity();
209      # actions if integrity fails      # actions if integrity fails
210      if (!$locator->{status}->{integrity}) {      if (!$locator->{status}->{integrity}) {
211        $logger->error( "testIntegrity failed on $log_prefix" );        $logger->error( "$log_prefix: testIntegrity failed" );
       print "Try a $locator->{name}.deploySchema()? (y/n) ";  
       my $res = <STDIN>;  
       if ($res =~ m/y/i) {  
         $storage->deploySchema();  
       }  
212      }      }
213    }    }
214    
# Line 159  sub initStorage { Line 217  sub initStorage {
217    # don't connect right here, do an implicit connect on (later) usage    # don't connect right here, do an implicit connect on (later) usage
218    # maybe set ->{meta}->{connectmethod} = "connect" here    # maybe set ->{meta}->{connectmethod} = "connect" here
219    #return unless $storage->connect();    #return unless $storage->connect();
220    $storage->connect();    $storage->connect() if $locator->{status}->{integrity};
221    
222    # should we check emptyness?    # should we check emptyness?
223    if ( $locator->{test_emptyness} && $locator->{status}->{integrity} ) {    if ($locator->{status}->{availability} && $locator->{test_emptyness}) {
224        #print "test empty", "\n";
225      if ( !@{$storage->getChildNodes()} ) {      if ( !@{$storage->getChildNodes()} ) {
226        $locator->{status}->{empty} = 1;        $locator->{status}->{empty} = 1;
227        $logger->warning( "$log_prefix is empty");        $logger->warning( "$log_prefix: Storage is empty.");
228        #return;        #return;
229      }      }
230    }    }
231    
232    # expand logging?    # expand logging?
233    if ( $locator->{logger} && $locator->{status}->{integrity} ) {    if ( $locator->{status}->{integrity} && $locator->{logger} ) {
234      # expand logging (to Tangram-Database)      # expand logging (to Tangram-Database)
235      # TODO:      # TODO:
236      # - move configuration data from this code to db_config somehow      # - move configuration data from this code to db_config somehow
# Line 188  sub initStorage { Line 247  sub initStorage {
247    }    }
248    
249    #$self->{storage}->{$name} = $storage;    #$self->{storage}->{$name} = $storage;
250      #print "add storage: $name", "\n";
251    $self->addStorage($name, $storage);    $self->addStorage($name, $storage);
252    
253    return 1;    return 1;
# Line 197  sub initStorage { Line 257  sub initStorage {
257  sub initStorages {  sub initStorages {
258    my $self = shift;    my $self = shift;
259    foreach (keys %{$self->{locator}}) {    foreach (keys %{$self->{locator}}) {
260        #print $_, "\n";
261      $self->initStorage($_);      $self->initStorage($_);
262    }    }
263  }  }
# Line 215  sub _dbErrorHandler { Line 276  sub _dbErrorHandler {
276  }  }
277    
278  1;  1;
279    __END__

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

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