6 |
## |
## |
7 |
## ---------------------------------------------------------------------------------------- |
## ---------------------------------------------------------------------------------------- |
8 |
## $Log$ |
## $Log$ |
9 |
|
## Revision 1.11 2002/12/23 07:10:59 joko |
10 |
|
## + using MD5 for checksum generation again - the 32-bit integer hash from DBI seems to be too lazy |
11 |
|
## |
12 |
|
## Revision 1.10 2002/12/19 01:07:16 joko |
13 |
|
## + fixed output done via $logger |
14 |
|
## |
15 |
|
## Revision 1.9 2002/12/16 07:02:34 jonen |
16 |
|
## + added comment |
17 |
|
## |
18 |
|
## Revision 1.8 2002/12/15 02:03:09 joko |
19 |
|
## + fixed logging-messages |
20 |
|
## + additional metadata-checks |
21 |
|
## |
22 |
|
## Revision 1.7 2002/12/13 21:49:34 joko |
23 |
|
## + sub configure |
24 |
|
## + sub checkOptions |
25 |
|
## |
26 |
|
## Revision 1.6 2002/12/06 04:49:10 jonen |
27 |
|
## + disabled output-puffer here |
28 |
|
## |
29 |
|
## Revision 1.5 2002/12/05 08:06:05 joko |
30 |
|
## + bugfix with determining empty fields (Null) with DBD::CSV |
31 |
|
## + debugging |
32 |
|
## + updated comments |
33 |
|
## |
34 |
|
## Revision 1.4 2002/12/03 15:54:07 joko |
35 |
|
## + {import}-flag is now {prepare}-flag |
36 |
|
## |
37 |
|
## Revision 1.3 2002/12/01 22:26:59 joko |
38 |
|
## + minor cosmetics for logging |
39 |
|
## |
40 |
|
## Revision 1.2 2002/12/01 04:43:25 joko |
41 |
|
## + mapping deatil entries may now be either an ARRAY or a HASH |
42 |
|
## + erase flag is used now (for export-operations) |
43 |
|
## + expressions to refer to values inside deep nested structures |
44 |
|
## - removed old mappingV2-code |
45 |
|
## + cosmetics |
46 |
|
## + sub _erase_all |
47 |
|
## |
48 |
## Revision 1.1 2002/11/29 04:45:50 joko |
## Revision 1.1 2002/11/29 04:45:50 joko |
49 |
## + initial check in |
## + initial check in |
50 |
## |
## |
59 |
use warnings; |
use warnings; |
60 |
|
|
61 |
use Data::Dumper; |
use Data::Dumper; |
62 |
|
#use Hash::Merge qw( merge ); |
63 |
|
|
64 |
use misc::HashExt; |
use misc::HashExt; |
65 |
use libp qw( md5_base64 ); |
use libp qw( md5_base64 ); |
66 |
use libdb qw( quotesql hash2Sql ); |
use libdb qw( quotesql hash2Sql ); |
67 |
use Data::Transform::OO qw( hash2object ); |
use Data::Transform::Deep qw( hash2object refexpr2perlref ); |
68 |
use Data::Compare::Struct qw( getDifference isEmpty ); |
use Data::Compare::Struct qw( getDifference isEmpty ); |
69 |
|
|
70 |
# get logger instance |
# get logger instance |
71 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
72 |
|
|
73 |
|
$| = 1; |
74 |
|
|
75 |
sub new { |
sub new { |
76 |
my $invocant = shift; |
my $invocant = shift; |
77 |
my $class = ref($invocant) || $invocant; |
my $class = ref($invocant) || $invocant; |
78 |
my $self = { @_ }; |
my $self = {}; |
79 |
$logger->debug( __PACKAGE__ . "->new(@_)" ); |
$logger->debug( __PACKAGE__ . "->new(@_)" ); |
80 |
bless $self, $class; |
bless $self, $class; |
81 |
$self->_init(); |
$self->configure(@_); |
82 |
return $self; |
return $self; |
83 |
} |
} |
84 |
|
|
85 |
|
|
86 |
|
sub configure { |
87 |
|
my $self = shift; |
88 |
|
my @args = @_; |
89 |
|
if (!isEmpty(\@args)) { |
90 |
|
my %properties = @_; |
91 |
|
# merge args to properties |
92 |
|
map { $self->{$_} = $properties{$_}; } keys %properties; |
93 |
|
$self->_init(); |
94 |
|
} else { |
95 |
|
#print "no args!", "\n"; |
96 |
|
} |
97 |
|
#print Dumper($self); |
98 |
|
} |
99 |
|
|
100 |
sub _init { |
sub _init { |
101 |
my $self = shift; |
my $self = shift; |
102 |
|
|
103 |
|
$self->{configured} = 1; |
104 |
|
|
105 |
# build new container if necessary |
# build new container if necessary |
106 |
$self->{container} = Data::Storage::Container->new() if !$self->{container}; |
$self->{container} = Data::Storage::Container->new() if !$self->{container}; |
119 |
} |
} |
120 |
|
|
121 |
|
|
122 |
|
sub prepareOptions { |
123 |
|
|
124 |
|
my $self = shift; |
125 |
|
my $opts = shift; |
126 |
|
|
127 |
|
#print Dumper($opts); |
128 |
|
|
129 |
|
$opts->{mode} ||= ''; |
130 |
|
$opts->{erase} ||= 0; |
131 |
|
#$opts->{import} ||= 0; |
132 |
|
|
133 |
|
$logger->notice( __PACKAGE__ . "->prepareOptions( source_node $opts->{source_node} mode $opts->{mode} erase $opts->{erase} prepare $opts->{prepare} )"); |
134 |
|
|
135 |
|
if (!$opts->{mapping} || !$opts->{mapping_module}) { |
136 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: No mapping supplied - please check key 'mappings' in BizWorks/Config.pm"); |
137 |
|
} |
138 |
|
|
139 |
|
my $evstring = "use $opts->{mapping_module};"; |
140 |
|
eval($evstring); |
141 |
|
if ($@) { |
142 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: error while trying to access mapping - $@"); |
143 |
|
return; |
144 |
|
} |
145 |
|
|
146 |
|
# resolve mapping metadata (returned from sub) |
147 |
|
my $mapObject = $opts->{mapping_module}->new(); |
148 |
|
#print Dumper($map); |
149 |
|
my $source_node_name = $opts->{source_node}; |
150 |
|
# check if mapping for certain node is contained in mapping object |
151 |
|
if (!$mapObject->can($source_node_name)) { |
152 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: Can't access mapping for node \"$source_node_name\" - please check $opts->{mapping_module}."); |
153 |
|
return; |
154 |
|
} |
155 |
|
my $map = $mapObject->$source_node_name; |
156 |
|
|
157 |
|
# remove asymmetries from $map (patch keys) |
158 |
|
$map->{source_node} = $map->{source}; delete $map->{source}; |
159 |
|
$map->{target_node} = $map->{target}; delete $map->{target}; |
160 |
|
$map->{mapping} = $map->{details}; delete $map->{details}; |
161 |
|
$map->{direction} = $map->{mode}; delete $map->{mode}; |
162 |
|
|
163 |
|
# defaults (mostly for backward-compatibility) |
164 |
|
$map->{source_node} ||= $source_node_name; |
165 |
|
$map->{source_ident} ||= 'storage_method:id'; |
166 |
|
$map->{target_ident} ||= 'property:oid'; |
167 |
|
$map->{direction} ||= $opts->{mode}; # | PUSH | PULL | FULL |
168 |
|
$map->{method} ||= 'checksum'; # | timestamp |
169 |
|
$map->{source_exclude} ||= [qw( cs )]; |
170 |
|
|
171 |
|
# merge map to opts |
172 |
|
map { $opts->{$_} = $map->{$_}; } keys %$map; |
173 |
|
|
174 |
|
#print Dumper($opts); |
175 |
|
|
176 |
|
# TODO: move this to checkOptions... |
177 |
|
|
178 |
|
# check - do we have a target? |
179 |
|
if (!$opts->{target_node}) { |
180 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: No target given - please check metadata declaration."); |
181 |
|
return; |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
#return $opts; |
186 |
|
return 1; |
187 |
|
|
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
|
sub checkOptions { |
192 |
|
my $self = shift; |
193 |
|
my $opts = shift; |
194 |
|
|
195 |
|
my $result = 1; |
196 |
|
|
197 |
|
# check - do we have a target node? |
198 |
|
if (!$opts->{target_node}) { |
199 |
|
$logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'target node' could be determined."); |
200 |
|
$result = 0; |
201 |
|
} |
202 |
|
|
203 |
|
# check - do we have a mapping? |
204 |
|
if (!$opts->{mapping} && !$opts->{mapping_module}) { |
205 |
|
$logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'mapping' could be determined."); |
206 |
|
$result = 0; |
207 |
|
} |
208 |
|
|
209 |
|
return $result; |
210 |
|
|
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
# 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) |
215 |
sub syncNodes { |
sub syncNodes { |
216 |
|
|
217 |
my $self = shift; |
my $self = shift; |
218 |
my $args = shift; |
my $args = shift; |
219 |
|
|
220 |
|
if (!$self->{configured}) { |
221 |
|
$logger->critical( __PACKAGE__ . "->syncNodes: Synchronization object is not configured/initialized correctly." ); |
222 |
|
return; |
223 |
|
} |
224 |
|
|
225 |
# remember arguments through the whole processing |
# remember arguments through the whole processing |
226 |
$self->{args} = $args; |
$self->{args} = $args; |
227 |
|
|
244 |
} |
} |
245 |
|
|
246 |
# decompose identifiers for each partner |
# decompose identifiers for each partner |
247 |
# TODO: take this list from already established/given metadata |
# TODO: refactor!!! take this list from already established/given metadata |
248 |
foreach ('source', 'target') { |
foreach ('source', 'target') { |
249 |
|
|
250 |
# get/set metadata for further processing |
# get/set metadata for further processing |
299 |
#print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n"; |
#print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n"; |
300 |
} |
} |
301 |
|
|
302 |
|
#print Dumper($self->{meta}); |
303 |
|
|
304 |
$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}" ); |
305 |
|
|
306 |
# build mapping |
# build mapping |
307 |
|
# incoming: and Array of node map entries (Array or Hash) - e.g. |
308 |
|
# [ 'source:item_name' => 'target:class_val' ] |
309 |
|
# { source => 'event->startDateTime', target => 'begindate' } |
310 |
foreach (@{$self->{args}->{mapping}}) { |
foreach (@{$self->{args}->{mapping}}) { |
311 |
my @key1 = split(':', $_->[0]); |
if (ref $_ eq 'ARRAY') { |
312 |
my @key2 = split(':', $_->[1]); |
my @entry1 = split(':', $_->[0]); |
313 |
push @{$self->{meta}->{$key1[0]}->{childnodes}}, $key1[1]; |
my @entry2 = split(':', $_->[1]); |
314 |
push @{$self->{meta}->{$key2[0]}->{childnodes}}, $key2[1]; |
my $descent = []; |
315 |
|
my $node = []; |
316 |
|
$descent->[0] = $entry1[0]; |
317 |
|
$descent->[1] = $entry2[0]; |
318 |
|
$node->[0] = $entry1[1]; |
319 |
|
$node->[1] = $entry2[1]; |
320 |
|
push @{$self->{meta}->{$descent->[0]}->{childnodes}}, $node->[0]; |
321 |
|
push @{$self->{meta}->{$descent->[1]}->{childnodes}}, $node->[1]; |
322 |
|
} elsif (ref $_ eq 'HASH') { |
323 |
|
foreach my $entry_key (keys %$_) { |
324 |
|
my $entry_val = $_->{$entry_key}; |
325 |
|
push @{$self->{meta}->{$entry_key}->{childnodes}}, $entry_val; |
326 |
|
} |
327 |
|
} |
328 |
|
|
329 |
} |
} |
330 |
|
|
331 |
|
#print Dumper($self->{meta}); |
332 |
|
|
333 |
# check partners/nodes: does partner exist / is node available? |
# check partners/nodes: does partner exist / is node available? |
334 |
foreach my $partner (keys %{$self->{meta}}) { |
foreach my $partner (keys %{$self->{meta}}) { |
335 |
next if $self->{meta}->{$partner}->{storage}->{locator}->{type} eq 'DBI'; # for DBD::CSV - re-enable for others |
|
336 |
|
# 1. check partners & storages |
337 |
|
if (!$self->{meta}->{$partner}) { |
338 |
|
$logger->critical( __PACKAGE__ . "->syncNodes: Could not find partner '$partner' in configuration metadata." ); |
339 |
|
return; |
340 |
|
} |
341 |
|
|
342 |
|
my $dbkey = $self->{meta}->{$partner}->{dbkey}; |
343 |
|
|
344 |
|
if (!$self->{meta}->{$partner}->{storage}) { |
345 |
|
$logger->critical( __PACKAGE__ . "->syncNodes: Could not access storage of partner '$partner' (named '$dbkey'), looks like a configuration-error." ); |
346 |
|
return; |
347 |
|
} |
348 |
|
|
349 |
|
# TODO: |
350 |
|
# 2. check if partners (and nodes?) are actually available.... |
351 |
|
# eventually pre-check mode of access-attempt (read/write) here to provide an "early-croak" if possible |
352 |
|
|
353 |
|
# 3. check nodes |
354 |
|
next if $self->{meta}->{$partner}->{storage}->{locator}->{type} eq 'DBI'; # HACK for DBD::CSV - re-enable for others |
355 |
|
# get node-name |
356 |
my $node = $self->{meta}->{$partner}->{node}; |
my $node = $self->{meta}->{$partner}->{node}; |
357 |
if (!$self->{meta}->{$partner}->{storage}->existsChildNode($node)) { |
if (!$self->{meta}->{$partner}->{storage}->existsChildNode($node)) { |
358 |
$logger->critical( __PACKAGE__ . "->syncNodes: Could not reach \"$node\" at \"$partner\"." ); |
$logger->critical( __PACKAGE__ . "->syncNodes: Could not reach node \"$node\" at partner \"$partner\"." ); |
359 |
return; |
return; |
360 |
} |
} |
361 |
|
|
362 |
} |
} |
363 |
|
|
364 |
# TODO: |
# TODO: |
382 |
|
|
383 |
# import flag means: prepare the source node to be syncable |
# import flag means: prepare the source node to be syncable |
384 |
# this is useful if there are e.g. no "ident" or "checksum" columns yet inside a DBI like (row-based) storage |
# this is useful if there are e.g. no "ident" or "checksum" columns yet inside a DBI like (row-based) storage |
385 |
if ($self->{args}->{import}) { |
if ($self->{args}->{prepare}) { |
386 |
$self->_prepareNode_MetaProperties('source'); |
$self->_prepareNode_MetaProperties('source'); |
387 |
$self->_prepareNode_DummyIdent('source'); |
$self->_prepareNode_DummyIdent('source'); |
388 |
#return; |
#return; |
389 |
#$self->_erase_all($opts->{source_node}); |
#$self->_erase_all($opts->{source_node}); |
390 |
} |
} |
391 |
|
|
392 |
|
# erase flag means: erase the target |
393 |
|
#if ($opts->{erase}) { |
394 |
|
if ($self->{args}->{erase}) { |
395 |
|
# TODO: move this method to the scope of the synchronization core and wrap it around different handlers |
396 |
|
#print "ERASE", "\n"; |
397 |
|
$self->_erase_all('target'); |
398 |
|
} |
399 |
|
|
400 |
$self->_syncNodes(); |
$self->_syncNodes(); |
401 |
|
|
402 |
} |
} |
421 |
$results ||= $self->_getNodeList('source', $filter); |
$results ||= $self->_getNodeList('source', $filter); |
422 |
} |
} |
423 |
|
|
424 |
# get reference to node list from convenient method provided by corehandle |
# get reference to node list from convenient method provided by CORE-HANDLE |
425 |
#$results ||= $self->{source}->getListUnfiltered($self->{meta}->{source}->{node}); |
#$results ||= $self->{source}->getListUnfiltered($self->{meta}->{source}->{node}); |
426 |
#$results ||= $self->{meta}->{source}->{storage}->getListUnfiltered($self->{meta}->{source}->{node}); |
#$results ||= $self->{meta}->{source}->{storage}->getListUnfiltered($self->{meta}->{source}->{node}); |
427 |
$results ||= $self->_getNodeList('source'); |
$results ||= $self->_getNodeList('source'); |
499 |
my $identOK = $self->_resolveNodeIdent('source'); |
my $identOK = $self->_resolveNodeIdent('source'); |
500 |
#if (!$identOK && lc $self->{args}->{direction} ne 'import') { |
#if (!$identOK && lc $self->{args}->{direction} ne 'import') { |
501 |
if (!$identOK) { |
if (!$identOK) { |
502 |
$logger->critical( __PACKAGE__ . "->syncNodes: Can not synchronize: No ident found in source node, maybe try to \"import\" this node first." ); |
#print Dumper($self->{meta}->{source}); |
503 |
|
$logger->critical( __PACKAGE__ . "->syncNodes: No ident found in source node \"$self->{meta}->{source}->{node}\", try to \"prepare\" this node first?" ); |
504 |
return; |
return; |
505 |
} |
} |
506 |
|
|
507 |
#print "statload", "\n"; |
#print "statload", "\n"; |
508 |
#print "ident: ", $self->{node}->{source}->{ident}, "\n"; |
#print "ident: ", $self->{node}->{source}->{ident}, "\n"; |
509 |
|
#print Dumper($self->{node}); |
510 |
|
|
511 |
my $statOK = $self->_statloadNode('target', $self->{node}->{source}->{ident}); |
my $statOK = $self->_statloadNode('target', $self->{node}->{source}->{ident}); |
512 |
|
|
513 |
|
#print Dumper($self->{node}); |
514 |
|
|
515 |
# mark node as new either if there's no ident or if stat/load failed |
# mark node as new either if there's no ident or if stat/load failed |
516 |
if (!$statOK) { |
if (!$statOK) { |
597 |
$tc->{attempt_new}++; |
$tc->{attempt_new}++; |
598 |
$self->_doTransferToTarget('insert'); |
$self->_doTransferToTarget('insert'); |
599 |
# asymmetry: refetch node from target to re-calculate new ident and checksum (TODO: is IdentAuthority of relevance here?) |
# asymmetry: refetch node from target to re-calculate new ident and checksum (TODO: is IdentAuthority of relevance here?) |
600 |
|
#print Dumper($self->{node}); |
601 |
$self->_statloadNode('target', $self->{node}->{target}->{ident}, 1); |
$self->_statloadNode('target', $self->{node}->{target}->{ident}, 1); |
602 |
$self->_readChecksum('target'); |
$self->_readChecksum('target'); |
603 |
|
|
623 |
# change ident in source (take from target), if transfer was ok and target is an IdentAuthority |
# change ident in source (take from target), if transfer was ok and target is an IdentAuthority |
624 |
# this is (for now) called a "retransmit" indicated by a "r"-character when verbosing |
# this is (for now) called a "retransmit" indicated by a "r"-character when verbosing |
625 |
if ($self->{node}->{status}->{ok} && $self->{meta}->{target}->{storage}->{isIdentAuthority}) { |
if ($self->{node}->{status}->{ok} && $self->{meta}->{target}->{storage}->{isIdentAuthority}) { |
626 |
|
print "r" if $self->{verbose}; |
627 |
#print Dumper($self->{meta}); |
#print Dumper($self->{meta}); |
628 |
#print Dumper($self->{node}); |
#print Dumper($self->{node}); |
629 |
#exit; |
#exit; |
630 |
$self->_doModifySource_IdentChecksum($self->{node}->{target}->{ident}); |
$self->_doModifySource_IdentChecksum($self->{node}->{target}->{ident}); |
|
print "r" if $self->{verbose}; |
|
631 |
} |
} |
632 |
|
|
633 |
print ":" if $self->{verbose}; |
print ":" if $self->{verbose}; |
637 |
print "\n" if $self->{verbose}; |
print "\n" if $self->{verbose}; |
638 |
|
|
639 |
# build user-message from some stats |
# build user-message from some stats |
640 |
my $msg = "stats: $tc"; |
my $msg = "statistics: $tc"; |
641 |
|
|
642 |
if ($tc->{error_per_row}) { |
if ($tc->{error_per_row}) { |
643 |
$msg .= "\n"; |
$msg .= "\n"; |
644 |
$msg .= "errors:" . "\n"; |
$msg .= "errors from \"error_per_row\":" . "\n"; |
645 |
$msg .= Dumper($tc->{error_per_row}); |
$msg .= Dumper($tc->{error_per_row}); |
646 |
} |
} |
647 |
|
|
654 |
} |
} |
655 |
|
|
656 |
|
|
657 |
|
# refactor this as some core-function to do a generic dump resolving data-encapsulations of e.g. Set::Object |
658 |
sub _dumpCompact { |
sub _dumpCompact { |
659 |
my $self = shift; |
my $self = shift; |
660 |
|
|
666 |
my $item = {}; |
my $item = {}; |
667 |
foreach my $key (keys %$_) { |
foreach my $key (keys %$_) { |
668 |
my $val = $_->{$key}; |
my $val = $_->{$key}; |
669 |
|
|
670 |
|
#print Dumper($val); |
671 |
|
|
672 |
if (ref $val eq 'Set::Object') { |
if (ref $val eq 'Set::Object') { |
673 |
#print "========================= SET", "\n"; |
#print "========================= SET", "\n"; |
674 |
#print Dumper($val); |
#print Dumper($val); |
675 |
#print Dumper($val->members()); |
#print Dumper($val->members()); |
676 |
#$val = $val->members(); |
#$val = $val->members(); |
677 |
#$vars->[$count]->{$key} = $val->members() if $val->can("members"); |
#$vars->[$count]->{$key} = $val->members() if $val->can("members"); |
678 |
#$item->{$key} = $val->members() if $val->can("members"); |
#$item->{$key} = $val->members() if $val->can("members"); |
679 |
$item->{$key} = $val->members(); |
$item->{$key} = $val->members(); |
680 |
#print Dumper($vars->[$count]->{$key}); |
#print Dumper($vars->[$count]->{$key}); |
681 |
|
|
682 |
} else { |
} else { |
683 |
$item->{$key} = $val; |
$item->{$key} = $val; |
684 |
} |
} |
685 |
|
|
686 |
} |
} |
687 |
push @data, $item; |
push @data, $item; |
688 |
$count++; |
$count++; |
689 |
} |
} |
690 |
|
|
691 |
#print "Dump:", "\n"; |
#print "Dump:", Dumper(@data), "\n"; |
|
#print Dumper(@data); |
|
692 |
|
|
693 |
$Data::Dumper::Indent = 0; |
$Data::Dumper::Indent = 0; |
694 |
my $result = Dumper(@data); |
my $result = Dumper(@data); |
695 |
$Data::Dumper::Indent = 2; |
$Data::Dumper::Indent = 2; |
696 |
return $result; |
return $result; |
697 |
|
|
698 |
} |
} |
699 |
|
|
700 |
|
|
718 |
#$logger->dump( __PACKAGE__ . ": " . $dump ); |
#$logger->dump( __PACKAGE__ . ": " . $dump ); |
719 |
|
|
720 |
# calculate checksum from dump |
# calculate checksum from dump |
721 |
|
# note: the 32-bit integer hash from DBI seems |
722 |
|
# to generate duplicates with small payloads already in ranges of hundreds of items/rows!!! |
723 |
|
# try to avoid to use it or try to use it only for payloads greater than, hmmm, let's say 30 chars? |
724 |
|
# (we had about 15 chars average per item (row)) |
725 |
|
|
726 |
# md5-based fingerprint, base64 encoded (from Digest::MD5) |
# md5-based fingerprint, base64 encoded (from Digest::MD5) |
727 |
#my $checksum_cur = md5_base64($objdump) . '=='; |
$self->{node}->{$descent}->{checksum} = md5_base64($dump) . '=='; |
728 |
# 32-bit integer "hash" value (maybe faster?) (from DBI) |
# 32-bit integer "hash" value (maybe faster?) (from DBI) |
729 |
$self->{node}->{$descent}->{checksum} = DBI::hash($dump, 1); |
#$self->{node}->{$descent}->{checksum} = DBI::hash($dump, 1); |
730 |
|
|
731 |
# signal good |
# signal good |
732 |
return 1; |
return 1; |
793 |
for (my $mapidx = 0; $mapidx <= $#childnodes; $mapidx++) { |
for (my $mapidx = 0; $mapidx <= $#childnodes; $mapidx++) { |
794 |
#my $map_right = $self->{args}->{mapping}->{$key}; |
#my $map_right = $self->{args}->{mapping}->{$key}; |
795 |
|
|
796 |
|
$self->{node}->{source}->{propcache} = {}; |
797 |
|
$self->{node}->{target}->{propcache} = {}; |
798 |
|
|
799 |
# get property name |
# get property name |
800 |
$self->{node}->{source}->{propcache}->{property} = $self->{meta}->{source}->{childnodes}->[$mapidx]; |
$self->{node}->{source}->{propcache}->{property} = $self->{meta}->{source}->{childnodes}->[$mapidx]; |
801 |
$self->{node}->{target}->{propcache}->{property} = $self->{meta}->{target}->{childnodes}->[$mapidx]; |
$self->{node}->{target}->{propcache}->{property} = $self->{meta}->{target}->{childnodes}->[$mapidx]; |
813 |
$self->{node}->{source}->{propcache}->{value} = $self->{node}->{source}->{payload}->{$self->{node}->{source}->{propcache}->{property}}; |
$self->{node}->{source}->{propcache}->{value} = $self->{node}->{source}->{payload}->{$self->{node}->{source}->{propcache}->{property}}; |
814 |
} |
} |
815 |
#$self->{node}->{map}->{$key} = $value; |
#$self->{node}->{map}->{$key} = $value; |
816 |
|
|
817 |
|
# detect expression |
818 |
|
# for transferring deeply nested structures described by expressions |
819 |
|
#print "val: $self->{node}->{source}->{propcache}->{value}", "\n"; |
820 |
|
if ($self->{node}->{source}->{propcache}->{property} =~ s/^expr://) { |
821 |
|
|
822 |
|
# create an anonymous sub to act as callback target dispatcher |
823 |
|
my $cb_dispatcher = sub { |
824 |
|
#print "=============== CALLBACK DISPATCHER", "\n"; |
825 |
|
#print "ident: ", $self->{node}->{source}->{ident}, "\n"; |
826 |
|
#return $self->{node}->{source}->{ident}; |
827 |
|
|
828 |
|
}; |
829 |
|
|
830 |
|
|
831 |
|
#print Dumper($self->{node}); |
832 |
|
|
833 |
|
# build callback map for helper function |
834 |
|
#my $cbmap = { $self->{meta}->{source}->{IdentProvider}->{arg} => $cb_dispatcher }; |
835 |
|
my $cbmap = {}; |
836 |
|
my $value = refexpr2perlref($self->{node}->{source}->{payload}, $self->{node}->{source}->{propcache}->{property}, $cbmap); |
837 |
|
$self->{node}->{source}->{propcache}->{value} = $value; |
838 |
|
} |
839 |
|
|
840 |
# encode values dependent on type of underlying storage here - expand cases... |
# encode values dependent on type of underlying storage here - expand cases... |
841 |
my $storage_type = $self->{meta}->{target}->{storage}->{locator}->{type}; |
my $storage_type = $self->{meta}->{target}->{storage}->{locator}->{type}; |
842 |
if ($storage_type eq 'DBI') { |
if ($storage_type eq 'DBI') { |
843 |
# ...for sql |
# ...for sql |
844 |
$self->{node}->{source}->{propcache}->{value} = quotesql($self->{node}->{source}->{propcache}->{value}); |
$self->{node}->{source}->{propcache}->{value} = quotesql($self->{node}->{source}->{propcache}->{value}); |
845 |
} elsif ($storage_type eq 'Tangram') { |
} |
846 |
|
elsif ($storage_type eq 'Tangram') { |
847 |
|
# iso? utf8 already possible? |
848 |
|
|
849 |
} elsif ($storage_type eq 'LDAP') { |
} elsif ($storage_type eq 'LDAP') { |
850 |
# TODO: encode utf8 here? |
# TODO: encode utf8 here? |
851 |
} |
} |
856 |
} |
} |
857 |
} |
} |
858 |
|
|
|
#print "self->{entry}: ", Dumper($self->{node}), "\n"; exit; |
|
|
|
|
|
# for transferring deeply nested structures described by expressions |
|
|
# this currently does not work! |
|
|
# TODO: re-enable this! |
|
|
if ($self->{args}->{mappingV2}) { |
|
|
|
|
|
# apply mapping from $self->{args}->{mappingV2} to $self->{node}->{map} |
|
|
foreach my $mapStep (@{$self->{args}->{mappingV2}}) { |
|
|
|
|
|
# prepare left/right keys/values |
|
|
my $left_key = $mapStep->{left}; |
|
|
my $left_val = _resolveMapStepExpr( $self->{node}->{source}->{payload}, $mapStep->{left} ); |
|
|
my $right_key = $mapStep->{right}; |
|
|
my $right_val = ( $mapStep->{right} ); |
|
|
#print "map: $map_right", "\n"; |
|
|
|
|
|
if ($mapStep->{method}) { |
|
|
if ($mapStep->{method} eq 'v:1') { |
|
|
$left_val = $left_key; |
|
|
} |
|
|
} |
|
|
|
|
|
#$self->{node}->{map}->{$key} = $value; |
|
|
#if ( grep(!/$right_key/, @{$self->{args}->{exclude}}) ) { |
|
|
$self->{node}->{map}->{$right_key} = $self->{R}->quoteSql($left_val); |
|
|
#} |
|
|
} |
|
|
} |
|
859 |
|
|
860 |
# TODO: $logger->dump( ... ); |
# TODO: $logger->dump( ... ); |
861 |
#$logger->debug( "sqlmap:" . "\n" . Dumper($self->{node}->{map}) ); |
#$logger->debug( "sqlmap:" . "\n" . Dumper($self->{node}->{map}) ); |
929 |
} |
} |
930 |
} |
} |
931 |
|
|
932 |
|
|
933 |
|
#print Dumper($self->{meta}); |
934 |
|
|
935 |
# DBI speaks SQL |
# DBI speaks SQL |
936 |
if ($self->{meta}->{$descent}->{storage}->{locator}->{type} eq 'DBI') { |
if ($self->{meta}->{$descent}->{storage}->{locator}->{type} eq 'DBI') { |
944 |
#print $action, "\n"; |
#print $action, "\n"; |
945 |
#$action = "anc"; |
#$action = "anc"; |
946 |
#print "yai", "\n"; |
#print "yai", "\n"; |
947 |
|
|
948 |
|
#print Dumper($map); |
949 |
|
#delete $map->{cs}; |
950 |
|
|
951 |
if (lc($action) eq 'insert') { |
if (lc($action) eq 'insert') { |
952 |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_INSERT'); |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_INSERT'); |
953 |
} elsif (lc $action eq 'update') { |
} elsif (lc $action eq 'update') { |
955 |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_UPDATE', $crit); |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_UPDATE', $crit); |
956 |
} |
} |
957 |
|
|
958 |
#print "sql: ", $sql_main, "\n"; |
#$sql_main = "UPDATE currencies_csv SET oid='abcdef' WHERE text='Australian Dollar' AND key='AUD';"; |
959 |
#exit; |
#$sql_main = "UPDATE currencies_csv SET oid='huhu2' WHERE ekey='AUD'"; |
960 |
|
|
961 |
|
#print "sql: ", $sql_main, "\n"; |
962 |
|
#exit; |
963 |
|
|
964 |
# transfer data |
# transfer data |
965 |
my $sqlHandle = $self->{meta}->{$descent}->{storage}->sendCommand($sql_main); |
my $sqlHandle = $self->{meta}->{$descent}->{storage}->sendCommand($sql_main); |
966 |
|
|
967 |
|
#exit; |
968 |
|
|
969 |
# handle errors |
# handle errors |
970 |
if ($sqlHandle->err) { |
if ($sqlHandle->err) { |
971 |
#if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; } |
#if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; } |
1041 |
hash2object($object, $map); |
hash2object($object, $map); |
1042 |
|
|
1043 |
# ... and re-update@orm. |
# ... and re-update@orm. |
1044 |
|
#print Dumper($object); |
1045 |
$self->{meta}->{$descent}->{storage}->update($object); |
$self->{meta}->{$descent}->{storage}->update($object); |
1046 |
|
|
1047 |
# asymmetry: get ident after insert |
# asymmetry: get ident after insert |
1048 |
# TODO: |
# TODO: |
1049 |
# - just do this if it is an IdentAuthority |
# - just do this if it is an IdentAuthority |
1050 |
# - use IdentProvider metadata here |
# - use IdentProvider metadata here |
1051 |
$self->{node}->{$descent}->{ident} = $self->{meta}->{$descent}->{storage}->id($object); |
#print Dumper($self->{meta}->{$descent}); |
1052 |
|
my $oid = $self->{meta}->{$descent}->{storage}->id($object); |
1053 |
|
#print "oid: $oid", "\n"; |
1054 |
|
$self->{node}->{$descent}->{ident} = $oid; |
1055 |
|
|
1056 |
|
|
1057 |
} elsif (lc $action eq 'update') { |
} elsif (lc $action eq 'update') { |
1148 |
#print "\n", "Attempt to fetch entry implicitely by ident failed: no ident given! This may result in an insert if no write-protection is in the way.", "\n"; |
#print "\n", "Attempt to fetch entry implicitely by ident failed: no ident given! This may result in an insert if no write-protection is in the way.", "\n"; |
1149 |
return; |
return; |
1150 |
} |
} |
1151 |
|
|
1152 |
|
# patch for DBD::CSV |
1153 |
|
if ($ident && $ident eq 'Null') { |
1154 |
|
return; |
1155 |
|
} |
1156 |
|
|
1157 |
|
#print "yai!", "\n"; |
1158 |
|
|
1159 |
my $result = $self->{meta}->{$descent}->{storage}->sendQuery({ |
my $query = { |
1160 |
node => $self->{meta}->{$descent}->{node}, |
node => $self->{meta}->{$descent}->{node}, |
1161 |
subnodes => [qw( cs )], |
subnodes => [qw( cs )], |
1162 |
criterias => [ |
criterias => [ |
1164 |
op => 'eq', |
op => 'eq', |
1165 |
val => $ident }, |
val => $ident }, |
1166 |
] |
] |
1167 |
}); |
}; |
1168 |
|
|
1169 |
|
#print Dumper($query); |
1170 |
|
|
1171 |
|
my $result = $self->{meta}->{$descent}->{storage}->sendQuery($query); |
1172 |
|
|
1173 |
my $entry = $result->getNextEntry(); |
my $entry = $result->getNextEntry(); |
1174 |
|
|
1175 |
|
#print Dumper($entry); |
1176 |
|
#print "pers: " . $self->{meta}->{$descent}->{storage}->is_persistent($entry), "\n"; |
1177 |
|
#my $state = $self->{meta}->{$descent}->{storage}->_fetch_object_state($entry, { name => 'TransactionHop' } ); |
1178 |
|
#print Dumper($state); |
1179 |
|
|
1180 |
my $status = $result->getStatus(); |
my $status = $result->getStatus(); |
1181 |
|
|
1182 |
|
#print Dumper($status); |
1183 |
|
|
1184 |
# TODO: enhance error handling (store inside tc) |
# TODO: enhance error handling (store inside tc) |
1185 |
#if (!$row) { |
#if (!$row) { |
1186 |
# print "\n", "row error", "\n"; |
# print "\n", "row error", "\n"; |
1187 |
# next; |
# next; |
1188 |
#} |
#} |
1189 |
if (($status && $status->{err}) || !$entry) { |
|
1190 |
#$logger->critical( __PACKAGE__ . "->_loadNode (ident=\"$ident\") failed" ); |
# these checks run before actually loading payload- and meta-data to node-container |
1191 |
return; |
|
1192 |
} |
# 1st level - hard error |
1193 |
|
if ($status && $status->{err}) { |
1194 |
|
$logger->debug( __PACKAGE__ . "->_statloadNode (ident=\"$ident\") failed - hard error (that's ok): $status->{err}" ); |
1195 |
|
return; |
1196 |
|
} |
1197 |
|
|
1198 |
|
# 2nd level - logical (empty/notfound) error |
1199 |
|
if (($status && $status->{empty}) || !$entry) { |
1200 |
|
$logger->debug( __PACKAGE__ . "->_statloadNode (ident=\"$ident\") failed - logical error (that's ok)" ); |
1201 |
|
#print "no entry (logical)", "\n"; |
1202 |
|
return; |
1203 |
|
} |
1204 |
|
|
1205 |
|
#print Dumper($entry); |
1206 |
|
|
1207 |
# was: |
# was: |
1208 |
# $self->{node}->{$descent}->{ident} = $ident; |
# $self->{node}->{$descent}->{ident} = $ident; |
1209 |
# is: |
# is: |
1210 |
# TODO: re-resolve ident from entry via metadata "IdentProvider" |
# TODO: re-resolve ident from entry via metadata "IdentProvider" here - like elsewhere |
1211 |
$self->{node}->{$descent}->{ident} = $ident; |
$self->{node}->{$descent}->{ident} = $ident; |
1212 |
$self->{node}->{$descent}->{payload} = $entry; |
$self->{node}->{$descent}->{payload} = $entry; |
1213 |
|
|
1214 |
} |
} |
1215 |
|
|
1216 |
return 1; |
return 1; |
1234 |
$self->{meta}->{source}->{IdentProvider}->{arg} => $ident_new, |
$self->{meta}->{source}->{IdentProvider}->{arg} => $ident_new, |
1235 |
cs => $self->{node}->{target}->{checksum}, |
cs => $self->{node}->{target}->{checksum}, |
1236 |
}; |
}; |
1237 |
#print Dumper($map); |
|
1238 |
#print Dumper($self->{node}); |
#print Dumper($map); |
1239 |
#exit; |
#print Dumper($self->{node}); |
1240 |
|
#exit; |
1241 |
|
|
1242 |
$self->_modifyNode('source', 'update', $map); |
$self->_modifyNode('source', 'update', $map); |
1243 |
} |
} |
1244 |
|
|
1320 |
} |
} |
1321 |
my $crit = join ' AND ', @crits; |
my $crit = join ' AND ', @crits; |
1322 |
print "p" if $self->{verbose}; |
print "p" if $self->{verbose}; |
1323 |
|
|
1324 |
|
#print Dumper($map); |
1325 |
|
#print Dumper($crit); |
1326 |
|
|
1327 |
$self->_modifyNode($descent, 'update', $map, $crit); |
$self->_modifyNode($descent, 'update', $map, $crit); |
1328 |
$i++; |
$i++; |
1329 |
} |
} |
1339 |
# TODO: handle this in an abstract way (wipe out use of 'source' and/or 'target' inside core) |
# TODO: handle this in an abstract way (wipe out use of 'source' and/or 'target' inside core) |
1340 |
sub _otherSide { |
sub _otherSide { |
1341 |
my $self = shift; |
my $self = shift; |
1342 |
my $side = shift; |
my $descent = shift; |
1343 |
return 'source' if $side eq 'target'; |
return 'source' if $descent eq 'target'; |
1344 |
return 'target' if $side eq 'source'; |
return 'target' if $descent eq 'source'; |
1345 |
return ''; |
return ''; |
1346 |
} |
} |
1347 |
|
|
1348 |
|
sub _erase_all { |
1349 |
|
my $self = shift; |
1350 |
|
my $descent = shift; |
1351 |
|
#my $node = shift; |
1352 |
|
my $node = $self->{meta}->{$descent}->{node}; |
1353 |
|
$self->{meta}->{$descent}->{storage}->eraseAll($node); |
1354 |
|
} |
1355 |
|
|
1356 |
|
|
1357 |
=pod |
=pod |
1358 |
|
|