int rmsgpack_write_int(int fd, int64_t value) { int16_t tmp_i16; int32_t tmp_i32; uint8_t tmpval = 0; int written = sizeof(uint8_t); if (value >=0 && value < 128) { if (write(fd, &value, sizeof(int8_t)) == -1) return -errno; } else if (value < 0 && value > -32) { tmpval = (value) | 0xe0; if (write(fd, &tmpval, sizeof(uint8_t)) == -1) return -errno; } else if (value == (int8_t)value) { if (write(fd, &MPF_INT8, sizeof(MPF_INT8)) == -1) return -errno; if (write(fd, &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) return -errno; tmp_i16 = httobe16(value); if (write(fd, &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) return -errno; tmp_i32 = httobe32(value); if (write(fd, &tmp_i32, sizeof(int32_t)) == -1) return -errno; written += sizeof(int32_t); } else { if (write(fd, &MPF_INT64, sizeof(MPF_INT64)) == -1) return -errno; value = httobe64(value); if (write(fd, &value, sizeof(int64_t)) == -1) return -errno; written += sizeof(int64_t); } return written; }
int rmsgpack_write_uint(int fd, uint64_t value) { uint16_t tmp_i16; uint32_t tmp_i32; int written = sizeof(uint8_t); if (value == (uint8_t)value) { if (write(fd, &MPF_UINT8, sizeof(MPF_UINT8)) == -1) return -errno; if (write(fd, &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) return -errno; tmp_i16 = httobe16(value); if (write(fd, &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) return -errno; tmp_i32 = httobe32(value); if (write(fd, &tmp_i32, sizeof(uint32_t)) == -1) return -errno; written += sizeof(uint32_t); } else { if (write(fd, &MPF_UINT64, sizeof(MPF_UINT64)) == -1) return -errno; value = httobe64(value); if (write(fd, &value, sizeof(uint64_t)) == -1) return -errno; written += sizeof(uint64_t); } return written; }
int libretrodb_create(int fd, libretrodb_value_provider value_provider, void * ctx) { int rv; off_t root; libretrodb_metadata_t md; uint64_t item_count = 0; struct rmsgpack_dom_value item = {}; libretrodb_header_t header = {}; memcpy(header.magic_number, MAGIC_NUMBER, sizeof(MAGIC_NUMBER)-1); root = lseek(fd, 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); while ((rv = value_provider(ctx, &item)) == 0) { if ((rv = validate_document(&item)) < 0) goto clean; if ((rv = rmsgpack_dom_write(fd, &item)) < 0) goto clean; item_count++; } if (rv < 0) goto clean; if ((rv = rmsgpack_dom_write(fd, &sentinal)) < 0) goto clean; header.metadata_offset = httobe64(lseek(fd, 0, SEEK_CUR)); md.count = item_count; libretrodb_write_metadata(fd, &md); lseek(fd, root, SEEK_SET); write(fd, &header, sizeof(header)); clean: rmsgpack_dom_value_free(&item); return rv; }