Skip to content

Commit 82912b1

Browse files
yliang412ChaosZhai
andauthored
feat: add observe remove set stubs for p0 (#681)
* feat: add observe remove set stubs for p0 Signed-off-by: Yuchen Liang <[email protected]> * feat: add observe remove set stubs for p0 Signed-off-by: Yuchen Liang <[email protected]> * fix format Signed-off-by: Yuchen Liang <[email protected]> * fix format Signed-off-by: Yuchen Liang <[email protected]> * fix public-ci Signed-off-by: Yuchen Liang <[email protected]> * P0: add sql test, version counter * fix primer cmakelist * fix release CI * remove atomics Signed-off-by: Yuchen Liang <[email protected]> --------- Signed-off-by: Yuchen Liang <[email protected]> Co-authored-by: chaoszhai <[email protected]>
1 parent 8db7bfc commit 82912b1

File tree

9 files changed

+573
-16
lines changed

9 files changed

+573
-16
lines changed

CMakeLists.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,12 @@ add_custom_target(fix-clang-tidy-diff
250250
# hardcode some files to check here for each project.
251251
# ##########################################################
252252
set(P0_FILES
253-
"src/include/primer/trie_answer.h"
254-
"src/include/primer/trie_store.h"
255-
"src/include/primer/trie.h"
256-
"src/primer/trie_store.cpp"
257-
"src/primer/trie.cpp"
258253
"src/planner/plan_func_call.cpp"
259254
"src/include/execution/expressions/string_expression.h"
255+
"src/include/primer/orset.h"
256+
"src/primer/orset.cpp"
260257
)
258+
261259
add_custom_target(check-clang-tidy-p0
262260
${BUSTUB_BUILD_SUPPORT_DIR}/run_clang_tidy.py # run LLVM's clang-tidy script
263261
-clang-tidy-binary ${CLANG_TIDY_BIN} # using our clang-tidy binary

src/include/primer/orset.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
6+
namespace bustub {
7+
8+
/** @brief Unique ID type. */
9+
using uid_t = int64_t;
10+
11+
/** @brief The observed remove set datatype. */
12+
template <typename T>
13+
class ORSet {
14+
public:
15+
ORSet() = default;
16+
17+
/**
18+
* @brief Checks if an element is in the set.
19+
*
20+
* @param elem the element to check
21+
* @return true if the element is in the set, and false otherwise.
22+
*/
23+
auto Contains(const T &elem) const -> bool;
24+
25+
/**
26+
* @brief Adds an element to the set.
27+
*
28+
* @param elem the element to add
29+
* @param uid unique token associated with the add operation.
30+
*/
31+
void Add(const T &elem, uid_t uid);
32+
33+
/**
34+
* @brief Removes an element from the set if it exists.
35+
*
36+
* @param elem the element to remove.
37+
*/
38+
void Remove(const T &elem);
39+
40+
/**
41+
* @brief Merge changes from another ORSet.
42+
*
43+
* @param other another ORSet
44+
*/
45+
void Merge(const ORSet<T> &other);
46+
47+
/**
48+
* @brief Gets all the elements in the set.
49+
*
50+
* @return all the elements in the set.
51+
*/
52+
auto Elements() const -> std::vector<T>;
53+
54+
/**
55+
* @brief Gets a string representation of the set.
56+
*
57+
* @return a string representation of the set.
58+
*/
59+
auto ToString() const -> std::string;
60+
61+
private:
62+
// TODO(student): Add your private memeber variables to represent ORSet.
63+
};
64+
65+
} // namespace bustub

src/include/primer/orset_driver.h

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <vector>
5+
#include "primer/orset.h"
6+
7+
namespace bustub {
8+
9+
/** @brief Unique ID type. */
10+
using uid_t = int64_t;
11+
12+
template <typename T>
13+
class ORSetDriver;
14+
15+
template <typename T>
16+
class ORSetNode {
17+
public:
18+
ORSetNode() = delete;
19+
20+
explicit ORSetNode(ORSetDriver<T> *driver, size_t node_id, size_t n)
21+
: driver_(driver), node_id_(node_id), peer_size_(n), last_read_version_(n, 0) {}
22+
23+
/**
24+
* @brief Adds an element to the local ORSet.
25+
*
26+
* @param elem the element to add
27+
*/
28+
inline void Add(const T &elem) { orset_.Add(elem, driver_->GenerateUid()); }
29+
30+
/**
31+
* @brief Removes an element from the local ORSet.
32+
*
33+
* @param elem the element to remove.
34+
*/
35+
inline void Remove(const T &elem) { orset_.Remove(elem); }
36+
37+
/**
38+
* @brief Checks if an element is in the local ORSet.
39+
*
40+
* @param elem the element to check
41+
* @return true if the element is in the set, and false otherwise.
42+
*/
43+
inline auto Contains(const T &elem) -> bool { return orset_.Contains(elem); }
44+
45+
/**
46+
* @brief Merges another ORSet to the local ORSet.
47+
*
48+
* @param to_be_merged the ORSet to be merged.
49+
*/
50+
inline void Merge(const ORSet<T> to_be_merged) { orset_.Merge(to_be_merged); }
51+
52+
/**
53+
* @brief Saves all local changes to the driver.
54+
*/
55+
void Save();
56+
57+
/**
58+
* @brief Loads all the remote changes to the local ORSet.
59+
*/
60+
void Load();
61+
62+
/**
63+
* @brief Gets a copy of the local ORSet.
64+
*
65+
* @return the local ORSet.
66+
*/
67+
inline auto GetORSet() -> ORSet<T> { return orset_; }
68+
69+
private:
70+
/** @brief The local ORSet. */
71+
ORSet<T> orset_;
72+
73+
/** @brief ORSet Driver. */
74+
ORSetDriver<T> *driver_;
75+
76+
/** @brief node id */
77+
size_t node_id_;
78+
79+
/** @brief total number of nodes in the same network */
80+
size_t peer_size_;
81+
82+
/** @brief last read version number of each peer's copy */
83+
std::vector<uint32_t> last_read_version_;
84+
};
85+
86+
/** @brief A driver class for managing ORSets. */
87+
template <typename T>
88+
class ORSetDriver {
89+
friend class ORSetNode<T>;
90+
91+
public:
92+
explicit ORSetDriver(size_t num_orset_node);
93+
94+
/**
95+
* @brief Gets the ORSetNode at index.
96+
*/
97+
inline auto operator[](size_t index) -> std::unique_ptr<ORSetNode<T>> & { return orset_nodes_[index]; }
98+
auto operator[](size_t index) const -> const std::unique_ptr<ORSetNode<T>> & { return orset_nodes_[index]; }
99+
100+
/**
101+
* @brief Gets the ORSet node at index.
102+
*
103+
* @param index index of the ORSet node.
104+
* @return the ORSet node associated with the index.
105+
*/
106+
inline auto At(size_t index) -> std::unique_ptr<ORSetNode<T>> & { return orset_nodes_[index]; }
107+
108+
/**
109+
* @brief Saves changes in all nodes and then load all the changes.
110+
*/
111+
void Sync();
112+
113+
private:
114+
/**
115+
* @brief Generates a unique id.
116+
*
117+
* @return a unique id.
118+
*/
119+
inline auto GenerateUid() -> uid_t { return next_uid_++; }
120+
121+
/** @brief A list of ORSet nodes. */
122+
std::vector<std::unique_ptr<ORSetNode<T>>> orset_nodes_;
123+
124+
/** @brief List of saved copies of ORSet. */
125+
std::vector<ORSet<T>> saved_copies_;
126+
127+
/** @brief latest version number of each node */
128+
std::vector<uint32_t> version_counter_;
129+
130+
/** @brief Monotonically increasing unique id for the elements. */
131+
uid_t next_uid_ = 0;
132+
};
133+
134+
} // namespace bustub

src/primer/.clang-tidy

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/primer/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
add_library(
22
bustub_primer
33
OBJECT
4+
orset.cpp
5+
orset_driver.cpp
46
trie.cpp
57
trie_store.cpp)
68

src/primer/orset.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "primer/orset.h"
2+
#include <algorithm>
3+
#include <string>
4+
#include <vector>
5+
#include "common/exception.h"
6+
#include "fmt/format.h"
7+
8+
namespace bustub {
9+
10+
template <typename T>
11+
auto ORSet<T>::Contains(const T &elem) const -> bool {
12+
// TODO(student): Implement this
13+
throw NotImplementedException("ORSet<T>::Contains is not implemented");
14+
}
15+
16+
template <typename T>
17+
void ORSet<T>::Add(const T &elem, uid_t uid) {
18+
// TODO(student): Implement this
19+
throw NotImplementedException("ORSet<T>::Add is not implemented");
20+
}
21+
22+
template <typename T>
23+
void ORSet<T>::Remove(const T &elem) {
24+
// TODO(student): Implement this
25+
throw NotImplementedException("ORSet<T>::Remove is not implemented");
26+
}
27+
28+
template <typename T>
29+
void ORSet<T>::Merge(const ORSet<T> &other) {
30+
// TODO(student): Implement this
31+
throw NotImplementedException("ORSet<T>::Merge is not implemented");
32+
}
33+
34+
template <typename T>
35+
auto ORSet<T>::Elements() const -> std::vector<T> {
36+
// TODO(student): Implement this
37+
throw NotImplementedException("ORSet<T>::Elements is not implemented");
38+
}
39+
40+
template <typename T>
41+
auto ORSet<T>::ToString() const -> std::string {
42+
auto elements = Elements();
43+
std::sort(elements.begin(), elements.end());
44+
return fmt::format("{{{}}}", fmt::join(elements, ", "));
45+
}
46+
47+
template class ORSet<int>;
48+
template class ORSet<std::string>;
49+
50+
} // namespace bustub

src/primer/orset_driver.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "primer/orset_driver.h"
2+
#include <string>
3+
4+
namespace bustub {
5+
6+
template <typename T>
7+
void ORSetNode<T>::Load() {
8+
for (size_t i = 0; i < peer_size_; ++i) {
9+
if (i == node_id_) {
10+
continue;
11+
}
12+
uint32_t curr_version = driver_->version_counter_[i];
13+
if (last_read_version_[i] < curr_version) {
14+
Merge(driver_->saved_copies_[i]);
15+
last_read_version_[i] = curr_version;
16+
}
17+
}
18+
}
19+
20+
template <typename T>
21+
void ORSetNode<T>::Save() {
22+
driver_->saved_copies_[node_id_] = orset_;
23+
driver_->version_counter_[node_id_]++;
24+
}
25+
26+
template <typename T>
27+
ORSetDriver<T>::ORSetDriver(size_t num_orset_node) : version_counter_(num_orset_node) {
28+
orset_nodes_.reserve(num_orset_node);
29+
for (size_t i = 0; i < num_orset_node; ++i) {
30+
orset_nodes_.emplace_back(std::make_unique<ORSetNode<T>>(this, i, num_orset_node));
31+
version_counter_[i] = 0;
32+
}
33+
saved_copies_.resize(num_orset_node);
34+
}
35+
36+
template <typename T>
37+
void ORSetDriver<T>::Sync() {
38+
for (const auto &node : orset_nodes_) {
39+
node->Save();
40+
}
41+
for (const auto &node : orset_nodes_) {
42+
node->Load();
43+
}
44+
}
45+
46+
template class ORSetNode<int>;
47+
template class ORSetNode<std::string>;
48+
template class ORSetDriver<int>;
49+
template class ORSetDriver<std::string>;
50+
51+
} // namespace bustub

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ file(GLOB_RECURSE BUSTUB_TEST_SOURCES "${PROJECT_SOURCE_DIR}/test/*/*test.cpp")
1313
# #########################################
1414
add_custom_target(build-tests COMMAND ${CMAKE_CTEST_COMMAND} --show-only)
1515
add_custom_target(check-tests COMMAND ${CMAKE_CTEST_COMMAND} --verbose)
16-
add_custom_target(check-public-ci-tests COMMAND ${CMAKE_CTEST_COMMAND} --verbose -E "\"SQLLogicTest|Trie|BPlusTreeContentionTest\"")
16+
add_custom_target(check-public-ci-tests COMMAND ${CMAKE_CTEST_COMMAND} --verbose -E "\"SQLLogicTest|Trie|ORSet|BPlusTreeContentionTest\"")
1717

1818
# #########################################
1919
# "make XYZ_test"

0 commit comments

Comments
 (0)