Skip to content

Commit

Permalink
COMPLETE project4C & PASS
Browse files Browse the repository at this point in the history
  • Loading branch information
Metafora072 committed Jul 24, 2024
1 parent 7c02f10 commit 890f633
Show file tree
Hide file tree
Showing 8 changed files with 546 additions and 8 deletions.
449 changes: 445 additions & 4 deletions kv/server/server.go

Large diffs are not rendered by default.

80 changes: 78 additions & 2 deletions kv/transaction/mvcc/scanner.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,100 @@
package mvcc

import (
"bytes"
"github.com/pingcap-incubator/tinykv/kv/util/engine_util"
)

// Scanner is used for reading multiple sequential key/value pairs from the storage layer. It is aware of the implementation
// of the storage layer and returns results suitable for users.
// Invariant: either the scanner is finished and cannot be used, or it is ready to return a value immediately.
// Scanner 用于从存储层读取多个连续的键/值对。它了解存储层的实现,并返回适合用户的结果。
// 不变性:要么扫描器已完成且不能使用,要么它准备立即返回一个值。
type Scanner struct {
// Your Data Here (4C).
it engine_util.DBIterator
txn *MvccTxn
nextKey []byte
finished bool
}

// NewScanner creates a new scanner ready to read from the snapshot in txn.
// NewScanner 创建一个新的 Scanner. 准备从 txn 中的快照读取。
func NewScanner(startKey []byte, txn *MvccTxn) *Scanner {
// Your Code Here (4C).
return nil
scanner := &Scanner{
it: txn.Reader.IterCF(engine_util.CfWrite),
txn: txn,
nextKey: startKey,
finished: false,
}
return scanner
}

func (scan *Scanner) Close() {
// Your Code Here (4C).
if scan.it == nil {
return
}
scan.it.Close()
}

// Next returns the next key/value pair from the scanner. If the scanner is exhausted, then it will return `nil, nil, nil`.
// Next 返回扫描器中的下一个键/值对。如果扫描器已耗尽,则返回 `nil, nil, nil`。
func (scan *Scanner) Next() ([]byte, []byte, error) {
// Your Code Here (4C).
return nil, nil, nil
if scan.finished {
return nil, nil, nil
}

// 查找 nextKey
scan.it.Seek(EncodeKey(scan.nextKey,scan.txn.StartTS))
if scan.it.Valid() == false {
scan.finished = true
return nil, nil, nil
}

//// 获取当前迭代到的 curKey
item := scan.it.Item()
initKey := scan.nextKey
curKey := scan.it.Item().KeyCopy(nil)
userKey := DecodeUserKey(curKey)

if bytes.Equal(userKey, scan.nextKey) == false { // ???
scan.nextKey = userKey
return scan.Next()
}

for scan.it.Next(); scan.it.Valid(); scan.it.Next() {
curKey := scan.it.Item().KeyCopy(nil)
userKey := DecodeUserKey(curKey)
if bytes.Equal(userKey, scan.nextKey) == false { // 找到了下一个不同的 key
scan.nextKey = userKey
break
}
}

if scan.it.Valid() == false {
scan.finished = true
//return scan.nextKey, nil, nil
}

value, err := item.ValueCopy(nil)
if err != nil {
return initKey, nil, err
}
write, err := ParseWrite(value)
if err != nil {
return initKey, nil, err
}

if write.Kind == WriteKindDelete {
return initKey, nil, nil
}

// 查找对应的 CfDefault
goatValue, err := scan.txn.Reader.GetCF(engine_util.CfDefault,EncodeKey(initKey,write.StartTS))


return initKey, goatValue, err
}
1 change: 1 addition & 0 deletions kv/transaction/mvcc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func decodeTimestamp(key []byte) uint64 {
}

// PhysicalTime returns the physical time part of the timestamp.
// PhysicalTime 函数的作用是从一个时间戳中提取物理时间部分。
func PhysicalTime(ts uint64) uint64 {
return ts >> tsoutil.PhysicalShiftBits
}
Binary file added note/project4.assets/QQ截图20240723162856.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added note/project4.assets/QQ截图20240723164712.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added note/project4.assets/QQ截图20240723180049.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added note/project4.assets/QQ截图20240724222041.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 22 additions & 2 deletions note/project4.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

**4B:** project4B 主要实现事务的两段提交,即 prewrite 和 commit,需要完善的文件是 server.go。要注意的是,这要需要通过 server.Latches 对 keys 进行加锁。

**4C:**
**4C:** project4C 要在 project4B 的基础上,实现扫面、事务状态检查、批量回滚、清除锁这四个操作。

## 注意

1. 编码key问题,注意key还是encodekey



Expand All @@ -20,7 +24,7 @@



4B KvGet方法,图中如果不判断 RegionError 会怎样?
4B KvGet方法,图中如果不判断 RegionError 会怎样?提示中给出了信息

![](project4.assets/QQ截图20240723003649.png)

Expand All @@ -32,6 +36,22 @@

![](project4.assets/QQ截图20240723022822.png)

4C Next 方法, 图中不判断会怎样?(no used)

![](project4.assets/QQ截图20240723162856.png)

4C Next方法,图中不返回key返回nil会怎样?不判断kind==delete会怎样?

![](project4.assets/QQ截图20240723164712.png)

4C KvCheckTxnStatus 方法,计算超时时间要注意什么?提示中给出了信息

![](project4.assets/QQ截图20240723180049.png)

4C KvScan 方法,limit无法等于

![](project4.assets/QQ截图20240724222041.png)

## 说明

![ ](project4.assets/QQ截图20240722230151.png)

0 comments on commit 890f633

Please sign in to comment.