Skip to content

Commit

Permalink
Add small macro to set string responses on the mini server
Browse files Browse the repository at this point in the history
  • Loading branch information
codepr committed Apr 6, 2024
1 parent 33d5c9f commit 226ebc1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 44 deletions.
37 changes: 20 additions & 17 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,30 +267,33 @@ int main(void) {

/* tsdb_close(db); */

Statement s = parse("CREATE timeseries");
/* Statement s = parse("CREATE timeseries"); */

print_statement(&s);
printf("\n");
/* print_statement(&s); */
/* printf("\n"); */

s = parse("INSERT temperatures INTO db_test 12, 98.2, 15, 96.2, 18, 99.1 ");
/* s = parse("INSERT temperatures INTO db_test 12, 98.2, 15, 96.2, 18, 99.1
* "); */

print_statement(&s);
printf("\n");
/* print_statement(&s); */
/* printf("\n"); */

s = parse("SELECT temperatures FROM test_db RANGE 10 TO 45 WHERE value > "
"67.8 AGGREGATE AVG BY 3600");
/* s = parse("SELECT temperatures FROM test_db RANGE 10 TO 45 WHERE value >
* " */
/* "67.8 AGGREGATE AVG BY 3600"); */

print_statement(&s);
/* print_statement(&s); */

uint8_t dst[64];
Response rs = {.type = STRING_RSP,
.string_response = {.length = 3, .rc = 0, .message = "Ok!"}};
encode_response(&rs, &dst[0]);
printf("%s", dst);
Response rsb;
decode_response(&dst[0], &rsb);
printf("(%i) %s (%lu)\n", rsb.type, rsb.string_response.message,
rsb.string_response.length);
/* Response rs = {.type = STRING_RSP, */
/* .string_response = {.length = 3, .rc = 0, .message =
* "Ok!"}}; */
/* encode_response(&rs, &dst[0]); */
/* printf("%s", dst); */
/* Response rsb; */
/* decode_response(&dst[0], &rsb); */
/* printf("(%i) %s (%lu)\n", rsb.type, rsb.string_response.message, */
/* rsb.string_response.length); */

Request rq = {.length = 36,
.query = "SELECT temp FROM test RANGE 10 TO 45"};
Expand Down
2 changes: 1 addition & 1 deletion src/roach_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void print_response(const Response *rs) {
printf("%s\n", rs->string_response.message);
} else {
for (size_t i = 0; i < rs->array_response.length; ++i)
printf("%lu %.20f\n", rs->array_response.records[i].timestamp,
printf("%lu %.6f\n", rs->array_response.records[i].timestamp,
rs->array_response.records[i].value);
}
}
Expand Down
67 changes: 41 additions & 26 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@

#define BACKLOG 128

#define add_string_response(resp, str, rc) \
do { \
(resp).type = STRING_RSP; \
size_t length = strlen((str)); \
memset((resp).string_response.message, 0x00, \
sizeof((resp).string_response.message)); \
strncpy((resp).string_response.message, (str), length); \
(resp).string_response.length = length; \
} while (0)

// testing dummy
static Timeseries_DB *db = NULL;

Expand All @@ -24,28 +34,33 @@ static Response execute_statement(const Statement *statement) {
case STATEMENT_CREATE:
if (statement->create.mask == 0) {
db = tsdb_init(statement->create.db_name);
if (!db)
goto err;
} else {
if (!db)
db = tsdb_init(statement->create.db_name);

if (!db)
goto err;

ts = ts_create(db, statement->create.ts_name, 0, DP_IGNORE);
}
rs.type = STRING_RSP;
rs.string_response.rc = err;
if (!ts) {
strncpy(rs.string_response.message, "Nok", 4);
rs.string_response.length = 4;
} else {
strncpy(rs.string_response.message, "Ok", 3);
rs.string_response.length = 3;
}
if (!ts)
goto err;
else
add_string_response(rs, "Ok", 0);
break;
case STATEMENT_INSERT:
if (!db)
db = tsdb_init(statement->insert.db_name);

if (!db)
goto err;

ts = ts_get(db, statement->insert.ts_name);
if (!ts)
goto errdefer;
goto err_not_found;

uint64_t timestamp = 0;
for (size_t i = 0; i < statement->insert.record_len; ++i) {
if (statement->insert.records[i].timestamp == -1) {
Expand All @@ -54,27 +69,26 @@ static Response execute_statement(const Statement *statement) {
} else {
timestamp = statement->insert.records[i].timestamp;
}

err = ts_insert(ts, timestamp, statement->insert.records[i].value);
if (err < 0) {
rs.string_response.rc = err;
strncpy(rs.string_response.message, "Nok", 4);
rs.string_response.length = 4;
} else {
rs.string_response.rc = 0;
strncpy(rs.string_response.message, "Ok", 3);
rs.string_response.length = 3;
}
if (err < 0)
goto err;
else
add_string_response(rs, "Ok", 0);
}

break;
case STATEMENT_SELECT:
if (!db)
db = tsdb_init(statement->select.db_name);

if (!db)
goto err;

ts = ts_get(db, statement->select.ts_name);
if (!ts)
goto errdefer;
ts_print(ts);
goto err_not_found;

int err = 0;
Points coll;
vec_new(coll);
Expand All @@ -84,7 +98,7 @@ static Response execute_statement(const Statement *statement) {
if (err < 0) {
log_error("Couldn't find the record %lu",
statement->select.start_time);
goto errdefer;
goto err_not_found;
} else {
log_info("Record found: %lu %.2lf", r.timestamp, r.value);
rs.type = ARRAY_RSP;
Expand Down Expand Up @@ -124,12 +138,13 @@ static Response execute_statement(const Statement *statement) {

return rs;

errdefer:
err:
add_string_response(rs, "Err", err);
return rs;

rs.type = STRING_RSP;
rs.string_response.length = 9;
strncpy(rs.string_response.message, "Not found", 10);
err_not_found:

add_string_response(rs, "Not found", err);
return rs;
}

Expand Down

0 comments on commit 226ebc1

Please sign in to comment.