Sync/Advanced.pm untested

filter: Statementstmt n/a Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a SCAR -
1 #########################################################################
2 # This file contains POD only - it's the advanced usage documentation
3 #
4 #
5 #########################################################################
6 package Data::Sync::Advanced;
7  
8 =pod
9  
10 =head1 NAME
11  
12 Data::Sync - A simple metadirectory/datapump module (advanced usage)
13  
14  
15 =head1 DESCRIPTION
16  
17 The basic functionality of Data::Sync is described in the main POD documentation. This documentation details more complex or rarely used functionality. You may have a requirement to customise Data::Sync by subclassing it, or you may wish to have more granular control over data flow for example. Or you may just want to use individual functions of the module as a sort of toolkit. If any of these are the case, you're reading the right document
18  
19
20 =head1 EXTRA METHODS
21  
22 =head2 get
23  
24 my $AoH = $sync->get();
25  
26 This function triggers the read function defined in the Data::Sync constructor, and returns the resulting data as an array of hashes (see perlref if you are unfamilar with how to access this kind of data structure).
27
28 =head2 put
29  
30 my $result = $sync->put($AoH);
31  
32 This function writes the content of $AoH (an array of hashes) to the target defined in the Data::Sync constructor, and returns the result (1 for success).
33  
34 =head2 hashrecord
35  
36 my $hash = $sync->hashrecord($hashref,$arrayref);
37  
38 This function is used internally by Data::Sync to detect changes in records (for minimising writes), but is also accessible for other uses. It takes a hashref of the db record, and an arrayref of attributes to include in the hash. It returns a hex representation of the MD5 hash of the concatenated record values. You can also call it with
39  
40 my $hash = Data::Sync->hashrecord($hashref,$arrayref);
41
42 (i.e. it does not depend on values set in the object constructor).
43  
44 =head2 remap
45  
46 my $remapped = $sync->remap($AoH)
47
48 Remaps the field names, as defined in $sync->mappings. Takes array of hashes, returns array of hashes with the hash keys renamed.
49  
50 =head2 runtransform
51  
52 my $transformed = $sync->runtransform($AoH);
53
54 Runs the transformations defined in $sync->transform. Takes array of hashes, returns array of hashes. You can use this function in conjunction with mappings as a utility function to perform recursive transformations of data:
55  
56 my $sync = Data::Sync->new();
57 $sync->transform(name=>lowercase);
58 my $transformed = $sync->runtransform($AoH);
59
60 will recurse through a data structure (an array of arrays, array of hashes, array of hashes of arrays of hashes etc) of arbitrary depth, performing the transform on all hash values where the key matches (including every value of hash values containing anonymous arrays).
61  
62 =head2 makebuiltattributes
63  
64 my $transformed = $sync->makebuiltattributes($AoH);
65
66 Create attributes in the data set as defined in buildattributes().
67  
68 =head2 scanhashtable
69  
70 my $hashed = $self->scanhashtable($AoH);
71
72 Check the dataset against the stored hashtable, to filter out unchanged records. Returns a dataset with unchanged records removed. Requires that hashattributes has been set in the target definition.
73  
74 =head2 hashrecord
75  
76 my $hash = $self->hashrecord(\%record,[ATTRIB,ATTRIB]);
77
78 returns a hexdigest MD5 hash of the attributes in the record.
79  
80 =head2 getdeletes
81  
82 my @deletes = $self->getdeletes();
83
84 returns a list of TARGETINDEX=>value entries for all entries that have been deleted. This is a function of hashing, so will only detect deletes if the following steps are followed (the run method does this):
85  
86 1) read from source
87 2) transform etc
88 3) hash the entries
89
90 Note that you don't need to write for deletes to be detected - the hashing of entries is done by scanhashtable before the write to target function is called. This means that using get() and scanhashtable() you can set the deltas to the current state without performing a write.
91  
92 =head1 batch update mode
93  
94 $sync->source($handle,{ batchsize=>x,
95 controls=>[$ldapcontrol] } );
96  
97 Batch update mode is only used in the standard (i.e. $sync->run based) usage of Data::Sync. Its primarily intended to handle asynchronous, persistent/paged LDAP searches, or SQL database queries where you want to see the updates as quickly as possible (without necessarily waiting for them all to complete). For details on how to construct persistent & asynchronous LDAP controls, see Net::LDAP::Control. This will read a batch from the handle, perform the operation, read the next batch from the handle, and so on. Note that with a DBI handle, this will still be working against an entire record set matching your criteria, so the memory advantages are limited.
98  
99 =head1 subclassing Data::Sync
100  
101 For whatever reason, you may want to subclass Data::Sync - perhaps to implement RDBMS specific read or write functions, or to add new functionality. Data::Sync is (or should be) subclass safe. The main functions you are likely to want to overload are:
102  
103 readdbi
104 writedbi
105 readldap
106 writeldap
107  
108 each will be passed the following parameters in sequential order:
109  
110 self
111 db/ldap handle
112 anonymous hash of parameters (see 'source' and 'target' methods.
113
114 and called by get/put or run().
115  
116 You might also want to overload ::run with your own variant. ::run calls the read, remap, transform and writes methods sequentially (If you look at ::run, it also calls sourceToAoH - this is a vital call to convert a DBI or LDAP handle into an array of hashes record set, before remapping and transforming the records).
117  
118 You may also wish to use a database other than SQLite to hash records, in which case you need to overload getdeletes() and scanhashtable(). See the code for more details.
119  
120 =head1 EXAMPLE USES
121  
122 my $source1 = Data::Sync->new();
123 $source1->source($dbhandle,"select * from sourcetable1");
124 $source1->mappings(NAME=>NEWNAME);
125 $source1->transform(NEWNAME=>lowercase);
126
127 my $source2->new();
128 $source2->source($dbhandle2,"select * from sourcetable2");
129 $source2->mappings(FULLNAME=>NEWNAME);
130 $source2->transform(NEWNAME=>lowercase);
131
132 my $target=Data::Sync->new();
133 $target->target($dbhandle3,{index=>NEWNAME});
134
135 my $set1 = $source1->get();
136 $set1 = $source1->remap($set1);
137 $set1 = $source1->runtransform($set1);
138
139 my $set2 = $source2->get();
140 $set2 = $source2->remap($set2);
141 $set2 = $source2->runtransform($set2);
142
143 my @recordset = (@$set1,@$set2);
144 $target->put(\@recordset);
145
146 Would read the two defined sources, remap & transform the contents, join the two datasets together, and write them both to the target.