Skip to content

Commit

Permalink
Use newlines not null characters to seperate data in the mock db prot…
Browse files Browse the repository at this point in the history
…ocol
  • Loading branch information
creationix committed May 16, 2012
1 parent 72bb2b0 commit 3a65fab
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.markdown
Expand Up @@ -12,9 +12,9 @@ To accept the challenge, fork this repo on github, add an implementation of the

Most real apps have some sort of persistent database. Provided in this repo is a simple mock database. The source is in `db.c`. Build is using the included Makefile. Make sure to pull in the libuv submodule.

This is a small and fast event-based tcp server. To query, simply connect and write your key to the socket as a null-terminated string with table first, then row key. For example, the key `orange` in the table `colors` would be queried with `"colors/orange\0"`.
This is a small and fast event-based tcp server. To query, simply connect and write your key to the socket as a newling-terminated string with table first, then row key. For example, the key `orange` in the table `colors` would be queried with `"colors/orange\n"`.

It will then respond with the JSON representation of that entry as a null terminated string. This single-threaded server is event-based so you can have as many concurrent connections to it as you want.
It will then respond with the JSON representation of that entry as a newline terminated string. This single-threaded server is event-based so you can have as many concurrent connections to it as you want.

Also it supports pipelining. Meaning you can send a second query on the same socket connection before waiting for the response to the first.

Expand Down
17 changes: 9 additions & 8 deletions db.c
Expand Up @@ -15,8 +15,8 @@ typedef struct {
int offset;
} client_t;

static const char user_key[] = "users/creationix";
static int user_key_len = sizeof(user_key);
static const char user_key[] = "users/creationix\n";
static int user_key_len = sizeof(user_key) - 1;

static char user[] = \
"{\"name\":\"Tim Caswell\"" \
Expand All @@ -25,17 +25,17 @@ static char user[] = \
",\"irc\":\"creationix\"" \
",\"projects\":[\"node\",\"Luvit\",\"Luvmonkey\",\"candor.io\",\"vfs\",\"architect\",\"wheat\",\"step\"]"\
",\"websites\":[\"http://howtonode.org/\",\"http://creationix.com/\",\"http://nodebits.org/\"]"\
"}";
static int user_len = sizeof(user);
"}\n";
static int user_len = sizeof(user) - 1;

static const char session_key[] = "sessions/eo299pqyw9791jie7yp";
static int session_key_len = sizeof(session_key);
static const char session_key[] = "sessions/eo299pqyw9791jie7yp\n";
static int session_key_len = sizeof(session_key) - 1;

static char session[] = \
"{\"username\": \"creationix\"" \
",\"pageViews\": 0" \
"}";
static int session_len = sizeof(session);
"}\n";
static int session_len = sizeof(session) - 1;


static uv_buf_t on_alloc(uv_handle_t* handle, size_t suggested_size) {
Expand Down Expand Up @@ -146,6 +146,7 @@ int main(int argc, char *argv[]) {
}
int port = atoi(argv[1]);


uv_tcp_t server;

uv_tcp_init(uv_default_loop(), &server);
Expand Down
2 changes: 1 addition & 1 deletion testdb.js
Expand Up @@ -6,7 +6,7 @@ function connect(port, callback) {
var db = new EventEmitter();
db.query = function (table, key, callback) {
callbacks.push(callback);
socket.write(table + "/" + key + "\0");
socket.write(table + "/" + key + "\n");
};
db.close = function () {
socket.end();
Expand Down
2 changes: 1 addition & 1 deletion testdb.lua
Expand Up @@ -27,7 +27,7 @@ local function connect(port, callback)
local db = Emitter:new()
local socket
function db.query(table, key, callback)
socket:write(table .. "/" .. key .. "\0")
socket:write(table .. "/" .. key .. "\n")
Queue.push(callbacks, callback)
end
function db.close()
Expand Down

0 comments on commit 3a65fab

Please sign in to comment.