This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V0.42.9 with cache kv improvement (#24)
* use memdb for cachekv * Remove remaining n^2 nature of CacheKV (Speedup dirtyItems) * Delete deadcode, fix lint Co-authored-by: mconcat <[email protected]>
- Loading branch information
1 parent
cf69ece
commit d961550
Showing
2 changed files
with
80 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,51 @@ | ||
package cachekv | ||
|
||
import ( | ||
"errors" | ||
|
||
dbm "github.com/tendermint/tm-db" | ||
|
||
"github.com/cosmos/cosmos-sdk/types/kv" | ||
"github.com/cosmos/cosmos-sdk/store/types" | ||
) | ||
|
||
// Iterates over iterKVCache items. | ||
// if key is nil, means it was deleted. | ||
// Implements Iterator. | ||
type memIterator struct { | ||
start, end []byte | ||
items []*kv.Pair | ||
ascending bool | ||
} | ||
|
||
func newMemIterator(start, end []byte, items *kv.List, ascending bool) *memIterator { | ||
itemsInDomain := make([]*kv.Pair, 0, items.Len()) | ||
|
||
var entered bool | ||
|
||
for e := items.Front(); e != nil; e = e.Next() { | ||
item := e.Value | ||
if !dbm.IsKeyInDomain(item.Key, start, end) { | ||
if entered { | ||
break | ||
} | ||
|
||
continue | ||
} | ||
|
||
itemsInDomain = append(itemsInDomain, item) | ||
entered = true | ||
} | ||
|
||
return &memIterator{ | ||
start: start, | ||
end: end, | ||
items: itemsInDomain, | ||
ascending: ascending, | ||
} | ||
} | ||
|
||
func (mi *memIterator) Domain() ([]byte, []byte) { | ||
return mi.start, mi.end | ||
} | ||
types.Iterator | ||
|
||
func (mi *memIterator) Valid() bool { | ||
return len(mi.items) > 0 | ||
deleted map[string]struct{} | ||
} | ||
|
||
func (mi *memIterator) assertValid() { | ||
if err := mi.Error(); err != nil { | ||
panic(err) | ||
} | ||
} | ||
func newMemIterator(start, end []byte, items *dbm.MemDB, deleted map[string]struct{}, ascending bool) *memIterator { | ||
var iter types.Iterator | ||
var err error | ||
|
||
func (mi *memIterator) Next() { | ||
mi.assertValid() | ||
if ascending { | ||
iter, err = items.Iterator(start, end) | ||
} else { | ||
iter, err = items.ReverseIterator(start, end) | ||
} | ||
|
||
if mi.ascending { | ||
mi.items = mi.items[1:] | ||
} else { | ||
mi.items = mi.items[:len(mi.items)-1] | ||
} | ||
} | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
func (mi *memIterator) Key() []byte { | ||
mi.assertValid() | ||
newDeleted := make(map[string]struct{}) | ||
for k, v := range deleted { | ||
newDeleted[k]=v | ||
} | ||
|
||
if mi.ascending { | ||
return mi.items[0].Key | ||
} | ||
return &memIterator { | ||
Iterator: iter, | ||
|
||
return mi.items[len(mi.items)-1].Key | ||
deleted: newDeleted, | ||
} | ||
} | ||
|
||
func (mi *memIterator) Value() []byte { | ||
mi.assertValid() | ||
|
||
if mi.ascending { | ||
return mi.items[0].Value | ||
} | ||
|
||
return mi.items[len(mi.items)-1].Value | ||
} | ||
|
||
func (mi *memIterator) Close() error { | ||
mi.start = nil | ||
mi.end = nil | ||
mi.items = nil | ||
|
||
return nil | ||
} | ||
|
||
// Error returns an error if the memIterator is invalid defined by the Valid | ||
// method. | ||
func (mi *memIterator) Error() error { | ||
if !mi.Valid() { | ||
return errors.New("invalid memIterator") | ||
} | ||
|
||
return nil | ||
key := mi.Iterator.Key() | ||
if _, ok := mi.deleted[string(key)]; ok { | ||
return nil | ||
} | ||
return mi.Iterator.Value() | ||
} |
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