10
10
#include <fcntl.h>
11
11
#include <unistd.h>
12
12
#include <stdio.h>
13
+ #include <sys/stat.h>
13
14
14
15
#define MAX (a , b ) ((a) > (b) ? (a) : (b))
15
16
#define MIN (a , b ) ((a) < (b) ? (a) : (b))
@@ -81,6 +82,26 @@ Node *right_rotate(Node *node)
81
82
return left ;
82
83
}
83
84
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
+
84
105
Node * new_node (
85
106
const struct iovec key ,
86
107
const struct iovec value )
@@ -106,6 +127,7 @@ Node *new_node(
106
127
((uint8_t * )node -> array .raw .iov_base )[0 ] = '+' ;
107
128
memcpy (node -> array .raw .iov_base + 1 , value .iov_base , value .iov_len );
108
129
node -> array .acc_lens [0 ] = value .iov_len + 1 ;
130
+ append_file (key , value );
109
131
return node ;
110
132
}
111
133
@@ -149,6 +171,7 @@ Node *put(
149
171
array -> raw .iov_base + previous_acc_len + 1 ,
150
172
value .iov_base ,
151
173
value .iov_len );
174
+ append_file (key , value );
152
175
}
153
176
else if (cmp > 0 )
154
177
{
@@ -233,7 +256,12 @@ const DataGetResult data_get(
233
256
return empty_result ;
234
257
}
235
258
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 (
237
265
int fd ,
238
266
const struct iovec a ,
239
267
const struct iovec b )
@@ -297,12 +325,12 @@ void serialize(
297
325
if (node -> left )
298
326
{
299
327
serialize (fd , node -> left );
300
- write_arrow (fd , node -> key , node -> left -> key );
328
+ serialize_arrow (fd , node -> key , node -> left -> key );
301
329
}
302
330
if (node -> right )
303
331
{
304
332
serialize (fd , node -> right );
305
- write_arrow (fd , node -> key , node -> right -> key );
333
+ serialize_arrow (fd , node -> key , node -> right -> key );
306
334
}
307
335
}
308
336
0 commit comments