/[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.2 - (hide annotations)
Sun Feb 9 05:05:58 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.1: +367 -21 lines
+ major structure changes
- refactored code to sister modules
+ refactored code to this place

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

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