use buffered io with databases

This commit is contained in:
U-ALEIRADE-BR\aleirade 2015-05-30 13:17:11 -03:00
parent fbbc51f42b
commit a2a19ed827
6 changed files with 191 additions and 166 deletions

View File

@ -28,16 +28,22 @@ struct node_iter_ctx
static struct rmsgpack_dom_value sentinal;
static int libretrodb_read_metadata(int fd, libretrodb_metadata_t *md)
static inline off_t flseek(FILE *fp, off_t offset, int whence)
{
return rmsgpack_dom_read_into(fd, "count", &md->count, NULL);
fseeko(fp, offset, whence);
return ftello(fp);
}
static int libretrodb_write_metadata(int fd, libretrodb_metadata_t *md)
static int libretrodb_read_metadata(FILE *fp, libretrodb_metadata_t *md)
{
rmsgpack_write_map_header(fd, 1);
rmsgpack_write_string(fd, "count", strlen("count"));
return rmsgpack_write_uint(fd, md->count);
return rmsgpack_dom_read_into(fp, "count", &md->count, NULL);
}
static int libretrodb_write_metadata(FILE *fp, libretrodb_metadata_t *md)
{
rmsgpack_write_map_header(fp, 1);
rmsgpack_write_string(fp, "count", strlen("count"));
return rmsgpack_write_uint(fp, md->count);
}
static int validate_document(const struct rmsgpack_dom_value * doc)
@ -74,7 +80,7 @@ static int validate_document(const struct rmsgpack_dom_value * doc)
return rv;
}
int libretrodb_create(int fd, libretrodb_value_provider value_provider,
int libretrodb_create(FILE *fp, libretrodb_value_provider value_provider,
void * ctx)
{
int rv;
@ -85,19 +91,19 @@ int libretrodb_create(int fd, libretrodb_value_provider value_provider,
libretrodb_header_t header = {};
memcpy(header.magic_number, MAGIC_NUMBER, sizeof(MAGIC_NUMBER)-1);
root = lseek(fd, 0, SEEK_CUR);
root = flseek(fp, 0, SEEK_CUR);
/* We write the header in the end because we need to know the size of
* the db first */
lseek(fd, sizeof(libretrodb_header_t), SEEK_CUR);
flseek(fp, sizeof(libretrodb_header_t), SEEK_CUR);
while ((rv = value_provider(ctx, &item)) == 0)
{
if ((rv = validate_document(&item)) < 0)
goto clean;
if ((rv = rmsgpack_dom_write(fd, &item)) < 0)
if ((rv = rmsgpack_dom_write(fp, &item)) < 0)
goto clean;
item_count++;
@ -106,37 +112,37 @@ int libretrodb_create(int fd, libretrodb_value_provider value_provider,
if (rv < 0)
goto clean;
if ((rv = rmsgpack_dom_write(fd, &sentinal)) < 0)
if ((rv = rmsgpack_dom_write(fp, &sentinal)) < 0)
goto clean;
header.metadata_offset = httobe64(lseek(fd, 0, SEEK_CUR));
header.metadata_offset = httobe64(flseek(fp, 0, SEEK_CUR));
md.count = item_count;
libretrodb_write_metadata(fd, &md);
lseek(fd, root, SEEK_SET);
write(fd, &header, sizeof(header));
libretrodb_write_metadata(fp, &md);
flseek(fp, root, SEEK_SET);
fwrite(&header, 1, sizeof(header), fp);
clean:
rmsgpack_dom_value_free(&item);
return rv;
}
static int libretrodb_read_index_header(int fd, libretrodb_index_t *idx)
static int libretrodb_read_index_header(FILE *fp, libretrodb_index_t *idx)
{
uint64_t name_len = 50;
return rmsgpack_dom_read_into(fd,
return rmsgpack_dom_read_into(fp,
"name", idx->name, &name_len,
"key_size", &idx->key_size,
"next", &idx->next, NULL);
}
static void libretrodb_write_index_header(int fd, libretrodb_index_t * idx)
static void libretrodb_write_index_header(FILE *fp, libretrodb_index_t * idx)
{
rmsgpack_write_map_header(fd, 3);
rmsgpack_write_string(fd, "name", strlen("name"));
rmsgpack_write_string(fd, idx->name, strlen(idx->name));
rmsgpack_write_string(fd, "key_size", strlen("key_size"));
rmsgpack_write_uint(fd, idx->key_size);
rmsgpack_write_string(fd, "next", strlen("next"));
rmsgpack_write_uint(fd, idx->next);
rmsgpack_write_map_header(fp, 3);
rmsgpack_write_string(fp, "name", strlen("name"));
rmsgpack_write_string(fp, idx->name, strlen(idx->name));
rmsgpack_write_string(fp, "key_size", strlen("key_size"));
rmsgpack_write_uint(fp, idx->key_size);
rmsgpack_write_string(fp, "next", strlen("next"));
rmsgpack_write_uint(fp, idx->next);
}
void libretrodb_close(libretrodb_t *db)
@ -144,8 +150,8 @@ void libretrodb_close(libretrodb_t *db)
if (!db)
return;
close(db->fd);
db->fd = -1;
fclose(db->fp);
db->fp = NULL;
}
int libretrodb_open(const char *path, libretrodb_t *db)
@ -154,18 +160,18 @@ int libretrodb_open(const char *path, libretrodb_t *db)
libretrodb_metadata_t md;
int rv;
#ifdef _WIN32
int fd = open(path, O_RDWR | O_BINARY);
FILE *fp = fopen(path, "rb");
#else
int fd = open(path, O_RDWR);
FILE *fp = fopen(path, "r");
#endif
if (fd == -1)
if (fp == NULL)
return -errno;
strcpy(db->path, path);
db->root = lseek(fd, 0, SEEK_CUR);
db->root = flseek(fp, 0, SEEK_CUR);
if ((rv = read(fd, &header, sizeof(header))) == -1)
if ((rv = fread(&header, 1, sizeof(header), fp)) != sizeof(header))
{
rv = -errno;
goto error;
@ -178,37 +184,37 @@ int libretrodb_open(const char *path, libretrodb_t *db)
}
header.metadata_offset = betoht64(header.metadata_offset);
lseek(fd, header.metadata_offset, SEEK_SET);
flseek(fp, header.metadata_offset, SEEK_SET);
if (libretrodb_read_metadata(fd, &md) < 0)
if (libretrodb_read_metadata(fp, &md) < 0)
{
rv = -EINVAL;
goto error;
}
db->count = md.count;
db->first_index_offset = lseek(fd, 0, SEEK_CUR);
db->fd = fd;
db->first_index_offset = flseek(fp, 0, SEEK_CUR);
db->fp = fp;
return 0;
error:
close(fd);
fclose(fp);
return rv;
}
static int libretrodb_find_index(libretrodb_t *db, const char *index_name,
libretrodb_index_t *idx)
{
off_t eof = lseek(db->fd, 0, SEEK_END);
off_t offset = lseek(db->fd, db->first_index_offset, SEEK_SET);
off_t eof = flseek(db->fp, 0, SEEK_END);
off_t offset = flseek(db->fp, db->first_index_offset, SEEK_SET);
while (offset < eof)
{
libretrodb_read_index_header(db->fd, idx);
libretrodb_read_index_header(db->fp, idx);
if (strncmp(index_name, idx->name, strlen(idx->name)) == 0)
return 0;
offset = lseek(db->fd, idx->next, SEEK_CUR);
offset = flseek(db->fp, idx->next, SEEK_CUR);
}
return -1;
@ -264,7 +270,7 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
while (nread < bufflen)
{
void * buff_ = (uint64_t *)buff + nread;
rv = read(db->fd, buff_, bufflen - nread);
rv = fread(buff_, 1, bufflen - nread, db->fp);
if (rv <= 0)
{
@ -278,9 +284,9 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
free(buff);
if (rv == 0)
lseek(db->fd, offset, SEEK_SET);
flseek(db->fp, offset, SEEK_SET);
rv = rmsgpack_dom_read(db->fd, out);
rv = rmsgpack_dom_read(db->fp, out);
if (rv < 0)
return rv;
@ -299,7 +305,7 @@ int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
int libretrodb_cursor_reset(libretrodb_cursor_t *cursor)
{
cursor->eof = 0;
return lseek(cursor->fd,
return flseek(cursor->fp,
cursor->db->root + sizeof(libretrodb_header_t),
SEEK_SET);
}
@ -313,7 +319,7 @@ int libretrodb_cursor_read_item(libretrodb_cursor_t *cursor,
return EOF;
retry:
rv = rmsgpack_dom_read(cursor->fd, out);
rv = rmsgpack_dom_read(cursor->fp, out);
if (rv < 0)
return rv;
@ -343,9 +349,9 @@ void libretrodb_cursor_close(libretrodb_cursor_t *cursor)
if (!cursor)
return;
close(cursor->fd);
fclose(cursor->fp);
cursor->is_valid = 0;
cursor->fd = -1;
cursor->fp = NULL;
cursor->eof = 1;
cursor->db = NULL;
@ -368,9 +374,13 @@ void libretrodb_cursor_close(libretrodb_cursor_t *cursor)
int libretrodb_cursor_open(libretrodb_t *db, libretrodb_cursor_t *cursor,
libretrodb_query_t *q)
{
cursor->fd = dup(db->fd);
#ifdef _WIN32
cursor->fp = fopen(db->path, "rb");
#else
cursor->fp = fopen(db->path, "r");
#endif
if (cursor->fd == -1)
if (cursor->fp == NULL)
return -errno;
cursor->db = db;
@ -387,9 +397,9 @@ int libretrodb_cursor_open(libretrodb_t *db, libretrodb_cursor_t *cursor,
static int node_iter(void * value, void * ctx)
{
struct node_iter_ctx *nictx = (struct node_iter_ctx*)ctx;
size_t size = nictx->idx->key_size + sizeof(uint64_t);
if (write(nictx->db->fd, value,
nictx->idx->key_size + sizeof(uint64_t)) > 0)
if (fwrite(value, 1, size, nictx->db->fp) == size)
return 0;
return -1;
@ -397,7 +407,7 @@ static int node_iter(void * value, void * ctx)
static uint64_t libretrodb_tell(libretrodb_t *db)
{
return lseek(db->fd, 0, SEEK_CUR);
return ftello(db->fp);
}
int libretrodb_create_index(libretrodb_t *db,
@ -501,13 +511,13 @@ int libretrodb_create_index(libretrodb_t *db,
(void)rv;
(void)idx_header_offset;
idx_header_offset = lseek(db->fd, 0, SEEK_END);
idx_header_offset = flseek(db->fp, 0, SEEK_END);
strncpy(idx.name, name, 50);
idx.name[49] = '\0';
idx.key_size = field_size;
idx.next = db->count * (field_size + sizeof(uint64_t));
libretrodb_write_index_header(db->fd, &idx);
libretrodb_write_index_header(db->fp, &idx);
nictx.db = db;
nictx.idx = &idx;

View File

@ -1,6 +1,7 @@
#ifndef __LIBRETRODB_H__
#define __LIBRETRODB_H__
#include <stdio.h>
#include <stdint.h>
#ifdef _WIN32
#include <direct.h>
@ -19,7 +20,7 @@ typedef struct libretrodb_query libretrodb_query_t;
typedef struct libretrodb
{
int fd;
FILE *fp;
uint64_t root;
uint64_t count;
uint64_t first_index_offset;
@ -47,7 +48,7 @@ typedef struct libretrodb_header
typedef struct libretrodb_cursor
{
int is_valid;
int fd;
FILE *fp;
int eof;
libretrodb_query_t * query;
libretrodb_t * db;
@ -56,7 +57,7 @@ typedef struct libretrodb_cursor
typedef int (* libretrodb_value_provider)(void * ctx,
struct rmsgpack_dom_value * out);
int libretrodb_create(int fd, libretrodb_value_provider value_provider,
int libretrodb_create(FILE *fp, libretrodb_value_provider value_provider,
void * ctx);
void libretrodb_close(libretrodb_t * db);

View File

@ -55,7 +55,19 @@ static const uint8_t MPF_UINT64 = 0xcf;
static const uint8_t MPF_NIL = 0xc0;
int rmsgpack_write_array_header(int fd, uint32_t size)
static inline ssize_t fpwrite(FILE *fp, const void *buf, size_t count)
{
size_t num_written = fwrite(buf, 1, count, fp);
return num_written != count ? -1 : (ssize_t)count;
}
static inline ssize_t fpread(FILE *fp, void *buf, size_t count)
{
size_t num_read = fread(buf, 1, count, fp);
return num_read != count && ferror(fp) ? -1 : (ssize_t)num_read;
}
int rmsgpack_write_array_header(FILE *fp, uint32_t size)
{
uint16_t tmp_i16;
uint32_t tmp_i32;
@ -63,29 +75,29 @@ int rmsgpack_write_array_header(int fd, uint32_t size)
if (size < 16)
{
size = (size | MPF_FIXARRAY);
if (write(fd, &size, sizeof(int8_t)) == -1)
if (fpwrite(fp, &size, sizeof(int8_t)) == -1)
return -errno;
return sizeof(int8_t);
}
else if (size == (uint16_t)size)
{
if (write(fd, &MPF_ARRAY16, sizeof(MPF_ARRAY16)) == -1)
if (fpwrite(fp, &MPF_ARRAY16, sizeof(MPF_ARRAY16)) == -1)
return -errno;
tmp_i16 = httobe16(size);
if (write(fd, (void *)(&tmp_i16), sizeof(uint16_t)) == -1)
if (fpwrite(fp, (void *)(&tmp_i16), sizeof(uint16_t)) == -1)
return -errno;
return sizeof(int8_t) + sizeof(uint16_t);
}
if (write(fd, &MPF_ARRAY32, sizeof(MPF_ARRAY32)) == -1)
if (fpwrite(fp, &MPF_ARRAY32, sizeof(MPF_ARRAY32)) == -1)
return -errno;
tmp_i32 = httobe32(size);
if (write(fd, (void *)(&tmp_i32), sizeof(uint32_t)) == -1)
if (fpwrite(fp, (void *)(&tmp_i32), sizeof(uint32_t)) == -1)
return -errno;
return sizeof(int8_t) + sizeof(uint32_t);
}
int rmsgpack_write_map_header(int fd, uint32_t size)
int rmsgpack_write_map_header(FILE *fp, uint32_t size)
{
uint16_t tmp_i16;
uint32_t tmp_i32;
@ -93,29 +105,29 @@ int rmsgpack_write_map_header(int fd, uint32_t size)
if (size < 16)
{
size = (size | MPF_FIXMAP);
if (write(fd, &size, sizeof(int8_t)) == -1)
if (fpwrite(fp, &size, sizeof(int8_t)) == -1)
return -errno;
return sizeof(int8_t);
}
else if (size < (uint16_t)size)
{
if (write(fd, &MPF_MAP16, sizeof(MPF_MAP16)) == -1)
if (fpwrite(fp, &MPF_MAP16, sizeof(MPF_MAP16)) == -1)
return -errno;
tmp_i16 = httobe16(size);
if (write(fd, (void *)(&tmp_i16), sizeof(uint16_t)) == -1)
if (fpwrite(fp, (void *)(&tmp_i16), sizeof(uint16_t)) == -1)
return -errno;
return sizeof(uint8_t) + sizeof(uint16_t);
}
tmp_i32 = httobe32(size);
if (write(fd, &MPF_MAP32, sizeof(MPF_MAP32)) == -1)
if (fpwrite(fp, &MPF_MAP32, sizeof(MPF_MAP32)) == -1)
return -errno;
if (write(fd, (void *)(&tmp_i32), sizeof(uint32_t)) == -1)
if (fpwrite(fp, (void *)(&tmp_i32), sizeof(uint32_t)) == -1)
return -errno;
return sizeof(int8_t) + sizeof(uint32_t);
}
int rmsgpack_write_string(int fd, const char *s, uint32_t len)
int rmsgpack_write_string(FILE *fp, const char *s, uint32_t len)
{
int8_t fixlen = 0;
uint16_t tmp_i16;
@ -125,43 +137,43 @@ int rmsgpack_write_string(int fd, const char *s, uint32_t len)
if (len < 32)
{
fixlen = len | MPF_FIXSTR;
if (write(fd, &fixlen, sizeof(int8_t)) == -1)
if (fpwrite(fp, &fixlen, sizeof(int8_t)) == -1)
return -errno;
}
else if (len < (1 << 8))
{
if (write(fd, &MPF_STR8, sizeof(MPF_STR8)) == -1)
if (fpwrite(fp, &MPF_STR8, sizeof(MPF_STR8)) == -1)
return -errno;
if (write(fd, &len, sizeof(uint8_t)) == -1)
if (fpwrite(fp, &len, sizeof(uint8_t)) == -1)
return -errno;
written += sizeof(uint8_t);
}
else if (len < (1 << 16))
{
if (write(fd, &MPF_STR16, sizeof(MPF_STR16)) == -1)
if (fpwrite(fp, &MPF_STR16, sizeof(MPF_STR16)) == -1)
return -errno;
tmp_i16 = httobe16(len);
if (write(fd, &tmp_i16, sizeof(uint16_t)) == -1)
if (fpwrite(fp, &tmp_i16, sizeof(uint16_t)) == -1)
return -errno;
written += sizeof(uint16_t);
}
else
{
if (write(fd, &MPF_STR32, sizeof(MPF_STR32)) == -1)
if (fpwrite(fp, &MPF_STR32, sizeof(MPF_STR32)) == -1)
return -errno;
tmp_i32 = httobe32(len);
if (write(fd, &tmp_i32, sizeof(uint32_t)) == -1)
if (fpwrite(fp, &tmp_i32, sizeof(uint32_t)) == -1)
return -errno;
written += sizeof(uint32_t);
}
if (write(fd, s, len) == -1)
if (fpwrite(fp, s, len) == -1)
return -errno;
written += len;
return written;
}
int rmsgpack_write_bin(int fd, const void *s, uint32_t len)
int rmsgpack_write_bin(FILE *fp, const void *s, uint32_t len)
{
uint16_t tmp_i16;
uint32_t tmp_i32;
@ -169,58 +181,58 @@ int rmsgpack_write_bin(int fd, const void *s, uint32_t len)
if (len == (uint8_t)len)
{
if (write(fd, &MPF_BIN8, sizeof(MPF_BIN8)) == -1)
if (fpwrite(fp, &MPF_BIN8, sizeof(MPF_BIN8)) == -1)
return -errno;
if (write(fd, &len, sizeof(uint8_t)) == -1)
if (fpwrite(fp, &len, sizeof(uint8_t)) == -1)
return -errno;
written += sizeof(uint8_t);
}
else if (len == (uint16_t)len)
{
if (write(fd, &MPF_BIN16, sizeof(MPF_BIN16)) == -1)
if (fpwrite(fp, &MPF_BIN16, sizeof(MPF_BIN16)) == -1)
return -errno;
tmp_i16 = httobe16(len);
if (write(fd, &tmp_i16, sizeof(uint16_t)) == -1)
if (fpwrite(fp, &tmp_i16, sizeof(uint16_t)) == -1)
return -errno;
written += sizeof(uint16_t);
}
else
{
if (write(fd, &MPF_BIN32, sizeof(MPF_BIN32)) == -1)
if (fpwrite(fp, &MPF_BIN32, sizeof(MPF_BIN32)) == -1)
return -errno;
tmp_i32 = httobe32(len);
if (write(fd, &tmp_i32, sizeof(uint32_t)) == -1)
if (fpwrite(fp, &tmp_i32, sizeof(uint32_t)) == -1)
return -errno;
written += sizeof(uint32_t);
}
if (write(fd, s, len) == -1)
if (fpwrite(fp, s, len) == -1)
return -errno;
written += len;
return 0;
}
int rmsgpack_write_nil(int fd)
int rmsgpack_write_nil(FILE *fp)
{
if (write(fd, &MPF_NIL, sizeof(MPF_NIL)) == -1)
if (fpwrite(fp, &MPF_NIL, sizeof(MPF_NIL)) == -1)
return -errno;
return sizeof(uint8_t);
}
int rmsgpack_write_bool(int fd, int value)
int rmsgpack_write_bool(FILE *fp, int value)
{
if (value)
{
if (write(fd, &MPF_TRUE, sizeof(MPF_TRUE)) == -1)
if (fpwrite(fp, &MPF_TRUE, sizeof(MPF_TRUE)) == -1)
return -errno;
}
if (write(fd, &MPF_FALSE, sizeof(MPF_FALSE)) == -1)
if (fpwrite(fp, &MPF_FALSE, sizeof(MPF_FALSE)) == -1)
return -errno;
return sizeof(uint8_t);
}
int rmsgpack_write_int(int fd, int64_t value)
int rmsgpack_write_int(FILE *fp, int64_t value)
{
int16_t tmp_i16;
int32_t tmp_i32;
@ -229,58 +241,58 @@ int rmsgpack_write_int(int fd, int64_t value)
if (value >=0 && value < 128)
{
if (write(fd, &value, sizeof(int8_t)) == -1)
if (fpwrite(fp, &value, sizeof(int8_t)) == -1)
return -errno;
}
else if (value < 0 && value > -32)
{
tmpval = (value) | 0xe0;
if (write(fd, &tmpval, sizeof(uint8_t)) == -1)
if (fpwrite(fp, &tmpval, sizeof(uint8_t)) == -1)
return -errno;
}
else if (value == (int8_t)value)
{
if (write(fd, &MPF_INT8, sizeof(MPF_INT8)) == -1)
if (fpwrite(fp, &MPF_INT8, sizeof(MPF_INT8)) == -1)
return -errno;
if (write(fd, &value, sizeof(int8_t)) == -1)
if (fpwrite(fp, &value, sizeof(int8_t)) == -1)
return -errno;
written += sizeof(int8_t);
}
else if (value == (int16_t)value)
{
if (write(fd, &MPF_INT16, sizeof(MPF_INT16)) == -1)
if (fpwrite(fp, &MPF_INT16, sizeof(MPF_INT16)) == -1)
return -errno;
tmp_i16 = httobe16(value);
if (write(fd, &tmp_i16, sizeof(int16_t)) == -1)
if (fpwrite(fp, &tmp_i16, sizeof(int16_t)) == -1)
return -errno;
written += sizeof(int16_t);
}
else if (value == (int32_t)value)
{
if (write(fd, &MPF_INT32, sizeof(MPF_INT32)) == -1)
if (fpwrite(fp, &MPF_INT32, sizeof(MPF_INT32)) == -1)
return -errno;
tmp_i32 = httobe32(value);
if (write(fd, &tmp_i32, sizeof(int32_t)) == -1)
if (fpwrite(fp, &tmp_i32, sizeof(int32_t)) == -1)
return -errno;
written += sizeof(int32_t);
}
else
{
if (write(fd, &MPF_INT64, sizeof(MPF_INT64)) == -1)
if (fpwrite(fp, &MPF_INT64, sizeof(MPF_INT64)) == -1)
return -errno;
value = httobe64(value);
if (write(fd, &value, sizeof(int64_t)) == -1)
if (fpwrite(fp, &value, sizeof(int64_t)) == -1)
return -errno;
written += sizeof(int64_t);
}
return written;
}
int rmsgpack_write_uint(int fd, uint64_t value)
int rmsgpack_write_uint(FILE *fp, uint64_t value)
{
uint16_t tmp_i16;
uint32_t tmp_i32;
@ -288,51 +300,51 @@ int rmsgpack_write_uint(int fd, uint64_t value)
if (value == (uint8_t)value)
{
if (write(fd, &MPF_UINT8, sizeof(MPF_UINT8)) == -1)
if (fpwrite(fp, &MPF_UINT8, sizeof(MPF_UINT8)) == -1)
return -errno;
if (write(fd, &value, sizeof(uint8_t)) == -1)
if (fpwrite(fp, &value, sizeof(uint8_t)) == -1)
return -errno;
written += sizeof(uint8_t);
}
else if (value == (uint16_t)value)
{
if (write(fd, &MPF_UINT16, sizeof(MPF_UINT16)) == -1)
if (fpwrite(fp, &MPF_UINT16, sizeof(MPF_UINT16)) == -1)
return -errno;
tmp_i16 = httobe16(value);
if (write(fd, &tmp_i16, sizeof(uint16_t)) == -1)
if (fpwrite(fp, &tmp_i16, sizeof(uint16_t)) == -1)
return -errno;
written += sizeof(uint16_t);
}
else if (value == (uint32_t)value)
{
if (write(fd, &MPF_UINT32, sizeof(MPF_UINT32)) == -1)
if (fpwrite(fp, &MPF_UINT32, sizeof(MPF_UINT32)) == -1)
return -errno;
tmp_i32 = httobe32(value);
if (write(fd, &tmp_i32, sizeof(uint32_t)) == -1)
if (fpwrite(fp, &tmp_i32, sizeof(uint32_t)) == -1)
return -errno;
written += sizeof(uint32_t);
}
else
{
if (write(fd, &MPF_UINT64, sizeof(MPF_UINT64)) == -1)
if (fpwrite(fp, &MPF_UINT64, sizeof(MPF_UINT64)) == -1)
return -errno;
value = httobe64(value);
if (write(fd, &value, sizeof(uint64_t)) == -1)
if (fpwrite(fp, &value, sizeof(uint64_t)) == -1)
return -errno;
written += sizeof(uint64_t);
}
return written;
}
static int read_uint(int fd, uint64_t *out, size_t size)
static int read_uint(FILE *fp, uint64_t *out, size_t size)
{
uint64_t tmp;
if (read(fd, &tmp, size) == -1)
if (fpread(fp, &tmp, size) == -1)
return -errno;
switch (size)
@ -353,14 +365,14 @@ static int read_uint(int fd, uint64_t *out, size_t size)
return 0;
}
static int read_int(int fd, int64_t *out, size_t size)
static int read_int(FILE *fp, int64_t *out, size_t size)
{
uint8_t tmp8 = 0;
uint16_t tmp16;
uint32_t tmp32;
uint64_t tmp64;
if (read(fd, &tmp64, size) == -1)
if (fpread(fp, &tmp64, size) == -1)
return -errno;
(void)tmp8;
@ -386,16 +398,16 @@ static int read_int(int fd, int64_t *out, size_t size)
return 0;
}
static int read_buff(int fd, size_t size, char **pbuff, uint64_t *len)
static int read_buff(FILE *fp, size_t size, char **pbuff, uint64_t *len)
{
uint64_t tmp_len = 0;
if (read_uint(fd, &tmp_len, size) == -1)
if (read_uint(fp, &tmp_len, size) == -1)
return -errno;
*pbuff = (char *)calloc(tmp_len + 1, sizeof(char));
if (read(fd, *pbuff, tmp_len) == -1)
if (fpread(fp, *pbuff, tmp_len) == -1)
{
free(*pbuff);
return -errno;
@ -405,7 +417,7 @@ static int read_buff(int fd, size_t size, char **pbuff, uint64_t *len)
return 0;
}
static int read_map(int fd, uint32_t len,
static int read_map(FILE *fp, uint32_t len,
struct rmsgpack_read_callbacks *callbacks, void *data)
{
int rv;
@ -417,16 +429,16 @@ static int read_map(int fd, uint32_t len,
for (i = 0; i < len; i++)
{
if ((rv = rmsgpack_read(fd, callbacks, data)) < 0)
if ((rv = rmsgpack_read(fp, callbacks, data)) < 0)
return rv;
if ((rv = rmsgpack_read(fd, callbacks, data)) < 0)
if ((rv = rmsgpack_read(fp, callbacks, data)) < 0)
return rv;
}
return 0;
}
static int read_array(int fd, uint32_t len,
static int read_array(FILE *fp, uint32_t len,
struct rmsgpack_read_callbacks *callbacks, void *data)
{
int rv;
@ -438,14 +450,14 @@ static int read_array(int fd, uint32_t len,
for (i = 0; i < len; i++)
{
if ((rv = rmsgpack_read(fd, callbacks, data)) < 0)
if ((rv = rmsgpack_read(fp, callbacks, data)) < 0)
return rv;
}
return 0;
}
int rmsgpack_read(int fd,
int rmsgpack_read(FILE *fp,
struct rmsgpack_read_callbacks *callbacks, void *data)
{
int rv;
@ -455,7 +467,7 @@ int rmsgpack_read(int fd,
uint8_t type = 0;
char *buff = NULL;
if (read(fd, &type, sizeof(uint8_t)) == -1)
if (fpread(fp, &type, sizeof(uint8_t)) == -1)
return -errno;
if (type < MPF_FIXMAP)
@ -467,12 +479,12 @@ int rmsgpack_read(int fd,
else if (type < MPF_FIXARRAY)
{
tmp_len = type - MPF_FIXMAP;
return read_map(fd, tmp_len, callbacks, data);
return read_map(fp, tmp_len, callbacks, data);
}
else if (type < MPF_FIXSTR)
{
tmp_len = type - MPF_FIXARRAY;
return read_array(fd, tmp_len, callbacks, data);
return read_array(fp, tmp_len, callbacks, data);
}
else if (type < MPF_NIL)
{
@ -480,7 +492,7 @@ int rmsgpack_read(int fd,
buff = (char *)calloc(tmp_len + 1, sizeof(char));
if (!buff)
return -ENOMEM;
if (read(fd, buff, tmp_len) == -1)
if (fpread(fp, buff, tmp_len) == -1)
{
free(buff);
return -errno;
@ -517,7 +529,7 @@ int rmsgpack_read(int fd,
case 0xc4:
case 0xc5:
case 0xc6:
if ((rv = read_buff(fd, 1<<(type - 0xc4),
if ((rv = read_buff(fp, 1<<(type - 0xc4),
&buff, &tmp_len)) < 0)
return rv;
@ -530,7 +542,7 @@ int rmsgpack_read(int fd,
case 0xcf:
tmp_len = 1ULL << (type - 0xcc);
tmp_uint = 0;
if (read_uint(fd, &tmp_uint, tmp_len) == -1)
if (read_uint(fp, &tmp_uint, tmp_len) == -1)
return -errno;
if (callbacks->read_uint)
@ -542,7 +554,7 @@ int rmsgpack_read(int fd,
case 0xd3:
tmp_len = 1ULL << (type - 0xd0);
tmp_int = 0;
if (read_int(fd, &tmp_int, tmp_len) == -1)
if (read_int(fp, &tmp_int, tmp_len) == -1)
return -errno;
if (callbacks->read_int)
@ -551,7 +563,7 @@ int rmsgpack_read(int fd,
case 0xd9:
case 0xda:
case 0xdb:
if ((rv = read_buff(fd, 1<<(type - 0xd9), &buff, &tmp_len)) < 0)
if ((rv = read_buff(fp, 1<<(type - 0xd9), &buff, &tmp_len)) < 0)
return rv;
if (callbacks->read_string)
@ -559,16 +571,16 @@ int rmsgpack_read(int fd,
break;
case 0xdc:
case 0xdd:
if (read_uint(fd, &tmp_len, 2<<(type - 0xdc)) == -1)
if (read_uint(fp, &tmp_len, 2<<(type - 0xdc)) == -1)
return -errno;
return read_array(fd, tmp_len, callbacks, data);
return read_array(fp, tmp_len, callbacks, data);
case 0xde:
case 0xdf:
if (read_uint(fd, &tmp_len, 2<<(type - 0xde)) == -1)
if (read_uint(fp, &tmp_len, 2<<(type - 0xde)) == -1)
return -errno;
return read_map(fd, tmp_len, callbacks, data);
return read_map(fp, tmp_len, callbacks, data);
}
return 0;

View File

@ -1,6 +1,7 @@
#ifndef __RARCHDB_MSGPACK_H__
#define __RARCHDB_MSGPACK_H__
#include <stdio.h>
#include <stdint.h>
struct rmsgpack_read_callbacks {
@ -39,39 +40,39 @@ struct rmsgpack_read_callbacks {
int rmsgpack_write_array_header(
int fd,
FILE *fp,
uint32_t size
);
int rmsgpack_write_map_header(
int fd,
FILE *fp,
uint32_t size
);
int rmsgpack_write_string(
int fd,
FILE *fp,
const char * s,
uint32_t len
);
int rmsgpack_write_bin(
int fd,
FILE *fp,
const void * s,
uint32_t len
);
int rmsgpack_write_nil(int fd);
int rmsgpack_write_nil(FILE *fp);
int rmsgpack_write_bool(
int fd,
FILE *fp,
int value
);
int rmsgpack_write_int(
int fd,
FILE *fp,
int64_t value
);
int rmsgpack_write_uint(
int fd,
FILE *fp,
uint64_t value
);
int rmsgpack_read(
int fd,
FILE *fp,
struct rmsgpack_read_callbacks * callbacks,
void * data
);

View File

@ -378,7 +378,7 @@ void rmsgpack_dom_value_print(struct rmsgpack_dom_value *obj)
printf("]");
}
}
int rmsgpack_dom_write(int fd, const struct rmsgpack_dom_value *obj)
int rmsgpack_dom_write(FILE *fp, const struct rmsgpack_dom_value *obj)
{
unsigned i;
int rv = 0;
@ -387,40 +387,40 @@ int rmsgpack_dom_write(int fd, const struct rmsgpack_dom_value *obj)
switch (obj->type)
{
case RDT_NULL:
return rmsgpack_write_nil(fd);
return rmsgpack_write_nil(fp);
case RDT_BOOL:
return rmsgpack_write_bool(fd, obj->bool_);
return rmsgpack_write_bool(fp, obj->bool_);
case RDT_INT:
return rmsgpack_write_int(fd, obj->int_);
return rmsgpack_write_int(fp, obj->int_);
case RDT_UINT:
return rmsgpack_write_uint(fd, obj->uint_);
return rmsgpack_write_uint(fp, obj->uint_);
case RDT_STRING:
return rmsgpack_write_string(fd, obj->string.buff, obj->string.len);
return rmsgpack_write_string(fp, obj->string.buff, obj->string.len);
case RDT_BINARY:
return rmsgpack_write_bin(fd, obj->binary.buff, obj->binary.len);
return rmsgpack_write_bin(fp, obj->binary.buff, obj->binary.len);
case RDT_MAP:
if ((rv = rmsgpack_write_map_header(fd, obj->map.len)) < 0)
if ((rv = rmsgpack_write_map_header(fp, obj->map.len)) < 0)
return rv;
written += rv;
for (i = 0; i < obj->map.len; i++)
{
if ((rv = rmsgpack_dom_write(fd, &obj->map.items[i].key)) < 0)
if ((rv = rmsgpack_dom_write(fp, &obj->map.items[i].key)) < 0)
return rv;
written += rv;
if ((rv = rmsgpack_dom_write(fd, &obj->map.items[i].value)) < 0)
if ((rv = rmsgpack_dom_write(fp, &obj->map.items[i].value)) < 0)
return rv;
written += rv;
}
break;
case RDT_ARRAY:
if ((rv = rmsgpack_write_array_header(fd, obj->array.len)) < 0)
if ((rv = rmsgpack_write_array_header(fp, obj->array.len)) < 0)
return rv;
written += rv;
for (i = 0; i < obj->array.len; i++)
{
if ((rv = rmsgpack_dom_write(fd, &obj->array.items[i])) < 0)
if ((rv = rmsgpack_dom_write(fp, &obj->array.items[i])) < 0)
return rv;
written += rv;
}
@ -428,7 +428,7 @@ int rmsgpack_dom_write(int fd, const struct rmsgpack_dom_value *obj)
return written;
}
int rmsgpack_dom_read(int fd, struct rmsgpack_dom_value *out)
int rmsgpack_dom_read(FILE *fp, struct rmsgpack_dom_value *out)
{
struct dom_reader_state s;
int rv = 0;
@ -436,7 +436,7 @@ int rmsgpack_dom_read(int fd, struct rmsgpack_dom_value *out)
s.i = 0;
s.stack[0] = out;
rv = rmsgpack_read(fd, &dom_reader_callbacks, &s);
rv = rmsgpack_read(fp, &dom_reader_callbacks, &s);
if (rv < 0)
rmsgpack_dom_value_free(out);
@ -444,7 +444,7 @@ int rmsgpack_dom_read(int fd, struct rmsgpack_dom_value *out)
return rv;
}
int rmsgpack_dom_read_into(int fd, ...)
int rmsgpack_dom_read_into(FILE *fp, ...)
{
va_list ap;
struct rmsgpack_dom_value map;
@ -459,9 +459,9 @@ int rmsgpack_dom_read_into(int fd, ...)
uint64_t min_len;
int value_type = 0;
va_start(ap, fd);
va_start(ap, fp);
rv = rmsgpack_dom_read(fd, &map);
rv = rmsgpack_dom_read(fp, &map);
(void)value_type;

View File

@ -1,6 +1,7 @@
#ifndef __RARCHDB_MSGPACK_DOM_H__
#define __RARCHDB_MSGPACK_DOM_H__
#include <stdio.h>
#include <stdint.h>
#ifdef __cplusplus
@ -61,15 +62,15 @@ struct rmsgpack_dom_value * rmsgpack_dom_value_map_value(
);
int rmsgpack_dom_read(
int fd,
FILE *fp,
struct rmsgpack_dom_value * out
);
int rmsgpack_dom_write(
int fd,
FILE *fp,
const struct rmsgpack_dom_value * obj
);
int rmsgpack_dom_read_into(int fd, ...);
int rmsgpack_dom_read_into(FILE *fp, ...);
#ifdef __cplusplus
}