CMU15445-2023spring 笔记:Project 0 - Copy-On-Write Trie

CMU15445-2023spring 笔记:Project 0 - Copy-On-Write Trie

In this project, you will implement a key-value store backed by a copy-on-write trie. Tries are efficient ordered-tree data structures for retrieving a value for a given key. To simplify the explanation, we will assume that the keys are variable-length strings, but in practice they can be any arbitrary type.

Task #1 - Copy-On-Write Trie

copy-on-write 是指操作并不会直接修改原来的 trie node,而是创建一个新的 node 用来修改数据,并返回新的 trie 树的 root 节点。

使用 copy-on-write 的好处在于,能够在操作完成的任意时刻,以很小的代价访问 trie 树。

img

如图所示:

在老的 trie 树上,插入新的 ("b", 3),会创建一个新的 root 节点,然后重复使用之前的非根节点,这样做之后。我们能够得到插入之前和之后的内容。只要我们有 trie node 的根节点,我们能够访问在那一时刻的 trie 树中的数据。

这个 task 要修改 trie.h 和 trie.cpp 中的代码实现下图所示的方法:
img

在遍历过程中,碰到需要修改的 node,需要做两个操作:

  1. copy 这个 node 然后创建一个 TrieNode 或者 TrieNodeWithValue,将这个新创建的节点指针给 copy_node map 里对应的 <char, ptr>。
  2. 将这个 node 在父节点中的指针,改为指向这个 copy_node 的指针。由于父节点实际上也进行了修改,所以也要进行 copy-on-write,在这个过程中需要使用一个栈来记录从根节点到给定字符节点的路径。

Task #2 - Concurrent Key-Value Store

需要注意 Get 方法只需要在获取根节点的时候加 root 锁,Put 和 Remove 方法在整个过程中都要一直持有 write_lock 锁,并且在读写根节点的时候加 root 锁。

std::optional 是 C++ 17 引入的新标准,用来统一接收一个类型的值或者其“异常”状态。

Task #3 - Debugging

Task #4 - SQL String Functions

Result

img

posted on 2023-08-04 22:04  LambdaQ  阅读(663)  评论(0编辑  收藏  举报