Skip to content

Commit

Permalink
Refactor getting object by type
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberfined committed Sep 2, 2023
1 parent bbbf351 commit 2ba9f0f
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 77 deletions.
8 changes: 6 additions & 2 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,14 @@ static inline bool print_response(
res &= write_to_output(out, "\"", 1);
break;
case RES_INT:
case RES_UINT:
uint32_t ival;
memcpy(&ival, &buf[RESPONSE_TYPE_LEN], INT_LEN);
ival = ntohl(ival);
num_size = itoa((int32_t)ival, num_buf);
if(type == RES_INT)
num_size = itoa((int32_t)ival, num_buf);
else
num_size = utoa(ival, num_buf);
res = write_to_output(out, num_buf, num_size);
break;
case RES_DOUBLE:
Expand Down Expand Up @@ -399,7 +403,7 @@ static bool read_and_print_response(
if(type == RES_NIL) {
msg_len = 0;
is_msg_len_set = true;
} else if(type == RES_INT || type == RES_ERR) {
} else if(type == RES_INT || type == RES_UINT || type == RES_ERR) {
msg_len = INT_LEN;
is_msg_len_set = true;
} else if(type == RES_DOUBLE) {
Expand Down
16 changes: 11 additions & 5 deletions event.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,29 @@
#endif

EventLoop* create_event_loop(size_t max_events) {
EventLoop *event_loop = malloc(sizeof(EventLoop));
EventLoop *event_loop = calloc(1, sizeof(EventLoop));
if(!event_loop) {
perror("create_event_loop (malloc)");
return NULL;
perror("create_event_loop (calloc)");
goto error;
}

event_loop->max_events = max_events;
event_loop->events = malloc(sizeof(Event) * max_events);
if(!event_loop->events) {
perror("create_event_loop (malloc)");
return NULL;
goto error;
}

if(!init_event_api(event_loop))
return NULL;
goto error;

return event_loop;
error:
if(event_loop) {
if(event_loop->events) free(event_loop->events);
free(event_loop);
}
return NULL;
}

void free_event_loop(EventLoop *event_loop) {
Expand Down
4 changes: 2 additions & 2 deletions hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ static uint32_t jenkins_hash(const void *_key, size_t length) {
return hash;
}

HashTableNode* hash_table_get(HashTable *htable, char *key) {
HashTableNode* hash_table_get(HashTable *htable, const char *key) {
if(htable->size == 0)
return NULL;

Expand Down Expand Up @@ -177,7 +177,7 @@ static void rehash(HashTable *htable, int next_capacity_index) {
next_capacity = capacities[next_capacity_index];
HashTableNode **new_buckets = realloc(
htable->buckets,
next_capacity * sizeof(HashTableNode**)
next_capacity * sizeof(HashTableNode*)
);
if(!new_buckets) {
perror("hash_table_rehash (realloc)");
Expand Down
2 changes: 1 addition & 1 deletion hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ static inline bool hash_table_has_next(HashTableIterator *it) {
return it->node != NULL;
}
void hash_table_next(HashTableIterator *it);
HashTableNode* hash_table_get(HashTable *htable, char *key);
HashTableNode* hash_table_get(HashTable *htable, const char *key);
HashTableNode* hash_table_set(HashTable *htable, char *key, void *value);
void hash_table_remove(HashTable *htable, HashTableNode *node);
21 changes: 10 additions & 11 deletions keys_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ void get_handler(void) {
if(!next_string_arg(&arg_state, &key))
return;

HashTableNode *value_node = hash_table_get(state.keys, key);
if(!value_node) {
char *value;
if(!get_string_by_key(key, &value))
goto end;

if(value)
send_str(value, strlen(value));
else
send_nil();
} else {
Object *obj = value_node->value;
if(obj->type != OBJ_STRING)
send_err(ERR_TYPE_MISMATCH);
else
send_str(obj->ptr, strlen(obj->ptr));
}
end:
cmd_restore(&arg_state);
}

Expand Down Expand Up @@ -73,15 +72,15 @@ void del_handler(void) {
if(!next_string_arg(&arg_state, &key))
return;
HashTableNode *value_node = hash_table_get(state.keys, key);
int32_t result;
uint32_t result;
if(value_node) {
hash_table_remove(state.keys, value_node);
result = 1;
} else {
result = 0;
}
cmd_restore(&arg_state);
send_int(result);
send_uint(result);
}

// KEYS
Expand Down
1 change: 1 addition & 0 deletions proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef enum {
RES_ERR,
RES_STR,
RES_INT,
RES_UINT,
RES_DOUBLE,
RES_ARR,
RES_MAX
Expand Down
29 changes: 29 additions & 0 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ bool send_int(int32_t val) {
return generic_send_int(RES_INT, val);
}

bool send_uint(uint32_t val) {
return generic_send_int(RES_UINT, val);
}

bool send_err(ErrorCode code) {
return generic_send_int(RES_ERR, code);
}
Expand Down Expand Up @@ -252,6 +256,31 @@ void end_arr(uint32_t size) {
memcpy(&buf[RESPONSE_TYPE_LEN], &size, SIZE_SEGMENT_LEN);
}

static inline bool assert_type_get(ObjectType type, const char *key, void **value) {
HashTableNode *node = hash_table_get(state.keys, key);
if(!node) {
*value = NULL;
return true;
}

Object *obj = node->value;
if(obj->type != type) {
send_err(ERR_TYPE_MISMATCH);
return false;
}

*value = obj->ptr;
return true;
}

bool get_string_by_key(const char *key, char **string) {
return assert_type_get(OBJ_STRING, key, (void**)string);
}

bool get_zset_by_key(const char *key, ZSet **zset) {
return assert_type_get(OBJ_ZSET, key, (void**)zset);
}

static inline void send_command_error(CommandCheckResult error) {
Conn *conn = state.current_client;
ErrorCode code;
Expand Down
5 changes: 5 additions & 0 deletions server.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "event.h"
#include "hashtable.h"
#include "object.h"
#include "zset.h"

typedef struct {
int fd;
Expand Down Expand Up @@ -79,11 +80,15 @@ void cmd_restore(CmdArgState *arg_state);
bool send_nil(void);
bool send_str(const char *msg, uint32_t msg_len);
bool send_int(int32_t val);
bool send_uint(uint32_t val);
bool send_double(double val);
bool send_err(ErrorCode code);
bool send_arr(void);
void end_arr(uint32_t size);

bool get_string_by_key(const char *key, char **string);
bool get_zset_by_key(const char *key, ZSet **zset);

// key-value commands
void get_handler(void);
void set_handler(void);
Expand Down
Loading

0 comments on commit 2ba9f0f

Please sign in to comment.