2
0

_hashindex.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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__ == __ORDER_BIG_ENDIAN__)
  12. #error This code is not big endian safe yet
  13. #endif
  14. typedef struct {
  15. char magic[8];
  16. int32_t num_entries;
  17. int32_t num_buckets;
  18. int8_t key_size;
  19. int8_t value_size;
  20. } __attribute__((__packed__)) HashHeader;
  21. typedef struct {
  22. char *path;
  23. void *map_addr;
  24. off_t map_length;
  25. void *buckets;
  26. int num_entries;
  27. int num_buckets;
  28. int key_size;
  29. int value_size;
  30. int bucket_size;
  31. int limit;
  32. } HashIndex;
  33. #define MAGIC "DARCHASH"
  34. #define EMPTY ((int32_t)-1)
  35. #define DELETED ((int32_t)-2)
  36. #define BUCKET_ADDR_READ(index, idx) (index->buckets + (idx * index->bucket_size))
  37. #define BUCKET_ADDR_WRITE(index, idx) (index->buckets + (idx * index->bucket_size))
  38. #define BUCKET_IS_DELETED(index, idx) (*((int32_t *)(BUCKET_ADDR_READ(index, idx) + index->key_size)) == DELETED)
  39. #define BUCKET_IS_EMPTY(index, idx) (*((int32_t *)(BUCKET_ADDR_READ(index, idx) + index->key_size)) == EMPTY)
  40. #define BUCKET_MATCHES_KEY(index, idx, key) (memcmp(key, BUCKET_ADDR_READ(index, idx), index->key_size) == 0)
  41. #define BUCKET_MARK_DELETED(index, idx) (*((int32_t *)(BUCKET_ADDR_WRITE(index, idx) + index->key_size)) = DELETED)
  42. static HashIndex *hashindex_open(const char *path);
  43. static void hashindex_close(HashIndex *index);
  44. static void hashindex_clear(HashIndex *index);
  45. static void hashindex_flush(HashIndex *index);
  46. static HashIndex *hashindex_create(const char *path, int capacity, int key_size, int value_size);
  47. static const void *hashindex_get(HashIndex *index, const void *key);
  48. static void hashindex_set(HashIndex *index, const void *key, const void *value);
  49. static void hashindex_delete(HashIndex *index, const void *key);
  50. static void *hashindex_next_key(HashIndex *index, const void *key);
  51. /* Private API */
  52. static int
  53. hashindex_index(HashIndex *index, const void *key)
  54. {
  55. return *((uint32_t *)key) % index->num_buckets;
  56. }
  57. static int
  58. hashindex_lookup(HashIndex *index, const void *key)
  59. {
  60. int didx = -1;
  61. int start = hashindex_index(index, key);
  62. int idx = start;
  63. for(;;) {
  64. if(BUCKET_IS_EMPTY(index, idx))
  65. {
  66. return -1;
  67. }
  68. if(BUCKET_IS_DELETED(index, idx)) {
  69. if(didx == -1) {
  70. didx = idx;
  71. }
  72. }
  73. else if(BUCKET_MATCHES_KEY(index, idx, key)) {
  74. if (didx != -1) {
  75. memcpy(BUCKET_ADDR_WRITE(index, didx), BUCKET_ADDR_READ(index, idx), index->bucket_size);
  76. BUCKET_MARK_DELETED(index, idx);
  77. idx = didx;
  78. }
  79. return idx;
  80. }
  81. idx = (idx + 1) % index->num_buckets;
  82. if(idx == start) {
  83. return -1;
  84. }
  85. }
  86. }
  87. static void
  88. hashindex_resize(HashIndex *index, int capacity)
  89. {
  90. char *new_path = malloc(strlen(index->path) + 5);
  91. strcpy(new_path, index->path);
  92. strcat(new_path, ".tmp");
  93. HashIndex *new = hashindex_create(new_path, capacity, index->key_size, index->value_size);
  94. void *key = NULL;
  95. while((key = hashindex_next_key(index, key))) {
  96. hashindex_set(new, key, hashindex_get(index, key));
  97. }
  98. munmap(index->map_addr, index->map_length);
  99. index->map_addr = new->map_addr;
  100. index->map_length = new->map_length;
  101. index->num_buckets = new->num_buckets;
  102. index->limit = new->limit;
  103. index->buckets = new->buckets;
  104. unlink(index->path);
  105. rename(new_path, index->path);
  106. free(new_path);
  107. free(new->path);
  108. free(new);
  109. }
  110. /* Public API */
  111. static HashIndex *
  112. hashindex_open(const char *path)
  113. {
  114. int fd = open(path, O_RDWR);
  115. if(fd < 0) {
  116. fprintf(stderr, "Failed to open %s\n", path);
  117. return NULL;
  118. }
  119. off_t length = lseek(fd, 0, SEEK_END);
  120. void *addr = mmap(0, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  121. close(fd);
  122. if(addr == MAP_FAILED) {
  123. fprintf(stderr, "Failed to mmap %s", path);
  124. }
  125. HashHeader *header = (HashHeader *)addr;
  126. HashIndex *index = malloc(sizeof(HashIndex));
  127. index->path = malloc(strlen(path) + 1);
  128. strcpy(index->path, path);
  129. index->map_addr = addr;
  130. index->map_length = length;
  131. index->num_entries = header->num_entries;
  132. index->num_buckets = header->num_buckets;
  133. index->key_size = header->key_size;
  134. index->value_size = header->value_size;
  135. index->bucket_size = index->key_size + index->value_size;
  136. index->buckets = (addr + sizeof(HashHeader));
  137. index->limit = (int)(index->num_buckets * .75);
  138. return index;
  139. }
  140. static HashIndex *
  141. hashindex_create(const char *path, int capacity, int key_size, int value_size)
  142. {
  143. FILE *fd;
  144. int i;
  145. if(!(fd = fopen(path, "w"))) {
  146. fprintf(stderr, "Failed to create %s\n", path);
  147. return NULL;
  148. }
  149. HashHeader header;
  150. memcpy(header.magic, MAGIC, sizeof(MAGIC) - 1);
  151. header.num_entries = 0;
  152. header.num_buckets = capacity;
  153. header.key_size = key_size;
  154. header.value_size = value_size;
  155. int bucket_size = key_size + value_size;
  156. char *bucket = calloc(bucket_size, 1);
  157. if(fwrite(&header, 1, sizeof(header), fd) != sizeof(header))
  158. goto error;
  159. *((int32_t *)(bucket + key_size)) = EMPTY;
  160. for(i = 0; i < capacity; i++) {
  161. if(fwrite(bucket, 1, bucket_size, fd) != bucket_size)
  162. goto error;
  163. }
  164. free(bucket);
  165. fclose(fd);
  166. return hashindex_open(path);
  167. error:
  168. fclose(fd);
  169. free(bucket);
  170. return NULL;
  171. }
  172. static void
  173. hashindex_clear(HashIndex *index)
  174. {
  175. int i;
  176. for(i = 0; i < index->num_buckets; i++) {
  177. BUCKET_MARK_DELETED(index, i);
  178. }
  179. index->num_entries = 0;
  180. hashindex_resize(index, 16);
  181. }
  182. static void
  183. hashindex_flush(HashIndex *index)
  184. {
  185. *((int32_t *)(index->map_addr + 8)) = index->num_entries;
  186. *((int32_t *)(index->map_addr + 12)) = index->num_buckets;
  187. msync(index->map_addr, index->map_length, MS_SYNC);
  188. }
  189. static void
  190. hashindex_close(HashIndex *index)
  191. {
  192. hashindex_flush(index);
  193. munmap(index->map_addr, index->map_length);
  194. free(index->path);
  195. free(index);
  196. }
  197. static const void *
  198. hashindex_get(HashIndex *index, const void *key)
  199. {
  200. int idx = hashindex_lookup(index, key);
  201. if(idx < 0) {
  202. return NULL;
  203. }
  204. return BUCKET_ADDR_READ(index, idx) + index->key_size;
  205. }
  206. static void
  207. hashindex_set(HashIndex *index, const void *key, const void *value)
  208. {
  209. int idx = hashindex_lookup(index, key);
  210. uint8_t *ptr;
  211. if(idx < 0)
  212. {
  213. if(index->num_entries > index->limit) {
  214. hashindex_resize(index, index->num_buckets * 2);
  215. }
  216. idx = hashindex_index(index, key);
  217. while(!BUCKET_IS_EMPTY(index, idx) && !BUCKET_IS_DELETED(index, idx)) {
  218. idx = (idx + 1) % index->num_buckets;
  219. }
  220. ptr = BUCKET_ADDR_WRITE(index, idx);
  221. memcpy(ptr, key, index->key_size);
  222. memcpy(ptr + index->key_size, value, index->value_size);
  223. index->num_entries += 1;
  224. }
  225. else
  226. {
  227. memcpy(BUCKET_ADDR_WRITE(index, idx) + index->key_size, value, index->value_size);
  228. }
  229. }
  230. static void
  231. hashindex_delete(HashIndex *index, const void *key)
  232. {
  233. int idx = hashindex_lookup(index, key);
  234. if (idx < 0) {
  235. return;
  236. }
  237. BUCKET_MARK_DELETED(index, idx);
  238. index->num_entries -= 1;
  239. }
  240. static void *
  241. hashindex_next_key(HashIndex *index, const void *key)
  242. {
  243. int idx = 0;
  244. if(key) {
  245. idx = 1 + (key - index->buckets) / index->bucket_size;
  246. }
  247. if (idx == index->num_buckets)
  248. return NULL;
  249. while(BUCKET_IS_EMPTY(index, idx) || BUCKET_IS_DELETED(index, idx)) {
  250. idx ++;
  251. if (idx == index->num_buckets)
  252. return NULL;
  253. }
  254. return BUCKET_ADDR_READ(index, idx);
  255. }
  256. static int
  257. hashindex_get_size(HashIndex *index)
  258. {
  259. return index->num_entries;
  260. }