borg/src/borg/_hashindex.c

468 lines
14 KiB
C
Raw Normal View History

2010-12-16 19:23:22 +00:00
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
2016-03-21 08:18:43 +00:00
#if defined (__SVR4) && defined (__sun)
#include <sys/isa_defs.h>
#endif
#if (defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)) || \
2017-01-21 14:04:01 +00:00
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) || \
(defined(_BIG_ENDIAN) && defined(__SVR4)&&defined(__sun))
#define BORG_BIG_ENDIAN 1
#elif (defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)) || \
(defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
(defined(_LITTLE_ENDIAN) && defined(__SVR4)&&defined(__sun))
#define BORG_BIG_ENDIAN 0
#else
#error Unknown byte order
#endif
#if BORG_BIG_ENDIAN
2014-01-29 20:34:21 +00:00
#define _le32toh(x) __builtin_bswap32(x)
#define _htole32(x) __builtin_bswap32(x)
#else
2014-01-29 20:34:21 +00:00
#define _le32toh(x) (x)
#define _htole32(x) (x)
#endif
2015-06-02 00:41:23 +00:00
#define MAGIC "BORG_IDX"
#define MAGIC_LEN 8
2010-12-16 19:23:22 +00:00
typedef struct {
2015-06-02 00:41:23 +00:00
char magic[MAGIC_LEN];
2010-12-16 19:23:22 +00:00
int32_t num_entries;
int32_t num_buckets;
int8_t key_size;
int8_t value_size;
} __attribute__((__packed__)) HashHeader;
2013-05-28 12:35:55 +00:00
typedef struct {
void *buckets;
int num_entries;
int num_buckets;
int key_size;
int value_size;
off_t bucket_size;
2013-07-03 10:19:16 +00:00
int lower_limit;
int upper_limit;
2013-05-28 12:35:55 +00:00
} HashIndex;
2010-12-16 19:23:22 +00:00
2016-01-14 02:56:12 +00:00
/* prime (or w/ big prime factors) hash table sizes
* not sure we need primes for borg's usage (as we have a hash function based
* on sha256, we can assume an even, seemingly random distribution of values),
* but OTOH primes don't harm.
* also, growth of the sizes starts with fast-growing 2x steps, but slows down
* more and more down to 1.1x. this is to avoid huge jumps in memory allocation,
* like e.g. 4G -> 8G.
* these values are generated by hash_sizes.py.
*/
static int hash_sizes[] = {
1031, 2053, 4099, 8209, 16411, 32771, 65537, 131101, 262147, 445649,
757607, 1287917, 2189459, 3065243, 4291319, 6007867, 8410991,
11775359, 16485527, 23079703, 27695653, 33234787, 39881729, 47858071,
57429683, 68915617, 82698751, 99238507, 119086189, 144378011, 157223263,
173476439, 190253911, 209915011, 230493629, 253169431, 278728861,
306647623, 337318939, 370742809, 408229973, 449387209, 493428073,
543105119, 596976533, 657794869, 722676499, 795815791, 874066969,
962279771, 1057701643, 1164002657, 1280003147, 1407800297, 1548442699,
1703765389, 1873768367, 2062383853, /* 32bit int ends about here */
};
#define HASH_MIN_LOAD .25
#define HASH_MAX_LOAD .75 /* don't go higher than 0.75, otherwise performance severely suffers! */
2013-07-03 10:19:16 +00:00
#define MAX(x, y) ((x) > (y) ? (x): (y))
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
#define EMPTY _htole32(0xffffffff)
#define DELETED _htole32(0xfffffffe)
#define BUCKET_ADDR(index, idx) (index->buckets + (idx * index->bucket_size))
2010-12-16 19:23:22 +00:00
#define BUCKET_MATCHES_KEY(index, idx, key) (memcmp(key, BUCKET_ADDR(index, idx), index->key_size) == 0)
#define BUCKET_IS_DELETED(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) == DELETED)
#define BUCKET_IS_EMPTY(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) == EMPTY)
2010-12-16 19:23:22 +00:00
#define BUCKET_MARK_DELETED(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) = DELETED)
#define BUCKET_MARK_EMPTY(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) = EMPTY)
2010-12-16 19:23:22 +00:00
#define EPRINTF_MSG(msg, ...) fprintf(stderr, "hashindex: " msg "\n", ##__VA_ARGS__)
#define EPRINTF_MSG_PATH(path, msg, ...) fprintf(stderr, "hashindex: %s: " msg "\n", path, ##__VA_ARGS__)
#define EPRINTF(msg, ...) fprintf(stderr, "hashindex: " msg "(%s)\n", ##__VA_ARGS__, strerror(errno))
#define EPRINTF_PATH(path, msg, ...) fprintf(stderr, "hashindex: %s: " msg " (%s)\n", path, ##__VA_ARGS__, strerror(errno))
2013-07-03 10:19:16 +00:00
static HashIndex *hashindex_read(const char *path);
static int hashindex_write(HashIndex *index, const char *path);
static HashIndex *hashindex_init(int capacity, int key_size, int value_size);
2013-05-28 12:35:55 +00:00
static const void *hashindex_get(HashIndex *index, const void *key);
2013-07-03 10:19:16 +00:00
static int hashindex_set(HashIndex *index, const void *key, const void *value);
static int hashindex_delete(HashIndex *index, const void *key);
2013-05-28 12:35:55 +00:00
static void *hashindex_next_key(HashIndex *index, const void *key);
2010-12-16 19:23:22 +00:00
/* Private API */
static void hashindex_free(HashIndex *index);
2010-12-16 19:23:22 +00:00
static int
hashindex_index(HashIndex *index, const void *key)
{
2014-01-29 20:34:21 +00:00
return _le32toh(*((uint32_t *)key)) % index->num_buckets;
2010-12-16 19:23:22 +00:00
}
static int
hashindex_lookup(HashIndex *index, const void *key, int *start_idx)
2010-12-16 19:23:22 +00:00
{
int didx = -1;
int start = hashindex_index(index, key);
int idx = start;
for(;;) {
2010-12-16 19:23:22 +00:00
if(BUCKET_IS_EMPTY(index, idx))
{
break;
2010-12-16 19:23:22 +00:00
}
if(BUCKET_IS_DELETED(index, idx)) {
if(didx == -1) {
didx = idx;
}
}
else if(BUCKET_MATCHES_KEY(index, idx, key)) {
if (didx != -1) {
memcpy(BUCKET_ADDR(index, didx), BUCKET_ADDR(index, idx), index->bucket_size);
2010-12-16 19:23:22 +00:00
BUCKET_MARK_DELETED(index, idx);
idx = didx;
}
return idx;
}
idx = (idx + 1) % index->num_buckets;
if(idx == start) {
break;
}
2010-12-16 19:23:22 +00:00
}
if (start_idx != NULL) {
(*start_idx) = (didx == -1) ? idx : didx;
}
return -1;
2010-12-16 19:23:22 +00:00
}
2013-07-03 10:19:16 +00:00
static int
2010-12-16 19:23:22 +00:00
hashindex_resize(HashIndex *index, int capacity)
{
HashIndex *new;
void *key = NULL;
int32_t key_size = index->key_size;
2013-07-03 10:19:16 +00:00
if(!(new = hashindex_init(capacity, key_size, index->value_size))) {
2013-07-03 10:19:16 +00:00
return 0;
}
2010-12-16 19:23:22 +00:00
while((key = hashindex_next_key(index, key))) {
if(!hashindex_set(new, key, key + key_size)) {
/* This can only happen if there's a bug in the code calculating capacity */
hashindex_free(new);
return 0;
}
2010-12-16 19:23:22 +00:00
}
free(index->buckets);
index->buckets = new->buckets;
2010-12-16 19:23:22 +00:00
index->num_buckets = new->num_buckets;
2013-07-03 10:19:16 +00:00
index->lower_limit = new->lower_limit;
index->upper_limit = new->upper_limit;
2010-12-16 19:23:22 +00:00
free(new);
return 1;
2010-12-16 19:23:22 +00:00
}
int get_lower_limit(int num_buckets){
int min_buckets = hash_sizes[0];
if (num_buckets <= min_buckets)
return 0;
return (int)(num_buckets * HASH_MIN_LOAD);
}
int get_upper_limit(int num_buckets){
int max_buckets = hash_sizes[NELEMS(hash_sizes) - 1];
if (num_buckets >= max_buckets)
return num_buckets;
return (int)(num_buckets * HASH_MAX_LOAD);
}
int size_idx(int size){
/* find the hash_sizes index with entry >= size */
int elems = NELEMS(hash_sizes);
int entry, i=0;
do{
entry = hash_sizes[i++];
}while((entry < size) && (i < elems));
if (i >= elems)
return elems - 1;
i--;
return i;
}
int fit_size(int current){
int i = size_idx(current);
return hash_sizes[i];
}
int grow_size(int current){
int i = size_idx(current) + 1;
int elems = NELEMS(hash_sizes);
if (i >= elems)
return hash_sizes[elems - 1];
return hash_sizes[i];
}
int shrink_size(int current){
int i = size_idx(current) - 1;
if (i < 0)
return hash_sizes[0];
return hash_sizes[i];
}
2010-12-16 19:23:22 +00:00
/* Public API */
2013-05-28 12:35:55 +00:00
static HashIndex *
hashindex_read(const char *path)
2010-12-16 19:23:22 +00:00
{
FILE *fd;
off_t length, buckets_length, bytes_read;
HashHeader header;
HashIndex *index = NULL;
if((fd = fopen(path, "rb")) == NULL) {
2015-05-31 15:48:19 +00:00
EPRINTF_PATH(path, "fopen for reading failed");
2010-12-16 19:23:22 +00:00
return NULL;
}
bytes_read = fread(&header, 1, sizeof(HashHeader), fd);
if(bytes_read != sizeof(HashHeader)) {
if(ferror(fd)) {
EPRINTF_PATH(path, "fread header failed (expected %ju, got %ju)",
(uintmax_t) sizeof(HashHeader), (uintmax_t) bytes_read);
}
else {
EPRINTF_MSG_PATH(path, "fread header failed (expected %ju, got %ju)",
(uintmax_t) sizeof(HashHeader), (uintmax_t) bytes_read);
}
goto fail;
}
if(fseek(fd, 0, SEEK_END) < 0) {
EPRINTF_PATH(path, "fseek failed");
goto fail;
2013-07-03 10:19:16 +00:00
}
if((length = ftell(fd)) < 0) {
EPRINTF_PATH(path, "ftell failed");
goto fail;
2013-07-03 10:19:16 +00:00
}
if(fseek(fd, sizeof(HashHeader), SEEK_SET) < 0) {
EPRINTF_PATH(path, "fseek failed");
goto fail;
}
2015-06-02 00:41:23 +00:00
if(memcmp(header.magic, MAGIC, MAGIC_LEN)) {
2015-05-31 15:48:19 +00:00
EPRINTF_MSG_PATH(path, "Unknown MAGIC in header");
goto fail;
2013-07-03 10:19:16 +00:00
}
buckets_length = (off_t)_le32toh(header.num_buckets) * (header.key_size + header.value_size);
if((size_t) length != sizeof(HashHeader) + buckets_length) {
EPRINTF_MSG_PATH(path, "Incorrect file length (expected %ju, got %ju)",
(uintmax_t) sizeof(HashHeader) + buckets_length, (uintmax_t) length);
goto fail;
2013-07-03 10:19:16 +00:00
}
if(!(index = malloc(sizeof(HashIndex)))) {
2015-05-31 15:48:19 +00:00
EPRINTF_PATH(path, "malloc header failed");
goto fail;
}
if(!(index->buckets = malloc(buckets_length))) {
2015-05-31 15:48:19 +00:00
EPRINTF_PATH(path, "malloc buckets failed");
free(index);
index = NULL;
goto fail;
2010-12-16 19:23:22 +00:00
}
bytes_read = fread(index->buckets, 1, buckets_length, fd);
if(bytes_read != buckets_length) {
if(ferror(fd)) {
EPRINTF_PATH(path, "fread buckets failed (expected %ju, got %ju)",
(uintmax_t) buckets_length, (uintmax_t) bytes_read);
}
else {
EPRINTF_MSG_PATH(path, "fread buckets failed (expected %ju, got %ju)",
(uintmax_t) buckets_length, (uintmax_t) bytes_read);
}
free(index->buckets);
free(index);
index = NULL;
goto fail;
}
index->num_entries = _le32toh(header.num_entries);
index->num_buckets = _le32toh(header.num_buckets);
index->key_size = header.key_size;
index->value_size = header.value_size;
2010-12-16 19:23:22 +00:00
index->bucket_size = index->key_size + index->value_size;
index->lower_limit = get_lower_limit(index->num_buckets);
index->upper_limit = get_upper_limit(index->num_buckets);
fail:
if(fclose(fd) < 0) {
EPRINTF_PATH(path, "fclose failed");
2013-07-03 10:19:16 +00:00
}
2010-12-16 19:23:22 +00:00
return index;
}
2013-05-28 12:35:55 +00:00
static HashIndex *
hashindex_init(int capacity, int key_size, int value_size)
2010-12-16 19:23:22 +00:00
{
HashIndex *index;
int i;
capacity = fit_size(capacity);
2013-07-03 10:19:16 +00:00
if(!(index = malloc(sizeof(HashIndex)))) {
2015-05-31 15:48:19 +00:00
EPRINTF("malloc header failed");
2010-12-16 19:23:22 +00:00
return NULL;
}
if(!(index->buckets = calloc(capacity, key_size + value_size))) {
2015-05-31 15:48:19 +00:00
EPRINTF("malloc buckets failed");
free(index);
2013-07-03 10:19:16 +00:00
return NULL;
2010-12-16 19:23:22 +00:00
}
index->num_entries = 0;
index->key_size = key_size;
index->value_size = value_size;
index->num_buckets = capacity;
index->bucket_size = index->key_size + index->value_size;
index->lower_limit = get_lower_limit(index->num_buckets);
index->upper_limit = get_upper_limit(index->num_buckets);
for(i = 0; i < capacity; i++) {
BUCKET_MARK_EMPTY(index, i);
2013-07-03 10:19:16 +00:00
}
return index;
2010-12-16 19:23:22 +00:00
}
static void
hashindex_free(HashIndex *index)
{
free(index->buckets);
free(index);
}
2013-07-03 10:19:16 +00:00
static int
hashindex_write(HashIndex *index, const char *path)
2010-12-16 19:23:22 +00:00
{
off_t buckets_length = (off_t)index->num_buckets * index->bucket_size;
FILE *fd;
HashHeader header = {
.magic = MAGIC,
.num_entries = _htole32(index->num_entries),
.num_buckets = _htole32(index->num_buckets),
.key_size = index->key_size,
.value_size = index->value_size
};
int ret = 1;
if((fd = fopen(path, "wb")) == NULL) {
2015-05-31 15:48:19 +00:00
EPRINTF_PATH(path, "fopen for writing failed");
2013-07-03 10:19:16 +00:00
return 0;
}
if(fwrite(&header, 1, sizeof(header), fd) != sizeof(header)) {
2015-05-31 15:48:19 +00:00
EPRINTF_PATH(path, "fwrite header failed");
ret = 0;
}
if(fwrite(index->buckets, 1, buckets_length, fd) != (size_t) buckets_length) {
2015-05-31 15:48:19 +00:00
EPRINTF_PATH(path, "fwrite buckets failed");
ret = 0;
2013-07-03 10:19:16 +00:00
}
if(fclose(fd) < 0) {
EPRINTF_PATH(path, "fclose failed");
2013-07-03 10:19:16 +00:00
}
return ret;
2010-12-16 19:23:22 +00:00
}
2013-05-28 12:35:55 +00:00
static const void *
2010-12-16 19:23:22 +00:00
hashindex_get(HashIndex *index, const void *key)
{
int idx = hashindex_lookup(index, key, NULL);
2010-12-16 19:23:22 +00:00
if(idx < 0) {
return NULL;
}
return BUCKET_ADDR(index, idx) + index->key_size;
2010-12-16 19:23:22 +00:00
}
2013-07-03 10:19:16 +00:00
static int
2010-12-16 19:23:22 +00:00
hashindex_set(HashIndex *index, const void *key, const void *value)
{
int start_idx;
int idx = hashindex_lookup(index, key, &start_idx);
2011-08-14 18:06:30 +00:00
uint8_t *ptr;
2010-12-16 19:23:22 +00:00
if(idx < 0)
{
2013-07-03 10:19:16 +00:00
if(index->num_entries > index->upper_limit) {
if(!hashindex_resize(index, grow_size(index->num_buckets))) {
2013-07-03 10:19:16 +00:00
return 0;
}
start_idx = hashindex_index(index, key);
2010-12-16 19:23:22 +00:00
}
idx = start_idx;
2010-12-16 19:23:22 +00:00
while(!BUCKET_IS_EMPTY(index, idx) && !BUCKET_IS_DELETED(index, idx)) {
idx = (idx + 1) % index->num_buckets;
}
ptr = BUCKET_ADDR(index, idx);
2011-08-14 18:06:30 +00:00
memcpy(ptr, key, index->key_size);
memcpy(ptr + index->key_size, value, index->value_size);
2010-12-16 19:23:22 +00:00
index->num_entries += 1;
}
else
{
memcpy(BUCKET_ADDR(index, idx) + index->key_size, value, index->value_size);
2010-12-16 19:23:22 +00:00
}
2013-07-03 10:19:16 +00:00
return 1;
2010-12-16 19:23:22 +00:00
}
2013-07-03 10:19:16 +00:00
static int
2010-12-16 19:23:22 +00:00
hashindex_delete(HashIndex *index, const void *key)
{
int idx = hashindex_lookup(index, key, NULL);
2010-12-16 19:23:22 +00:00
if (idx < 0) {
2013-07-03 10:19:16 +00:00
return 1;
2010-12-16 19:23:22 +00:00
}
BUCKET_MARK_DELETED(index, idx);
index->num_entries -= 1;
2013-07-03 10:19:16 +00:00
if(index->num_entries < index->lower_limit) {
if(!hashindex_resize(index, shrink_size(index->num_buckets))) {
2013-07-03 10:19:16 +00:00
return 0;
}
}
return 1;
2010-12-16 19:23:22 +00:00
}
2013-05-28 12:35:55 +00:00
static void *
2010-12-16 19:23:22 +00:00
hashindex_next_key(HashIndex *index, const void *key)
{
int idx = 0;
if(key) {
idx = 1 + (key - index->buckets) / index->bucket_size;
}
2013-07-03 10:19:16 +00:00
if (idx == index->num_buckets) {
2010-12-16 19:23:22 +00:00
return NULL;
2013-07-03 10:19:16 +00:00
}
2010-12-16 19:23:22 +00:00
while(BUCKET_IS_EMPTY(index, idx) || BUCKET_IS_DELETED(index, idx)) {
idx ++;
2013-07-03 10:19:16 +00:00
if (idx == index->num_buckets) {
2010-12-16 19:23:22 +00:00
return NULL;
2013-07-03 10:19:16 +00:00
}
2010-12-16 19:23:22 +00:00
}
return BUCKET_ADDR(index, idx);
2010-12-16 19:23:22 +00:00
}
2013-05-28 12:35:55 +00:00
static int
hashindex_len(HashIndex *index)
2010-12-16 19:23:22 +00:00
{
return index->num_entries;
}
static int
hashindex_size(HashIndex *index)
{
return sizeof(HashHeader) + index->num_buckets * index->bucket_size;
}