Skip to content

Commit 5bb1d51

Browse files
committed
fix
1 parent a0a797f commit 5bb1d51

File tree

8 files changed

+107
-47
lines changed

8 files changed

+107
-47
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"uio.h": "c",
55
"data.private.h": "c",
66
"string.h": "c",
7-
"stdio.h": "c"
7+
"stdio.h": "c",
8+
"stdlib.h": "c"
89
}
910
}

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ debug_kadb: src/data.c src/socket.c src/ring.c src/main.c
1010
$(DEBUG_CC) -o $@ $^ -luring
1111

1212
test: test_data kadb
13+
cp -r init_data data
1314
./test_data
1415
diff data.dot data.ground.dot
1516

@@ -22,4 +23,4 @@ test_%: src/%.c test/%.c
2223
$(CC) -o $@ $^ -luring
2324

2425
clean:
25-
rm -rf kadb test_*
26+
rm -rf kadb test_* debug_* data

data.ground.dot

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
digraph {
2-
p[shape=record,label="{key=p|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000004|acc_lens=0000000000000006|raw=+dummy}"]
3-
n[shape=record,label="{key=n|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000002|acc_lens=0000000000000006|raw=+dummy}"]
4-
b[shape=record,label="{key=b|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000001|acc_lens=0000000000000003|raw=+12}"]
5-
a[shape=record,label="{key=a|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000000|acc_lens=0000000000000002|raw=+1}"]
2+
o[shape=record,label="{key=o|max_items=0000000000000004|n_items=0000000000000003|height=0000000000000004|acc_lens=00000000000000040000000000000009000000000000000f|raw=123;4567;dummy;}"]
3+
b[shape=record,label="{key=b|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000002|acc_lens=0000000000000003|raw=12;}"]
4+
a[shape=record,label="{key=a|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000000|acc_lens=0000000000000002|raw=1;}"]
65
b -> a
7-
m[shape=record,label="{key=m|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000000|acc_lens=0000000000000006|raw=+dummy}"]
6+
m[shape=record,label="{key=m|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000001|acc_lens=0000000000000006|raw=dummy;}"]
7+
n[shape=record,label="{key=n|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000000|acc_lens=0000000000000006|raw=dummy;}"]
8+
m -> n
89
b -> m
9-
n -> b
10-
o[shape=record,label="{key=o|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000000|acc_lens=0000000000000006|raw=+dummy}"]
11-
n -> o
12-
p -> n
13-
cd[shape=record,label="{key=cd|max_items=0000000000000004|n_items=0000000000000003|height=0000000000000003|acc_lens=00000000000000040000000000000009000000000000000f|raw=+345+6789+dummy}"]
14-
r[shape=record,label="{key=r|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000002|acc_lens=0000000000000006|raw=+dummy}"]
15-
q[shape=record,label="{key=q|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000000|acc_lens=0000000000000006|raw=+dummy}"]
16-
r -> q
17-
s[shape=record,label="{key=s|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000001|acc_lens=0000000000000006|raw=+dummy}"]
18-
t[shape=record,label="{key=t|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000000|acc_lens=0000000000000006|raw=+dummy}"]
10+
o -> b
11+
cd[shape=record,label="{key=cd|max_items=0000000000000004|n_items=0000000000000003|height=0000000000000003|acc_lens=00000000000000040000000000000009000000000000000f|raw=345;6789;dummy;}"]
12+
q[shape=record,label="{key=q|max_items=0000000000000004|n_items=0000000000000003|height=0000000000000002|acc_lens=00000000000000050000000000000009000000000000000f|raw=abcd;efg;dummy;}"]
13+
p[shape=record,label="{key=p|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000000|acc_lens=0000000000000006|raw=dummy;}"]
14+
q -> p
15+
s[shape=record,label="{key=s|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000001|acc_lens=0000000000000006|raw=dummy;}"]
16+
r[shape=record,label="{key=r|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000000|acc_lens=0000000000000006|raw=dummy;}"]
17+
s -> r
18+
t[shape=record,label="{key=t|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000000|acc_lens=0000000000000006|raw=dummy;}"]
1919
s -> t
20-
r -> s
21-
cd -> r
22-
efg[shape=record,label="{key=efg|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000001|acc_lens=0000000000000006|raw=+dummy}"]
23-
hijk[shape=record,label="{key=hijk|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000000|acc_lens=0000000000000006|raw=+dummy}"]
20+
q -> s
21+
cd -> q
22+
efg[shape=record,label="{key=efg|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000001|acc_lens=0000000000000006|raw=dummy;}"]
23+
hijk[shape=record,label="{key=hijk|max_items=0000000000000001|n_items=0000000000000001|height=0000000000000000|acc_lens=0000000000000006|raw=dummy;}"]
2424
efg -> hijk
2525
cd -> efg
26-
p -> cd
26+
o -> cd
2727
}

include/utils.h

+38-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
#include <sys/uio.h>
44
#include <string.h>
5+
#include <alloca.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <stdint.h>
9+
#include <string.h>
10+
11+
static const uint8_t DATA_DIR[] = "data/";
512

