2
0

_hashindex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #if defined (__SVR4) && defined (__sun)
  11. #include <sys/isa_defs.h>
  12. #endif
  13. #if (defined(BYTE_ORDER)&&(BYTE_ORDER == BIG_ENDIAN)) || \
  14. (defined(_BIG_ENDIAN)&&defined(__SVR4)&&defined(__sun))
  15. #define _le32toh(x) __builtin_bswap32(x)
  16. #define _htole32(x) __builtin_bswap32(x)
  17. #elif (defined(BYTE_ORDER)&&(BYTE_ORDER == LITTLE_ENDIAN)) || \
  18. (defined(_LITTLE_ENDIAN)&&defined(__SVR4)&&defined(__sun))
  19. #define _le32toh(x) (x)
  20. #define _htole32(x) (x)
  21. #else
  22. #error Unknown byte order
  23. #endif
  24. #define MAGIC "BORG_IDX"
  25. #define MAGIC_LEN 8
  26. typedef struct {
  27. char magic[MAGIC_LEN];
  28. int32_t num_entries;
  29. int32_t num_buckets;
  30. int8_t key_size;
  31. int8_t value_size;
  32. } __attribute__((__packed__)) HashHeader;
  33. typedef struct {
  34. void *buckets;
  35. int num_entries;
  36. int num_buckets;
  37. int key_size;
  38. int value_size;
  39. off_t bucket_size;
  40. int lower_limit;
  41. int upper_limit;
  42. } HashIndex;
  43. /* prime (or w/ big prime factors) hash table sizes
  44. * not sure we need primes for borg's usage (as we have a hash function based
  45. * on sha256, we can assume an even, seemingly random distribution of values),
  46. * but OTOH primes don't harm.
  47. * also, growth of the sizes starts with fast-growing 2x steps, but slows down
  48. * more and more down to 1.1x. this is to avoid huge jumps in memory allocation,
  49. * like e.g. 4G -> 8G.
  50. * these values are generated by hash_sizes.py.
  51. */
  52. static int hash_sizes[] = {
  53. 1031, 2053, 4099, 8209, 16411, 32771, 65537, 131101, 262147, 445649,
  54. 757607, 1287917, 2189459, 3065243, 4291319, 6007867, 8410991,
  55. 11775359, 16485527, 23079703, 27695653, 33234787, 39881729, 47858071,
  56. 57429683, 68915617, 82698751, 99238507, 119086189, 144378011, 157223263,
  57. 173476439, 190253911, 209915011, 230493629, 253169431, 278728861,
  58. 306647623, 337318939, 370742809, 408229973, 449387209, 493428073,
  59. 543105119, 596976533, 657794869, 722676499, 795815791, 874066969,
  60. 962279771, 1057701643, 1164002657, 1280003147, 1407800297, 1548442699,
  61. 1703765389, 1873768367, 2062383853, /* 32bit int ends about here */
  62. };
  63. #define HASH_MIN_LOAD .25
  64. #define HASH_MAX_LOAD .75 /* don't go higher than 0.75, otherwise performance severely suffers! */
  65. #define MAX(x, y) ((x) > (y) ? (x): (y))
  66. #define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
  67. #define EMPTY _htole32(0xffffffff)
  68. #define DELETED _htole32(0xfffffffe)
  69. #define BUCKET_ADDR(index, idx) (index->buckets + (idx * index->bucket_size))
  70. #define BUCKET_MATCHES_KEY(index, idx, key) (memcmp(key, BUCKET_ADDR(index, idx), index->key_size) == 0)
  71. #define BUCKET_IS_DELETED(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) == DELETED)
  72. #define BUCKET_IS_EMPTY(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) == EMPTY)
  73. #define BUCKET_MARK_DELETED(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) = DELETED)
  74. #define BUCKET_MARK_EMPTY(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) = EMPTY)
  75. #define EPRINTF_MSG(msg, ...) fprintf(stderr, "hashindex: " msg "\n", ##__VA_ARGS__)
  76. #define EPRINTF_MSG_PATH(path, msg, ...) fprintf(stderr, "hashindex: %s: " msg "\n", path, ##__VA_ARGS__)
  77. #define EPRINTF(msg, ...) fprintf(stderr, "hashindex: " msg "(%s)\n", ##__VA_ARGS__, strerror(errno))
  78. #define EPRINTF_PATH(path, msg, ...) fprintf(stderr, "hashindex: %s: " msg " (%s)\n", path, ##__VA_ARGS__, strerror(errno))
  79. static HashIndex *hashindex_read(const char *path);
  80. static int hashindex_write(HashIndex *index, const char *path);
  81. static HashIndex *hashindex_init(int capacity, int key_size, int value_size);
  82. static const void *hashindex_get(HashIndex *index, const void *key);
  83. static int hashindex_set(HashIndex *index, const void *key, const void *value);
  84. static int hashindex_delete(HashIndex *index, const void *key);
  85. static void *hashindex_next_key(HashIndex *index, const void *key);
  86. /* Private API */
  87. static void hashindex_free(HashIndex *index);
  88. static int
  89. hashindex_index(HashIndex *index, const void *key)
  90. {
  91. return _le32toh(*((uint32_t *)key)) % index->num_buckets;
  92. }
  93. static int
  94. hashindex_lookup(HashIndex *index, const void *key)
  95. {
  96. int didx = -1;
  97. int start = hashindex_index(index, key);
  98. int idx = start;
  99. for(;;) {
  100. if(BUCKET_IS_EMPTY(index, idx))
  101. {
  102. return -1;
  103. }
  104. if(BUCKET_IS_DELETED(index, idx)) {
  105. if(didx == -1) {
  106. didx = idx;
  107. }
  108. }
  109. else if(BUCKET_MATCHES_KEY(index, idx, key)) {
  110. if (didx != -1) {
  111. memcpy(BUCKET_ADDR(index, didx), BUCKET_ADDR(index, idx), index->bucket_size);
  112. BUCKET_MARK_DELETED(index, idx);
  113. idx = didx;
  114. }
  115. return idx;
  116. }
  117. idx = (idx + 1) % index->num_buckets;
  118. if(idx == start) {
  119. return -1;
  120. }
  121. }
  122. }
  123. static int
  124. hashindex_resize(HashIndex *index, int capacity)
  125. {
  126. HashIndex *new;
  127. void *key = NULL;
  128. int32_t key_size = index->key_size;
  129. if(!(new = hashindex_init(capacity, key_size, index->value_size))) {
  130. return 0;
  131. }
  132. while((key = hashindex_next_key(index, key))) {
  133. if(!hashindex_set(new, key, key + key_size)) {
  134. /* This can only happen if there's a bug in the code calculating capacity */
  135. hashindex_free(new);
  136. return 0;
  137. }
  138. }
  139. free(index->buckets);
  140. index->buckets = new->buckets;
  141. index->num_buckets = new->num_buckets;
  142. index->lower_limit = new->lower_limit;
  143. index->upper_limit = new->upper_limit;
  144. free(new);
  145. return 1;
  146. }
  147. int get_lower_limit(int num_buckets){
  148. int min_buckets = hash_sizes[0];
  149. if (num_buckets <= min_buckets)
  150. return 0;
  151. return (int)(num_buckets * HASH_MIN_LOAD);
  152. }
  153. int get_upper_limit(int num_buckets){
  154. int max_buckets = hash_sizes[NELEMS(hash_sizes) - 1];
  155. if (num_buckets >= max_buckets)
  156. return num_buckets;
  157. return (int)(num_buckets * HASH_MAX_LOAD);
  158. }
  159. int size_idx(int size){
  160. /* find the hash_sizes index with entry >= size */
  161. int elems = NELEMS(hash_sizes);
  162. int entry, i=0;
  163. do{
  164. entry = hash_sizes[i++];
  165. }while((entry < size) && (i < elems));
  166. if (i >= elems)
  167. return elems - 1;
  168. i--;
  169. return i;
  170. }
  171. int fit_size(int current){
  172. int i = size_idx(current);
  173. return hash_sizes[i];
  174. }
  175. int grow_size(int current){
  176. int i = size_idx(current) + 1;
  177. int elems = NELEMS(hash_sizes);
  178. if (i >= elems)
  179. return hash_sizes[elems - 1];
  180. return hash_sizes[i];
  181. }
  182. int shrink_size(int current){
  183. int i = size_idx(current) - 1;
  184. if (i < 0)
  185. return hash_sizes[0];
  186. return hash_sizes[i];
  187. }
  188. /* Public API */
  189. static HashIndex *
  190. hashindex_read(const char *path)
  191. {
  192. FILE *fd;
  193. off_t length, buckets_length, bytes_read;
  194. HashHeader header;
  195. HashIndex *index = NULL;
  196. if((fd = fopen(path, "rb")) == NULL) {
  197. EPRINTF_PATH(path, "fopen for reading failed");
  198. return NULL;
  199. }
  200. bytes_read = fread(&header, 1, sizeof(HashHeader), fd);
  201. if(bytes_read != sizeof(HashHeader)) {
  202. if(ferror(fd)) {
  203. EPRINTF_PATH(path, "fread header failed (expected %ju, got %ju)",
  204. (uintmax_t) sizeof(HashHeader), (uintmax_t) bytes_read);
  205. }
  206. else {
  207. EPRINTF_MSG_PATH(path, "fread header failed (expected %ju, got %ju)",
  208. (uintmax_t) sizeof(HashHeader), (uintmax_t) bytes_read);
  209. }
  210. goto fail;
  211. }
  212. if(fseek(fd, 0, SEEK_END) < 0) {
  213. EPRINTF_PATH(path, "fseek failed");
  214. goto fail;
  215. }
  216. if((length = ftell(fd)) < 0) {
  217. EPRINTF_PATH(path, "ftell failed");
  218. goto fail;
  219. }
  220. if(fseek(fd, sizeof(HashHeader), SEEK_SET) < 0) {
  221. EPRINTF_PATH(path, "fseek failed");
  222. goto fail;
  223. }
  224. if(memcmp(header.magic, MAGIC, MAGIC_LEN)) {
  225. EPRINTF_MSG_PATH(path, "Unknown MAGIC in header");
  226. goto fail;
  227. }
  228. buckets_length = (off_t)_le32toh(header.num_buckets) * (header.key_size + header.value_size);
  229. if((size_t) length != sizeof(HashHeader) + buckets_length) {
  230. EPRINTF_MSG_PATH(path, "Incorrect file length (expected %ju, got %ju)",
  231. (uintmax_t) sizeof(HashHeader) + buckets_length, (uintmax_t) length);
  232. goto fail;
  233. }
  234. if(!(index = malloc(sizeof(HashIndex)))) {
  235. EPRINTF_PATH(path, "malloc header failed");
  236. goto fail;
  237. }
  238. if(!(index->buckets = malloc(buckets_length))) {
  239. EPRINTF_PATH(path, "malloc buckets failed");
  240. free(index);
  241. index = NULL;
  242. goto fail;
  243. }
  244. bytes_read = fread(index->buckets, 1, buckets_length, fd);
  245. if(bytes_read != buckets_length) {
  246. if(ferror(fd)) {
  247. EPRINTF_PATH(path, "fread buckets failed (expected %ju, got %ju)",
  248. (uintmax_t) buckets_length, (uintmax_t) bytes_read);
  249. }
  250. else {
  251. EPRINTF_MSG_PATH(path, "fread buckets failed (expected %ju, got %ju)",
  252. (uintmax_t) buckets_length, (uintmax_t) bytes_read);
  253. }
  254. free(index->buckets);
  255. free(index);
  256. index = NULL;
  257. goto fail;
  258. }
  259. index->num_entries = _le32toh(header.num_entries);
  260. index->num_buckets = _le32toh(header.num_buckets);
  261. index->key_size = header.key_size;
  262. index->value_size = header.value_size;
  263. index->bucket_size = index->key_size + index->value_size;
  264. index->lower_limit = get_lower_limit(index->num_buckets);
  265. index->upper_limit = get_upper_limit(index->num_buckets);
  266. fail:
  267. if(fclose(fd) < 0) {
  268. EPRINTF_PATH(path, "fclose failed");
  269. }
  270. return index;
  271. }
  272. static HashIndex *
  273. hashindex_init(int capacity, int key_size, int value_size)
  274. {
  275. HashIndex *index;
  276. int i;
  277. capacity = fit_size(capacity);
  278. if(!(index = malloc(sizeof(HashIndex)))) {
  279. EPRINTF("malloc header failed");
  280. return NULL;
  281. }
  282. if(!(index->buckets = calloc(capacity, key_size + value_size))) {
  283. EPRINTF("malloc buckets failed");
  284. free(index);
  285. return NULL;
  286. }
  287. index->num_entries = 0;
  288. index->key_size = key_size;
  289. index->value_size = value_size;
  290. index->num_buckets = capacity;
  291. index->bucket_size = index->key_size + index->value_size;
  292. index->lower_limit = get_lower_limit(index->num_buckets);
  293. index->upper_limit = get_upper_limit(index->num_buckets);
  294. for(i = 0; i < capacity; i++) {
  295. BUCKET_MARK_EMPTY(index, i);
  296. }
  297. return index;
  298. }
  299. static void
  300. hashindex_free(HashIndex *index)
  301. {
  302. free(index->buckets);
  303. free(index);
  304. }
  305. static int
  306. hashindex_write(HashIndex *index, const char *path)
  307. {
  308. off_t buckets_length = (off_t)index->num_buckets * index->bucket_size;
  309. FILE *fd;
  310. HashHeader header = {
  311. .magic = MAGIC,
  312. .num_entries = _htole32(index->num_entries),
  313. .num_buckets = _htole32(index->num_buckets),
  314. .key_size = index->key_size,
  315. .value_size = index->value_size
  316. };
  317. int ret = 1;
  318. if((fd = fopen(path, "wb")) == NULL) {
  319. EPRINTF_PATH(path, "fopen for writing failed");
  320. return 0;
  321. }
  322. if(fwrite(&header, 1, sizeof(header), fd) != sizeof(header)) {
  323. EPRINTF_PATH(path, "fwrite header failed");
  324. ret = 0;
  325. }
  326. if(fwrite(index->buckets, 1, buckets_length, fd) != (size_t) buckets_length) {
  327. EPRINTF_PATH(path, "fwrite buckets failed");
  328. ret = 0;
  329. }
  330. if(fclose(fd) < 0) {
  331. EPRINTF_PATH(path, "fclose failed");
  332. }
  333. return ret;
  334. }
  335. static const void *
  336. hashindex_get(HashIndex *index, const void *key)
  337. {
  338. int idx = hashindex_lookup(index, key);
  339. if(idx < 0) {
  340. return NULL;
  341. }
  342. return BUCKET_ADDR(index, idx) + index->key_size;
  343. }
  344. static int
  345. hashindex_set(HashIndex *index, const void *key, const void *value)
  346. {
  347. int idx = hashindex_lookup(index, key);
  348. uint8_t *ptr;
  349. if(idx < 0)
  350. {
  351. if(index->num_entries > index->upper_limit) {
  352. if(!hashindex_resize(index, grow_size(index->num_buckets))) {
  353. return 0;
  354. }
  355. }
  356. idx = hashindex_index(index, key);
  357. while(!BUCKET_IS_EMPTY(index, idx) && !BUCKET_IS_DELETED(index, idx)) {
  358. idx = (idx + 1) % index->num_buckets;
  359. }
  360. ptr = BUCKET_ADDR(index, idx);
  361. memcpy(ptr, key, index->key_size);
  362. memcpy(ptr + index->key_size, value, index->value_size);
  363. index->num_entries += 1;
  364. }
  365. else
  366. {
  367. memcpy(BUCKET_ADDR(index, idx) + index->key_size, value, index->value_size);
  368. }
  369. return 1;
  370. }
  371. static int
  372. hashindex_delete(HashIndex *index, const void *key)
  373. {
  374. int idx = hashindex_lookup(index, key);
  375. if (idx < 0) {
  376. return 1;
  377. }
  378. BUCKET_MARK_DELETED(index, idx);
  379. index->num_entries -= 1;
  380. if(index->num_entries < index->lower_limit) {
  381. if(!hashindex_resize(index, shrink_size(index->num_buckets))) {
  382. return 0;
  383. }
  384. }
  385. return 1;
  386. }
  387. static void *
  388. hashindex_next_key(HashIndex *index, const void *key)
  389. {
  390. int idx = 0;
  391. if(key) {
  392. idx = 1 + (key - index->buckets) / index->bucket_size;
  393. }
  394. if (idx == index->num_buckets) {
  395. return NULL;
  396. }
  397. while(BUCKET_IS_EMPTY(index, idx) || BUCKET_IS_DELETED(index, idx)) {
  398. idx ++;
  399. if (idx == index->num_buckets) {
  400. return NULL;
  401. }
  402. }
  403. return BUCKET_ADDR(index, idx);
  404. }
  405. static int
  406. hashindex_get_size(HashIndex *index)
  407. {
  408. return index->num_entries;
  409. }