|
| 1 | +/* |
| 2 | + This file is part of RIBS2.0 (Robust Infrastructure for Backend Systems). |
| 3 | + RIBS is an infrastructure for building great SaaS applications (but not |
| 4 | + limited to). |
| 5 | +
|
| 6 | + Copyright (C) Adap.tv, Inc. |
| 7 | +
|
| 8 | + RIBS is free software: you can redistribute it and/or modify |
| 9 | + it under the terms of the GNU Lesser General Public License as published by |
| 10 | + the Free Software Foundation, version 2.1 of the License. |
| 11 | +
|
| 12 | + RIBS is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + GNU Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public License |
| 18 | + along with RIBS. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +*/ |
| 20 | +#ifndef _LSHASHTABLE__H_ |
| 21 | +#define _LSHASHTABLE__H_ |
| 22 | + |
| 23 | +#include "ribs.h" |
| 24 | +#include "ringbuf.h" |
| 25 | +#include "ringfile.h" |
| 26 | + |
| 27 | +/* log structured hashtable */ |
| 28 | + |
| 29 | +#define LSHASHTABLE_INITIALIZER { 0, 0, 0, NULL, 0, VMBUF_INITIALIZER, VMBUF_INITIALIZER, _lshashtable_is_expired_noop } |
| 30 | + |
| 31 | +struct lshashtable_val { |
| 32 | + uint32_t val_size; |
| 33 | + void *val; |
| 34 | +}; |
| 35 | + |
| 36 | +#pragma pack(push,1) |
| 37 | +struct lshashtable_entry { |
| 38 | + uint32_t hashcode; |
| 39 | + void *rec; |
| 40 | + uint16_t rs_id; |
| 41 | +}; |
| 42 | + |
| 43 | +struct lshashtable_rec { |
| 44 | + uint16_t key_size; |
| 45 | + uint32_t val_size; |
| 46 | + char data[]; |
| 47 | +}; |
| 48 | + |
| 49 | +#pragma pack(pop) |
| 50 | + |
| 51 | +struct lshashtable { |
| 52 | + uint32_t mask; |
| 53 | + uint32_t size; |
| 54 | + size_t capacity; |
| 55 | + char *basedir; |
| 56 | + uint16_t next_rs_id; |
| 57 | + struct vmbuf index; |
| 58 | + struct vmbuf record_stores; |
| 59 | + int (*is_expired)(void *rec); |
| 60 | +}; |
| 61 | + |
| 62 | +struct lshashtable_store { |
| 63 | + uint32_t store_id; |
| 64 | + size_t num_blocks; |
| 65 | + size_t num_free_blocks; |
| 66 | +}; |
| 67 | + |
| 68 | +int lshashtable_init(struct lshashtable *lsht, const char *basedir, size_t capacity); |
| 69 | +static inline int lshashtable_is_init(struct lshashtable *lsht) { return lsht->mask > 0; } |
| 70 | +static inline void lshashtable_set_is_expired(struct lshashtable *lsht, int (*is_expired)(void *rec)) { lsht->is_expired = is_expired; } |
| 71 | +int lshashtable_close(struct lshashtable *lsht); |
| 72 | +void *lshashtable_put_key(struct lshashtable *lsht, const void *key, size_t key_size, size_t val_size, struct lshashtable_val *oldval); |
| 73 | +int lshashtable_find_key(struct lshashtable *lsht, const void *key, size_t key_size, struct lshashtable_val *val); |
| 74 | +int lshashtable_del_key(struct lshashtable *lsht, const void *key, size_t key_size); |
| 75 | +int lshashtable_stats(struct lshashtable *lsht, struct vmbuf *buf); /* TODO: remove */ |
| 76 | +int lshashtable_test(struct lshashtable *lsht); |
| 77 | +static inline int _lshashtable_is_expired_noop(void *rec __attribute__((unused))) { return 0; } |
| 78 | +static inline void *lshashtable_get_val(void *rec) { struct lshashtable_rec *r = rec; return r->data + r->key_size; } |
| 79 | +int lshashtable_foreach(struct lshashtable *lsht, int (*store_callback)(struct lshashtable_store*), int (*rec_callback)(struct lshashtable_rec*)); |
| 80 | + |
| 81 | + |
| 82 | +#endif // _LSHASHTABLE__H_ |
0 commit comments