_hashindex.c 6.5 KB

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