From ab9b6a8eae1d0ab9ef6d69be5c2341e3ffb399c3 Mon Sep 17 00:00:00 2001 From: Connor1996 Date: Wed, 29 Apr 2020 18:07:58 +0800 Subject: [PATCH] add scanner framework Signed-off-by: Connor1996 --- courses/project4-Transaction.md | 2 +- kv/transaction/mvcc/scanner.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 kv/transaction/mvcc/scanner.go diff --git a/courses/project4-Transaction.md b/courses/project4-Transaction.md index 7bb16a92..3b805874 100644 --- a/courses/project4-Transaction.md +++ b/courses/project4-Transaction.md @@ -82,7 +82,7 @@ In this part, you will implement `KvScan`, `KvCheckTxnStatus`, `KvBatchRollback` > Hints: > -> - For scanning, you might find it helpful to implement your own scanner (iterator) abstraction which iterates over logical values, rather than the raw values in underlying storage. +> - For scanning, you might find it helpful to implement your own scanner (iterator) abstraction which iterates over logical values, rather than the raw values in underlying storage. `kv/transaction/mvcc/scanner.go` is a framework for you. > - When scanning, some errors can be recorded for an individual key and should not cause the whole scan to stop. For other commands, any single key causing an error should cause the whole operation to stop. > - Since `KvResolveLock` either commits or rolls back its keys, you should be able to share code with the `KvBatchRollback` and `KvCommit` implementations. > - A timestamp consists of a physical and a logical component. The physical part is roughly a monotonic version of wall-clock time. Usually we use the whole timestamp, for example when comparing timestamps for equality. However, when calculating timeouts, we must only use the physical part of the timestamp. To do this you may find the `PhysicalTime` function in [server.go](/kv/server/server.go) useful. diff --git a/kv/transaction/mvcc/scanner.go b/kv/transaction/mvcc/scanner.go new file mode 100644 index 00000000..6e4890d0 --- /dev/null +++ b/kv/transaction/mvcc/scanner.go @@ -0,0 +1,24 @@ +package mvcc + +// 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. +type Scanner struct { + // Your Data Here (4C). +} + +// NewScanner creates a new scanner ready to read from the snapshot in txn. +func NewScanner(startKey []byte, txn *MvccTxn) *Scanner { + // Your Code Here (4C). + return nil +} + +func (scan *Scanner) Close() { + // Your Code Here (4C). +} + +// Next returns the next key/value pair from the scanner. If the scanner is exhausted, then it will return `nil, nil, nil`. +func (scan *Scanner) Next() ([]byte, []byte, error) { + // Your Code Here (4C). + return nil, nil, nil +}