//通过文件-传递代码进行块匹配
/*
* Block matching used by the file-transfer code.
*
* Copyright (C) 1996 Andrew Tridgell
* Copyright (C) 1996 Paul Mackerras
* Copyright (C) 2003-2009 Wayne Davison
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, visit the http://fsf.org website.
*/
#include "rsync.h"
extern int verbose;
extern int do_progress;
extern int checksum_seed;
extern int append_mode;
int updating_basis_file;
static int false_alarms;
static int hash_hits;
static int matches;
static int64 data_transfer;
static int total_false_alarms;
static int total_hash_hits;
static int total_matches;
extern struct stats stats;
#define TRADITIONAL_TABLESIZE (1<<16)
static uint32 tablesize;
static int32 *hash_table;
//哈希函数
#define SUM2HASH2(s1,s2) (((s1) + (s2)) & 0xFFFF)
#define SUM2HASH(sum) SUM2HASH2((sum)&0xFFFF,(sum)>>16)
#define BIG_SUM2HASH(sum) ((sum)%tablesize)
//建立哈希表
static void build_hash_table(struct sum_struct *s)
{
static uint32 alloc_size;
int32 i;
//动态计算哈希表的大小,使哈希表能够容纳80%的大文件,一个比传统大小大的数必须是技术,否则s2将不能跨越整个集合
/* Dynamically calculate the hash table size so that the hash load
* for big files is about 80%. A number greater than the traditional
* size must be odd or s2 will not be able to span the entire set. */
tablesize = (uint32)(s->count/8) * 10 + 11;
if (tablesize < TRADITIONAL_TABLESIZE)
tablesize = TRADITIONAL_TABLESIZE;
if (tablesize > alloc_size || tablesize < alloc_size - 16*1024) {
if (hash_table)
free(hash_table);
hash_table = new_array(int32, tablesize);
if (!hash_table)
out_of_memory("build_hash_table");
alloc_size = tablesize;
}
memset(hash_table, 0xFF, tablesize * sizeof hash_table[0]);
if (tablesize == TRADITIONAL_TABLESIZE) {
for (i = 0; i < s->count; i++) {
uint32 t = SUM2HASH(s->sums[i].sum1);
s->sums[i].chain = hash_table[t];
hash_table[t] = i;
}
} else {
for (i = 0; i < s->count; i++) {
uint32 t = BIG_SUM2HASH(
Rsync源码——校验块匹配(math.c)
最新推荐文章于 2021-09-10 12:57:29 发布
本文档详细介绍了Rsync源码中的块匹配算法,包括哈希函数、哈希表构建、匹配查找过程等。通过对文件进行校验和计算,建立哈希表以提高匹配效率,减少数据传输。在源码中,`build_hash_table`用于创建哈希表,`hash_search`进行块匹配搜索,`matched`则负责传输数据或匹配标记。
摘要由CSDN通过智能技术生成