Skip to content
This repository was archived by the owner on Jul 14, 2019. It is now read-only.

Commit 2a17b11

Browse files
committed
log structured on disk or LRU in-memory hashtable
1 parent 8237578 commit 2a17b11

File tree

7 files changed

+933
-3
lines changed

7 files changed

+933
-3
lines changed

examples/mydump/src/mydumper.mk

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@ RIBIFY=libmysqlclient.a # ribify this needed library
88
CFLAGS+= -I ../../../include
99

1010
# we need to tell the linker where to find ribs2 libraries
11-
LDFLAGS+= -L ../../../lib -lribs2 -lribs2_mysql
11+
LDFLAGS+= -L ../../../lib -lribs2_mysql -lribs2
1212

1313
# as well as where to find the mysql libraries
1414
LDFLAGS+=-L../ribified -pthread -lribs2_mysql -lmysqlclient \
1515
-lm -lz -ldl -L../../ribs2/lib -lribs2
1616

17+
# to statically link libmysqlclient, we need stdc++ for mysql >= 5.6
18+
MYSQLVER_GTE_5_6=$(shell expr `mysql_config --version` \>= 5.6)
19+
ifeq ($(MYSQLVER_GTE_5_6),1)
20+
LDFLAGS+=-lstdc++
21+
endif
22+
23+
1724
include ../../../make/ribs.mk # include ribs2 make system

include/lshashtable.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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_

include/ringbuf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ int ringbuf_free(struct ringbuf *rb);
3737
_RIBS_INLINE_ void *ringbuf_mem(struct ringbuf *rb);
3838
_RIBS_INLINE_ void *ringbuf_wloc(struct ringbuf *rb);
3939
_RIBS_INLINE_ void *ringbuf_rloc(struct ringbuf *rb);
40+
_RIBS_INLINE_ size_t ringbuf_wlocpos(struct ringbuf *rb);
41+
_RIBS_INLINE_ size_t ringbuf_rlocpos(struct ringbuf *rb);
4042
_RIBS_INLINE_ void ringbuf_wseek(struct ringbuf *rb, size_t by);
4143
_RIBS_INLINE_ void ringbuf_rseek(struct ringbuf *rb, size_t by);
4244
_RIBS_INLINE_ size_t ringbuf_size(struct ringbuf *rb);

make/ribs.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ $(DEP): $(DIRS)
101101

102102
../ribified/%: $(RIBIFY_DIR)
103103
@echo " (RIBIFY) $(@:../ribified/%=%) [ $@ $(RIBIFYFLAGS) ]"
104-
@objcopy $(shell find $(RIBIFY_LIB_PATH) /usr/lib -name $(@:../ribified/%=%) 2>/dev/null) $@ $(RIBIFYFLAGS)
104+
@objcopy $(shell find $(RIBIFY_LIB_PATH) /usr/lib64 /usr/lib -name $(@:../ribified/%=%) 2>/dev/null) $@ $(RIBIFYFLAGS)
105105

106106
../bin/%: $(OBJ) $(RIBIFY:%=../ribified/%) $(EXTRA_DEPS)
107107
@echo " (LD) $(@:../bin/%=%) [ -o $@ $(OBJ) $(LDFLAGS) ]"

src/_ringbuf.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ _RIBS_INLINE_ void *ringbuf_rloc(struct ringbuf *rb) {
2929
return rb->mem + rb->read_loc;
3030
}
3131

32+
_RIBS_INLINE_ size_t ringbuf_wlocpos(struct ringbuf *rb) {
33+
return rb->write_loc;
34+
}
35+
36+
_RIBS_INLINE_ size_t ringbuf_rlocpos(struct ringbuf *rb) {
37+
return rb->read_loc;
38+
}
39+
40+
3241
_RIBS_INLINE_ void ringbuf_wseek(struct ringbuf *rb, size_t by) {
3342
rb->write_loc += by;
3443
}
@@ -45,6 +54,10 @@ _RIBS_INLINE_ size_t ringbuf_size(struct ringbuf *rb) {
4554
return rb->write_loc - rb->read_loc;
4655
}
4756

57+
_RIBS_INLINE_ size_t ringbuf_capacity(struct ringbuf *rb) {
58+
return rb->capacity;
59+
}
60+
4861
_RIBS_INLINE_ size_t ringbuf_avail(struct ringbuf *rb) {
4962
return rb->capacity - ringbuf_size(rb);
5063
}

0 commit comments

Comments
 (0)