_hashindex.c 6.5 KB

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