_hashindex.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. #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 lower_limit;
  32. int upper_limit;
  33. int readonly;
  34. } HashIndex;
  35. #define MAGIC "ATTICIDX"
  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, int readonly);
  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. int ret = 0;
  101. strcpy(new_path, index->path);
  102. strcat(new_path, ".tmp");
  103. HashIndex *new;
  104. if(!(new = hashindex_create(new_path, capacity, index->key_size, index->value_size))) {
  105. free(new_path);
  106. return 0;
  107. }
  108. void *key = NULL;
  109. while((key = hashindex_next_key(index, key))) {
  110. hashindex_set(new, key, hashindex_get(index, key));
  111. }
  112. munmap(index->map_addr, index->map_length);
  113. index->map_addr = new->map_addr;
  114. index->map_length = new->map_length;
  115. index->num_buckets = new->num_buckets;
  116. index->lower_limit = new->lower_limit;
  117. index->upper_limit = new->upper_limit;
  118. index->buckets = new->buckets;
  119. if(unlink(index->path) < 0) {
  120. EPRINTF("unlink failed");
  121. goto out;
  122. }
  123. if(rename(new_path, index->path) < 0) {
  124. EPRINTF_PATH(new_path, "rename failed");
  125. goto out;
  126. }
  127. ret = 1;
  128. out:
  129. free(new_path);
  130. free(new->path);
  131. free(new);
  132. return ret;
  133. }
  134. /* Public API */
  135. static HashIndex *
  136. hashindex_open(const char *path, int readonly)
  137. {
  138. void *addr;
  139. int fd, oflags, prot;
  140. off_t length;
  141. HashHeader *header;
  142. HashIndex *index;
  143. if(readonly) {
  144. oflags = O_RDONLY;
  145. prot = PROT_READ;
  146. }
  147. else {
  148. oflags = O_RDWR;
  149. prot = PROT_READ | PROT_WRITE;
  150. }
  151. if((fd = open(path, oflags)) < 0) {
  152. EPRINTF_PATH(path, "open failed");
  153. fprintf(stderr, "Failed to open %s\n", path);
  154. return NULL;
  155. }
  156. if((length = lseek(fd, 0, SEEK_END)) < 0) {
  157. EPRINTF_PATH(path, "lseek failed");
  158. if(close(fd) < 0) {
  159. EPRINTF_PATH(path, "close failed");
  160. }
  161. return NULL;
  162. }
  163. addr = mmap(0, length, prot, MAP_SHARED, fd, 0);
  164. if(close(fd) < 0) {
  165. EPRINTF_PATH(path, "close failed");
  166. return NULL;
  167. }
  168. if(addr == MAP_FAILED) {
  169. EPRINTF_PATH(path, "mmap failed");
  170. return NULL;
  171. }
  172. header = (HashHeader *)addr;
  173. if(memcmp(header->magic, MAGIC, 8)) {
  174. EPRINTF_PATH(path, "Unknown file header");
  175. return NULL;
  176. }
  177. if(length != sizeof(HashHeader) + header->num_buckets * (header->key_size + header->value_size)) {
  178. EPRINTF_PATH(path, "Incorrect file length");
  179. return NULL;
  180. }
  181. if(!(index = malloc(sizeof(HashIndex)))) {
  182. EPRINTF_PATH(path, "malloc failed");
  183. return NULL;
  184. }
  185. index->readonly = readonly;
  186. index->map_addr = addr;
  187. index->map_length = length;
  188. index->num_entries = header->num_entries;
  189. index->num_buckets = header->num_buckets;
  190. index->key_size = header->key_size;
  191. index->value_size = header->value_size;
  192. index->bucket_size = index->key_size + index->value_size;
  193. index->buckets = (addr + sizeof(HashHeader));
  194. index->lower_limit = index->num_buckets > MIN_BUCKETS ? ((int)(index->num_buckets * BUCKET_LOWER_LIMIT)) : 0;
  195. index->upper_limit = (int)(index->num_buckets * BUCKET_UPPER_LIMIT);
  196. if(!(index->path = strdup(path))) {
  197. EPRINTF_PATH(path, "strdup failed");
  198. free(index);
  199. return NULL;
  200. }
  201. return index;
  202. }
  203. static HashIndex *
  204. hashindex_create(const char *path, int capacity, int key_size, int value_size)
  205. {
  206. FILE *fd;
  207. char bucket[MAX_BUCKET_SIZE] = {};
  208. int i, bucket_size;
  209. HashHeader header = {
  210. .magic = MAGIC, .num_entries = 0, .key_size = key_size, .value_size = value_size
  211. };
  212. capacity = MAX(MIN_BUCKETS, capacity);
  213. header.num_buckets = capacity;
  214. if(!(fd = fopen(path, "w"))) {
  215. EPRINTF_PATH(path, "fopen failed");
  216. return NULL;
  217. }
  218. bucket_size = key_size + value_size;
  219. if(fwrite(&header, 1, sizeof(header), fd) != sizeof(header)) {
  220. goto error;
  221. }
  222. *((int32_t *)(bucket + key_size)) = EMPTY;
  223. for(i = 0; i < capacity; i++) {
  224. if(fwrite(bucket, 1, bucket_size, fd) != bucket_size) {
  225. goto error;
  226. }
  227. }
  228. if(fclose(fd) < 0) {
  229. EPRINTF_PATH(path, "fclose failed");
  230. if(unlink(path) < 0) {
  231. EPRINTF_PATH(path, "unlink failed");
  232. }
  233. return NULL;
  234. }
  235. return hashindex_open(path, 0);
  236. error:
  237. if(unlink(path) < 0) {
  238. EPRINTF_PATH(path, "unlink failed");
  239. }
  240. EPRINTF_PATH(path, "fwrite failed");
  241. if(fclose(fd) < 0) {
  242. EPRINTF_PATH(path, "fclose failed");
  243. }
  244. return NULL;
  245. }
  246. static int
  247. hashindex_clear(HashIndex *index)
  248. {
  249. int i;
  250. for(i = 0; i < index->num_buckets; i++) {
  251. BUCKET_MARK_DELETED(index, i);
  252. }
  253. index->num_entries = 0;
  254. return hashindex_resize(index, MIN_BUCKETS);
  255. }
  256. static int
  257. hashindex_flush(HashIndex *index)
  258. {
  259. if(index->readonly) {
  260. return 1;
  261. }
  262. *((int32_t *)(index->map_addr + 8)) = index->num_entries;
  263. *((int32_t *)(index->map_addr + 12)) = index->num_buckets;
  264. if(msync(index->map_addr, index->map_length, MS_SYNC) < 0) {
  265. EPRINTF("msync failed");
  266. return 0;
  267. }
  268. return 1;
  269. }
  270. static int
  271. hashindex_close(HashIndex *index)
  272. {
  273. int rv = 1;
  274. if(hashindex_flush(index) < 0) {
  275. rv = 0;
  276. }
  277. if(munmap(index->map_addr, index->map_length) < 0) {
  278. EPRINTF("munmap failed");
  279. rv = 0;
  280. }
  281. free(index->path);
  282. free(index);
  283. return rv;
  284. }
  285. static const void *
  286. hashindex_get(HashIndex *index, const void *key)
  287. {
  288. int idx = hashindex_lookup(index, key);
  289. if(idx < 0) {
  290. return NULL;
  291. }
  292. return BUCKET_ADDR_READ(index, idx) + index->key_size;
  293. }
  294. static int
  295. hashindex_set(HashIndex *index, const void *key, const void *value)
  296. {
  297. int idx = hashindex_lookup(index, key);
  298. uint8_t *ptr;
  299. if(idx < 0)
  300. {
  301. if(index->num_entries > index->upper_limit) {
  302. if(!hashindex_resize(index, index->num_buckets * 2)) {
  303. return 0;
  304. }
  305. }
  306. idx = hashindex_index(index, key);
  307. while(!BUCKET_IS_EMPTY(index, idx) && !BUCKET_IS_DELETED(index, idx)) {
  308. idx = (idx + 1) % index->num_buckets;
  309. }
  310. ptr = BUCKET_ADDR_WRITE(index, idx);
  311. memcpy(ptr, key, index->key_size);
  312. memcpy(ptr + index->key_size, value, index->value_size);
  313. index->num_entries += 1;
  314. }
  315. else
  316. {
  317. memcpy(BUCKET_ADDR_WRITE(index, idx) + index->key_size, value, index->value_size);
  318. }
  319. return 1;
  320. }
  321. static int
  322. hashindex_delete(HashIndex *index, const void *key)
  323. {
  324. int idx = hashindex_lookup(index, key);
  325. if (idx < 0) {
  326. return 1;
  327. }
  328. BUCKET_MARK_DELETED(index, idx);
  329. index->num_entries -= 1;
  330. if(index->num_entries < index->lower_limit) {
  331. if(!hashindex_resize(index, index->num_buckets / 2)) {
  332. return 0;
  333. }
  334. }
  335. return 1;
  336. }
  337. static void *
  338. hashindex_next_key(HashIndex *index, const void *key)
  339. {
  340. int idx = 0;
  341. if(key) {
  342. idx = 1 + (key - index->buckets) / index->bucket_size;
  343. }
  344. if (idx == index->num_buckets) {
  345. return NULL;
  346. }
  347. while(BUCKET_IS_EMPTY(index, idx) || BUCKET_IS_DELETED(index, idx)) {
  348. idx ++;
  349. if (idx == index->num_buckets) {
  350. return NULL;
  351. }
  352. }
  353. return BUCKET_ADDR_READ(index, idx);
  354. }
  355. static int
  356. hashindex_get_size(HashIndex *index)
  357. {
  358. return index->num_entries;
  359. }