_hashindex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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(BYTE_ORDER)&&(BYTE_ORDER == BIG_ENDIAN)
  11. #define _le32toh(x) __builtin_bswap32(x)
  12. #define _htole32(x) __builtin_bswap32(x)
  13. #elif defined(BYTE_ORDER)&&(BYTE_ORDER == LITTLE_ENDIAN)
  14. #define _le32toh(x) (x)
  15. #define _htole32(x) (x)
  16. #else
  17. #error Unknown byte order
  18. #endif
  19. typedef struct {
  20. char magic[8];
  21. int32_t num_entries;
  22. int32_t num_buckets;
  23. int8_t key_size;
  24. int8_t value_size;
  25. } __attribute__((__packed__)) HashHeader;
  26. typedef struct {
  27. void *buckets;
  28. int num_entries;
  29. int num_buckets;
  30. int key_size;
  31. int value_size;
  32. off_t bucket_size;
  33. int lower_limit;
  34. int upper_limit;
  35. } HashIndex;
  36. #define MAGIC "ATTICIDX"
  37. #define EMPTY _htole32(0xffffffff)
  38. #define DELETED _htole32(0xfffffffe)
  39. #define MAX_BUCKET_SIZE 512
  40. #define BUCKET_LOWER_LIMIT .25
  41. #define BUCKET_UPPER_LIMIT .90
  42. #define MIN_BUCKETS 1024
  43. #define MAX(x, y) ((x) > (y) ? (x): (y))
  44. #define BUCKET_ADDR(index, idx) (index->buckets + (idx * index->bucket_size))
  45. #define BUCKET_IS_DELETED(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) == DELETED)
  46. #define BUCKET_IS_EMPTY(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) == EMPTY)
  47. #define BUCKET_MATCHES_KEY(index, idx, key) (memcmp(key, BUCKET_ADDR(index, idx), index->key_size) == 0)
  48. #define BUCKET_MARK_DELETED(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) = DELETED)
  49. #define BUCKET_MARK_EMPTY(index, idx) (*((uint32_t *)(BUCKET_ADDR(index, idx) + index->key_size)) = EMPTY)
  50. #define EPRINTF_MSG(msg, ...) fprintf(stderr, "hashindex: " msg "\n", ##__VA_ARGS__)
  51. #define EPRINTF_MSG_PATH(path, msg, ...) fprintf(stderr, "hashindex: %s: " msg "\n", path, ##__VA_ARGS__)
  52. #define EPRINTF(msg, ...) fprintf(stderr, "hashindex: " msg "(%s)\n", ##__VA_ARGS__, strerror(errno))
  53. #define EPRINTF_PATH(path, msg, ...) fprintf(stderr, "hashindex: %s: " msg " (%s)\n", path, ##__VA_ARGS__, strerror(errno))
  54. static HashIndex *hashindex_read(const char *path);
  55. static int hashindex_write(HashIndex *index, const char *path);
  56. static HashIndex *hashindex_init(int capacity, int key_size, int value_size);
  57. static const void *hashindex_get(HashIndex *index, const void *key);
  58. static int hashindex_set(HashIndex *index, const void *key, const void *value);
  59. static int hashindex_delete(HashIndex *index, const void *key);
  60. static void *hashindex_next_key(HashIndex *index, const void *key);
  61. /* Private API */
  62. static int
  63. hashindex_index(HashIndex *index, const void *key)
  64. {
  65. return _le32toh(*((uint32_t *)key)) % index->num_buckets;
  66. }
  67. static int
  68. hashindex_lookup(HashIndex *index, const void *key)
  69. {
  70. int didx = -1;
  71. int start = hashindex_index(index, key);
  72. int idx = start;
  73. for(;;) {
  74. if(BUCKET_IS_EMPTY(index, idx))
  75. {
  76. return -1;
  77. }
  78. if(BUCKET_IS_DELETED(index, idx)) {
  79. if(didx == -1) {
  80. didx = idx;
  81. }
  82. }
  83. else if(BUCKET_MATCHES_KEY(index, idx, key)) {
  84. if (didx != -1) {
  85. memcpy(BUCKET_ADDR(index, didx), BUCKET_ADDR(index, idx), index->bucket_size);
  86. BUCKET_MARK_DELETED(index, idx);
  87. idx = didx;
  88. }
  89. return idx;
  90. }
  91. idx = (idx + 1) % index->num_buckets;
  92. if(idx == start) {
  93. return -1;
  94. }
  95. }
  96. }
  97. static int
  98. hashindex_resize(HashIndex *index, int capacity)
  99. {
  100. HashIndex *new;
  101. void *key = NULL;
  102. if(!(new = hashindex_init(capacity, index->key_size, index->value_size))) {
  103. return 0;
  104. }
  105. while((key = hashindex_next_key(index, key))) {
  106. hashindex_set(new, key, hashindex_get(index, key));
  107. }
  108. free(index->buckets);
  109. index->buckets = new->buckets;
  110. index->num_buckets = new->num_buckets;
  111. index->lower_limit = new->lower_limit;
  112. index->upper_limit = new->upper_limit;
  113. free(new);
  114. return 1;
  115. }
  116. /* Public API */
  117. static HashIndex *
  118. hashindex_read(const char *path)
  119. {
  120. FILE *fd;
  121. off_t length, buckets_length;
  122. HashHeader header;
  123. HashIndex *index = NULL;
  124. if((fd = fopen(path, "r")) == NULL) {
  125. EPRINTF_PATH(path, "fopen failed");
  126. return NULL;
  127. }
  128. if(fread(&header, 1, sizeof(HashHeader), fd) != sizeof(HashHeader)) {
  129. if(ferror(fd)) {
  130. EPRINTF_PATH(path, "fread failed");
  131. }
  132. else {
  133. EPRINTF_MSG_PATH(path, "failed to read %ld bytes", sizeof(HashHeader));
  134. }
  135. goto fail;
  136. }
  137. if(fseek(fd, 0, SEEK_END) < 0) {
  138. EPRINTF_PATH(path, "fseek failed");
  139. goto fail;
  140. }
  141. if((length = ftell(fd)) < 0) {
  142. EPRINTF_PATH(path, "ftell failed");
  143. goto fail;
  144. }
  145. if(fseek(fd, sizeof(HashHeader), SEEK_SET) < 0) {
  146. EPRINTF_PATH(path, "fseek failed");
  147. goto fail;
  148. }
  149. if(memcmp(header.magic, MAGIC, 8)) {
  150. EPRINTF_MSG_PATH(path, "Unknown file header");
  151. goto fail;
  152. }
  153. buckets_length = (off_t)_le32toh(header.num_buckets) * (header.key_size + header.value_size);
  154. if(length != sizeof(HashHeader) + buckets_length) {
  155. EPRINTF_MSG_PATH(path, "Incorrect file length");
  156. goto fail;
  157. }
  158. if(!(index = malloc(sizeof(HashIndex)))) {
  159. EPRINTF_PATH(path, "malloc failed");
  160. goto fail;
  161. }
  162. if(!(index->buckets = malloc(buckets_length))) {
  163. EPRINTF_PATH(path, "malloc failed");
  164. free(index);
  165. index = NULL;
  166. goto fail;
  167. }
  168. if(fread(index->buckets, 1, buckets_length, fd) != buckets_length) {
  169. if(ferror(fd)) {
  170. EPRINTF_PATH(path, "fread failed");
  171. }
  172. else {
  173. EPRINTF_MSG_PATH(path, "failed to read %ld bytes", length);
  174. }
  175. free(index->buckets);
  176. free(index);
  177. index = NULL;
  178. goto fail;
  179. }
  180. index->num_entries = _le32toh(header.num_entries);
  181. index->num_buckets = _le32toh(header.num_buckets);
  182. index->key_size = header.key_size;
  183. index->value_size = header.value_size;
  184. index->bucket_size = index->key_size + index->value_size;
  185. index->lower_limit = index->num_buckets > MIN_BUCKETS ? ((int)(index->num_buckets * BUCKET_LOWER_LIMIT)) : 0;
  186. index->upper_limit = (int)(index->num_buckets * BUCKET_UPPER_LIMIT);
  187. fail:
  188. if(fclose(fd) < 0) {
  189. EPRINTF_PATH(path, "fclose failed");
  190. }
  191. return index;
  192. }
  193. static HashIndex *
  194. hashindex_init(int capacity, int key_size, int value_size)
  195. {
  196. off_t buckets_length;
  197. HashIndex *index;
  198. int i;
  199. capacity = MAX(MIN_BUCKETS, capacity);
  200. if(!(index = malloc(sizeof(HashIndex)))) {
  201. EPRINTF("malloc failed");
  202. return NULL;
  203. }
  204. buckets_length = (off_t)capacity * (key_size + value_size);
  205. if(!(index->buckets = calloc(buckets_length, 1))) {
  206. EPRINTF("malloc failed");
  207. free(index);
  208. return NULL;
  209. }
  210. index->num_entries = 0;
  211. index->key_size = key_size;
  212. index->value_size = value_size;
  213. index->num_buckets = capacity;
  214. index->bucket_size = index->key_size + index->value_size;
  215. index->lower_limit = index->num_buckets > MIN_BUCKETS ? ((int)(index->num_buckets * BUCKET_LOWER_LIMIT)) : 0;
  216. index->upper_limit = (int)(index->num_buckets * BUCKET_UPPER_LIMIT);
  217. for(i = 0; i < capacity; i++) {
  218. BUCKET_MARK_EMPTY(index, i);
  219. }
  220. return index;
  221. }
  222. static void
  223. hashindex_free(HashIndex *index)
  224. {
  225. free(index->buckets);
  226. free(index);
  227. }
  228. static int
  229. hashindex_write(HashIndex *index, const char *path)
  230. {
  231. off_t buckets_length = (off_t)index->num_buckets * index->bucket_size;
  232. FILE *fd;
  233. HashHeader header = {
  234. .magic = MAGIC,
  235. .num_entries = _htole32(index->num_entries),
  236. .num_buckets = _htole32(index->num_buckets),
  237. .key_size = index->key_size,
  238. .value_size = index->value_size
  239. };
  240. int ret = 1;
  241. if((fd = fopen(path, "w")) == NULL) {
  242. EPRINTF_PATH(path, "open failed");
  243. return 0;
  244. }
  245. if(fwrite(&header, 1, sizeof(header), fd) != sizeof(header)) {
  246. EPRINTF_PATH(path, "fwrite failed");
  247. ret = 0;
  248. }
  249. if(fwrite(index->buckets, 1, buckets_length, fd) != buckets_length) {
  250. EPRINTF_PATH(path, "fwrite failed");
  251. ret = 0;
  252. }
  253. if(fclose(fd) < 0) {
  254. EPRINTF_PATH(path, "fclose failed");
  255. }
  256. return ret;
  257. }
  258. static const void *
  259. hashindex_get(HashIndex *index, const void *key)
  260. {
  261. int idx = hashindex_lookup(index, key);
  262. if(idx < 0) {
  263. return NULL;
  264. }
  265. return BUCKET_ADDR(index, idx) + index->key_size;
  266. }
  267. static int
  268. hashindex_set(HashIndex *index, const void *key, const void *value)
  269. {
  270. int idx = hashindex_lookup(index, key);
  271. uint8_t *ptr;
  272. if(idx < 0)
  273. {
  274. if(index->num_entries > index->upper_limit) {
  275. if(!hashindex_resize(index, index->num_buckets * 2)) {
  276. return 0;
  277. }
  278. }
  279. idx = hashindex_index(index, key);
  280. while(!BUCKET_IS_EMPTY(index, idx) && !BUCKET_IS_DELETED(index, idx)) {
  281. idx = (idx + 1) % index->num_buckets;
  282. }
  283. ptr = BUCKET_ADDR(index, idx);
  284. memcpy(ptr, key, index->key_size);
  285. memcpy(ptr + index->key_size, value, index->value_size);
  286. index->num_entries += 1;
  287. }
  288. else
  289. {
  290. memcpy(BUCKET_ADDR(index, idx) + index->key_size, value, index->value_size);
  291. }
  292. return 1;
  293. }
  294. static int
  295. hashindex_delete(HashIndex *index, const void *key)
  296. {
  297. int idx = hashindex_lookup(index, key);
  298. if (idx < 0) {
  299. return 1;
  300. }
  301. BUCKET_MARK_DELETED(index, idx);
  302. index->num_entries -= 1;
  303. if(index->num_entries < index->lower_limit) {
  304. if(!hashindex_resize(index, index->num_buckets / 2)) {
  305. return 0;
  306. }
  307. }
  308. return 1;
  309. }
  310. static void *
  311. hashindex_next_key(HashIndex *index, const void *key)
  312. {
  313. int idx = 0;
  314. if(key) {
  315. idx = 1 + (key - index->buckets) / index->bucket_size;
  316. }
  317. if (idx == index->num_buckets) {
  318. return NULL;
  319. }
  320. while(BUCKET_IS_EMPTY(index, idx) || BUCKET_IS_DELETED(index, idx)) {
  321. idx ++;
  322. if (idx == index->num_buckets) {
  323. return NULL;
  324. }
  325. }
  326. return BUCKET_ADDR(index, idx);
  327. }
  328. static int
  329. hashindex_get_size(HashIndex *index)
  330. {
  331. return index->num_entries;
  332. }
  333. static void
  334. hashindex_summarize(HashIndex *index, long long *total_size, long long *total_csize, long long *total_unique_size, long long *total_unique_csize)
  335. {
  336. int64_t size = 0, csize = 0, unique_size = 0, unique_csize = 0;
  337. const int32_t *values;
  338. void *key = NULL;
  339. while((key = hashindex_next_key(index, key))) {
  340. values = key + 32;
  341. unique_size += values[1];
  342. unique_csize += values[2];
  343. size += values[0] * values[1];
  344. csize += values[0] * values[2];
  345. }
  346. *total_size = size;
  347. *total_csize = csize;
  348. *total_unique_size = unique_size;
  349. *total_unique_csize = unique_csize;
  350. }