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

Commit e6ffd04

Browse files
author
Lior Amram
committed
thashtable
1 parent f0d91a4 commit e6ffd04

File tree

5 files changed

+390
-1
lines changed

5 files changed

+390
-1
lines changed

include/ribs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "context.h"
2727
#include "ctx_pool.h"
2828
#include "hashtable.h"
29+
#include "thashtable.h"
2930
#include "lhashtable.h"
3031
#include "epoll_worker.h"
3132
#include "logger.h"

include/thashtable.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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) 2014 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 _THASHTABLE__H_
21+
#define _THASHTABLE__H_
22+
23+
#include "ribs_defs.h"
24+
#include <string.h>
25+
26+
struct thashtable;
27+
struct thashtable_internal_rec;
28+
29+
typedef struct thashtable_internal_rec thashtable_rec_t;
30+
31+
struct thashtable *thashtable_create(void);
32+
thashtable_rec_t *thashtable_insert(struct thashtable *tht, const void *key, size_t key_len, const void *val, size_t val_len, int *inserted);
33+
thashtable_rec_t *thashtable_put(struct thashtable *tht, const void *key, size_t key_len, const void *val, size_t val_len);
34+
thashtable_rec_t *thashtable_insert_alloc(struct thashtable *tht, const void *key, size_t key_len, size_t val_len);
35+
thashtable_rec_t *thashtable_lookup(struct thashtable *tht, const void *key, size_t key_len);
36+
thashtable_rec_t *thashtable_remove(struct thashtable *tht, const void *key, size_t key_len);
37+
int thashtable_foreach(struct thashtable *tht, int (*func)(thashtable_rec_t *rec));
38+
39+
static inline void *thashtable_get_key(thashtable_rec_t *rec);
40+
static inline uint32_t thashtable_get_key_size(thashtable_rec_t *rec);
41+
static inline void *thashtable_get_val(thashtable_rec_t *rec);
42+
static inline uint32_t thashtable_get_val_size(thashtable_rec_t *rec);
43+
static inline uint32_t thashtable_get_size(struct thashtable *tht);
44+
static inline const char *thashtable_lookup_str(struct thashtable *tht, const char *key, const char *default_val);
45+
46+
#include "../src/_thashtable.c"
47+
48+
#endif // _THASHTABLE__H_

src/_thashtable.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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) 2014 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+
struct tht_entry {
21+
uint32_t hashcode;
22+
thashtable_rec_t *rec;
23+
};
24+
25+
struct thashtable {
26+
uint32_t mask;
27+
uint32_t size;
28+
struct tht_entry *buckets[32];
29+
};
30+
31+
struct thashtable_internal_rec {
32+
uint32_t key_size;
33+
uint32_t val_size;
34+
char data[];
35+
};
36+
37+
static inline void *thashtable_get_key(thashtable_rec_t *rec) {
38+
return ((struct thashtable_internal_rec *)rec)->data;
39+
}
40+
41+
static inline uint32_t thashtable_get_key_size(thashtable_rec_t *rec) {
42+
return ((struct thashtable_internal_rec *)rec)->key_size;
43+
}
44+
45+
static inline void *thashtable_get_val(thashtable_rec_t *rec) {
46+
return ((struct thashtable_internal_rec *)rec)->data + ((struct thashtable_internal_rec *)rec)->key_size;
47+
}
48+
49+
static inline uint32_t thashtable_get_val_size(thashtable_rec_t *rec) {
50+
return ((struct thashtable_internal_rec *)rec)->val_size;
51+
}
52+
53+
static inline uint32_t thashtable_get_size(struct thashtable *tht) {
54+
return tht->size;
55+
}
56+
57+
static inline const char *thashtable_lookup_str(struct thashtable *tht, const char *key, const char *default_val) {
58+
thashtable_rec_t *rec = thashtable_lookup(tht, key, strlen(key));
59+
return (rec ? thashtable_get_val(rec) : default_val);
60+
}

src/ribs2.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
TARGET=ribs2.a
22

3-
SRC=context.c epoll_worker.c ctx_pool.c http_server.c hashtable.c mime_types.c http_client_pool.c timeout_handler.c ribify.c logger.c daemonize.c http_headers.c http_cookies.c file_mapper.c ds_var_field.c file_utils.c lhashtable.c search.c json.c memalloc.c mempool.c sleep.c timer.c ringbuf.c ringfile.c sendemail.c ds_loader.c heap.c vmallocator.c base64.c http_file_server.c http_vhost.c
3+
SRC=context.c epoll_worker.c ctx_pool.c http_server.c hashtable.c mime_types.c http_client_pool.c timeout_handler.c ribify.c logger.c daemonize.c http_headers.c http_cookies.c file_mapper.c ds_var_field.c file_utils.c lhashtable.c search.c json.c memalloc.c mempool.c sleep.c timer.c ringbuf.c ringfile.c sendemail.c ds_loader.c heap.c vmallocator.c base64.c http_file_server.c http_vhost.c thashtable.c
44
ASM=context_asm.S
55

66
CFLAGS+= -I ../include

0 commit comments

Comments
 (0)