/[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.8 - (hide annotations)
Wed Apr 9 07:27:02 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.7: +13 -19 lines
minor update (just cosmetics)

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

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