Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
mossid committed Feb 26, 2019
1 parent d53512a commit 106541f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 82 deletions.
59 changes: 9 additions & 50 deletions store/cachekv/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cachekv

import (
"bytes"
"fmt"

cmn "github.com/tendermint/tendermint/libs/common"
)
Expand All @@ -20,13 +19,11 @@ type cmnpairs []cmn.KVPair
var _ pairs = (*cmnpairs)(nil)

func (pairs *cmnpairs) get(i int) cmn.KVPair {
//fmt.Println("g", i, pairs.length())
return (*pairs)[i]
}

func (pairs *cmnpairs) set(i int, pair cmn.KVPair) {
(*pairs)[i] = pair
fmt.Println("s", i, pairs.length())
}

func (pairs *cmnpairs) swap(i, j int) {
Expand Down Expand Up @@ -98,10 +95,8 @@ func (ibk indexByKey) get(bz []byte) (int, bool) {

func (ibk indexByKey) set(bz []byte, index int) {
if ibk == nil {
fmt.Println("ibk nil")
return
}
fmt.Printf("ibks %d %X\n", index, bz)
ibk[string(bz)] = index
}

Expand Down Expand Up @@ -202,7 +197,6 @@ func (this *hptr) swap(that *hptr) {
}

func (parent *hptr) isParent(child *hptr) bool {
fmt.Printf("ip %d %d\n", parent.index, child.index)
comp := bytes.Compare(parent.key(), child.key())
if parent.heap.ascending {
return comp < 0 // parent should be smaller than child
Expand Down Expand Up @@ -280,22 +274,6 @@ func (it *hptr) siftDown() {
*/
}

// XXX: test purpose, delete before merge
func (it *hptr) visualize(depth int) {
for i := 0; i < depth; i++ {
fmt.Printf(" ")
}
fmt.Printf("- %X\n", it.get().Key)
left := it.leftChild()
if left.exists() {
left.visualize(depth + 1)
}
right := it.rightChild()
if right.exists() {
it.rightChild().visualize(depth + 1)
}
}

type heap struct {
pairs
indexByKey indexByKey
Expand All @@ -309,6 +287,7 @@ func newHeapFromCache(cache map[string]cValue, ascending bool) (res *heap) {
if !cv.dirty {
continue
}

pairs = append(pairs, cmn.KVPair{
Key: []byte(k),
Value: cv.value,
Expand All @@ -325,9 +304,10 @@ func newHeap(pairs cmnpairs, ascending bool) (res *heap) {
ascending: ascending,
}

for i := 0; i < pairs.length(); i++ {
fmt.Printf("%X %X\n", pairs.get(i).Key, pairs.get(i).Value)
if pairs.length() == 0 {
return
}

for i := 0; i < pairs.length(); i++ {
res.indexByKey.set(pairs.get(i).Key, i)
}
Expand All @@ -339,11 +319,13 @@ func newHeap(pairs cmnpairs, ascending bool) (res *heap) {
fmt.Printf("%X %d\n", []byte(k), v)
}
*/
fmt.Println("done")
return
}

func (parent *heap) cache() (res *heap) {
if parent == nil {
return nil
}
return &heap{
pairs: newCachePairs(parent.pairs),
// indexByKey is for updating pairs efficiently,
Expand All @@ -353,20 +335,6 @@ func (parent *heap) cache() (res *heap) {
}
}

func (heap *heap) visualize() {
if heap == nil {
return
}
fmt.Printf("len %d\n", heap.length())
for k, v := range heap.indexByKey {
fmt.Printf("%d -> %X\n", v, k)
}
if heap.length() == 0 {
return
}
heap.ptr(0).visualize(0)
}

func (heap *heap) ptr(i int) *hptr {
return &hptr{
heap: heap,
Expand All @@ -379,21 +347,12 @@ func (heap *heap) isEmpty() bool {
}

func (heap *heap) push(pair cmn.KVPair) {
fmt.Printf("push %X\n", pair.Key)
for k, v := range heap.indexByKey {
fmt.Printf("%X %d\n", k, v)
}
if index, ok := heap.indexByKey.get(pair.Key); ok {
ptr := heap.ptr(index)
if pair.Value == nil {
fmt.Printf("del %d %X %X\n", ptr.index, pair.Key, pair.Value)
ptr.del()
} else {
ptr.set(pair)
}
heap.ptr(index).set(pair)
return
}
heap.appendAssign(pair)
heap.indexByKey.set(pair.Key, heap.length()-1)
heap.ptr(heap.length() - 1).siftUp()
}

Expand Down
49 changes: 45 additions & 4 deletions store/cachekv/heap_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package cachekv

/*
import (
"bytes"
// "math/rand"
"math/rand"
"sort"
"testing"
"github.com/stretchr/testify/require"
// cmn "github.com/tendermint/tendermint/libs/common"
cmn "github.com/tendermint/tendermint/libs/common"
)
type ascpairs cmnpairs
Expand Down Expand Up @@ -60,7 +61,6 @@ func assertValidHeap(t *testing.T, it *hptr) {
}
}
/*
func testHeap(t *testing.T, size int, ascending bool, bzgen func(i int) []byte) {
pairs1 := make([]cmn.KVPair, 0, size)
pairs2 := make([]cmn.KVPair, 0, size)
Expand Down Expand Up @@ -105,4 +105,45 @@ func TestAscendingHeapRandomElements(t *testing.T) {
func TestDescendingHeapRandomElements(t *testing.T) {
testHeap(t, 100000, false, randgen)
}*/
}
func TestHeapDuplicateElements(t *testing.T) {
size := 1000
pairs1 := make([]cmn.KVPair, 0, size)
pairs2 := make([]cmn.KVPair, 0, size)
for i := 0; i < size; i++ {
bz := randgen(i)
pairs1 = append(pairs1, cmn.KVPair{Key: bz, Value: bz})
pairs2 = append(pairs2, cmn.KVPair{Key: bz, Value: bz})
}
heap := newHeap(cmnpairs(pairs1), true)
for _, pair := range pairs2 {
heap.push(pair)
}
require.Equal(t, size, heap.length())
}
func TestHeapDeleteElements(t *testing.T) {
size := 1000
pairs1 := make([]cmn.KVPair, 0, size)
pairs2 := make([]cmn.KVPair, 0, size)
for i := 0; i < size; i++ {
bz := randgen(i)
pairs1 = append(pairs1, cmn.KVPair{Key: bz, Value: bz})
pairs2 = append(pairs2, cmn.KVPair{Key: bz, Value: bz})
}
heap := newHeap(cmnpairs(pairs1), true)
for _, pair := range pairs2 {
heap.del(pair.Key)
}
require.Equal(t, 0, heap.length())
}
*/
1 change: 1 addition & 0 deletions store/cachekv/memiterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (mi *memIterator) Valid() bool {
if mi.items == nil {
return false
}

return !mi.items.isEmpty()
}

Expand Down
8 changes: 1 addition & 7 deletions store/cachekv/mergeiterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package cachekv

import (
"bytes"
"fmt"

"github.com/cosmos/cosmos-sdk/store/types"
)

// cacheMergeIterator merges a parent Iterator and a cache Iterator.
// The cache iterator may return nil keys to signal that an item
// The cache iterator may return nil values to signal that an item
// had been deleted (but not deleted in the parent).
// If the cache iterator has the same key as the parent, the
// cache shadows (overrides) the parent.
Expand Down Expand Up @@ -89,13 +88,11 @@ func (iter *cacheMergeIterator) Key() []byte {

// If parent is invalid, get the cache key.
if !iter.parent.Valid() {
fmt.Println("cac")
return iter.cache.Key()
}

// If cache is invalid, get the parent key.
if !iter.cache.Valid() {
fmt.Println("par")
return iter.parent.Key()
}

Expand All @@ -104,13 +101,10 @@ func (iter *cacheMergeIterator) Key() []byte {
cmp := iter.compare(keyP, keyC)
switch cmp {
case -1: // parent < cache
fmt.Println("cpa")
return keyP
case 0: // parent == cache
fmt.Println("cpa")
return keyP
case 1: // parent > cache
fmt.Println("cca")
return keyC
default:
panic("invalid compare result")
Expand Down
8 changes: 0 additions & 8 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ func (store *Store) Delete(key []byte) {
defer store.mtx.Unlock()
types.AssertValidKey(key)

store.items.heap.visualize()
store.setCacheValue(key, nil, true, true)
store.items.heap.visualize()
}

// Implements Cachetypes.KVStore.
Expand Down Expand Up @@ -164,19 +162,13 @@ func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator {

items := store.dirtyItems(start, end, ascending)

// fmt.Printf("s: %+v\n", store.items.heap.pairs)
cache = newMemIterator(start, end, items)
// fmt.Printf("c: %+v\n", items.pairs)

return newCacheMergeIterator(parent, cache, ascending)
}

// Constructs a slice of dirty items, to use w/ memIterator.
func (store *Store) dirtyItems(start, end []byte, ascending bool) (res *heap) {
if len(store.cache) == 0 {
return nil
}

if !store.items.inited {
store.items = items{
heap: newHeapFromCache(store.cache, ascending),
Expand Down
18 changes: 5 additions & 13 deletions store/cachekv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ func TestCacheKVStoreNested(t *testing.T) {
}

func TestCacheKVIteratorBounds(t *testing.T) {
fmt.Println("666666666")

st := newCacheKVStore()

// set some items
Expand Down Expand Up @@ -171,7 +169,6 @@ func TestCacheKVMergeIteratorBasics(t *testing.T) {

// remove it in cache and assert its not
st.Delete(k)
fmt.Println("///")
assertIterateDomain(t, st, 0)

// write the delete and assert its not there
Expand Down Expand Up @@ -202,35 +199,27 @@ func TestCacheKVMergeIteratorBasics(t *testing.T) {
}

func TestCacheKVMergeIteratorDeleteLast(t *testing.T) {
fmt.Println("a")
st := newCacheKVStore()

fmt.Println("b")
// set some items and write them
nItems := 5
for i := 0; i < nItems; i++ {
st.Set(keyFmt(i), valFmt(i))
}
fmt.Println("c")
st.Write()

fmt.Println("d")
// set some more items and leave dirty
for i := nItems; i < nItems*2; i++ {
st.Set(keyFmt(i), valFmt(i))
}

fmt.Println("e")
// iterate over all of them
assertIterateDomain(t, st, nItems*2)

fmt.Println("f")
// delete them all
for i := 0; i < nItems*2; i++ {
last := nItems*2 - 1 - i
fmt.Println("del1")
st.Delete(keyFmt(last))
fmt.Println("del2")
assertIterateDomain(t, st, last)
}
}
Expand All @@ -244,12 +233,14 @@ func TestCacheKVMergeIteratorDeletes(t *testing.T) {
for i := 0; i < nItems; i++ {
doOp(st, truth, opSet, i)
}

st.Write()

// delete every other item, starting from 0
for i := 0; i < nItems; i += 2 {
doOp(st, truth, opDel, i)
assertIterateDomainCompare(t, st, truth)

}

// reset
Expand Down Expand Up @@ -309,7 +300,7 @@ func TestCacheKVMergeIteratorRandom(t *testing.T) {
setRange(st, truth, start, end)

// do an op, test the iterator
for i := 0; i < 2000; i++ {
for i := 0; i < 10000; i++ {
doRandomOp(st, truth, max)
assertIterateDomainCompare(t, st, truth)
}
Expand Down Expand Up @@ -388,7 +379,6 @@ func assertIterateDomain(t *testing.T, st types.KVStore, expectedN int) {
var i = 0
for ; itr.Valid(); itr.Next() {
k, v := itr.Key(), itr.Value()
fmt.Printf("aid %X %X\n", k, v)
require.Equal(t, keyFmt(i), k)
require.Equal(t, valFmt(i), v)
i++
Expand Down Expand Up @@ -435,13 +425,15 @@ func assertIterateDomainCompare(t *testing.T, st types.KVStore, mem dbm.DB) {
}

func checkIterators(t *testing.T, itr, itr2 types.Iterator) {
i := 0
for ; itr.Valid(); itr.Next() {
require.True(t, itr2.Valid())
k, v := itr.Key(), itr.Value()
k2, v2 := itr2.Key(), itr2.Value()
require.Equal(t, k, k2)
require.Equal(t, v, v2)
itr2.Next()
i++
}
require.False(t, itr.Valid())
require.False(t, itr2.Valid())
Expand Down

0 comments on commit 106541f

Please sign in to comment.