Adapted to new blockstore interface (using unique_ref instead of unique_ptr)

This commit is contained in:
Sebastian Messmer 2015-07-21 15:00:57 +02:00
parent 7fc4115129
commit a923165360
5 changed files with 18 additions and 27 deletions

View File

@ -28,10 +28,9 @@ DataNodeStore::DataNodeStore(unique_ref<BlockStore> blockstore, uint32_t blocksi
DataNodeStore::~DataNodeStore() {
}
unique_ref<DataNode> DataNodeStore::load(unique_ptr<Block> block) {
unique_ref<DataNode> DataNodeStore::load(unique_ref<Block> block) {
assert(block->size() == _layout.blocksizeBytes());
//TODO Don't use cpputils::nullcheck, but make the parameter a unique_ref
DataNodeView node(cpputils::nullcheck(std::move(block)).value());
DataNodeView node(std::move(block));
if (node.Depth() == 0) {
return make_unique_ref<DataLeafNode>(std::move(node));
@ -57,17 +56,17 @@ unique_ref<DataLeafNode> DataNodeStore::createNewLeafNode() {
optional<unique_ref<DataNode>> DataNodeStore::load(const Key &key) {
auto block = _blockstore->load(key);
if (block == nullptr) {
if (block == none) {
return none;
} else {
return load(std::move(*block));
}
return load(std::move(block));
}
unique_ref<DataNode> DataNodeStore::createNewNodeAsCopyFrom(const DataNode &source) {
assert(source.node().layout().blocksizeBytes() == _layout.blocksizeBytes()); // This might be violated if source is from a different DataNodeStore
auto newBlock = blockstore::utils::copyToNewBlock(_blockstore.get(), source.node().block());
//TODO Don't use to_unique_ptr
return load(cpputils::to_unique_ptr(std::move(newBlock)));
return load(std::move(newBlock));
}
unique_ref<DataNode> DataNodeStore::overwriteNodeWith(unique_ref<DataNode> target, const DataNode &source) {
@ -87,8 +86,7 @@ unique_ref<DataNode> DataNodeStore::overwriteNodeWith(unique_ref<DataNode> targe
void DataNodeStore::remove(unique_ref<DataNode> node) {
auto block = node->node().releaseBlock();
cpputils::to_unique_ptr(std::move(node)).reset(); // Call destructor
//TODO Don't use to_unique_ptr
_blockstore->remove(cpputils::to_unique_ptr(std::move(block)));
_blockstore->remove(std::move(block));
}
uint64_t DataNodeStore::numNodes() const {

View File

@ -45,7 +45,7 @@ public:
//TODO Test overwriteNodeWith(), createNodeAsCopyFrom(), removeSubtree()
private:
cpputils::unique_ref<DataNode> load(std::unique_ptr<blockstore::Block> block);
cpputils::unique_ref<DataNode> load(cpputils::unique_ref<blockstore::Block> block);
cpputils::unique_ref<blockstore::BlockStore> _blockstore;
const DataNodeLayout _layout;

View File

@ -135,8 +135,7 @@ TEST_F(DataInnerNodeTest, InitializesCorrectly) {
TEST_F(DataInnerNodeTest, ReinitializesCorrectly) {
auto key = InitializeInnerNodeAddLeafReturnKey();
//TODO Don't use cpputils::nullcheck
auto node = DataInnerNode::InitializeNewNode(cpputils::nullcheck(blockStore->load(key)).value(), *leaf);
auto node = DataInnerNode::InitializeNewNode(blockStore->load(key).value(), *leaf);
EXPECT_EQ(1u, node->numChildren());
EXPECT_EQ(leaf->key(), node->getChild(0)->key());
@ -197,7 +196,7 @@ TEST_F(DataInnerNodeTest, ConvertToInternalNode) {
TEST_F(DataInnerNodeTest, ConvertToInternalNodeZeroesOutChildrenRegion) {
Key key = CreateNodeWithDataConvertItToInnerNodeAndReturnKey();
auto block = blockStore->load(key);
auto block = blockStore->load(key).value();
EXPECT_EQ(0, std::memcmp(ZEROES.data(), (uint8_t*)block->data()+DataNodeLayout::HEADERSIZE_BYTES+sizeof(DataInnerNode::ChildEntry), nodeStore->layout().maxBytesPerLeaf()-sizeof(DataInnerNode::ChildEntry)));
}

View File

@ -140,8 +140,7 @@ TEST_F(DataLeafNodeTest, InitializesCorrectly) {
TEST_F(DataLeafNodeTest, ReinitializesCorrectly) {
auto key = InitializeLeafGrowAndReturnKey();
//TODO Don't use nullcheck
auto leaf = DataLeafNode::InitializeNewNode(cpputils::nullcheck(blockStore->load(key)).value());
auto leaf = DataLeafNode::InitializeNewNode(blockStore->load(key).value());
EXPECT_EQ(0u, leaf->numBytes());
}
@ -208,14 +207,14 @@ TEST_F(DataLeafNodeTest, DataGetsZeroFilledWhenShrinking) {
uint32_t smaller_size = randomData.size() - 100;
{
//At first, we expect there to be random data in the underlying data block
auto block = blockStore->load(key);
auto block = blockStore->load(key).value();
EXPECT_EQ(0, std::memcmp((char*)randomData.data()+smaller_size, (uint8_t*)block->data()+DataNodeLayout::HEADERSIZE_BYTES+smaller_size, 100));
}
//After shrinking, we expect there to be zeroes in the underlying data block
ResizeLeaf(key, smaller_size);
{
auto block = blockStore->load(key);
auto block = blockStore->load(key).value();
EXPECT_EQ(0, std::memcmp(ZEROES.data(), (uint8_t*)block->data()+DataNodeLayout::HEADERSIZE_BYTES+smaller_size, 100));
}
}
@ -242,7 +241,7 @@ TEST_F(DataLeafNodeTest, ConvertToInternalNode) {
TEST_F(DataLeafNodeTest, ConvertToInternalNodeZeroesOutChildrenRegion) {
Key key = CreateLeafWithDataConvertItToInnerNodeAndReturnKey();
auto block = blockStore->load(key);
auto block = blockStore->load(key).value();
EXPECT_EQ(0, std::memcmp(ZEROES.data(), (uint8_t*)block->data()+DataNodeLayout::HEADERSIZE_BYTES+sizeof(DataInnerNode::ChildEntry), nodeStore->layout().maxBytesPerLeaf()-sizeof(DataInnerNode::ChildEntry)));
}

View File

@ -40,8 +40,7 @@ TEST_P(DataNodeViewDepthTest, DepthIsStored) {
DataNodeView view(std::move(block));
view.setDepth(GetParam());
}
//TODO Don't use nullcheck
DataNodeView view(cpputils::nullcheck(blockStore->load(key)).value());
DataNodeView view(blockStore->load(key).value());
EXPECT_EQ(GetParam(), view.Depth());
}
@ -56,8 +55,7 @@ TEST_P(DataNodeViewSizeTest, SizeIsStored) {
DataNodeView view(std::move(block));
view.setSize(GetParam());
}
//TODO Don't use nullcheck
DataNodeView view(cpputils::nullcheck(blockStore->load(key)).value());
DataNodeView view(blockStore->load(key).value());
EXPECT_EQ(GetParam(), view.Size());
}
@ -69,8 +67,7 @@ TEST_F(DataNodeViewTest, DataIsStored) {
DataNodeView view(std::move(block));
view.write(randomData.data(), 0, randomData.size());
}
//TODO Don't use nullcheck
DataNodeView view(cpputils::nullcheck(blockStore->load(key)).value());
DataNodeView view(blockStore->load(key).value());
EXPECT_EQ(0, std::memcmp(view.data(), randomData.data(), randomData.size()));
}
@ -79,14 +76,12 @@ TEST_F(DataNodeViewTest, HeaderAndBodyDontOverlap) {
auto block = blockStore->create(Data(BLOCKSIZE_BYTES));
auto key = block->key();
{
//TODO Don't use nullcheck
DataNodeView view(std::move(block));
view.setDepth(3);
view.setSize(1000000000u);
view.write(randomData.data(), 0, DATASIZE_BYTES);
}
//TODO Don't use nullcheck
DataNodeView view(cpputils::nullcheck(blockStore->load(key)).value());
DataNodeView view(blockStore->load(key).value());
EXPECT_EQ(3, view.Depth());
EXPECT_EQ(1000000000u, view.Size());
EXPECT_EQ(0, std::memcmp(view.data(), randomData.data(), DATASIZE_BYTES));