/[cvs]/nfo/perl/libs/Data/Transfer/Sync.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Transfer/Sync.pm

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

revision 1.6 by jonen, Fri Dec 6 04:49:10 2002 UTC revision 1.9 by jonen, Mon Dec 16 07:02:34 2002 UTC
# Line 6  Line 6 
6  ##  ##
7  ##    ----------------------------------------------------------------------------------------  ##    ----------------------------------------------------------------------------------------
8  ##    $Log$  ##    $Log$
9    ##    Revision 1.9  2002/12/16 07:02:34  jonen
10    ##    + added comment
11    ##
12    ##    Revision 1.8  2002/12/15 02:03:09  joko
13    ##    + fixed logging-messages
14    ##    + additional metadata-checks
15    ##
16    ##    Revision 1.7  2002/12/13 21:49:34  joko
17    ##    + sub configure
18    ##    + sub checkOptions
19    ##
20  ##    Revision 1.6  2002/12/06 04:49:10  jonen  ##    Revision 1.6  2002/12/06 04:49:10  jonen
21  ##    + disabled output-puffer here  ##    + disabled output-puffer here
22  ##  ##
# Line 42  use strict; Line 53  use strict;
53  use warnings;  use warnings;
54    
55  use Data::Dumper;  use Data::Dumper;
56    #use Hash::Merge qw( merge );
57    
58  use misc::HashExt;  use misc::HashExt;
59  use libp qw( md5_base64 );  use libp qw( md5_base64 );
60  use libdb qw( quotesql hash2Sql );  use libdb qw( quotesql hash2Sql );
# Line 56  $| = 1; Line 69  $| = 1;
69  sub new {  sub new {
70    my $invocant = shift;    my $invocant = shift;
71    my $class = ref($invocant) || $invocant;    my $class = ref($invocant) || $invocant;
72    my $self = { @_ };    my $self = {};
73    $logger->debug( __PACKAGE__ . "->new(@_)" );    $logger->debug( __PACKAGE__ . "->new(@_)" );
74    bless $self, $class;    bless $self, $class;
75    $self->_init();    $self->configure(@_);
76    return $self;    return $self;
77  }  }
78    
79    
80    sub configure {
81      my $self = shift;
82      my @args = @_;
83      if (!isEmpty(\@args)) {
84        my %properties = @_;
85        # merge args to properties
86        map { $self->{$_} = $properties{$_}; } keys %properties;
87        $self->_init();
88      } else {
89        #print "no args!", "\n";
90      }
91      #print Dumper($self);
92    }
93    
94  sub _init {  sub _init {
95    my $self = shift;    my $self = shift;
96    
97      $self->{configured} = 1;
98        
99    # build new container if necessary    # build new container if necessary
100    $self->{container} = Data::Storage::Container->new() if !$self->{container};    $self->{container} = Data::Storage::Container->new() if !$self->{container};
# Line 84  sub _init { Line 113  sub _init {
113  }  }
114    
115    
116    sub prepareOptions {
117    
118      my $self = shift;
119      my $opts = shift;
120    
121    #print Dumper($opts);
122    
123      $opts->{mode} ||= '';
124      $opts->{erase} ||= 0;
125      #$opts->{import} ||= 0;
126      
127      $logger->info( __PACKAGE__ . "->prepareOptions( source_node $opts->{source_node} mode $opts->{mode} erase $opts->{erase} prepare $opts->{prepare} )");
128    
129      if (!$opts->{mapping} || !$opts->{mapping_module}) {
130        $logger->warning( __PACKAGE__ . "->prepareOptions: No mapping supplied - please check key 'mappings' in BizWorks/Config.pm");
131      }
132    
133      my $evstring = "use $opts->{mapping_module};";
134      eval($evstring);
135      if ($@) {
136        $logger->warning( __PACKAGE__ . "->prepareOptions: error while trying to access mapping - $@");
137        return;
138      }
139    
140      # resolve mapping metadata (returned from sub)
141      my $mapObject = $opts->{mapping_module}->new();
142      #print Dumper($map);
143      my $source_node_name = $opts->{source_node};
144      # check if mapping for certain node is contained in mapping object
145      if (!$mapObject->can($source_node_name)) {
146        $logger->warning( __PACKAGE__ . "->prepareOptions: Can't access mapping for node \"$source_node_name\" - please check $opts->{mapping_module}.");
147        return;
148      }
149      my $map = $mapObject->$source_node_name;
150    
151      # remove asymmetries from $map (patch keys)
152      $map->{source_node} = $map->{source}; delete $map->{source};
153      $map->{target_node} = $map->{target}; delete $map->{target};
154      $map->{mapping} = $map->{details}; delete $map->{details};
155      $map->{direction} = $map->{mode}; delete $map->{mode};
156    
157      # defaults (mostly for backward-compatibility)
158      $map->{source_node} ||= $source_node_name;
159      $map->{source_ident} ||= 'storage_method:id';
160      $map->{target_ident} ||= 'property:oid';
161      $map->{direction} ||= $opts->{mode};         # | PUSH | PULL | FULL
162      $map->{method} ||= 'checksum';                # | timestamp
163      $map->{source_exclude} ||= [qw( cs )];
164    
165      # merge map to opts
166      map { $opts->{$_} = $map->{$_}; } keys %$map;
167        
168    #print Dumper($opts);
169    
170      # TODO: move this to checkOptions...
171      
172      # check - do we have a target?
173      if (!$opts->{target_node}) {
174        $logger->warning( __PACKAGE__ . "->prepareOptions: No target given - please check metadata declaration.");
175        return;
176      }
177    
178    
179      #return $opts;
180      return 1;
181    
182    }
183    
184    
185    sub checkOptions {
186      my $self = shift;
187      my $opts = shift;
188      
189      my $result = 1;
190      
191      # check - do we have a target node?
192      if (!$opts->{target_node}) {
193        $logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'target node' could be determined.");
194        $result = 0;
195      }
196    
197      # check - do we have a mapping?
198      if (!$opts->{mapping} && !$opts->{mapping_module}) {
199        $logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'mapping' could be determined.");
200        $result = 0;
201      }
202      
203      return $result;
204      
205    }
206    
207    
208  # TODO: some feature to show off the progress of synchronization (cur/max * 100)  # TODO: some feature to show off the progress of synchronization (cur/max * 100)
209  sub syncNodes {  sub syncNodes {
210    
211    my $self = shift;    my $self = shift;
212    my $args = shift;    my $args = shift;
213    
214      if (!$self->{configured}) {
215        $logger->critical( __PACKAGE__ . "->syncNodes: Synchronization object is not configured/initialized correctly." );
216        return;
217      }
218    
219    # remember arguments through the whole processing    # remember arguments through the whole processing
220    $self->{args} = $args;    $self->{args} = $args;
221    
# Line 112  sub syncNodes { Line 238  sub syncNodes {
238    }    }
239    
240    # decompose identifiers for each partner    # decompose identifiers for each partner
241    # TODO: take this list from already established/given metadata    # TODO: refactor!!! take this list from already established/given metadata
242    foreach ('source', 'target') {    foreach ('source', 'target') {
243            
244      # get/set metadata for further processing      # get/set metadata for further processing
# Line 167  sub syncNodes { Line 293  sub syncNodes {
293      #print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n";      #print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n";
294    }    }
295    
296    #print Dumper($self->{meta});
297    
298    $logger->info( __PACKAGE__ . "->syncNodes: source=$self->{meta}->{source}->{dbkey}/$self->{meta}->{source}->{node} $direction_arrow target=$self->{meta}->{target}->{dbkey}/$self->{meta}->{target}->{node}" );    $logger->info( __PACKAGE__ . "->syncNodes: source=$self->{meta}->{source}->{dbkey}/$self->{meta}->{source}->{node} $direction_arrow target=$self->{meta}->{target}->{dbkey}/$self->{meta}->{target}->{node}" );
299    
300    # build mapping    # build mapping
# Line 194  sub syncNodes { Line 322  sub syncNodes {
322    
323    }    }
324    
325    #print Dumper($self->{meta});
326      
327    # check partners/nodes: does partner exist / is node available?    # check partners/nodes: does partner exist / is node available?
328    foreach my $partner (keys %{$self->{meta}}) {    foreach my $partner (keys %{$self->{meta}}) {
329      next if $self->{meta}->{$partner}->{storage}->{locator}->{type} eq 'DBI';    # for DBD::CSV - re-enable for others      
330        # 1. check partners & storages
331        if (!$self->{meta}->{$partner}) {
332          $logger->critical( __PACKAGE__ . "->syncNodes: Could not find partner '$partner' in configuration metadata." );
333          return;
334        }
335    
336        my $dbkey = $self->{meta}->{$partner}->{dbkey};
337    
338        if (!$self->{meta}->{$partner}->{storage}) {
339          $logger->critical( __PACKAGE__ . "->syncNodes: Could not access storage of partner '$partner' (named '$dbkey'), looks like a configuration-error." );
340          return;
341        }
342        
343        # TODO:
344        # 2. check if partners (and nodes?) are actually available....
345        # eventually pre-check mode of access-attempt (read/write) here to provide an "early-croak" if possible
346        
347        # 3. check nodes
348        next if $self->{meta}->{$partner}->{storage}->{locator}->{type} eq 'DBI';    # HACK for DBD::CSV - re-enable for others
349        # get node-name
350      my $node = $self->{meta}->{$partner}->{node};      my $node = $self->{meta}->{$partner}->{node};
351      if (!$self->{meta}->{$partner}->{storage}->existsChildNode($node)) {      if (!$self->{meta}->{$partner}->{storage}->existsChildNode($node)) {
352        $logger->critical( __PACKAGE__ . "->syncNodes: Could not reach \"$node\" at \"$partner\"." );        $logger->critical( __PACKAGE__ . "->syncNodes: Could not reach node \"$node\" at partner \"$partner\"." );
353        return;        return;
354      }      }
355        
356    }    }
357    
358    # TODO:    # TODO:
# Line 497  sub _syncNodes { Line 648  sub _syncNodes {
648  }  }
649    
650    
651    # refactor this as some core-function to do a generic dump resolving data-encapsulations of e.g. Set::Object
652  sub _dumpCompact {  sub _dumpCompact {
653    my $self = shift;    my $self = shift;
654    
# Line 781  sub _modifyNode { Line 933  sub _modifyNode {
933      #print $action, "\n";      #print $action, "\n";
934  #$action = "anc";  #$action = "anc";
935  #print "yai", "\n";  #print "yai", "\n";
936    
937    #print Dumper($map);
938    #delete $map->{cs};
939    
940      if (lc($action) eq 'insert') {      if (lc($action) eq 'insert') {
941        $sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_INSERT');        $sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_INSERT');
942      } elsif (lc $action eq 'update') {      } elsif (lc $action eq 'update') {
# Line 788  sub _modifyNode { Line 944  sub _modifyNode {
944        $sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_UPDATE', $crit);        $sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_UPDATE', $crit);
945      }      }
946    
947      #print "sql: ", $sql_main, "\n";  #$sql_main = "UPDATE currencies_csv SET oid='abcdef' WHERE text='Australian Dollar' AND key='AUD';";
948      #exit;  #$sql_main = "UPDATE currencies_csv SET oid='huhu2' WHERE ekey='AUD'";
949    
950    #print "sql: ", $sql_main, "\n";
951    #exit;
952    
953      # transfer data      # transfer data
954      my $sqlHandle = $self->{meta}->{$descent}->{storage}->sendCommand($sql_main);      my $sqlHandle = $self->{meta}->{$descent}->{storage}->sendCommand($sql_main);
955    
956    #exit;
957    
958      # handle errors      # handle errors
959      if ($sqlHandle->err) {      if ($sqlHandle->err) {
960        #if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; }        #if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; }
# Line 982  sub _statloadNode { Line 1143  sub _statloadNode {
1143        return;        return;
1144      }      }
1145    
1146      my $result = $self->{meta}->{$descent}->{storage}->sendQuery({  #print "yai!", "\n";
1147    
1148        my $query = {
1149        node => $self->{meta}->{$descent}->{node},        node => $self->{meta}->{$descent}->{node},
1150        subnodes => [qw( cs )],        subnodes => [qw( cs )],
1151        criterias => [        criterias => [
# Line 990  sub _statloadNode { Line 1153  sub _statloadNode {
1153             op => 'eq',             op => 'eq',
1154             val => $ident },             val => $ident },
1155        ]        ]
1156      });      };
1157    
1158    #print Dumper($query);
1159    
1160        my $result = $self->{meta}->{$descent}->{storage}->sendQuery($query);
1161    
1162      my $entry = $result->getNextEntry();      my $entry = $result->getNextEntry();
1163    
1164    #print Dumper($entry);
1165    #print "pers: " . $self->{meta}->{$descent}->{storage}->is_persistent($entry), "\n";
1166    #my $state = $self->{meta}->{$descent}->{storage}->_fetch_object_state($entry, { name => 'TransactionHop' } );
1167    #print Dumper($state);
1168    
1169      my $status = $result->getStatus();      my $status = $result->getStatus();
1170    
1171  #print Dumper($status);  #print Dumper($status);
# Line 1007  sub _statloadNode { Line 1180  sub _statloadNode {
1180            
1181        # 1st level - hard error        # 1st level - hard error
1182        if ($status && $status->{err}) {        if ($status && $status->{err}) {
1183          $logger->debug( __PACKAGE__ . "->_statloadNode (ident=\"$ident\") failed - hard error (that's ok)" );          $logger->debug( __PACKAGE__ . "->_statloadNode (ident=\"$ident\") failed - hard error (that's ok): $status->{err}" );
1184          return;          return;
1185        }        }
1186        

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.9

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