Skip to content

Commit

Permalink
refactor write functions and endian operations
Browse files Browse the repository at this point in the history
  • Loading branch information
ban-nobuhiro committed Oct 6, 2023
1 parent 31c7c84 commit 11ec85c
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions src/limestone/log_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@
#pragma once

#include <cstdio>
#include <endian.h>
#include <istream>
#include <string>
#include <string_view>
#include <exception>

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/fstream.hpp>
#include <glog/logging.h>

#include <limestone/api/storage_id_type.h>
#include <limestone/api/write_version_type.h>
#include <limestone/logging.h>
#include "logging_helper.h"

namespace limestone::api {

Expand Down Expand Up @@ -96,11 +100,11 @@ class log_entry {
write_uint32le(strm, static_cast<std::uint32_t>(value_len));

write_uint64le(strm, static_cast<std::uint64_t>(storage_id));
fwrite(key.data(), static_cast<std::streamsize>(key_len), 1, strm); // NOLINT TODO: check error
write_bytes(strm, key.data(), key_len);

write_uint64le(strm, static_cast<std::uint64_t>(write_version.epoch_number_));
write_uint64le(strm, static_cast<std::uint64_t>(write_version.minor_write_version_));
fwrite(value.data(), static_cast<std::streamsize>(value_len), 1, strm); // NOLINT TODO: check error
write_bytes(strm, value.data(), value_len);
}

static void write(FILE* strm, std::string_view key_sid, std::string_view value_etc) {
Expand All @@ -115,8 +119,8 @@ class log_entry {
assert(value_len <= UINT32_MAX); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
write_uint32le(strm, static_cast<std::uint32_t>(value_len));

fwrite(key_sid.data(), static_cast<std::streamsize>(key_sid.length()), 1, strm); // NOLINT TODO: check error
fwrite(value_etc.data(), static_cast<std::streamsize>(value_etc.length()), 1, strm); // NOLINT TODO: check error
write_bytes(strm, key_sid.data(), key_sid.length());
write_bytes(strm, value_etc.data(), value_etc.length());
}

static void write_remove(FILE* strm, storage_id_type storage_id, std::string_view key, write_version_type write_version) {
Expand All @@ -128,7 +132,7 @@ class log_entry {
write_uint32le(strm, static_cast<std::uint32_t>(key_len));

write_uint64le(strm, static_cast<std::uint64_t>(storage_id));
fwrite(key.data(), static_cast<std::streamsize>(key_len), 1, strm); // NOLINT TODO: check error
write_bytes(strm, key.data(), key_len);

write_uint64le(strm, static_cast<std::uint64_t>(write_version.epoch_number_));
write_uint64le(strm, static_cast<std::uint64_t>(write_version.minor_write_version_));
Expand All @@ -142,8 +146,8 @@ class log_entry {
assert(key_len <= UINT32_MAX); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
write_uint32le(strm, static_cast<std::uint32_t>(key_len));

fwrite(key_sid.data(), static_cast<std::streamsize>(key_sid.length()), 1, strm); // NOLINT TODO: check error
fwrite(value_etc.data(), static_cast<std::streamsize>(value_etc.length()), 1, strm); // NOLINT TODO: check error
write_bytes(strm, key_sid.data(), key_sid.length());
write_bytes(strm, value_etc.data(), value_etc.length());
}

// for reader
Expand Down Expand Up @@ -236,41 +240,37 @@ class log_entry {
char one_char_{};

static void write_uint8(FILE* out, const std::uint8_t value) {
fputc(value, out); // NOLINT TODO: check error
int ret = fputc(value, out);
if (ret == EOF) {
LOG_LP(ERROR) << "fputc failed, errno = " << errno;
std::abort();
}
}
static void write_uint32le(FILE* out, const std::uint32_t value) {
fputc(static_cast<int>((value>>0U)&0xFFU), out); // NOLINT TODO: check error
fputc(static_cast<int>((value>>8U)&0xFFU), out); // NOLINT TODO: check error
fputc(static_cast<int>((value>>16U)&0xFFU), out); // NOLINT TODO: check error
fputc(static_cast<int>((value>>24U)&0xFFU), out); // NOLINT TODO: check error
std::uint32_t buf = htole32(value);
write_bytes(out, &buf, sizeof(std::uint32_t));
}
static std::uint32_t read_uint32le(std::istream& in) {
std::uint32_t value = (static_cast<std::uint8_t>(in.get())&0xFFU);
value |= (static_cast<std::uint8_t>(in.get())&0xFFU)<<8U;
value |= (static_cast<std::uint8_t>(in.get())&0xFFU)<<16U;
value |= (static_cast<std::uint8_t>(in.get())&0xFFU)<<24U;
return value;
std::uint32_t buf{};
in.read(reinterpret_cast<char*>(&buf), sizeof(std::uint32_t)); // NOLINT
return le32toh(buf);
}
static void write_uint64le(FILE* out, const std::uint64_t value) {
fputc(static_cast<int>((value>>0U)&0xFFU), out); // NOLINT TODO: check error
fputc(static_cast<int>((value>>8U)&0xFFU), out); // NOLINT TODO: check error
fputc(static_cast<int>((value>>16U)&0xFFU), out); // NOLINT TODO: check error
fputc(static_cast<int>((value>>24U)&0xFFU), out); // NOLINT TODO: check error
fputc(static_cast<int>((value>>32U)&0xFFU), out); // NOLINT TODO: check error
fputc(static_cast<int>((value>>40U)&0xFFU), out); // NOLINT TODO: check error
fputc(static_cast<int>((value>>48U)&0xFFU), out); // NOLINT TODO: check error
fputc(static_cast<int>((value>>56U)&0xFFU), out); // NOLINT TODO: check error
std::uint64_t buf = htole64(value);
write_bytes(out, &buf, sizeof(std::uint64_t));
}
static std::uint64_t read_uint64le(std::istream& in) {
std::uint64_t value_l = (static_cast<std::uint8_t>(in.get())&0xFFU);
value_l |= (static_cast<std::uint8_t>(in.get())&0xFFU)<<8U;
value_l |= (static_cast<std::uint8_t>(in.get())&0xFFU)<<16U;
value_l |= (static_cast<std::uint8_t>(in.get())&0xFFU)<<24U;
std::uint64_t value_u = (static_cast<std::uint8_t>(in.get())&0xFFU);
value_u |= (static_cast<std::uint8_t>(in.get())&0xFFU)<<8U;
value_u |= (static_cast<std::uint8_t>(in.get())&0xFFU)<<16U;
value_u |= (static_cast<std::uint8_t>(in.get())&0xFFU)<<24U;
return ((value_u << 32U) & 0xFFFFFFFF00000000UL) | value_l;
std::uint64_t buf{};
in.read(reinterpret_cast<char*>(&buf), sizeof(std::uint64_t)); // NOLINT
return le64toh(buf);
}
static void write_bytes(FILE* out, const void* buf, std::size_t len) {
if (len == 0) return; // nothing to write
auto ret = fwrite(buf, len, 1, out);
if (ret != 1) {
LOG_LP(ERROR) << "fwrite failed, errno = " << errno;
std::abort();
}
}
};

Expand Down

0 comments on commit 11ec85c

Please sign in to comment.