Skip to content

Commit 79b01e5

Browse files
committed
Updated README with latest
1 parent 162d2b6 commit 79b01e5

File tree

3 files changed

+88
-14
lines changed

3 files changed

+88
-14
lines changed

Diff for: README.md

+55
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,58 @@ int main() {
122122
}
123123

124124
```
125+
126+
### Roach server draft
127+
128+
Event based server (rely on [ev](https://github.com/codepr/ev.git) at least
129+
initially), TCP as the main transport protocol, text-based custom protocol
130+
inspired by RESP but simpler.
131+
132+
#### Simple query language
133+
134+
Definition of a simple, text-based format for clients to interact with the
135+
server, allowing them to send commands and receive responses.
136+
137+
##### Basic outline
138+
139+
- **Text-Based Format:** Use a text-based format where each command and
140+
response is represented as a single line of text.
141+
- **Commands:** Define a set of commands that clients can send to the server to
142+
perform various operations such as inserting data, querying data, and
143+
managing the database.
144+
- **Responses:** Define the format of responses that the server sends back to
145+
clients after processing commands. Responses should provide relevant
146+
information or acknowledge the completion of the requested operation.
147+
148+
##### Core commands
149+
150+
Define the basic operations in a SQL-like query language
151+
152+
- **CREATE** creates a database or a timeseries
153+
`CREATE <database name>`
154+
`CREATE <timeseries name> INTO <database name> [<retention period>] [<duplication policy>]`
155+
156+
- **INSERT** insertion of point(s) in a timeseries
157+
`INSERT <timeseries name> INTO <database name> <timestamp | *> <value>, ...`
158+
159+
- **SELECT** query a timeseries, selection of point(s) and aggregations
160+
`SELECT <timeseries name> FROM <database name> RANGE <start_timestamp> TO <end_timestamp> WHERE value [>|<|=|<=|>=|!=] <literal> AGGREGATE [AVG|MIN|MAX] BY <literal>`
161+
162+
- **DELETE** delete a timeseries or a database
163+
`DELETE <database name>`
164+
`DELETE <timeseries name> FROM <database name>`
165+
166+
##### Flow:
167+
168+
1. **Client Sends Command:** Clients send commands to the server in the
169+
specified text format.
170+
171+
2. **Server Parses Command:** The server parses the received command and
172+
executes the corresponding operation on the timeseries database.
173+
174+
3. **Server Sends Response:** After processing the command, the server sends a
175+
response back to the client indicating the result of the operation or
176+
providing requested data.
177+
178+
4. **Client Processes Response:** Clients receive and process the response from
179+
the server, handling success or error conditions accordingly.

Diff for: src/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ int main(void) {
325325
/* {.timestamp = 1982398, .value =
326326
* 0.7227}}}; */
327327

328-
/* roachdb_server_run("127.0.0.1", 17678); */
328+
roachdb_server_run("127.0.0.1", 17678);
329329

330330
return 0;
331331
}

Diff for: src/server.c

+32-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "ev_tcp.h"
55
#include "logging.h"
66
#include "parser.h"
7+
#include "protocol.h"
78
#include "server.h"
89
#include "timeseries.h"
910

@@ -59,9 +60,8 @@ static void execute_statement(Statement *statement) {
5960
} else {
6061
for (size_t i = 0; i < vec_size(coll); i++) {
6162
Record r = vec_at(coll, i);
62-
log_info(" %lu {.sec: %lu, .nsec: %lu .value: %.02f }", r.timestamp,
63-
r.tv.tv_sec, r.tv.tv_nsec, r.value);
64-
63+
log_info(" %lu {.sec: %lu, .nsec: %lu .value: %.02f }",
64+
r.timestamp, r.tv.tv_sec, r.tv.tv_nsec, r.value);
6565
}
6666
}
6767
}
@@ -92,23 +92,42 @@ static void on_write(ev_tcp_handle *client) {
9292
static void on_data(ev_tcp_handle *client) {
9393
if (client->buffer.size == 0)
9494
return;
95-
96-
char *line_start = client->buffer.buf;
97-
char *line_end;
98-
99-
while ((line_end = strstr(line_start, "\r\n")) != NULL) {
100-
*line_end = '\0';
101-
log_info("Line received %s", line_start);
102-
// Line gets processed here
95+
log_info("Data: %s", client->buffer.buf);
96+
Request rq;
97+
Response rs;
98+
ssize_t n = decode_request((const uint8_t *)client->buffer.buf, &rq);
99+
if (n < 0) {
100+
log_error("Can't decode a request from data");
101+
rs.type = STRING_RSP;
102+
rs.string_response.rc = 1;
103+
strncpy(rs.string_response.message, "Err", 4);
104+
rs.string_response.length = 4;
105+
} else {
103106
// Parse into Statement
104107
size_t total_tokens;
105-
Token *tokens = tokenize(line_start, &total_tokens);
108+
Token *tokens = tokenize(rq.query, &total_tokens);
106109
Statement statement = parse(tokens, total_tokens);
107110
// Execute it
108111
execute_statement(&statement);
109-
line_start = line_end + 2;
112+
rs.type = STRING_RSP;
113+
rs.string_response.rc = 0;
114+
strncpy(rs.string_response.message, "Ok", 3);
115+
rs.string_response.length = 3;
110116
}
111117

118+
(void)encode_response(&rs, (uint8_t *)client->buffer.buf);
119+
/* line_start = line_end + 2; */
120+
121+
/* char *line_start = client->buffer.buf; */
122+
/* char *line_end; */
123+
124+
/* while ((line_end = strstr(line_start, "\r\n")) != NULL) { */
125+
/* *line_end = '\0'; */
126+
/* log_info("Line received %s", line_start); */
127+
/* // Line gets processed here */
128+
/* } */
129+
/* } */
130+
112131
ev_tcp_queue_write(client);
113132
}
114133

0 commit comments

Comments
 (0)