Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add observe remove set stubs for p0 #681

Merged
merged 10 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,12 @@ add_custom_target(fix-clang-tidy-diff
# hardcode some files to check here for each project.
# ##########################################################
set(P0_FILES
"src/include/primer/trie_answer.h"
"src/include/primer/trie_store.h"
"src/include/primer/trie.h"
"src/primer/trie_store.cpp"
"src/primer/trie.cpp"
"src/planner/plan_func_call.cpp"
"src/include/execution/expressions/string_expression.h"
"src/include/primer/orset.h"
"src/primer/orset.cpp"
)

add_custom_target(check-clang-tidy-p0
${BUSTUB_BUILD_SUPPORT_DIR}/run_clang_tidy.py # run LLVM's clang-tidy script
-clang-tidy-binary ${CLANG_TIDY_BIN} # using our clang-tidy binary
Expand Down
65 changes: 65 additions & 0 deletions src/include/primer/orset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#include <string>
#include <vector>

namespace bustub {

/** @brief Unique ID type. */
using uid_t = int64_t;

/** @brief The observed remove set datatype. */
template <typename T>
class ORSet {
public:
ORSet() = default;

/**
* @brief Checks if an element is in the set.
*
* @param elem the element to check
* @return true if the element is in the set, and false otherwise.
*/
auto Contains(const T &elem) const -> bool;

/**
* @brief Adds an element to the set.
*
* @param elem the element to add
* @param uid unique token associated with the add operation.
*/
void Add(const T &elem, uid_t uid);

/**
* @brief Removes an element from the set if it exists.
*
* @param elem the element to remove.
*/
void Remove(const T &elem);

/**
* @brief Merge changes from another ORSet.
*
* @param other another ORSet
*/
void Merge(const ORSet<T> &other);

/**
* @brief Gets all the elements in the set.
*
* @return all the elements in the set.
*/
auto Elements() const -> std::vector<T>;

/**
* @brief Gets a string representation of the set.
*
* @return a string representation of the set.
*/
auto ToString() const -> std::string;

private:
// TODO(student): Add your private memeber variables to represent ORSet.
};

} // namespace bustub
134 changes: 134 additions & 0 deletions src/include/primer/orset_driver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#pragma once

#include <memory>
#include <vector>
#include "primer/orset.h"

namespace bustub {

/** @brief Unique ID type. */
using uid_t = int64_t;

template <typename T>
class ORSetDriver;

template <typename T>
class ORSetNode {
public:
ORSetNode() = delete;

explicit ORSetNode(ORSetDriver<T> *driver, size_t node_id, size_t n)
: driver_(driver), node_id_(node_id), peer_size_(n), last_read_version_(n, 0) {}

/**
* @brief Adds an element to the local ORSet.
*
* @param elem the element to add
*/
inline void Add(const T &elem) { orset_.Add(elem, driver_->GenerateUid()); }

/**
* @brief Removes an element from the local ORSet.
*
* @param elem the element to remove.
*/
inline void Remove(const T &elem) { orset_.Remove(elem); }

/**
* @brief Checks if an element is in the local ORSet.
*
* @param elem the element to check
* @return true if the element is in the set, and false otherwise.
*/
inline auto Contains(const T &elem) -> bool { return orset_.Contains(elem); }

/**
* @brief Merges another ORSet to the local ORSet.
*
* @param to_be_merged the ORSet to be merged.
*/
inline void Merge(const ORSet<T> to_be_merged) { orset_.Merge(to_be_merged); }

/**
* @brief Saves all local changes to the driver.
*/
void Save();

/**
* @brief Loads all the remote changes to the local ORSet.
*/
void Load();

/**
* @brief Gets a copy of the local ORSet.
*
* @return the local ORSet.
*/
inline auto GetORSet() -> ORSet<T> { return orset_; }

private:
/** @brief The local ORSet. */
ORSet<T> orset_;

/** @brief ORSet Driver. */
ORSetDriver<T> *driver_;

/** @brief node id */
size_t node_id_;

/** @brief total number of nodes in the same network */
size_t peer_size_;

/** @brief last read version number of each peer's copy */
std::vector<uint32_t> last_read_version_;
};

/** @brief A driver class for managing ORSets. */
template <typename T>
class ORSetDriver {
friend class ORSetNode<T>;

public:
explicit ORSetDriver(size_t num_orset_node);

/**
* @brief Gets the ORSetNode at index.
*/
inline auto operator[](size_t index) -> std::unique_ptr<ORSetNode<T>> & { return orset_nodes_[index]; }
auto operator[](size_t index) const -> const std::unique_ptr<ORSetNode<T>> & { return orset_nodes_[index]; }

/**
* @brief Gets the ORSet node at index.
*
* @param index index of the ORSet node.
* @return the ORSet node associated with the index.
*/
inline auto At(size_t index) -> std::unique_ptr<ORSetNode<T>> & { return orset_nodes_[index]; }

/**
* @brief Saves changes in all nodes and then load all the changes.
*/
void Sync();

private:
/**
* @brief Generates a unique id.
*
* @return a unique id.
*/
inline auto GenerateUid() -> uid_t { return next_uid_++; }

/** @brief A list of ORSet nodes. */
std::vector<std::unique_ptr<ORSetNode<T>>> orset_nodes_;

/** @brief List of saved copies of ORSet. */
std::vector<ORSet<T>> saved_copies_;

/** @brief latest version number of each node */
std::vector<uint32_t> version_counter_;

/** @brief Monotonically increasing unique id for the elements. */
uid_t next_uid_ = 0;
};

} // namespace bustub
10 changes: 0 additions & 10 deletions src/primer/.clang-tidy

This file was deleted.

2 changes: 2 additions & 0 deletions src/primer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
add_library(
bustub_primer
OBJECT
orset.cpp
orset_driver.cpp
trie.cpp
trie_store.cpp)

Expand Down
50 changes: 50 additions & 0 deletions src/primer/orset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "primer/orset.h"
#include <algorithm>
#include <string>
#include <vector>
#include "common/exception.h"
#include "fmt/format.h"

namespace bustub {

template <typename T>
auto ORSet<T>::Contains(const T &elem) const -> bool {
// TODO(student): Implement this
throw NotImplementedException("ORSet<T>::Contains is not implemented");
}

template <typename T>
void ORSet<T>::Add(const T &elem, uid_t uid) {
// TODO(student): Implement this
throw NotImplementedException("ORSet<T>::Add is not implemented");
}

template <typename T>
void ORSet<T>::Remove(const T &elem) {
// TODO(student): Implement this
throw NotImplementedException("ORSet<T>::Remove is not implemented");
}

template <typename T>
void ORSet<T>::Merge(const ORSet<T> &other) {
// TODO(student): Implement this
throw NotImplementedException("ORSet<T>::Merge is not implemented");
}

template <typename T>
auto ORSet<T>::Elements() const -> std::vector<T> {
// TODO(student): Implement this
throw NotImplementedException("ORSet<T>::Elements is not implemented");
}

template <typename T>
auto ORSet<T>::ToString() const -> std::string {
auto elements = Elements();
std::sort(elements.begin(), elements.end());
return fmt::format("{{{}}}", fmt::join(elements, ", "));
}

template class ORSet<int>;
template class ORSet<std::string>;

} // namespace bustub
51 changes: 51 additions & 0 deletions src/primer/orset_driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "primer/orset_driver.h"
#include <string>

namespace bustub {

template <typename T>
void ORSetNode<T>::Load() {
for (size_t i = 0; i < peer_size_; ++i) {
if (i == node_id_) {
continue;
}
uint32_t curr_version = driver_->version_counter_[i];
if (last_read_version_[i] < curr_version) {
Merge(driver_->saved_copies_[i]);
last_read_version_[i] = curr_version;
}
}
}

template <typename T>
void ORSetNode<T>::Save() {
driver_->saved_copies_[node_id_] = orset_;
driver_->version_counter_[node_id_]++;
}

template <typename T>
ORSetDriver<T>::ORSetDriver(size_t num_orset_node) : version_counter_(num_orset_node) {
orset_nodes_.reserve(num_orset_node);
for (size_t i = 0; i < num_orset_node; ++i) {
orset_nodes_.emplace_back(std::make_unique<ORSetNode<T>>(this, i, num_orset_node));
version_counter_[i] = 0;
}
saved_copies_.resize(num_orset_node);
}

template <typename T>
void ORSetDriver<T>::Sync() {
for (const auto &node : orset_nodes_) {
node->Save();
}
for (const auto &node : orset_nodes_) {
node->Load();
}
}

template class ORSetNode<int>;
template class ORSetNode<std::string>;
template class ORSetDriver<int>;
template class ORSetDriver<std::string>;

} // namespace bustub
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ file(GLOB_RECURSE BUSTUB_TEST_SOURCES "${PROJECT_SOURCE_DIR}/test/*/*test.cpp")
# #########################################
add_custom_target(build-tests COMMAND ${CMAKE_CTEST_COMMAND} --show-only)
add_custom_target(check-tests COMMAND ${CMAKE_CTEST_COMMAND} --verbose)
add_custom_target(check-public-ci-tests COMMAND ${CMAKE_CTEST_COMMAND} --verbose -E "\"SQLLogicTest|Trie|BPlusTreeContentionTest\"")
add_custom_target(check-public-ci-tests COMMAND ${CMAKE_CTEST_COMMAND} --verbose -E "\"SQLLogicTest|Trie|ORSet|BPlusTreeContentionTest\"")

# #########################################
# "make XYZ_test"
Expand Down
Loading
Loading