core: remove memstore routine from (*MemCachedStore).SeekAsync

It adds unnecessary overhead to computations.
This commit is contained in:
Anna Shaleva 2021-10-19 13:29:43 +03:00
parent dfe2c667e1
commit c88720bf45

View file

@ -102,7 +102,7 @@ func (s *MemCachedStore) Seek(key []byte, f func(k, v []byte)) {
// that key-value items are sorted by key in ascending way. // that key-value items are sorted by key in ascending way.
func (s *MemCachedStore) SeekAsync(ctx context.Context, key []byte, cutPrefix bool) chan KeyValue { func (s *MemCachedStore) SeekAsync(ctx context.Context, key []byte, cutPrefix bool) chan KeyValue {
// Create memory store `mem` and `del` snapshot not to hold the lock. // Create memory store `mem` and `del` snapshot not to hold the lock.
memRes := make([]KeyValueExists, 0) var memRes []KeyValueExists
sk := string(key) sk := string(key)
s.mut.RLock() s.mut.RLock()
for k, v := range s.MemoryStore.mem { for k, v := range s.MemoryStore.mem {
@ -110,7 +110,7 @@ func (s *MemCachedStore) SeekAsync(ctx context.Context, key []byte, cutPrefix bo
memRes = append(memRes, KeyValueExists{ memRes = append(memRes, KeyValueExists{
KeyValue: KeyValue{ KeyValue: KeyValue{
Key: []byte(k), Key: []byte(k),
Value: slice.Copy(v), Value: v,
}, },
Exists: true, Exists: true,
}) })
@ -133,23 +133,9 @@ func (s *MemCachedStore) SeekAsync(ctx context.Context, key []byte, cutPrefix bo
}) })
var ( var (
data1 = make(chan KeyValueExists)
data2 = make(chan KeyValue) data2 = make(chan KeyValue)
seekres = make(chan KeyValue) seekres = make(chan KeyValue)
) )
// Seek over memory store.
go func() {
loop:
for _, kv := range memRes {
select {
case <-ctx.Done():
break loop
default:
data1 <- kv
}
}
close(data1)
}()
// Seek over persistent store. // Seek over persistent store.
go func() { go func() {
@ -174,7 +160,16 @@ func (s *MemCachedStore) SeekAsync(ctx context.Context, key []byte, cutPrefix bo
// Merge results of seek operations in ascending order. // Merge results of seek operations in ascending order.
go func() { go func() {
kvMem, haveMem := <-data1 var (
kvMem KeyValueExists
haveMem bool
iMem int
)
if iMem < len(memRes) {
kvMem = memRes[iMem]
haveMem = true
iMem++
}
kvPs, havePs := <-data2 kvPs, havePs := <-data2
for { for {
if !haveMem && !havePs { if !haveMem && !havePs {
@ -191,7 +186,13 @@ func (s *MemCachedStore) SeekAsync(ctx context.Context, key []byte, cutPrefix bo
Value: kvMem.Value, Value: kvMem.Value,
} }
} }
kvMem, haveMem = <-data1 if iMem < len(memRes) {
kvMem = memRes[iMem]
haveMem = true
iMem++
} else {
haveMem = false
}
} else { } else {
if !bytes.Equal(kvMem.Key, kvPs.Key) { if !bytes.Equal(kvMem.Key, kvPs.Key) {
if cutPrefix { if cutPrefix {