Commit bb1e9ab9 authored by vipwzw's avatar vipwzw Committed by 33cn

update chain33 2019.03.11

parent d9613eb5
...@@ -205,25 +205,17 @@ func ReportErrEventToFront(logger log.Logger, client queue.Client, frommodule st ...@@ -205,25 +205,17 @@ func ReportErrEventToFront(logger log.Logger, client queue.Client, frommodule st
//DelDupKey 删除重复的key //DelDupKey 删除重复的key
func DelDupKey(kvs []*types.KeyValue) []*types.KeyValue { func DelDupKey(kvs []*types.KeyValue) []*types.KeyValue {
dupindex := make(map[string]int) dupindex := make(map[string]int)
hasdup := false n := 0
for i, kv := range kvs { for _, kv := range kvs {
skey := string(kv.Key) skey := string(kv.Key)
if index, ok := dupindex[skey]; ok { if index, ok := dupindex[skey]; ok {
hasdup = true //重复的key 替换老的key
kvs[index] = nil
}
dupindex[skey] = i
}
//没有重复的情况下,不需要重新处理
if !hasdup {
return kvs
}
index := 0
for _, kv := range kvs {
if kv != nil {
kvs[index] = kv kvs[index] = kv
index++ } else {
dupindex[skey] = n
kvs[n] = kv
n++
} }
} }
return kvs[0:index] return kvs[0:n]
} }
...@@ -144,8 +144,8 @@ func TestDelDupKey(t *testing.T) { ...@@ -144,8 +144,8 @@ func TestDelDupKey(t *testing.T) {
{Key: []byte("hello"), Value: []byte("world2")}, {Key: []byte("hello"), Value: []byte("world2")},
} }
result := []*types.KeyValue{ result := []*types.KeyValue{
{Key: []byte("hello1"), Value: []byte("world")},
{Key: []byte("hello"), Value: []byte("world2")}, {Key: []byte("hello"), Value: []byte("world2")},
{Key: []byte("hello1"), Value: []byte("world")},
} }
kvs = DelDupKey(kvs) kvs = DelDupKey(kvs)
assert.Equal(t, kvs, result) assert.Equal(t, kvs, result)
...@@ -173,6 +173,7 @@ func BenchmarkDelDupKey(b *testing.B) { ...@@ -173,6 +173,7 @@ func BenchmarkDelDupKey(b *testing.B) {
kvs = append(kvs, &types.KeyValue{Key: key, Value: value}) kvs = append(kvs, &types.KeyValue{Key: key, Value: value})
} }
} }
b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
testkv := make([]*types.KeyValue, len(kvs)) testkv := make([]*types.KeyValue, len(kvs))
copy(testkv, kvs) copy(testkv, kvs)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment