1 : /* Copyright (C) 2006 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2 :
3 : This program is free software; you can redistribute it and/or modify
4 : it under the terms of the GNU General Public License as published by
5 : the Free Software Foundation; version 2 of the License.
6 :
7 : This program is distributed in the hope that it will be useful,
8 : but WITHOUT ANY WARRANTY; without even the implied warranty of
9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 : GNU General Public License for more details.
11 :
12 : You should have received a copy of the GNU General Public License
13 : along with this program; if not, write to the Free Software
14 : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
15 :
16 : /* Calculate a checksum for a row */
17 :
18 : #include "maria_def.h"
19 :
20 : /**
21 : Calculate a checksum for the record
22 :
23 : _ma_checksum()
24 : @param info Maria handler
25 : @param record Record
26 :
27 : @note
28 : To ensure that the checksum is independent of the row format
29 : we need to always calculate the checksum in the original field order.
30 :
31 : @return checksum
32 : */
33 :
34 : ha_checksum _ma_checksum(MARIA_HA *info, const uchar *record)
35 479291 : {
36 479291 : ha_checksum crc=0;
37 : uint i,end;
38 479291 : MARIA_COLUMNDEF *base_column= info->s->columndef;
39 479291 : uint16 *column_nr= info->s->column_nr;
40 :
41 479291 : if (info->s->base.null_bytes)
42 35311 : crc= my_checksum(crc, record, info->s->base.null_bytes);
43 :
44 3360167 : for (i= 0, end= info->s->base.fields ; i < end ; i++)
45 : {
46 2880876 : MARIA_COLUMNDEF *column= base_column + column_nr[i];
47 : const uchar *pos;
48 : ulong length;
49 :
50 2880876 : if (record[column->null_pos] & column->null_bit)
51 2879029 : continue; /* Null field */
52 :
53 2879029 : pos= record + column->offset;
54 2879029 : switch (column->type) {
55 : case FIELD_BLOB:
56 : {
57 165776 : uint blob_size_length= column->length- portable_sizeof_char_ptr;
58 165776 : length= _ma_calc_blob_length(blob_size_length, pos);
59 165776 : if (length)
60 : {
61 37453 : memcpy((char*) &pos, pos + blob_size_length, sizeof(char*));
62 37453 : crc= my_checksum(crc, pos, length);
63 : }
64 : continue;
65 : }
66 : case FIELD_VARCHAR:
67 : {
68 2604 : uint pack_length= column->fill_length;
69 2604 : if (pack_length == 1)
70 1953 : length= (ulong) *pos;
71 : else
72 651 : length= uint2korr(pos);
73 2604 : pos+= pack_length; /* Skip length information */
74 2604 : break;
75 : }
76 : default:
77 2710649 : length= column->length;
78 : break;
79 : }
80 2713253 : crc= my_checksum(crc, pos, length);
81 : }
82 479291 : return crc;
83 : }
84 :
85 :
86 : ha_checksum _ma_static_checksum(MARIA_HA *info, const uchar *pos)
87 5821 : {
88 5821 : return my_checksum(0, pos, info->s->base.reclength);
89 : }
|