Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
cleaner code with enum; add fwmatch to test
Browse files Browse the repository at this point in the history
  • Loading branch information
mreiferson committed Mar 27, 2011
1 parent 528854f commit 3f5e21f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
10 changes: 6 additions & 4 deletions sortdb/sortdb.c
Expand Up @@ -13,7 +13,7 @@
#include <simplehttp/simplehttp.h>

#define NAME "sortdb"
#define VERSION "1.4"
#define VERSION "1.4.1"
#define DEBUG 1

void stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx);
Expand All @@ -35,6 +35,8 @@ static struct stat st;
static char deliminator = '\t';
static int fd = 0;

enum prefix_options { disable_prefix, enable_prefix };

static uint64_t get_hits = 0;
static uint64_t get_misses = 0;
static uint64_t fwmatch_hits = 0;
Expand Down Expand Up @@ -100,7 +102,7 @@ void fwmatch_cb(struct evhttp_request *req, struct evbuffer *evb,void *ctx)
if (!key) {
evbuffer_add_printf(evb, "missing argument: key\n");
evhttp_send_reply(req, HTTP_BADREQUEST, "MISSING_ARG_KEY", evb);
} else if ((line = map_search(key, keylen, (char *)map_base, (char *)map_base+st.st_size, &seeks, 1))) {
} else if ((line = map_search(key, keylen, (char *)map_base, (char *)map_base+st.st_size, &seeks, enable_prefix))) {
/*
* Walk backwards while key prefix matches.
* There's probably a better way to do this, however
Expand Down Expand Up @@ -155,7 +157,7 @@ void get_cb(struct evhttp_request *req, struct evbuffer *evb,void *ctx)
if (!key) {
evbuffer_add_printf(evb, "missing argument: key\n");
evhttp_send_reply(req, HTTP_BADREQUEST, "MISSING_ARG_KEY", evb);
} else if ((line = map_search(key, strlen(key), (char *)map_base, (char *)map_base+st.st_size, &seeks, 0))) {
} else if ((line = map_search(key, strlen(key), (char *)map_base, (char *)map_base+st.st_size, &seeks, disable_prefix))) {
sprintf(buf, "%d", seeks);
evhttp_add_header(req->output_headers, "x-sortdb-seeks", buf);
delim = strchr(line, deliminator);
Expand Down Expand Up @@ -194,7 +196,7 @@ void mget_cb(struct evhttp_request *req, struct evbuffer *evb,void *ctx)

if(DEBUG) fprintf(stderr, "/mget %s\n", key);

if ((line = map_search(key, strlen(key), (char *)map_base, (char *)map_base+st.st_size, &seeks, 0))) {
if ((line = map_search(key, strlen(key), (char *)map_base, (char *)map_base+st.st_size, &seeks, disable_prefix))) {
newline = strchr(line, '\n');
if (newline) {
// this is only supported by libevent2+
Expand Down
4 changes: 4 additions & 0 deletions sortdb/test.expected
Expand Up @@ -18,6 +18,10 @@ already-asleep
a first record
c d
o p
/fwmatch?key=prefix.
prefix.1 how
prefix.2 are
prefix.3 you
db reloaded
/get?key=a (should be a new key 'new db')
new db
Expand Down
4 changes: 4 additions & 0 deletions sortdb/test.sh
Expand Up @@ -24,6 +24,8 @@ run_vg (){
}
err=$?

LIBEVENT=/bitly/local make > $testsubdir/make.out 2>&1

ln -s -f test.tab test.db
run_vg sortdb "-f test.db -a 127.0.0.1 -p 8080"
sleep 1
Expand All @@ -33,6 +35,8 @@ for key in a b c m o zzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzz
done
echo "/mget?k=a&k=c&k=o" >> $testsubdir/test.out
curl --silent "localhost:8080/mget?k=a&k=c&k=o" >> $testsubdir/test.out
echo "/fwmatch?key=prefix." >> $testsubdir/test.out
curl --silent "localhost:8080/fwmatch?key=prefix." >> $testsubdir/test.out

# now swap the db and check keys again
ln -s -f test2.tab test.db
Expand Down
3 changes: 3 additions & 0 deletions sortdb/test.tab
Expand Up @@ -8,6 +8,9 @@ i j
k l
m n
o p
prefix.1 how
prefix.2 are
prefix.3 you
q r
s t
u v
Expand Down

0 comments on commit 3f5e21f

Please sign in to comment.