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

Contents of /nfo/perl/libs/Data/Transfer/Sync/Compare/Checksum.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Tue May 13 08:19:00 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.2: +32 -12 lines
switched to crc32

1 ## $Id: Checksum.pm,v 1.2 2003/02/11 09:53:07 joko Exp $
2 ##
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 ## $Log: Checksum.pm,v $
9 ## Revision 1.2 2003/02/11 09:53:07 joko
10 ## + metadata-structure-change, fixed some code here
11 ##
12 ## Revision 1.1 2003/02/09 05:10:13 joko
13 ## + initial commit
14 ##
15 ## ----------------------------------------------------------------------------------------
16
17
18 =pod
19
20 =head1 Todo
21
22 o Data::Transfer::Sync::Compare::Slot using Compare::Struct
23
24 =cut
25
26 package Data::Transfer::Sync::Compare::Checksum;
27
28 use strict;
29 use warnings;
30
31 use mixin::with qw( Data::Transfer::Sync );
32
33
34 use Data::Dumper;
35
36 # TODO: Load these appropriatly at runtime.
37 use Digest::MD5 qw( md5 md5_hex md5_base64 );
38 use String::CRC32;
39
40 # get logger instance
41 my $logger = Log::Dispatch::Config->instance;
42
43
44 # Maybe refactor to shortcuts::checksum?
45 sub _calcChecksum {
46
47 my $self = shift;
48 my $descent = shift;
49 my $specifier = shift;
50
51 # calculate checksum for current object
52 my $ident = $self->{node}->{$descent}->{ident};
53
54 # build dump of this node
55 my $payload = $self->{node}->{$descent}->{payload};
56 #my $dump = $ident . "\n" . $item->quickdump();
57 #my $dump = $ident . "\n" . Dumper($item);
58 my $dump = $ident . "\n" . $self->_dumpCompact($payload);
59
60 # TODO: $logger->dump( ... );
61 #$logger->debug( __PACKAGE__ . ": " . $dump );
62 #$logger->dump( __PACKAGE__ . ": " . $dump );
63
64 # calculate checksum from dump
65
66 # 1. md5-based fingerprint, base64 encoded (from Digest::MD5)
67 #$self->{node}->{$descent}->{checksum} = md5_base64($dump) . '==';
68
69 # 2. 32-bit integer "hash" value (maybe faster?) (from DBI)
70 # Note: The 32-bit integer hash from DBI seems to generate duplicates
71 # with small payloads already in ranges of hundreds of items/rows!!!
72 # Try to avoid it or try to use it only for payloads greater than, hmmm, let's say 30 chars?
73 # (we had about 15 chars average per item (row))
74 # Possible (generic) solution: Just generate checksum, if length(checksum(payload)) < payload
75 #$self->{node}->{$descent}->{checksum} = DBI::hash($dump, 1);
76
77 # 3. good ol' crc32???
78 $self->{node}->{$descent}->{checksum} = crc32($dump);
79
80 # 4. some more modern Digest::SHA1 or similar?
81
82 # signal good
83 return 1;
84
85 }
86
87
88 sub _readChecksum {
89 my $self = shift;
90
91 my $descent = shift;
92
93 # signal checksum bad
94 if (!$self->{node}->{$descent}) {
95 return;
96 }
97
98 # trace
99 #print "desc: $descent", "\n";
100 #print Dumper($self);
101 #print Dumper($self->{meta}->{$descent});
102 #exit;
103
104 # get checksum for current entry
105 # TODO: don't have the checksum column/property hardcoded as "cs" here, make this configurable somehow
106 if ($self->{meta}->{$descent}->{isChecksumAuthority}) {
107 $self->_calcChecksum($descent);
108 } else {
109 $self->{node}->{$descent}->{checksum} = $self->{node}->{$descent}->{payload}->{cs};
110 }
111
112 # signal checksum good
113 return 1;
114
115 }
116
117
118 1;
119 __END__

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