2015-01-22 23:37:03 +01:00
|
|
|
#include "DataLeafNode.h"
|
2015-01-24 00:54:27 +01:00
|
|
|
#include "DataInnerNode.h"
|
2014-12-09 18:53:11 +01:00
|
|
|
|
|
|
|
using std::unique_ptr;
|
2015-01-24 00:54:27 +01:00
|
|
|
using std::make_unique;
|
2014-12-09 18:53:11 +01:00
|
|
|
using blockstore::Block;
|
2014-12-10 16:48:00 +01:00
|
|
|
using blockstore::Data;
|
2014-12-13 19:17:08 +01:00
|
|
|
using blockstore::Key;
|
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-03-04 20:58:39 +01:00
|
|
|
assert(node().Depth() == 0);
|
2015-02-25 22:30:48 +01:00
|
|
|
assert(numBytes() <= maxStoreableBytes());
|
2014-12-09 18:53:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DataLeafNode::~DataLeafNode() {
|
|
|
|
}
|
|
|
|
|
2015-02-20 19:46:52 +01:00
|
|
|
unique_ptr<DataLeafNode> DataLeafNode::InitializeNewNode(unique_ptr<Block> block) {
|
|
|
|
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-02-20 19:46:52 +01:00
|
|
|
return make_unique<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 {
|
|
|
|
assert(offset <= node().Size() && offset + size <= node().Size()); // Also check offset, because the addition could lead to overflows
|
|
|
|
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) {
|
|
|
|
assert(offset <= node().Size() && offset + size <= node().Size()); // Also check offset, because the addition could lead to overflows
|
|
|
|
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-02-25 22:30:48 +01:00
|
|
|
assert(new_size <= maxStoreableBytes());
|
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-02-25 22:30:48 +01:00
|
|
|
uint32_t DataLeafNode::maxStoreableBytes() const {
|
|
|
|
return node().layout().maxBytesPerLeaf();
|
|
|
|
}
|
|
|
|
|
2014-12-09 18:53:11 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-13 19:17:08 +01:00
|
|
|
}
|