Skip to content

Commit a0a797f

Browse files
committed
feat: write to file
1 parent 9f0a393 commit a0a797f

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ test_*
33
debug_*
44
*.dot
55
!*.ground.dot
6+
data

include/data.h

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "types.h"
44

5+
void data_init();
6+
57
const DataGetResult data_get(
68
const struct iovec key,
79
const size_t offset,

src/data.c

+31-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <fcntl.h>
1111
#include <unistd.h>
1212
#include <stdio.h>
13+
#include <sys/stat.h>
1314

1415
#define MAX(a, b) ((a) > (b) ? (a) : (b))
1516
#define MIN(a, b) ((a) < (b) ? (a) : (b))
@@ -81,6 +82,26 @@ Node *right_rotate(Node *node)
8182
return left;
8283
}
8384

85+
FILE *open_file(
86+
const struct iovec key)
87+
{
88+
char *filename = alloca(5 + key.iov_len);
89+
memcpy(filename, "data/", 5);
90+
memcpy(filename + 5, key.iov_base, key.iov_len);
91+
filename[5 + key.iov_len] = 0;
92+
FILE *f = fopen(filename, "a");
93+
return f;
94+
}
95+
96+
void append_file(
97+
const struct iovec key,
98+
const struct iovec value)
99+
{
100+
FILE *f = open_file(key);
101+
fprintf(f, "+%*s", (int)value.iov_len, (char *)value.iov_base);
102+
fclose(f);
103+
}
104+
84105
Node *new_node(
85106
const struct iovec key,
86107
const struct iovec value)
@@ -106,6 +127,7 @@ Node *new_node(
106127
((uint8_t *)node->array.raw.iov_base)[0] = '+';
107128
memcpy(node->array.raw.iov_base + 1, value.iov_base, value.iov_len);
108129
node->array.acc_lens[0] = value.iov_len + 1;
130+
append_file(key, value);
109131
return node;
110132
}
111133

@@ -149,6 +171,7 @@ Node *put(
149171
array->raw.iov_base + previous_acc_len + 1,
150172
value.iov_base,
151173
value.iov_len);
174+
append_file(key, value);
152175
}
153176
else if (cmp > 0)
154177
{
@@ -233,7 +256,12 @@ const DataGetResult data_get(
233256
return empty_result;
234257
}
235258

236-
void write_arrow(
259+
void data_init()
260+
{
261+
mkdir("data/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
262+
}
263+
264+
void serialize_arrow(
237265
int fd,
238266
const struct iovec a,
239267
const struct iovec b)
@@ -297,12 +325,12 @@ void serialize(
297325
if (node->left)
298326
{
299327
serialize(fd, node->left);
300-
write_arrow(fd, node->key, node->left->key);
328+
serialize_arrow(fd, node->key, node->left->key);
301329
}
302330
if (node->right)
303331
{
304332
serialize(fd, node->right);
305-
write_arrow(fd, node->key, node->right->key);
333+
serialize_arrow(fd, node->key, node->right->key);
306334
}
307335
}
308336

src/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ int main()
147147
{
148148
signal(SIGINT, sigint);
149149
int socket = socket_create(PORT, MAX_CONNS);
150+
data_init();
150151
printf("Listening on http://localhost:%d\n", PORT);
151152
ring_listen(socket, MAX_QUEUE, handle_request);
152153
}

test/data.c

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void result_cmp(const DataGetResult a, const DataGetResult b)
1414

1515
int main()
1616
{
17+
data_init();
1718
#define COUNT 15
1819
u_int8_t *x[COUNT][2] = {
1920
{"a", "1"},

0 commit comments

Comments
 (0)