forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store/v2): Merge Feature Branch (cosmos#18150)
Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com> Co-authored-by: yihuang <huang@crypto.com>
- Loading branch information
1 parent
7b90fc0
commit 03bca7b
Showing
181 changed files
with
6,205 additions
and
16,229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package store | ||
|
||
// Batch is a write-only database that commits changes to the underlying database | ||
// when Write is called. A batch cannot be used concurrently. | ||
type Batch interface { | ||
Writer | ||
|
||
// Size retrieves the amount of data queued up for writing, this includes | ||
// the keys, values, and deleted keys. | ||
Size() int | ||
|
||
// Write flushes any accumulated data to disk. | ||
Write() error | ||
|
||
// Reset resets the batch. | ||
Reset() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# branchkv | ||
|
||
The `branchkv.Store` implementation defines a `BranchedKVStore` that contains a | ||
reference to a `VersionedDatabase`, i.e. an SS backend. The `branchkv.Store` is | ||
meant to be used as the primary store used in a `RootStore` implementation. It | ||
provides the ability to get the current `ChangeSet`, branching, and writing to | ||
a parent store (if one is defined). Note, all reads first pass through the | ||
staged, i.e. dirty writes. If a key is not found in the staged writes, the read | ||
is then passed to the parent store (if one is defined), finally falling back to | ||
the backing SS engine. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package branchkv | ||
|
||
import ( | ||
"slices" | ||
|
||
"cosmossdk.io/store/v2" | ||
) | ||
|
||
var _ store.Iterator = (*iterator)(nil) | ||
|
||
// iterator walks over both the KVStore's changeset, i.e. dirty writes, and the | ||
// parent iterator, which can either be another KVStore or the SS backend, at the | ||
// same time. | ||
// | ||
// Note, writes that happen on the KVStore over an iterator will not affect the | ||
// iterator. This is because when an iterator is created, it takes a current | ||
// snapshot of the changeset. | ||
type iterator struct { | ||
parentItr store.Iterator | ||
start []byte | ||
end []byte | ||
key []byte | ||
value []byte | ||
keys []string | ||
values []store.KVPair | ||
reverse bool | ||
exhausted bool // exhausted reflects if the parent iterator is exhausted or not | ||
} | ||
|
||
// Domain returns the domain of the iterator. The caller must not modify the | ||
// return values. | ||
func (itr *iterator) Domain() ([]byte, []byte) { | ||
return itr.start, itr.end | ||
} | ||
|
||
func (itr *iterator) Key() []byte { | ||
return slices.Clone(itr.key) | ||
} | ||
|
||
func (itr *iterator) Value() []byte { | ||
return slices.Clone(itr.value) | ||
} | ||
|
||
func (itr *iterator) Close() { | ||
itr.key = nil | ||
itr.value = nil | ||
itr.keys = nil | ||
itr.values = nil | ||
itr.parentItr.Close() | ||
} | ||
|
||
func (itr *iterator) Next() bool { | ||
for { | ||
switch { | ||
case itr.exhausted && len(itr.keys) == 0: // exhausted both | ||
itr.key = nil | ||
itr.value = nil | ||
return false | ||
|
||
case itr.exhausted: // exhausted parent iterator but not store (dirty writes) iterator | ||
nextKey := itr.keys[0] | ||
nextValue := itr.values[0] | ||
|
||
// pop off the key | ||
itr.keys[0] = "" | ||
itr.keys = itr.keys[1:] | ||
|
||
// pop off the value | ||
itr.values[0].Value = nil | ||
itr.values = itr.values[1:] | ||
|
||
if nextValue.Value != nil { | ||
itr.key = []byte(nextKey) | ||
itr.value = nextValue.Value | ||
return true | ||
} | ||
|
||
case len(itr.keys) == 0: // exhausted store (dirty writes) iterator but not parent iterator | ||
itr.key = itr.parentItr.Key() | ||
itr.value = itr.parentItr.Value() | ||
itr.exhausted = !itr.parentItr.Next() | ||
|
||
return true | ||
|
||
default: // parent iterator is not exhausted and we have store (dirty writes) remaining | ||
dirtyKey := itr.keys[0] | ||
dirtyVal := itr.values[0] | ||
|
||
parentKey := itr.parentItr.Key() | ||
parentKeyStr := string(parentKey) | ||
|
||
switch { | ||
case (!itr.reverse && dirtyKey < parentKeyStr) || (itr.reverse && dirtyKey > parentKeyStr): // dirty key should come before parent's key | ||
// pop off key | ||
itr.keys[0] = "" | ||
itr.keys = itr.keys[1:] | ||
|
||
// pop off value | ||
itr.values[0].Value = nil | ||
itr.values = itr.values[1:] | ||
|
||
if dirtyVal.Value != nil { | ||
itr.key = []byte(dirtyKey) | ||
itr.value = dirtyVal.Value | ||
return true | ||
} | ||
|
||
case (!itr.reverse && parentKeyStr < dirtyKey) || (itr.reverse && parentKeyStr > dirtyKey): // parent's key should come before dirty key | ||
itr.key = parentKey | ||
itr.value = itr.parentItr.Value() | ||
itr.exhausted = !itr.parentItr.Next() | ||
return true | ||
|
||
default: | ||
// pop off key | ||
itr.keys[0] = "" | ||
itr.keys = itr.keys[1:] | ||
|
||
// pop off value | ||
itr.values[0].Value = nil | ||
itr.values = itr.values[1:] | ||
|
||
itr.exhausted = !itr.parentItr.Next() | ||
|
||
if dirtyVal.Value != nil { | ||
itr.key = []byte(dirtyKey) | ||
itr.value = dirtyVal.Value | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (itr *iterator) Valid() bool { | ||
return itr.key != nil && itr.value != nil | ||
} | ||
|
||
func (itr *iterator) Error() error { | ||
return itr.parentItr.Error() | ||
} |
Oops, something went wrong.