613
static inline const int iovcmp(
714
const struct iovec a,
@@ -25,4 +32,34 @@ static inline const int iovncmp(
2532
static inline const struct iovec data_vec(u_int8_t *s)
2633
{
2734
return (const struct iovec){.iov_len = strlen(s), .iov_base = s};
28-
}
35+
}
36+
37+
static inline FILE *open_key_file(
38+
const struct iovec key)
39+
{
40+
size_t len = strlen(DATA_DIR);
41+
char *filename = alloca(len + key.iov_len + 1);
42+
memcpy(filename, DATA_DIR, len);
43+
memcpy(filename + len, key.iov_base, key.iov_len);
44+
filename[len + key.iov_len] = 0;
45+
FILE *f = fopen(filename, "a");
46+
return f;
47+
}
48+
49+
static inline const struct iovec read_key(
50+
const char *key)
51+
{
52+
char *filename = alloca(strlen(DATA_DIR) + strlen(key) + 1);
53+
sprintf(filename, "%s%s", DATA_DIR, key);
54+
FILE *f = fopen(filename, "rb");
55+
fseek(f, 0, SEEK_END);
56+
size_t fsize = ftell(f);
57+
fseek(f, 0, SEEK_SET);
58+
59+
uint8_t *iov_base = malloc(fsize);
60+
fread(iov_base, fsize, 1, f);
61+
fclose(f);
62+
return (const struct iovec){
63+
.iov_len = fsize,
64+
.iov_base = iov_base};
65+
}

init_data/o

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
123;4567;

init_data/q

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
abcd;efg;

src/data.c

+41-22
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <unistd.h>
1212
#include <stdio.h>
1313
#include <sys/stat.h>
14+
#include <dirent.h>
1415

1516
#define MAX(a, b) ((a) > (b) ? (a) : (b))
1617
#define MIN(a, b) ((a) < (b) ? (a) : (b))
@@ -82,23 +83,12 @@ Node *right_rotate(Node *node)
8283
return left;
8384
}
8485

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(
86+
void write_value(
9787
const struct iovec key,
9888
const struct iovec value)
9989
{
100-
FILE *f = open_file(key);
101-
fprintf(f, "+%*s", (int)value.iov_len, (char *)value.iov_base);
90+
FILE *f = open_key_file(key);
91+
fprintf(f, "%*s;", (int)value.iov_len, (char *)value.iov_base);
10292
fclose(f);
10393
}
10494

@@ -124,10 +114,10 @@ Node *new_node(
124114
},
125115
}};
126116
memcpy(node->key.iov_base, key.iov_base, key.iov_len);
127-
((uint8_t *)node->array.raw.iov_base)[0] = '+';
128-
memcpy(node->array.raw.iov_base + 1, value.iov_base, value.iov_len);
117+
memcpy(node->array.raw.iov_base, value.iov_base, value.iov_len);
118+
((uint8_t *)node->array.raw.iov_base)[value.iov_len] = ';';
129119
node->array.acc_lens[0] = value.iov_len + 1;
130-
append_file(key, value);
120+
write_value(key, value);
131121
return node;
132122
}
133123

@@ -166,12 +156,12 @@ Node *put(
166156
array->raw.iov_len * sizeof(uint8_t));
167157
}
168158
array->acc_lens[array->n_items - 1] = acc_len;
169-
((uint8_t *)node->array.raw.iov_base)[previous_acc_len] = '+';
170159
memcpy(
171-
array->raw.iov_base + previous_acc_len + 1,
160+
array->raw.iov_base + previous_acc_len,
172161
value.iov_base,
173162
value.iov_len);
174-
append_file(key, value);
163+
((uint8_t *)node->array.raw.iov_base)[acc_len - 1] = ';';
164+
write_value(key, value);
175165
}
176166
else if (cmp > 0)
177167
{
@@ -258,7 +248,36 @@ const DataGetResult data_get(
258248

259249
void data_init()
260250
{
261-
mkdir("data/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
251+
mkdir(DATA_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
252+
DIR *dir = opendir(DATA_DIR);
253+
if (dir)
254+
{
255+
struct dirent *entry;
256+
while ((entry = readdir(dir)) != NULL)
257+
{
258+
if (entry->d_type == DT_REG)
259+
{
260+
const struct iovec
261+
key = {
262+
.iov_len = strlen(entry->d_name),
263+
.iov_base = entry->d_name},
264+
raw = read_key(entry->d_name);
265+
size_t from = 0;
266+
for (size_t i = 0; i < raw.iov_len; i++)
267+
{
268+
if (((uint8_t *)raw.iov_base)[i] == ';')
269+
{
270+
const struct iovec value = {
271+
.iov_len = i - from,
272+
.iov_base = raw.iov_base + from};
273+
data_put(key, value);
274+
from = i + 1;
275+
}
276+
}
277+
}
278+
}
279+
closedir(dir);
280+
}
262281
}
263282

264283
void serialize_arrow(
@@ -350,4 +369,4 @@ void data_serialize()
350369
}
351370
writev(fd, &end, 1);
352371
close(fd);
353-
}
372+
}

test/data.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ int main()
5252
data_get(data_vec("cd"), 0, 2),
5353
(const DataGetResult){
5454
.n_items = 3,
55-
.raw = data_vec("+345+6789")});
55+
.raw = data_vec("345;6789;")});
5656

5757
// get 2 items with invalid len
5858
result_cmp(
5959
data_get(data_vec("cd"), 1, 3),
6060
(const DataGetResult){
6161
.n_items = 3,
62-
.raw = data_vec("+6789+dummy")});
62+
.raw = data_vec("6789;dummy;")});
6363
}

0 commit comments

Comments
 (0)