SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
RangeData.cc
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 
8 
10 #include "swcdb/core/Time.h"
11 
14 
15 
16 namespace SWC { namespace Ranger {
17 
18 
19 // SET
21  CellStore::Readers& cellstores) {
22  size_t sz = cellstores.encoded_length();
23  dst_buf.ensure(HEADER_SIZE + sz);
24 
26  Serialization::encode_i32(&dst_buf.ptr, sz);
27 
28  uint8_t* checksum_data_ptr = dst_buf.ptr;
29  Serialization::encode_i32(&dst_buf.ptr, 0);
30  uint8_t* checksum_header_ptr = dst_buf.ptr;
31  Serialization::encode_i32(&dst_buf.ptr, 0);
32 
33  const uint8_t* start_data_ptr = dst_buf.ptr;
34  cellstores.encode(&dst_buf.ptr);
35 
36  Core::checksum_i32(start_data_ptr, dst_buf.ptr, &checksum_data_ptr);
37  Core::checksum_i32(dst_buf.base, start_data_ptr, &checksum_header_ptr);
38 }
39 
40 void RangeData::save(int& err, CellStore::Readers& cellstores) {
41 
42  DynamicBuffer input;
43  write(input, cellstores);
44  StaticBuffer send_buf(input);
45 
47  err,
49  cellstores.range->get_path(DB::RangeBase::RANGE_FILE),
51  ),
52  1, // as hint-file without cellstores.range->cfg->file_replication()
53  send_buf
54  );
55 }
56 
57 
58 // GET
59 void RangeData::read(int& err, const uint8_t **ptr, size_t* remain,
60  CellStore::Readers& cellstores) {
61 
62  const uint8_t *ptr_end = *ptr+*remain;
63  cellstores.decode(err, ptr, remain);
64 
65  if(*ptr != ptr_end){
66  SWC_LOGF(LOG_WARN, "decode overrun remain=" SWC_FMT_LU, *remain);
67  cellstores.clear();
68  }
69 }
70 
72 void RangeData::load(int& err, CellStore::Readers& cellstores) {
73  StaticBuffer read_buf;
75  err,
76  cellstores.range->get_path(DB::RangeBase::RANGE_FILE),
77  &read_buf
78  );
79  if(!err) try {
80  const uint8_t *ptr = read_buf.base;
81  size_t remain = read_buf.size;
82 
83  Serialization::decode_i8(&ptr, &remain); //int8_t version =
84  size_t sz = Serialization::decode_i32(&ptr, &remain);
85 
86  size_t chksum_data = Serialization::decode_i32(&ptr, &remain);
87 
89  Serialization::decode_i32(&ptr, &remain),
91  !Core::checksum_i32_chk(chksum_data, ptr, sz)) {
93  } else {
94  read(err, &ptr, &sz, cellstores);
95  }
96  } catch(...) {
98  err = e.code();
100  }
101 
102  if(err || cellstores.empty()) {
103  err = Error::OK;
104  cellstores.load_from_path(err);
105  if(!err && !cellstores.empty())
106  save(err, cellstores);
107  }
108 }
109 
110 
111 }}
SWC::Ranger::CellStore::Readers::encoded_length
uint32_t SWC_PURE_FUNC encoded_length() const
Definition: CellStoreReaders.cc:152
SWC::Common::Files::Schema::VERSION
const uint8_t VERSION
Definition: Schema.h:25
SWC::Ranger::RangeData::save
void save(int &err, CellStore::Readers &cellstores)
Definition: RangeData.cc:40
SWC::Core::BufferDyn::ptr
value_type * ptr
Definition: Buffer.h:293
SWC::Error::Exception::code
constexpr SWC_CAN_INLINE int code() const noexcept
Definition: Exception.h:51
SWC_LOG_OSTREAM
#define SWC_LOG_OSTREAM
Definition: Logger.h:44
SWC_LOGF
#define SWC_LOGF(priority, fmt,...)
Definition: Logger.h:188
SWC_LOG_OUT
#define SWC_LOG_OUT(pr, _code_)
Definition: Logger.h:178
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::Ranger::CellStore::Readers
Definition: CellStoreReaders.h:15
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::Ranger::RangeData::load
void load(int &err, CellStore::Readers &cellstores)
Definition: RangeData.cc:72
SWC::Serialization::encode_i8
constexpr SWC_CAN_INLINE void encode_i8(uint8_t **bufp, uint8_t val) noexcept
Definition: Serialization.h:85
SWC::Common::Files::RgrData::HEADER_OFFSET_CHKSUM
static const uint8_t HEADER_OFFSET_CHKSUM
Definition: RgrData.h:24
SWC::Error::OK
@ OK
Definition: Error.h:45
SWC::Core::Vector::empty
constexpr SWC_CAN_INLINE bool empty() const noexcept
Definition: Vector.h:168
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC_CURRENT_EXCEPTION
#define SWC_CURRENT_EXCEPTION(_msg_)
Definition: Exception.h:119
SWC::Core::Buffer::base
value_type * base
Definition: Buffer.h:131
RangeData.h
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
Definition: Buffer.h:138
SWC::Core::Buffer
Definition: Buffer.h:18
SWC::Ranger::CellStore::Readers::load_from_path
void load_from_path(int &err)
Definition: CellStoreReaders.cc:180
CellStoreReaders.h
SWC::Core::Buffer::size
size_t size
Definition: Buffer.h:130
SWC::Ranger::CellStore::Readers::range
RangePtr range
Definition: CellStoreReaders.h:32
Serialization.h
SWC::Ranger::CellStore::Readers::decode
void decode(int &err, const uint8_t **ptr, size_t *remain)
Definition: CellStoreReaders.cc:167
Time.h
SWC::LOG_ERROR
@ LOG_ERROR
Definition: Logger.h:32
SWC_FMT_LU
#define SWC_FMT_LU
Definition: Compat.h:98
SWC::Ranger::RangeData::write
void write(DynamicBuffer &dst_buf, CellStore::Readers &cellstores)
Definition: RangeData.cc:20
SWC::Common::Files::Schema::save
void save(int &err, const DB::Schema::Ptr &schema, uint8_t replication=0)
Definition: Schema.h:70
SWC::Error::CHECKSUM_MISMATCH
@ CHECKSUM_MISMATCH
Definition: Error.h:62
SWC::Common::Files::Schema::write
void write(SWC::DynamicBuffer &dst_buf, const DB::Schema::Ptr &schema)
Definition: Schema.h:50
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::Ranger::CellStore::Readers::encode
void encode(uint8_t **ptr) const
Definition: CellStoreReaders.cc:160
SWC::LOG_WARN
@ LOG_WARN
Definition: Logger.h:33
SWC::Ranger::RangeData::read
void read(int &err, const uint8_t **ptr, size_t *remain, CellStore::Readers &cellstores)
Definition: RangeData.cc:59
SWC::Common::Files::RgrData::HEADER_SIZE
static const uint8_t HEADER_SIZE
Definition: RgrData.h:23
SWC::Serialization::decode_i8
constexpr SWC_CAN_INLINE uint8_t decode_i8(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:91
SWC::Ranger::CellStore::Readers::clear
void clear()
Definition: CellStoreReaders.cc:94
SWC::Error::Exception
Definition: Exception.h:21
SWC::Serialization::decode_i32
SWC_CAN_INLINE uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Definition: Serialization.h:143
Interval.h