dedup

data deduplication program
git clone git://git.2f30.org/dedup.git
Log | Files | Refs | README | LICENSE

commit f694a0bacfafdbbb2a8e5cd526ae27b28e929792
parent a855a36113a0833e57edaae139c3123bf392d310
Author: sin <sin@2f30.org>
Date:   Thu,  7 Mar 2019 10:28:11 +0000

Use blk_desc directly and remove cache_entry

Diffstat:
Mcache.c | 22+++++++++++-----------
Mdedup.c | 24+++++-------------------
Mdedup.h | 14+++-----------
3 files changed, 19 insertions(+), 41 deletions(-)

diff --git a/cache.c b/cache.c @@ -11,7 +11,7 @@ #include "tree.h" struct cache_node { - struct cache_entry ent; + struct blk_desc desc; RB_ENTRY(cache_node) e; }; RB_HEAD(cache_head, cache_node); @@ -25,7 +25,7 @@ cache_node_cmp(struct cache_node *e1, struct cache_node *e2) { int r; - r = memcmp(e1->ent.md, e2->ent.md, sizeof(e1->ent.md)); + r = memcmp(e1->desc.md, e2->desc.md, sizeof(e1->desc.md)); if (r > 0) return 1; else if (r < 0) @@ -36,14 +36,14 @@ static RB_PROTOTYPE(cache_head, cache_node, e, cache_node_cmp); static RB_GENERATE(cache_head, cache_node, e, cache_node_cmp); static struct cache_node * -alloc_cache_node(struct cache_entry *ent) +alloc_cache_node(struct blk_desc *desc) { struct cache_node *node; node = calloc(1, sizeof(*node)); if (node == NULL) err(1, "calloc"); - node->ent = *ent; + node->desc = *desc; return node; } @@ -78,24 +78,24 @@ free_cache(struct cache *cache) } void -add_cache_entry(struct cache *cache, struct cache_entry *ent) +add_cache_entry(struct cache *cache, struct blk_desc *desc) { struct cache_node *node; - node = alloc_cache_node(ent); + node = alloc_cache_node(desc); if (RB_INSERT(cache_head, &cache->nodes, node) != NULL) free_cache_node(node); } int -lookup_cache_entry(struct cache *cache, struct cache_entry *ent) +lookup_cache_entry(struct cache *cache, struct blk_desc *desc) { struct cache_node *node, key; - key.ent = *ent; + key.desc = *desc; node = RB_FIND(cache_head, &cache->nodes, &key); if (node != NULL) { - *ent = node->ent; + *desc = node->desc; return 0; } return -1; @@ -103,10 +103,10 @@ lookup_cache_entry(struct cache *cache, struct cache_entry *ent) void walk_cache(struct cache *cache, - int (*fn)(struct cache_entry *, void *), void *arg) + int (*fn)(struct blk_desc *, void *), void *arg) { struct cache_node *node; RB_FOREACH(node, cache_head, &cache->nodes) - (*fn)(&node->ent, arg); + (*fn)(&node->desc, arg); } diff --git a/dedup.c b/dedup.c @@ -225,7 +225,7 @@ static void dedup_chunk(struct snapshot *snap, uint8_t *chunkp, size_t chunk_size) { uint8_t md[MDSIZE]; - struct cache_entry cache_entry; + struct blk_desc blk_desc; uint8_t *compr_buf; size_t n; @@ -237,20 +237,15 @@ dedup_chunk(struct snapshot *snap, uint8_t *chunkp, size_t chunk_size) snap_hdr.st.orig_size += chunk_size; snap_hdr.st.compr_size += n; - memcpy(cache_entry.md, md, sizeof(cache_entry.md)); - if (lookup_cache_entry(cache, &cache_entry) < 0) { - struct blk_desc blk_desc; - - memcpy(&blk_desc.md, md, sizeof(blk_desc.md)); + memcpy(blk_desc.md, md, sizeof(blk_desc.md)); + if (lookup_cache_entry(cache, &blk_desc) < 0) { blk_desc.offset = blk_hdr.size; blk_desc.size = n; snap->blk_desc[snap->nr_blk_descs++] = blk_desc; append_blk(compr_buf, &blk_desc); - cache_entry.offset = blk_desc.offset; - cache_entry.size = blk_desc.size; - add_cache_entry(cache, &cache_entry); + add_cache_entry(cache, &blk_desc); cache_misses++; snap_hdr.st.dedup_size += blk_desc.size; @@ -261,11 +256,6 @@ dedup_chunk(struct snapshot *snap, uint8_t *chunkp, size_t chunk_size) if (blk_desc.size < snap_hdr.st.min_blk_size) snap_hdr.st.min_blk_size = blk_desc.size; } else { - struct blk_desc blk_desc; - - memcpy(&blk_desc.md, cache_entry.md, sizeof(blk_desc.md)); - blk_desc.offset = cache_entry.offset; - blk_desc.size = cache_entry.size; snap->blk_desc[snap->nr_blk_descs++] = blk_desc; cache_hits++; } @@ -400,14 +390,10 @@ build_cache(struct snapshot *snap, void *arg) buf = alloc_buf(compr_size(BLKSIZE_MAX)); for (i = 0; i < snap->nr_blk_descs; i++) { - struct cache_entry cache_entry; struct blk_desc *blk_desc; blk_desc = &snap->blk_desc[i]; - memcpy(cache_entry.md, blk_desc->md, sizeof(cache_entry.md)); - cache_entry.offset = blk_desc->offset; - cache_entry.size = blk_desc->size; - add_cache_entry(cache, &cache_entry); + add_cache_entry(cache, blk_desc); } free(buf); return WALK_CONTINUE; diff --git a/dedup.h b/dedup.h @@ -64,12 +64,6 @@ struct snapshot { struct blk_desc blk_desc[]; }; -struct cache_entry { - uint8_t md[MDSIZE]; /* hash of block */ - uint64_t offset; /* offset into store file */ - uint64_t size; /* size of block */ -}; - /* config.c */ extern int compr_enabled; @@ -79,10 +73,10 @@ extern int verbose; /* cache.c */ struct cache *alloc_cache(void); void free_cache(struct cache *cache); -void add_cache_entry(struct cache *cache, struct cache_entry *ent); -int lookup_cache_entry(struct cache *cache, struct cache_entry *ent); +void add_cache_entry(struct cache *cache, struct blk_desc *desc); +int lookup_cache_entry(struct cache *cache, struct blk_desc *desc); void walk_cache(struct cache *cache, - int (*fn)(struct cache_entry *, void *), void *arg); + int (*fn)(struct blk_desc *, void *), void *arg); /* chunker.c */ struct chunker *alloc_chunker(int fd, size_t cap); @@ -108,8 +102,6 @@ void read_snapshot(int fd, struct snapshot *snap); void read_snapshot_descs(int fd, struct snapshot *snap); void write_snapshot(int fd, struct snapshot *snap); void write_snapshot_blk_descs(int fd, struct snapshot *snap); -void read_cache_entry(int fd, struct cache_entry *cache_entry); -void write_cache_entry(int fd, struct cache_entry *cache_entry); /* utils.c */ void str2bin(char *s, uint8_t *d);