core: squash PS-seeking and merging routines in (*MemCachedStore).Seek

We don't need a separate routine to merge seek results.
This commit is contained in:
Anna Shaleva 2021-10-19 15:10:12 +03:00
parent c88720bf45
commit dcda7bec63

View file

@ -132,75 +132,87 @@ func (s *MemCachedStore) SeekAsync(ctx context.Context, key []byte, cutPrefix bo
return bytes.Compare(memRes[i].Key, memRes[j].Key) < 0 return bytes.Compare(memRes[i].Key, memRes[j].Key) < 0
}) })
var ( var seekres = make(chan KeyValue)
data2 = make(chan KeyValue)
seekres = make(chan KeyValue)
)
// Seek over persistent store. // Seek over persistent store.
go func() {
var done bool
ps.Seek(key, func(k, v []byte) {
if done {
return
}
select {
case <-ctx.Done():
done = true
default:
// Must copy here, #1468.
data2 <- KeyValue{
Key: slice.Copy(k),
Value: slice.Copy(v),
}
}
})
close(data2)
}()
// Merge results of seek operations in ascending order.
go func() { go func() {
var ( var (
done bool
iMem int
kvMem KeyValueExists kvMem KeyValueExists
haveMem bool haveMem bool
iMem int
) )
if iMem < len(memRes) { if iMem < len(memRes) {
kvMem = memRes[iMem] kvMem = memRes[iMem]
haveMem = true haveMem = true
iMem++ iMem++
} }
kvPs, havePs := <-data2
for { // Merge results of seek operations in ascending order.
if !haveMem && !havePs { ps.Seek(key, func(k, v []byte) {
break if done {
return
} }
var isMem = haveMem && (!havePs || (bytes.Compare(kvMem.Key, kvPs.Key) < 0)) kvPs := KeyValue{
if isMem { Key: slice.Copy(k),
if kvMem.Exists { Value: slice.Copy(v),
if cutPrefix { }
kvMem.Key = kvMem.Key[len(key):] loop:
} for {
seekres <- KeyValue{ select {
Key: kvMem.Key, case <-ctx.Done():
Value: kvMem.Value, done = true
break loop
default:
var isMem = haveMem && (bytes.Compare(kvMem.Key, kvPs.Key) < 0)
if isMem {
if kvMem.Exists {
if cutPrefix {
kvMem.Key = kvMem.Key[len(key):]
}
seekres <- KeyValue{
Key: kvMem.Key,
Value: kvMem.Value,
}
}
if iMem < len(memRes) {
kvMem = memRes[iMem]
haveMem = true
iMem++
} else {
haveMem = false
}
} else {
if !bytes.Equal(kvMem.Key, kvPs.Key) {
if cutPrefix {
kvPs.Key = kvPs.Key[len(key):]
}
seekres <- kvPs
}
break loop
} }
} }
if iMem < len(memRes) { }
kvMem = memRes[iMem]
haveMem = true })
iMem++ if !done && haveMem {
} else { loop:
haveMem = false for i := iMem - 1; i < len(memRes); i++ {
} select {
} else { case <-ctx.Done():
if !bytes.Equal(kvMem.Key, kvPs.Key) { break loop
if cutPrefix { default:
kvPs.Key = kvPs.Key[len(key):] kvMem = memRes[i]
if kvMem.Exists {
if cutPrefix {
kvMem.Key = kvMem.Key[len(key):]
}
seekres <- KeyValue{
Key: kvMem.Key,
Value: kvMem.Value,
}
} }
seekres <- kvPs
} }
kvPs, havePs = <-data2
} }
} }
close(seekres) close(seekres)