Make read-only functions of DataNode const

This commit is contained in:
Sebastian Messmer 2014-12-11 00:24:50 +01:00
parent d1a7712aee
commit eaf9277d81
5 changed files with 27 additions and 28 deletions

View File

@ -19,14 +19,14 @@ void DataInnerNode::InitializeNewNode() {
*_node.Size() = 0; *_node.Size() = 0;
} }
void DataInnerNode::read(off_t offset, size_t count, Data *result) { void DataInnerNode::read(off_t offset, size_t count, Data *result) const {
assert(count <= result->size()); assert(count <= result->size());
const uint64_t end = offset + count; const uint64_t end = offset + count;
assert(end <= numBytesInThisNode()); assert(end <= numBytesInThisNode());
uint8_t *target = (uint8_t*)result->data(); uint8_t *target = (uint8_t*)result->data();
ChildEntry *child = ChildContainingFirstByteAfterOffset(offset); const ChildEntry *child = ChildContainingFirstByteAfterOffset(offset);
off_t blockrelative_offset = offset - numBytesInLeftwardSiblings(child); off_t blockrelative_offset = offset - numBytesInLeftwardSiblings(child);
uint64_t already_read_bytes = readFromChild(child, blockrelative_offset, count, target); uint64_t already_read_bytes = readFromChild(child, blockrelative_offset, count, target);
while(numBytesInChildAndLeftwardSiblings(child) < end) { while(numBytesInChildAndLeftwardSiblings(child) < end) {
@ -36,7 +36,7 @@ void DataInnerNode::read(off_t offset, size_t count, Data *result) {
assert(already_read_bytes == count); assert(already_read_bytes == count);
} }
uint64_t DataInnerNode::readFromChild(const ChildEntry *child, off_t inner_offset, size_t count, uint8_t *target) { uint64_t DataInnerNode::readFromChild(const ChildEntry *child, off_t inner_offset, size_t count, uint8_t *target) const {
uint64_t readable_bytes = std::min(count, numBytesInChild(child) - inner_offset); uint64_t readable_bytes = std::min(count, numBytesInChild(child) - inner_offset);
//TODO READ... //TODO READ...
@ -44,7 +44,7 @@ uint64_t DataInnerNode::readFromChild(const ChildEntry *child, off_t inner_offse
return readable_bytes; return readable_bytes;
} }
DataInnerNode::ChildEntry *DataInnerNode::ChildContainingFirstByteAfterOffset(off_t offset) { const DataInnerNode::ChildEntry *DataInnerNode::ChildContainingFirstByteAfterOffset(off_t offset) const {
uint32_t offset_blocks = offset / _node.BLOCKSIZE_BYTES; uint32_t offset_blocks = offset / _node.BLOCKSIZE_BYTES;
return return
@ -53,34 +53,34 @@ DataInnerNode::ChildEntry *DataInnerNode::ChildContainingFirstByteAfterOffset(of
}); });
} }
uint64_t DataInnerNode::numBytesInThisNode() { uint64_t DataInnerNode::numBytesInThisNode() const {
return numBytesInChildAndLeftwardSiblings(ChildrenLast()); return numBytesInChildAndLeftwardSiblings(ChildrenLast());
} }
uint64_t DataInnerNode::numBytesInChild(const ChildEntry *child) { uint64_t DataInnerNode::numBytesInChild(const ChildEntry *child) const {
return numBytesInChildAndLeftwardSiblings(child) - numBytesInLeftwardSiblings(child); return numBytesInChildAndLeftwardSiblings(child) - numBytesInLeftwardSiblings(child);
} }
uint64_t DataInnerNode::numBytesInLeftwardSiblings(const ChildEntry *child) { uint64_t DataInnerNode::numBytesInLeftwardSiblings(const ChildEntry *child) const {
if (child == ChildrenBegin()) { if (child == ChildrenBegin()) {
return 0; return 0;
} }
return numBytesInChildAndLeftwardSiblings(child-1); return numBytesInChildAndLeftwardSiblings(child-1);
} }
uint64_t DataInnerNode::numBytesInChildAndLeftwardSiblings(const ChildEntry *child) { uint64_t DataInnerNode::numBytesInChildAndLeftwardSiblings(const ChildEntry *child) const {
return (uint64_t)child->numBlocksInThisAndLeftwardNodes * _node.BLOCKSIZE_BYTES; return (uint64_t)child->numBlocksInThisAndLeftwardNodes * _node.BLOCKSIZE_BYTES;
} }
DataInnerNode::ChildEntry *DataInnerNode::ChildrenBegin() { const DataInnerNode::ChildEntry *DataInnerNode::ChildrenBegin() const {
return _node.DataBegin<ChildEntry>(); return _node.DataBegin<ChildEntry>();
} }
DataInnerNode::ChildEntry *DataInnerNode::ChildrenEnd() { const DataInnerNode::ChildEntry *DataInnerNode::ChildrenEnd() const {
return ChildrenBegin() + *_node.Size(); return ChildrenBegin() + *_node.Size();
} }
DataInnerNode::ChildEntry *DataInnerNode::ChildrenLast() { const DataInnerNode::ChildEntry *DataInnerNode::ChildrenLast() const{
return ChildrenEnd()-1; return ChildrenEnd()-1;
} }

View File

@ -21,24 +21,24 @@ public:
void InitializeNewNode(); void InitializeNewNode();
void read(off_t offset, size_t count, blockstore::Data *result) override; void read(off_t offset, size_t count, blockstore::Data *result) const override;
void write(off_t offset, size_t count, const blockstore::Data &data) override; void write(off_t offset, size_t count, const blockstore::Data &data) override;
uint64_t numBytesInThisNode() override; uint64_t numBytesInThisNode() const override;
void resize(uint64_t newsize_bytes) override; void resize(uint64_t newsize_bytes) override;
private: private:
ChildEntry *ChildrenBegin(); const ChildEntry *ChildrenBegin() const;
ChildEntry *ChildrenEnd(); const ChildEntry *ChildrenEnd() const;
ChildEntry *ChildrenLast(); const ChildEntry *ChildrenLast() const;
uint64_t readFromChild(const ChildEntry *child, off_t inner_offset, size_t count, uint8_t *target); uint64_t readFromChild(const ChildEntry *child, off_t inner_offset, size_t count, uint8_t *target) const;
ChildEntry *ChildContainingFirstByteAfterOffset(off_t offset); const ChildEntry *ChildContainingFirstByteAfterOffset(off_t offset) const;
uint64_t numBytesInChildAndLeftwardSiblings(const ChildEntry *child); uint64_t numBytesInChildAndLeftwardSiblings(const ChildEntry *child) const;
uint64_t numBytesInLeftwardSiblings(const ChildEntry *child); uint64_t numBytesInLeftwardSiblings(const ChildEntry *child) const;
uint64_t numBytesInChild(const ChildEntry *child); uint64_t numBytesInChild(const ChildEntry *child) const;
}; };
} }

