Skip to content

Commit

Permalink
Some changes from Hannah's review.
Browse files Browse the repository at this point in the history
  • Loading branch information
joka921 committed Jul 22, 2021
1 parent ddc9cf2 commit f1f933f
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/index/Index.Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void Index::passContextFileIntoVector(const string& contextFile,
// this has to be repeated completely here because we have the possibility to
// only add a text index. In that case the Vocabulary has never been
// initialized before
_vocab = std::move(Vocabulary<CompressedString, TripleComponentComparator>());
_vocab = RdfsVocabulary{};
readConfiguration();
_vocab.readFromFile(_onDiskBase + ".vocabulary",
_onDiskLiterals ? _onDiskBase + ".literals-index" : "");
Expand Down
39 changes: 14 additions & 25 deletions src/index/IndexMetaDataImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ void IndexMetaData<MapType>::appendToFile(ad_utility::File* file) const {
file->seek(0, SEEK_END);
off_t startOfMeta = file->tell();
ad_utility::serialization::FileWriteSerializer serializer{std::move(*file)};
serializer&(*this);
*file = std::move(serializer).moveFileOut();
serializer << (*this);
*file = std::move(serializer).file();
file->write(&startOfMeta, sizeof(startOfMeta));
}

Expand All @@ -98,7 +98,7 @@ void IndexMetaData<MapType>::readFromFile(ad_utility::File* file) {
ad_utility::serialization::ByteBufferReadSerializer serializer{
std::move(buf)};

serializer&(*this);
serializer >> (*this);
}

// _____________________________________________________________________________
Expand Down Expand Up @@ -182,7 +182,7 @@ void serialize(Serializer& serializer, IndexMetaData<MapType>& metaData) {
using T = IndexMetaData<MapType>;
uint64_t magicNumber = T::MAGIC_NUMBER_FOR_SERIALIZATION;

serializer& magicNumber;
serializer | magicNumber;

// This check might only become false, if we are reading from the serializer
if (magicNumber != T::MAGIC_NUMBER_FOR_SERIALIZATION) {
Expand All @@ -191,7 +191,7 @@ void serialize(Serializer& serializer, IndexMetaData<MapType>& metaData) {
"Please rebuild the index.");
}

serializer& metaData._version;
serializer | metaData._version;
// This check might only become false, if we are reading from the serializer
if (metaData.getVersion() != V_CURRENT) {
throw WrongFormatException(
Expand All @@ -200,23 +200,12 @@ void serialize(Serializer& serializer, IndexMetaData<MapType>& metaData) {
}

// Serialize the rest of the data members
serializer& metaData._name;
serializer& metaData._data;
serializer& metaData._blockData;

serializer& metaData._offsetAfter;
serializer& metaData._totalElements;
serializer& metaData._totalBytes;
serializer& metaData._totalBlocks;
}

// This overload allows us to serialize from a const IndexMetaData& to a writing
// Serializer. This is ok, because then the serialize-function does not perform
// any non-const actions.
// TODO<C++20> using a requires clause we can actually enforce the const access
// in these functions.
template <typename Serializer, typename MapType>
void serialize(Serializer& serializer, const IndexMetaData<MapType>& metaData) {
static_assert(Serializer::IsWriteSerializer);
serialize(serializer, const_cast<IndexMetaData<MapType>&>(metaData));
}
serializer | metaData._name;
serializer | metaData._data;
serializer | metaData._blockData;

serializer | metaData._offsetAfter;
serializer | metaData._totalElements;
serializer | metaData._totalBytes;
serializer | metaData._totalBlocks;
}
4 changes: 2 additions & 2 deletions src/index/MetaDataHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class MetaDataWrapperDense {
// external vector has to be restored via the `setup()` call.
template <typename Serializer>
friend void serialize(Serializer& serializer, MetaDataWrapperDense& wrapper) {
serializer& wrapper._size;
serializer | wrapper._size;
}

// ___________________________________________________________
Expand Down Expand Up @@ -255,7 +255,7 @@ class MetaDataWrapperHashMap {
template <typename Serializer>
friend void serialize(Serializer& serializer,
MetaDataWrapperHashMap& metaDataWrapper) {
serializer& metaDataWrapper._map;
serializer | metaDataWrapper._map;
}

private:
Expand Down
6 changes: 3 additions & 3 deletions src/index/MetaDataTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ class BlockBasedRelationMetaData {

template <typename Serializer>
void serialize(Serializer& serializer, BlockBasedRelationMetaData& metaData) {
serializer& metaData._startRhs;
serializer& metaData._offsetAfter;
serializer& metaData._blocks;
serializer | metaData._startRhs;
serializer | metaData._offsetAfter;
serializer | metaData._blocks;
}

class RelationMetaData {
Expand Down
4 changes: 2 additions & 2 deletions src/util/Serializer/FileSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class FileWriteSerializer {

SerializationPosition getCurrentPosition() const { return _file.tell(); }

File&& moveFileOut() && { return std::move(_file); }
File&& file() && { return std::move(_file); }

private:
File _file;
Expand Down Expand Up @@ -66,7 +66,7 @@ class FileReadSerializer {
_file.seek(position, SEEK_SET);
}

File&& moveFileOut() && { return std::move(_file); }
File&& file() && { return std::move(_file); }

private:
File _file;
Expand Down
18 changes: 10 additions & 8 deletions src/util/Serializer/SerializeHashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,31 @@
#define QLEVER_SERIALIZEHASHMAP_H

#include "../../util/HashMap.h"
#include "./SerializePair.h"

template <typename Serializer, class K, class V, class HashFcn, class EqualKey, class Alloc>
namespace ad_utility::serialization {
template <typename Serializer, class K, class V, class HashFcn, class EqualKey,
class Alloc>
void serialize(Serializer& serializer,
ad_utility::HashMap<K, V, HashFcn, EqualKey, Alloc>& hashMap) {
if constexpr (Serializer::IsWriteSerializer) {
serializer& hashMap.size();
for (auto& [key, value] : hashMap) {
serializer& key;
serializer& value;
serializer << hashMap.size();
for (const auto& pair : hashMap) {
serializer << pair;
}
} else {
hashMap.clear();
auto size = hashMap.size();
serializer& size;
serializer >> size;

hashMap.reserve(size);
for (size_t i = 0; i < size; ++i) {
std::pair<K, V> pair;
serializer& pair.first;
serializer& pair.second;
serializer >> pair;
hashMap.insert(std::move(pair));
}
}
}
} // namespace ad_utility::serialization

#endif // QLEVER_SERIALIZEHASHMAP_H
17 changes: 17 additions & 0 deletions src/util/Serializer/SerializePair.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2021, University of Freiburg, Chair of Algorithms and Data
// Structures. Author: Johannes Kalmbach <kalmbacj@cs.uni-freiburg.de>

#ifndef QLEVER_SERIALIZEPAIR_H
#define QLEVER_SERIALIZEPAIR_H

#include <utility>

namespace ad_utility::serialization {
template <typename Serializer, typename First, typename Second>
void serialize(Serializer& serializer, std::pair<First, Second>& pair) {
serializer | pair.first;
serializer | pair.second;
}
} // namespace ad_utility::serialization

#endif // QLEVER_SERIALIZEPAIR_H
4 changes: 2 additions & 2 deletions src/util/Serializer/SerializeString.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace ad_utility::serialization {
template <typename Serializer>
void serialize(Serializer& serializer, std::string& string) {
if constexpr (Serializer::IsWriteSerializer) {
serializer& string.size();
serializer << string.size();
serializer.serializeBytes(string.data(), string.size());
} else {
auto size = string.size(); // just to get the right type
serializer& size;
serializer >> size;
string.resize(size);
serializer.serializeBytes(string.data(), string.size());
}
Expand Down
8 changes: 4 additions & 4 deletions src/util/Serializer/SerializeVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ namespace ad_utility::serialization {
template <typename Serializer, typename T, typename Alloc>
void serialize(Serializer& serializer, std::vector<T, Alloc>& vector) {
if constexpr (Serializer::IsWriteSerializer) {
serializer& vector.size();
serializer << vector.size();
for (const auto& el : vector) {
serializer& el;
serializer << el;
}
} else {
auto size = vector.size(); // just to get the right type
serializer& size;
serializer >> size;
vector.reserve(size);
for (size_t i = 0; i < size; ++i) {
vector.emplace_back();
serializer& vector.back();
serializer >> vector.back();
}
}
}
Expand Down
62 changes: 49 additions & 13 deletions src/util/Serializer/Serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class SerializationException : public std::runtime_error {
using std::runtime_error::runtime_error;
};

/**
* Serializer that writes to a buffer of bytes.
*/
class ByteBufferWriteSerializer {
public:
constexpr static bool IsWriteSerializer = true;
Expand All @@ -34,9 +37,12 @@ class ByteBufferWriteSerializer {
Storage&& data() && { return std::move(_data); }

private:
std::vector<char> _data;
Storage _data;
};

/**
* Serializer that reads from a buffer of bytes.
*/
class ByteBufferReadSerializer {
public:
constexpr static bool IsWriteSerializer = false;
Expand All @@ -56,29 +62,59 @@ class ByteBufferReadSerializer {
Storage::const_iterator _iterator{_data.begin()};
};

template <typename T>
static constexpr bool TriviallyCopyable =
std::is_trivially_copyable_v<std::decay_t<T>>;

template <typename Serializer, typename T,
typename = std::enable_if_t<TriviallyCopyable<T>>>
void serialize(Serializer& serializer, T&& t) {
/**
* Trivial serializer for basic types that simply takes all the bytes
* from the object as data. Corresponds to a shallow copy (for example,
* in an std::vector this would not serialize only the metadata, but
* not the actual data).
*/
template <typename Serializer, typename T>
requires(std::is_arithmetic_v<std::decay_t<T>>) void serialize(
Serializer& serializer, T&& t) {
if constexpr (Serializer::IsWriteSerializer) {
serializer.serializeBytes(reinterpret_cast<const char*>(&t), sizeof(t));
} else {
serializer.serializeBytes(reinterpret_cast<char*>(&t), sizeof(t));
}
}

/**
* Operator that allows to write something like:
*
* ByteBufferWriteSerializer writer;
* int x = 42;
* writer | x;
* ByteBufferReadSerializer reader(std::move(writer).data());
* int y;
* reader | y;
*/
template <typename Serializer, typename T>
void operator&(Serializer& serializer, T&& t) {
void operator|(Serializer& serializer, T&& t) {
serialize(serializer, std::forward<T>(t));
}

/*
template<typename Serializer>
concept WriteSerializ
*/
// Serialization operator for explicitly writing to a serializer.
template <typename Serializer, typename T> requires (Serializer::IsWriteSerializer)
void operator<<(Serializer& serializer, const T& t) {
serialize(serializer, t);
}

// Serialization operator for explicitly reading from a serializer.
template <typename Serializer, typename T> requires (!Serializer::IsWriteSerializer)
void operator>>(Serializer& serializer, T& t) {
serialize(serializer, t);
}

// Automatically allow serialization from reference to const into a
// writeSerializer CAREFUL: this does not enforce, that the serialization
// functions actually preserve the constness, this has to be made sure by the
// user.
template <typename Serializer, typename T>
requires(Serializer::IsWriteSerializer) void serialize(Serializer& serializer,
const T& t) {
serialize(serializer, const_cast<T&>(t));
}

} // namespace ad_utility::serialization

#endif // QLEVER_SERIALIZER_SERIALIZER
1 change: 0 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,4 @@ target_link_libraries(SortPerformanceEstimatorTest gtest_main SortPerformanceEst

add_executable(SerializerTest SerializerTest.cpp)
target_link_libraries(SerializerTest gtest_main absl::flat_hash_map ${CMAKE_THREAD_LIBS_INIT})
# this test runs for quite some time! If this is undesired, comment it out
add_test(SerializerTest SerializerTest)
18 changes: 9 additions & 9 deletions test/IndexTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,26 @@ TEST(IndexTest, createFromTsvTest) {
off_t bytesDone = 0;
// Relation b
// Pair index
EXPECT_EQ(Id(0), *reinterpret_cast<Id*>(buf + bytesDone));
ASSERT_EQ(Id(0), *reinterpret_cast<Id*>(buf + bytesDone));
bytesDone += sizeof(Id);
EXPECT_EQ(4u, *reinterpret_cast<Id*>(buf + bytesDone));
ASSERT_EQ(4u, *reinterpret_cast<Id*>(buf + bytesDone));
bytesDone += sizeof(Id);
EXPECT_EQ(0u, *reinterpret_cast<Id*>(buf + bytesDone));
ASSERT_EQ(0u, *reinterpret_cast<Id*>(buf + bytesDone));
bytesDone += sizeof(Id);
EXPECT_EQ(5u, *reinterpret_cast<Id*>(buf + bytesDone));
ASSERT_EQ(5u, *reinterpret_cast<Id*>(buf + bytesDone));
bytesDone += sizeof(Id);

// Relation b2
EXPECT_EQ(Id(0), *reinterpret_cast<Id*>(buf + bytesDone));
ASSERT_EQ(Id(0), *reinterpret_cast<Id*>(buf + bytesDone));
bytesDone += sizeof(Id);
EXPECT_EQ(4u, *reinterpret_cast<Id*>(buf + bytesDone));
ASSERT_EQ(4u, *reinterpret_cast<Id*>(buf + bytesDone));
bytesDone += sizeof(Id);
EXPECT_EQ(1u, *reinterpret_cast<Id*>(buf + bytesDone));
ASSERT_EQ(1u, *reinterpret_cast<Id*>(buf + bytesDone));
bytesDone += sizeof(Id);
EXPECT_EQ(5u, *reinterpret_cast<Id*>(buf + bytesDone));
ASSERT_EQ(5u, *reinterpret_cast<Id*>(buf + bytesDone));
bytesDone += sizeof(Id);
// No LHS & RHS
EXPECT_EQ(index._PSO.metaData().getOffsetAfter(), bytesDone);
ASSERT_EQ(index._PSO.metaData().getOffsetAfter(), bytesDone);

delete[] buf;
psoFile.close();
Expand Down

0 comments on commit f1f933f

Please sign in to comment.