_hashindex.c 9.7 KB

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