View File

@ -15,7 +15,7 @@ DataLeafNode::DataLeafNode(DataNodeView view)
DataLeafNode::~DataLeafNode() { DataLeafNode::~DataLeafNode() {
} }
void DataLeafNode::read(off_t offset, size_t count, Data *result) { void DataLeafNode::read(off_t offset, size_t count, Data *result) const {
assert(count <= result->size()); assert(count <= result->size());
assert(offset+count <= numBytesInThisNode()); assert(offset+count <= numBytesInThisNode());
std::memcpy(result->data(), _node.DataBegin<unsigned char>()+offset, count); std::memcpy(result->data(), _node.DataBegin<unsigned char>()+offset, count);
@ -37,7 +37,7 @@ void DataLeafNode::fillDataWithZeroesFromTo(off_t begin, off_t end) {
std::memset(_node.DataBegin<unsigned char>()+begin, 0, end-begin); std::memset(_node.DataBegin<unsigned char>()+begin, 0, end-begin);
} }
uint64_t DataLeafNode::numBytesInThisNode() { uint64_t DataLeafNode::numBytesInThisNode() const {
return *_node.Size(); return *_node.Size();
} }

View File

@ -16,10 +16,10 @@ public:
void InitializeNewNode(); void InitializeNewNode();
void read(off_t offset, size_t count, blockstore::Data *result) override; void read(off_t offset, size_t count, blockstore::Data *result) const override;
void write(off_t offset, size_t count, const blockstore::Data &data) override; void write(off_t offset, size_t count, const blockstore::Data &data) override;
uint64_t numBytesInThisNode() override; uint64_t numBytesInThisNode() const override;
void resize(uint64_t newsize_bytes) override; void resize(uint64_t newsize_bytes) override;
private: private:

View File

@ -13,12 +13,11 @@ class DataNode {
public: public:
virtual ~DataNode(); virtual ~DataNode();
//TODO MAke read, numBytesInThisNode const virtual void read(off_t offset, size_t count, blockstore::Data *result) const = 0;
virtual void read(off_t offset, size_t count, blockstore::Data *result) = 0;
virtual void write(off_t offset, size_t count, const blockstore::Data &data) = 0; virtual void write(off_t offset, size_t count, const blockstore::Data &data) = 0;
virtual void resize(uint64_t newsize_bytes) = 0; virtual void resize(uint64_t newsize_bytes) = 0;
virtual uint64_t numBytesInThisNode() = 0; virtual uint64_t numBytesInThisNode() const = 0;
static std::unique_ptr<DataNode> load(std::unique_ptr<blockstore::Block> block); static std::unique_ptr<DataNode> load(std::unique_ptr<blockstore::Block> block);
static std::unique_ptr<DataNode> createNewLeafNode(std::unique_ptr<blockstore::Block> block); static std::unique_ptr<DataNode> createNewLeafNode(std::unique_ptr<blockstore::Block> block);