-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #3058. Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
- Loading branch information
1 parent
64dac4c
commit 1c3f5fe
Showing
13 changed files
with
776 additions
and
62 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
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
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
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package object | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"math/big" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/nspcc-dev/neofs-sdk-go/client" | ||
) | ||
|
||
// SearchResultMerger merges SearchV2 results from several sources. Must not be | ||
// used concurrently. | ||
type SearchResultMerger struct { | ||
limit, n uint16 | ||
more bool | ||
buf []client.SearchResultItem | ||
iBuf, iNext *big.Int | ||
} | ||
|
||
// NewSearchResultMerger constructs SearchResultMerger merging up to limit | ||
// results. | ||
func NewSearchResultMerger(limit uint16) *SearchResultMerger { | ||
return &SearchResultMerger{limit: limit} | ||
} | ||
|
||
// Add accumulates the next result. | ||
func (x *SearchResultMerger) Add(withCursor bool, next []client.SearchResultItem) { | ||
if len(next) > int(x.limit) { | ||
panic(fmt.Sprintf("limit of items exceeded %d > %d", len(next), x.limit)) | ||
} | ||
if withCursor && len(next) < int(x.limit) { | ||
panic(fmt.Sprintf("cursor is set with fewer items %d < %d", len(next), x.limit)) | ||
} | ||
if len(next) == 0 { | ||
return | ||
} | ||
if x.buf == nil { | ||
x.buf = slices.Grow(next, int(x.limit))[:x.limit] | ||
x.n = uint16(len(next)) | ||
x.more = withCursor | ||
if len(next[0].Attributes) > 0 { | ||
x.iBuf, x.iNext = new(big.Int), new(big.Int) | ||
} | ||
return | ||
} | ||
buf := x.buf | ||
n := x.n | ||
next: | ||
for len(next) > 0 { | ||
for i := range n { | ||
if next[0].ID == buf[i].ID { | ||
if len(next) == 1 { | ||
x.more = x.more || withCursor | ||
return | ||
} | ||
buf, next = buf[i+1:], next[1:] | ||
n -= uint16(i) + 1 | ||
continue next | ||
} | ||
} | ||
var cmpInt bool | ||
if x.iBuf != nil { | ||
_, cmpInt = x.iNext.SetString(next[0].Attributes[0], 10) | ||
} | ||
var i uint16 | ||
for i = 0; i < n; i++ { //nolint:intrange // do not use range: if next[0] is the biggest, i=n is desired, following condition catches | ||
if x.iBuf != nil { | ||
if cmpInt { | ||
_, cmpInt = x.iBuf.SetString(buf[i].Attributes[0], 10) | ||
} | ||
if cmpInt { | ||
if c := x.iNext.Cmp(x.iBuf); c < 0 { | ||
break | ||
} else if c > 0 { | ||
continue | ||
} | ||
} else { | ||
if c := strings.Compare(next[0].Attributes[0], buf[i].Attributes[0]); c < 0 { | ||
break | ||
} else if c > 0 { | ||
continue | ||
} | ||
} | ||
} | ||
if c := bytes.Compare(next[0].ID[:], buf[i].ID[:]); c < 0 { // == 0 caught above | ||
break | ||
} | ||
} | ||
if i == uint16(len(buf)) { | ||
x.more = true | ||
return | ||
} | ||
if i == n { | ||
cpd := copy(buf[n:], next) | ||
x.n += uint16(cpd) | ||
x.more = withCursor || cpd < len(next) | ||
return | ||
} | ||
x.more = x.more || n == uint16(len(buf)) | ||
copy(buf[i+1:], buf[i:n]) | ||
buf[i] = next[0] | ||
x.n = min(x.n+1, x.limit) | ||
buf, next = buf[i+1:], next[1:] | ||
n -= i | ||
} | ||
} | ||
|
||
// Fin returns final result merged from the accumulated ones. | ||
func (x *SearchResultMerger) Fin() ([]client.SearchResultItem, bool) { return x.buf[:x.n], x.more } |
Oops, something went wrong.