summaryrefslogtreecommitdiff
path: root/c_src/permdb.c
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2016-02-11 15:51:34 +0100
committerLinus Nordberg <linus@nordu.net>2016-04-25 13:14:11 +0200
commit027f53e35bc57e0046bbb3c02f74e8504961b499 (patch)
treec51882957b0d206ec72f6271b7dd1d2853a5703b /c_src/permdb.c
parentac36edf0168b9b783e118812f3192b5b4f4d4505 (diff)
Added rudimentary support for chunks.
This means that the implementation should be compatible with the specification.
Diffstat (limited to 'c_src/permdb.c')
-rw-r--r--c_src/permdb.c25
1 files changed, 22 insertions, 3 deletions
diff --git a/c_src/permdb.c b/c_src/permdb.c
index c6ae1c9..624e1da 100644
--- a/c_src/permdb.c
+++ b/c_src/permdb.c
@@ -1087,6 +1087,16 @@ readnet32(void *ptr)
return ntohl(data);
}
+static uint16_t
+readnet16(void *ptr)
+{
+ uint16_t data;
+
+ memcpy(&data, ptr, sizeof(data));
+
+ return ntohs(data);
+}
+
static unsigned char *
readdatakeyandlen(permdb_object *state, node_offset offset, size_t *datalen)
{
@@ -1100,7 +1110,11 @@ readdatakeyandlen(permdb_object *state, node_offset offset, size_t *datalen)
return NULL;
}
unsigned char *result = memsub(data, sizeof(data_entry_cookie), keylen);
- *datalen = readnet32(data+sizeof(data_entry_cookie)+keylen);
+ uint16_t nchunks = readnet16(data+sizeof(data_entry_cookie)+keylen);
+ *datalen = readnet16(data+sizeof(data_entry_cookie)+keylen+sizeof(uint16_t));
+ if (nchunks != 1) {
+ errx(1, "number of chunks is %d, but only one chunk is supported right now", nchunks);
+ }
free(data);
return result;
}
@@ -1115,7 +1129,11 @@ readdata(permdb_object *state, node_offset offset, size_t datalen)
static node_offset
writedata(permdb_object *state, const unsigned char *key, const unsigned char *data, size_t datalength)
{
- uint32_t coded_datalength = htonl(datalength);
+ if (datalength > 65535) {
+ errx(1, "data length is %zu, but only < 64K lengths are supported right now", datalength);
+ }
+ uint16_t chunk_length = htons(datalength);
+ uint16_t nchunks = htons(1);
node_offset offset = state->datafile.datasize;
#if DEBUG_WRITE
@@ -1123,7 +1141,8 @@ writedata(permdb_object *state, const unsigned char *key, const unsigned char *d
#endif
writebuffer_add(&state->datafile, &data_entry_cookie, sizeof(data_entry_cookie));
writebuffer_add(&state->datafile, key, keylen);
- writebuffer_add(&state->datafile, &coded_datalength, 4);
+ writebuffer_add(&state->datafile, &nchunks, sizeof(uint16_t));
+ writebuffer_add(&state->datafile, &chunk_length, sizeof(uint16_t));
writebuffer_add(&state->datafile, data, datalength);
return offset;