_hashindex.c 11 KB

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