SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
RgrData.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #ifndef swcdb_common_Files_RgrData_h
8 #define swcdb_common_Files_RgrData_h
9 
10 
12 
13 
14 namespace SWC { namespace Common { namespace Files {
15 
16 
17 namespace RgrData {
18 /* file-format:
19  header: i32(data-len), i32(data-checksum), i32(header-checksum)
20  data: vi64(rgrid), i32(num-points), [endpoint]
21 */
22 
23 static const uint8_t HEADER_SIZE = 12;
24 static const uint8_t HEADER_OFFSET_CHKSUM = 8;
25 
26 
27 static void read(DB::RgrData& data, int &err, const std::string& filepath) {
28  StaticBuffer read_buf;
29  Env::FsInterface::interface()->read(err, filepath, &read_buf);
30  if(err)
31  return;
32 
33  const uint8_t *ptr = read_buf.base;
34  size_t remain = read_buf.size;
35 
36  size_t sz = Serialization::decode_i32(&ptr, &remain);
37  size_t chksum_data = Serialization::decode_i32(&ptr, &remain);
38 
40  Serialization::decode_i32(&ptr, &remain),
42  !Core::checksum_i32_chk(chksum_data, ptr, sz) ) {
44  return;
45  }
46  data.decode(ptr, remain);
47 }
48 
49 static inline void set_rgr(const DB::RgrData& data,
50  int &err, std::string&& filepath,
51  uint8_t replication) noexcept {
52  try {
53  DynamicBuffer dst_buf;
54 
55  size_t len = data.encoded_length();
56  dst_buf.ensure(HEADER_SIZE + len);
57  Serialization::encode_i32(&dst_buf.ptr, len);
58 
59  uint8_t* checksum_data_ptr = dst_buf.ptr;
60  Serialization::encode_i32(&dst_buf.ptr, 0);
61  uint8_t* checksum_header_ptr = dst_buf.ptr;
62  Serialization::encode_i32(&dst_buf.ptr, 0);
63 
64  const uint8_t* start_data_ptr = dst_buf.ptr;
65  data.encode(&dst_buf.ptr);
66 
67  Core::checksum_i32(start_data_ptr, dst_buf.ptr, &checksum_data_ptr);
68  Core::checksum_i32(dst_buf.base, start_data_ptr, &checksum_header_ptr);
69 
70  StaticBuffer send_buf(dst_buf);
72  err,
75  replication,
76  send_buf
77  );
78  } catch(...) {
79  err = SWC_CURRENT_EXCEPTION("").code();
80  }
81 }
82 
83 static void get_rgr(DB::RgrData& data ,const std::string& filepath) noexcept {
84  int err = Error::OK;
85  try {
86  read(data, err, filepath);
87  } catch(...) {
88  err = ENOKEY;
89  }
90  if(err) {
91  data.rgrid.store(0);
92  data.endpoints.clear();
93  }
94 }
95 
96 }}}} // SWC::Common::Files::RgrData namespace
97 
98 
99 
100 #endif // swcdb_common_Files_RgrData_h
SWC::Core::BufferDyn::ptr
value_type * ptr
Definition: Buffer.h:293
data
T data
Definition: BitFieldInt.h:1
SWC::Serialization::encode_i32
SWC_CAN_INLINE void encode_i32(uint8_t **bufp, uint32_t val) noexcept
Definition: Serialization.h:138
SWC::Env::FsInterface::interface
static SWC_CAN_INLINE FS::Interface::Ptr & interface() noexcept
Definition: Interface.h:150
SWC::FS::SmartFd::make_ptr
static SWC_CAN_INLINE Ptr make_ptr(const std::string &filepath, uint32_t flags, int32_t fd=-1, uint64_t pos=0)
Definition: SmartFd.h:40
SWC::Core::checksum_i32_chk
SWC_CAN_INLINE bool checksum_i32_chk(uint32_t checksum, const uint8_t *base, uint32_t len)
Definition: Checksum.h:94
SWC::Core::BufferDyn::ensure
SWC_CAN_INLINE void ensure(size_t len)
Definition: Buffer.h:212
SWC::Common::Files::RgrData::read
static void read(DB::RgrData &data, int &err, const std::string &filepath)
Definition: RgrData.h:27
SWC::Common::Files::RgrData::HEADER_OFFSET_CHKSUM
static const uint8_t HEADER_OFFSET_CHKSUM
Definition: RgrData.h:24
SWC::Common::Files::RgrData::get_rgr
static void get_rgr(DB::RgrData &data, const std::string &filepath) noexcept
Definition: RgrData.h:83
SWC::Error::OK
@ OK
Definition: Error.h:45
RgrData.h
SWC_CURRENT_EXCEPTION
#define SWC_CURRENT_EXCEPTION(_msg_)
Definition: Exception.h:119
SWC::Core::Buffer::base
value_type * base
Definition: Buffer.h:131
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::FS::OPEN_FLAG_OVERWRITE
@ OPEN_FLAG_OVERWRITE
Definition: FileSystem.h:35
SWC::Core::BufferDyn< StaticBuffer >
SWC::Core::Buffer
Definition: Buffer.h:18
SWC::Core::Buffer::size
size_t size
Definition: Buffer.h:130
SWC::DB::RgrData
Definition: RgrData.h:24
SWC::Common::Files::RgrData::set_rgr
static void set_rgr(const DB::RgrData &data, int &err, std::string &&filepath, uint8_t replication) noexcept
Definition: RgrData.h:49
SWC::Error::CHECKSUM_MISMATCH
@ CHECKSUM_MISMATCH
Definition: Error.h:62
SWC::Core::checksum_i32
SWC_CAN_INLINE void checksum_i32(const uint8_t *start, size_t len, uint8_t **ptr) noexcept
Definition: Checksum.h:62
SWC::Common::Files::Schema::filepath
std::string filepath(cid_t cid)
Definition: Schema.h:34
SWC::Common::Files::RgrData::HEADER_SIZE
static const uint8_t HEADER_SIZE
Definition: RgrData.h:23
SWC::Serialization::decode_i32
SWC_CAN_INLINE uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:143