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

Annotation of /nfo/perl/libs/Data/Transfer/Sync/StorageInterface.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations)
Thu Mar 27 15:31:16 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.6: +5 -2 lines
fixes to modules regarding new namespace(s) below Data::Mungle::*

1 joko 1.7 ## $Id: StorageInterface.pm,v 1.6 2003/02/21 01:47:53 joko Exp $
2 joko 1.1 ##
3     ## Copyright (c) 2002 Andreas Motl <andreas.motl@ilo.de>
4     ##
5     ## See COPYRIGHT section in pod text below for usage and distribution rights.
6     ##
7     ## ----------------------------------------------------------------------------------------
8 joko 1.2 ## $Log: StorageInterface.pm,v $
9 joko 1.7 ## Revision 1.6 2003/02/21 01:47:53 joko
10     ## renamed core function
11     ##
12 joko 1.6 ## Revision 1.5 2003/02/20 20:24:33 joko
13     ## + additional pre-flight checks
14     ##
15 joko 1.5 ## Revision 1.4 2003/02/14 14:14:38 joko
16     ## + new code refactored here
17     ##
18 joko 1.4 ## Revision 1.3 2003/02/11 07:54:55 joko
19     ## + modified module usage
20     ## + debugging trials
21     ##
22 joko 1.3 ## Revision 1.2 2003/02/09 05:05:58 joko
23     ## + major structure changes
24     ## - refactored code to sister modules
25     ## + refactored code to this place
26     ##
27 joko 1.2 ## Revision 1.1 2003/01/20 16:58:46 joko
28     ## + initial check-in: here they are....
29     ##
30 joko 1.1 ## Revision 1.1 2003/01/19 01:23:04 joko
31     ## + new from Data/Transfer/Sync.pm
32     ##
33     ## ----------------------------------------------------------------------------------------
34    
35    
36     package Data::Transfer::Sync::StorageInterface;
37    
38     use strict;
39     use warnings;
40    
41     use mixin::with qw( Data::Transfer::Sync );
42    
43    
44     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - main
45    
46 joko 1.2 use Data::Dumper;
47     use Hash::Merge qw( merge );
48 joko 1.3 use libdb qw( hash2Sql );
49 joko 1.7 use Data::Mungle::Transform::Deep qw( merge_to );
50 joko 1.2
51    
52 joko 1.1 # get logger instance
53     my $logger = Log::Dispatch::Config->instance;
54    
55     # this is a shortcut method
56     # ... let's try to avoid _any_ redundant code in here (ok... - at the cost of method lookups...)
57     sub _getNodeList {
58     my $self = shift;
59     my $descent = shift;
60     my $filter = shift;
61 joko 1.2 $logger->debug( __PACKAGE__ . "->_getNodeList( descent=$descent, accessorName=$self->{meta}->{$descent}->{accessorName} )" );
62 joko 1.1 #$results ||= $self->{source}->getListUnfiltered($self->{meta}->{source}->{node});
63     #$results ||= $self->{meta}->{source}->{storage}->getListUnfiltered($self->{meta}->{source}->{node});
64 joko 1.2 my $list = $self->{meta}->{$descent}->{storage}->getListFiltered($self->{meta}->{$descent}->{accessorName}, $filter);
65 joko 1.1 #print Dumper($list);
66     return $list;
67     }
68    
69     sub _resolveNodeIdent {
70     my $self = shift;
71     my $descent = shift;
72    
73 joko 1.3 # trace
74     #print Dumper($self->{node}->{$descent});
75     #print Dumper($self);
76     #exit;
77    
78 joko 1.2 $logger->debug( __PACKAGE__ . "->_resolveNodeIdent( descent=$descent, accessorName=$self->{meta}->{$descent}->{accessorName} )" );
79 joko 1.1
80     # get to the payload
81     #my $item = $specifier->{item};
82     my $payload = $self->{node}->{$descent}->{payload};
83    
84     # resolve method to get to the id of the given item
85     # we use global metadata and the given descent for this task
86     #my $ident = $self->{$descent}->id($item);
87     #my $ident = $self->{meta}->{$descent}->{storage}->id($item);
88    
89 joko 1.3 # trace
90     #print Dumper($self->{meta}->{$descent});
91     #exit;
92    
93 joko 1.1 my $ident;
94     my $provider_method = $self->{meta}->{$descent}->{IdentProvider}->{method};
95     my $provider_arg = $self->{meta}->{$descent}->{IdentProvider}->{arg};
96    
97 joko 1.2 # trace
98     #print "provider_method: $provider_method", "\n";
99     #print "provider_arg: $provider_arg", "\n";
100     #print Dumper($payload);
101 joko 1.1
102     # resolve to ident
103     if (lc $provider_method eq 'property') {
104     $ident = $payload->{$provider_arg};
105    
106     } elsif (lc $provider_method eq 'storage_method') {
107     #$ident = $self->{meta}->{$descent}->{storage}->id($item);
108     $ident = $self->{meta}->{$descent}->{storage}->$provider_arg($payload);
109    
110     } elsif (lc $provider_method eq 'code') {
111     $ident = 'ac';
112    
113     }
114    
115     $self->{node}->{$descent}->{ident} = $ident;
116    
117     return 1 if $ident;
118    
119     }
120    
121     # TODO:
122     # this should be split up into...
123     # - a "_statNode" (should just touch the node to check for existance)
124     # - a "_loadNode" (should load node completely)
125     # - maybe additionally a "loadNodeProperty" (may specify properties to load)
126     # - introduce $self->{nodecache} for this purpose
127     # TODO:
128     # should we:
129     # - not pass ident in here but resolve it via "$descent"?
130     # - refactor this and stuff it with additional debug/error message
131     # - this = the way the implicit load mechanism works
132     sub _statloadNode {
133    
134     my $self = shift;
135     my $descent = shift;
136     my $ident = shift;
137     my $force = shift;
138    
139 joko 1.5 =pod
140     #print "isa: ", UNIVERSAL::isa($self->{meta}->{$descent}->{storage}), "\n";
141    
142     # this seems to be the first time we access this side,
143     # so just check (again) for a valid storage handle
144     if (! ref $self->{meta}->{$descent}->{storage}) {
145     $logger->critical( __PACKAGE__ . "->_statloadNode( descent=$descent ident=$ident ): Storage handle undefined!" );
146     return;
147     }
148     =cut
149    
150 joko 1.3 #$logger->debug( __PACKAGE__ . "->_statloadNode( descent=$descent ident=$ident )" );
151 joko 1.2
152 joko 1.1 # fetch entry to retrieve checksum from
153     # was:
154     if (!$self->{node}->{$descent} || $force) {
155     # is:
156     #if (!$self->{node}->{$descent}->{item} || $force) {
157    
158     if (!$ident) {
159     #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";
160     return;
161     }
162    
163     # patch for DBD::CSV
164     if ($ident && $ident eq 'Null') {
165     return;
166     }
167    
168 joko 1.2 #print Dumper($self->{meta});
169 joko 1.1
170     my $query = {
171 joko 1.2 node => $self->{meta}->{$descent}->{accessorName},
172 joko 1.1 subnodes => [qw( cs )],
173     criterias => [
174     { key => $self->{meta}->{$descent}->{IdentProvider}->{arg},
175     op => 'eq',
176     val => $ident },
177     ]
178     };
179    
180 joko 1.2 # trace
181     #print "query:", "\n";
182     #print Dumper($query);
183    
184     # send query and fetch first entry from result
185     my $result = $self->{meta}->{$descent}->{storage}->sendQuery($query);
186     my $entry = $result->getNextEntry();
187    
188     # trace
189     #print Dumper($entry);
190     #print "pers: " . $self->{meta}->{$descent}->{storage}->is_persistent($entry), "\n";
191     #my $state = $self->{meta}->{$descent}->{storage}->_fetch_object_state($entry, { name => 'TransactionHop' } );
192     #print Dumper($state);
193 joko 1.1
194 joko 1.2 # be informed about the status of the query
195     my $status = $result->getStatus();
196 joko 1.1
197     #print Dumper($status);
198    
199     # TODO: enhance error handling (store inside tc)
200     #if (!$row) {
201     # print "\n", "row error", "\n";
202     # next;
203     #}
204    
205     # these checks run before actually loading payload- and meta-data to node-container
206    
207     # 1st level - hard error
208     if ($status && $status->{err}) {
209     $logger->debug( __PACKAGE__ . "->_statloadNode (ident=\"$ident\") failed - hard error (that's ok): $status->{err}" );
210     return;
211     }
212    
213     # 2nd level - logical (empty/notfound) error
214     if (($status && $status->{empty}) || !$entry) {
215     $logger->debug( __PACKAGE__ . "->_statloadNode (ident=\"$ident\") failed - logical error (that's ok)" );
216     #print "no entry (logical)", "\n";
217     return;
218     }
219    
220     #print Dumper($entry);
221    
222     # was:
223     # $self->{node}->{$descent}->{ident} = $ident;
224     # is:
225     # TODO: re-resolve ident from entry via metadata "IdentProvider" here - like elsewhere
226     $self->{node}->{$descent}->{ident} = $ident;
227     $self->{node}->{$descent}->{payload} = $entry;
228    
229     }
230    
231     return 1;
232    
233 joko 1.2 }
234    
235    
236     sub _touchNodeSet {
237     my $self = shift;
238    
239     $logger->debug( __PACKAGE__ . "->touchNodeSet" );
240    
241     # check descents/nodes: does descent exist / is node available?
242     foreach my $descent (keys %{$self->{meta}}) {
243    
244     # 1. check metadata of descent(s)
245     if (!$self->{meta}->{$descent}) {
246     $logger->critical( __PACKAGE__ . "->touchNodeSet: Could not find descent '$descent' in configuration metadata." );
247     next;
248     }
249    
250     # 2. check storage handle(s)
251     my $dbkey = $self->{meta}->{$descent}->{dbKey};
252     if (!$self->{meta}->{$descent}->{storage}) {
253     $logger->critical( __PACKAGE__ . "->touchNodeSet: Could not access storage ( descent='$descent', dbKey='$dbkey' ) - configuration-error?" );
254     next;
255     }
256    
257 joko 1.5 # 2.b check storage handle type
258     my $dbType = $self->{meta}->{$descent}->{storage}->{locator}->{type};
259     if (!$dbType) {
260     $logger->critical( __PACKAGE__ . "->touchNodeSet: Storage ( descent='$descent', dbKey='$dbkey' ) has no 'dbType' - configuration-error?" );
261     next;
262     }
263    
264 joko 1.2 # 3. check if descents (and nodes?) are actually available....
265     # TODO:
266     # eventually pre-check mode of access-attempt (read/write) here to provide an "early-croak" if possible
267    
268     # trace
269     # print Dumper($self->{meta}->{$descent}->{storage}->{locator});
270    
271    
272     my $nodeName = $self->{meta}->{$descent}->{nodeName};
273     my $accessorType = $self->{meta}->{$descent}->{accessorType};
274     my $accessorName = $self->{meta}->{$descent}->{accessorName};
275    
276    
277     # 4. check nodeset
278    
279     # debug message containing database type and used/determined accessor name
280     $logger->debug( __PACKAGE__ . "->touchNodeSet: Accessing dbType=$dbType, accessorName=$accessorName" );
281    
282     # if target node(s) do(es) not exist, check if we should create it automagically
283     if ($dbType ne 'DBI' && !$self->{meta}->{$descent}->{storage}->existsChildNode($accessorName)) {
284    
285     if ($descent eq 'target' && $self->{options}->{target}->{autocreateFolders}) {
286     if (!$self->{meta}->{$descent}->{storage}->createChildNode($accessorName)) {
287     $logger->critical( __PACKAGE__ . "->touchNodeSet: Could not create node '$self->{meta}->{$descent}->{nodeName}\@$self->{meta}->{$descent}->{dbKey}' [$self->{meta}->{$descent}->{nodeType}]." );
288     next;
289     }
290     } else {
291     $logger->critical( __PACKAGE__ . "->touchNodeSet: Could not reach node \"$nodeName\" (accessorName=$accessorName, accessorType=$accessorType) at descent \"$descent\"" );
292     next;
293     }
294     }
295    
296     }
297    
298     # trace
299     #print Dumper($self->{meta});
300     #exit;
301    
302    
303     return 1;
304    
305     }
306    
307    
308    
309     sub _modifyNode {
310     my $self = shift;
311     my $descent = shift;
312     my $action = shift;
313     my $map = shift;
314     my $crit = shift;
315    
316     # map for new style callbacks
317     my $map_callbacks = {};
318    
319     # checks go first!
320    
321     # TODO: this should be reviewed first - before extending ;-)
322     # TODO: this should be extended:
323     # count this cases inside the caller to this sub and provide a better overall message
324     # if this counts still zero in the end:
325     # "No nodes have been touched for modify: Do you have column-headers in your csv file?"
326     if (not defined $self->{node}) {
327     #$logger->critical( __PACKAGE__ . "->_modifyNode failed: \"$descent\" node is empty." );
328     #return;
329     }
330    
331     # transfer callback nodes from value map to callback map - handle them afterwards! - (new style callbacks)
332     if (my $callbacks = $self->{meta}->{$descent}->{Callback}) {
333     foreach my $callback (keys %{$callbacks->{write}}) {
334     $map_callbacks->{write}->{$callback} = $map->{$callback};
335     delete $map->{$callback};
336     }
337     }
338    
339    
340     # trace
341     #print Dumper($self->{meta});
342    
343    
344     # --------------------------------------------------------------------------
345     # DBI speaks SQL
346     if ($self->{meta}->{$descent}->{storage}->{locator}->{type} eq 'DBI') {
347    
348     # trace
349     #print Dumper($map);
350     #delete $map->{cs};
351    
352     # transfer data
353     # TODO: wrap this around '$storageHandle->sendQuery(...)'!?
354     my $sql_main;
355     if (lc($action) eq 'insert') {
356     $sql_main = hash2Sql($self->{meta}->{$descent}->{accessorName}, $map, 'SQL_INSERT');
357     } elsif (lc $action eq 'update') {
358     $crit ||= "$self->{meta}->{$descent}->{IdentProvider}->{arg}='$self->{node}->{$descent}->{ident}'";
359     $sql_main = hash2Sql($self->{meta}->{$descent}->{accessorName}, $map, 'SQL_UPDATE', $crit);
360     }
361     my $sqlHandle = $self->{meta}->{$descent}->{storage}->sendCommand($sql_main);
362    
363     # handle errors
364     if ($sqlHandle->err) {
365     #if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; }
366     $self->{node}->{status}->{error} = {
367     statement => $sql_main,
368     state => $sqlHandle->state,
369     err => $sqlHandle->err,
370     errstr => $sqlHandle->errstr,
371     };
372     } else {
373     $self->{node}->{status}->{ok} = 1;
374     }
375    
376    
377     # --------------------------------------------------------------------------
378     # Tangram does it the oo-way (naturally)
379     } elsif ($self->{meta}->{$descent}->{storage}->{locator}->{type} eq 'Tangram') {
380     my $sql_main;
381     my $object;
382    
383     # determine classname
384     my $classname = $self->{meta}->{$descent}->{nodeType};
385    
386     # attributes/properties to exclude
387     # push declared ones from metadata
388     my @exclude = @{$self->{meta}->{$descent}->{subnodes_exclude}};
389     # push the attributes associated with the identifier
390     if (my $identProvider = $self->{meta}->{$descent}->{IdentProvider}) {
391     push @exclude, $identProvider->{arg};
392     }
393    
394     # trace
395     #print Dumper($self->{meta});
396     #exit;
397    
398     # new feature:
399     # - check TypeProvider metadata property from other side
400     # - use argument (arg) inside as a classname for object creation on this side
401     #my $otherSide = $self->_otherSide($descent);
402     if (my $typeProvider = $self->{meta}->{$descent}->{TypeProvider}) {
403     #print Dumper($map);
404     $classname = $map->{$typeProvider->{arg}};
405     # remove nodes from map also (push nodes to "subnodes_exclude" list)
406     push @exclude, $typeProvider->{arg};
407     }
408    
409     # exclude banned properties (remove from map)
410     #map { delete $self->{node}->{map}->{$_} } @{$self->{args}->{exclude}};
411     map { delete $map->{$_} } @exclude;
412    
413     # list of properties
414     my @props = keys %{$map};
415    
416     # transfer data
417     if (lc $action eq 'insert') {
418    
419     # make the object persistent in four steps:
420     # - raw create (perl / class tangram scope)
421     # - orm insert (tangram scope) ... this establishes inheritance - don't try to fill in inherited properties before!
422     # is this a Tangram bug?
423     # - raw fill-in from hash (perl scope)
424     # - orm update (tangram scope) ... this updates all properties just filled in
425    
426    
427     # ==========================
428     # TODO: REVIEW HERE!!!
429     # can't we achieve this more elegant?
430     # o use DesignPattern::Object???
431     # o use Hash::Merge!!! (take care about the cloning behaviour)
432    
433     # check if object exists (is classname a valid perl package/module?)
434    
435     # we can just check if the classname is valid here
436     if (!$classname) {
437     $logger->critical( __PACKAGE__ . "->_modifyNode: classname is undefined" );
438     # FIXME: stop syncing here?
439     return;
440     }
441    
442     # try to match against the classes known by Class::Tangram
443     # FIXME: do "/i" on win32 only!
444     my $classname_find = quotemeta($classname);
445     if (!grep(m/$classname_find/i, Class::Tangram::known_classes())) {
446     $logger->critical( __PACKAGE__ . "->_modifyNode: Classname '$classname' is not known by Class::Tangram" );
447     # FIXME: stop syncing here?
448     return;
449     }
450    
451     # create new object ...
452     # V1
453     # build array to initialize object
454     #my @initarray = ();
455     #map { push @initarray, $_, undef; } @props;
456     #my $object = $classname->new( @initarray );
457     # V2
458     $object = $classname->new();
459     # V3
460     # $object = DesignPattern::Object->new($data);
461    
462     # ... pass to orm first ...
463     $self->{meta}->{$descent}->{storage}->insert($object);
464    
465     # ... and initialize with empty (undef'd) properties afterwards.
466     map { $object->{$_} = undef; } @props;
467    
468     # trace
469     #print "\n";
470     #print Dumper($map);
471     #print Dumper($object);
472     #exit;
473    
474     # mix in (merge) values ...
475     # TODO: use Hash::Merge here? benchmark!
476     # no! we'd need a Object::Merge here! it's *...2object*
477 joko 1.6 merge_to($object, $map);
478 joko 1.2
479     # trace
480     #print Dumper($object);
481     #exit;
482    
483     # TODO: REVIEW HERE!!!
484     # ==========================
485    
486     # ... and re-update@orm.
487     $self->{meta}->{$descent}->{storage}->update($object);
488    
489     # asymmetry: get ident after insert
490     # TODO:
491     # - just do this if it is an IdentAuthority
492     # - use IdentProvider metadata here
493     #print Dumper($self->{meta}->{$descent});
494     my $oid = $self->{meta}->{$descent}->{storage}->id($object);
495     #print "oid: $oid", "\n";
496     $self->{node}->{$descent}->{ident} = $oid;
497    
498    
499     } elsif (lc $action eq 'update') {
500    
501     # get fresh object from orm first
502     $object = $self->{meta}->{$descent}->{storage}->load($self->{node}->{$descent}->{ident});
503    
504     #print Dumper($self->{node});
505    
506     # mix in values
507     #print Dumper($object);
508     # TODO: use Hash::Merge here???
509 joko 1.6 merge_to($object, $map);
510 joko 1.2 #print Dumper($object);
511     #exit;
512    
513     # update orm
514     $self->{meta}->{$descent}->{storage}->update($object);
515    
516     }
517    
518     #exit;
519    
520     my $error = 0;
521    
522     # handle new style callbacks - this is a HACK - do this without an eval!
523     #print Dumper($map);
524     #print "cb: ", Dumper($self->{meta}->{$descent}->{Callback});
525     #print Dumper($map_callbacks);
526     foreach my $node (keys %{$map_callbacks->{write}}) {
527     #print Dumper($node);
528 joko 1.4
529     # ------------ half-redundant: make $self->callCallback($object, $value, $opts)
530     my $perl_callback = $self->{meta}->{$descent}->{nodeType} . '::' . $node . '_write';
531 joko 1.2 my $evalstring = $perl_callback . '( { object => $object, value => $map_callbacks->{write}->{$node}, storage => $self->{meta}->{$descent}->{storage} } );';
532     #print $evalstring, "\n"; exit;
533     eval($evalstring);
534     if ($@) {
535     $error = 1;
536 joko 1.4 $logger->error( __PACKAGE__ . "->_modifyNode: $@" );
537     next;
538 joko 1.2 }
539 joko 1.4 # ------------ half-redundant: make $self->callCallback($object, $value, $opts)
540 joko 1.2
541     #print "after eval", "\n";
542    
543     if (!$error) {
544     # re-update@orm
545     $self->{meta}->{$descent}->{storage}->update($object);
546     }
547     }
548    
549     # handle errors
550     if ($error) {
551     #print "error", "\n";
552     =pod
553     my $sqlHandle;
554     #if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; }
555     $self->{node}->{status}->{error} = {
556     statement => $sql_main,
557     state => $sqlHandle->state,
558     err => $sqlHandle->err,
559     errstr => $sqlHandle->errstr,
560     };
561     =cut
562     # rollback....
563     #print "rollback", "\n";
564     $self->{meta}->{$descent}->{storage}->erase($object);
565     #print "after rollback", "\n";
566     } else {
567     $self->{node}->{status}->{ok} = 1;
568     }
569    
570     }
571    
572 joko 1.1 }
573    
574 joko 1.4 sub _erase_all {
575     my $self = shift;
576     my $descent = shift;
577     #my $node = shift;
578     #print Dumper($self->{meta}->{$descent});
579     #my $node = $self->{meta}->{$descent}->{nodeName};
580     my $node = $self->{meta}->{$descent}->{accessorName};
581     $logger->debug( __PACKAGE__ . "->_erase_all( node $node )" );
582     $self->{meta}->{$descent}->{storage}->eraseAll($node);
583     }
584 joko 1.1
585     1;
586 joko 1.4 __END__

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