2015-01-22 23:37:03 +01:00
|
|
|
#include "DataLeafNode.h"
|
2015-01-24 00:54:27 +01:00
|
|
|
#include "DataInnerNode.h"
|
2015-07-22 13:42:44 +02:00
|
|
|
#include <messmer/cpp-utils/assert/assert.h>
|
2014-12-09 18:53:11 +01:00
|
|
|
|
|
|
|
using blockstore::Block;
|
2015-04-25 02:55:34 +02:00
|
|
|
using cpputils::Data;
|
2014-12-13 19:17:08 +01:00
|
|
|
using blockstore::Key;
|
2015-06-26 15:59:18 +02:00
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
2014-12-09 18:53:11 +01:00
|
|
|
|
|
|
|
namespace blobstore {
|
|
|
|
namespace onblocks {
|
2014-12-13 19:17:08 +01:00
|
|
|
namespace datanodestore {
|
2014-12-09 18:53:11 +01:00
|
|
|
|
2015-01-24 22:27:14 +01:00
|
|
|
DataLeafNode::DataLeafNode(DataNodeView view)
|
|
|
|
: DataNode(std::move(view)) {
|
2015-07-22 13:42:44 +02:00
|
|
|
ASSERT(node().Depth() == 0, "Leaf node must have depth 0. Is it an inner node instead?");
|
|
|
|
ASSERT(numBytes() <= maxStoreableBytes(), "Leaf says it stores more bytes than it has space for");
|
2014-12-09 18:53:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DataLeafNode::~DataLeafNode() {
|
|
|
|
}
|
|
|
|
|
2015-07-20 19:10:46 +02:00
|
|
|
unique_ref<DataLeafNode> DataLeafNode::InitializeNewNode(unique_ref<Block> block) {
|
2015-02-20 19:46:52 +01:00
|
|
|
DataNodeView node(std::move(block));
|
2015-03-04 20:58:39 +01:00
|
|
|
node.setDepth(0);
|
|
|
|
node.setSize(0);
|
2014-12-11 00:20:23 +01:00
|
|
|
//fillDataWithZeroes(); not needed, because a newly created block will be zeroed out. DataLeafNodeTest.SpaceIsZeroFilledWhenGrowing ensures this.
|
2015-06-26 15:59:18 +02:00
|
|
|
return make_unique_ref<DataLeafNode>(std::move(node));
|
2014-12-11 00:20:23 +01:00
|
|
|
}
|
|
|
|
|
2015-03-04 20:58:39 +01:00
|
|
|
void DataLeafNode::read(void *target, uint64_t offset, uint64_t size) const {
|
2015-07-22 13:42:44 +02:00
|
|
|
ASSERT(offset <= node().Size() && offset + size <= node().Size(), "Read out of valid area"); // Also check offset, because the addition could lead to overflows
|
2015-03-04 20:58:39 +01:00
|
|
|
std::memcpy(target, (uint8_t*)node().data() + offset, size);
|
2014-12-10 16:48:00 +01:00
|
|
|
}
|
|
|
|
|
2015-03-04 20:58:39 +01:00
|
|
|
void DataLeafNode::write(const void *source, uint64_t offset, uint64_t size) {
|
2015-07-22 13:42:44 +02:00
|
|
|
ASSERT(offset <= node().Size() && offset + size <= node().Size(), "Write out of valid area"); // Also check offset, because the addition could lead to overflows
|
2015-03-04 20:58:39 +01:00
|
|
|
node().write(source, offset, size);
|
2014-12-09 18:53:11 +01:00
|
|
|
}
|
|
|
|
|
2015-01-22 23:37:03 +01:00
|
|
|
uint32_t DataLeafNode::numBytes() const {
|
2015-03-04 20:58:39 +01:00
|
|
|
return node().Size();
|
2015-01-22 23:37:03 +01:00
|
|
|
}
|
2014-12-10 23:34:36 +01:00
|
|
|
|
2015-01-22 23:37:03 +01:00
|
|
|
void DataLeafNode::resize(uint32_t new_size) {
|
2015-07-22 13:42:44 +02:00
|
|
|
ASSERT(new_size <= maxStoreableBytes(), "Trying to resize to a size larger than the maximal size");
|
2015-03-04 20:58:39 +01:00
|
|
|
uint32_t old_size = node().Size();
|
2015-01-22 23:37:03 +01:00
|
|
|
if (new_size < old_size) {
|
|
|
|
fillDataWithZeroesFromTo(new_size, old_size);
|
2014-12-11 00:20:23 +01:00
|
|
|
}
|
2015-03-04 20:58:39 +01:00
|
|
|
node().setSize(new_size);
|
2015-01-22 23:37:03 +01:00
|
|
|
}
|
2014-12-11 00:20:23 +01:00
|
|
|
|
2015-01-22 23:37:03 +01:00
|
|
|
void DataLeafNode::fillDataWithZeroesFromTo(off_t begin, off_t end) {
|
2015-03-04 20:58:39 +01:00
|
|
|
Data ZEROES(end-begin);
|
|
|
|
ZEROES.FillWithZeroes();
|
|
|
|
node().write(ZEROES.data(), begin, end-begin);
|
2014-12-10 23:34:36 +01:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:18:17 +01:00
|
|
|
uint64_t DataLeafNode::maxStoreableBytes() const {
|
2015-02-25 22:30:48 +01:00
|
|
|
return node().layout().maxBytesPerLeaf();
|
|
|
|
}
|
|
|
|
|
2014-12-09 18:53:11 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-13 19:17:08 +01:00
|
|
|
}
|