Skip to content

Commit

Permalink
feat: add observe remove set stubs for p0
Browse files Browse the repository at this point in the history
Signed-off-by: Yuchen Liang <yuchenl3@andrew.cmu.edu>
  • Loading branch information
yliang412 committed Jan 11, 2024
1 parent 8db7bfc commit 8bb5b9f
Show file tree
Hide file tree
Showing 8 changed files with 553 additions and 17 deletions.
10 changes: 3 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,10 @@ 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
123 changes: 123 additions & 0 deletions src/include/primer/orset_driver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#pragma once

#include <atomic>
#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:
explicit ORSetNode(ORSetDriver<T> *driver, size_t node_id) : driver_(driver), node_id_(node_id) {}

/**
* @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 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 Monotonically increasing unique id for the elements. */
std::atomic<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
43 changes: 43 additions & 0 deletions src/primer/orset_driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "primer/orset_driver.h"
#include <string>

namespace bustub {

template <typename T>
ORSetDriver<T>::ORSetDriver(size_t 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));
}
saved_copies_.resize(num_orset_node);
}

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

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

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 ORSetDriver<int>;
template class ORSetDriver<std::string>;

} // namespace bustub
Loading

0 comments on commit 8bb5b9f

Please # to comment.