2023-09-14 19:30:17 +00:00
|
|
|
package main
|
|
|
|
|
2023-09-14 21:25:52 +00:00
|
|
|
type search struct {
|
|
|
|
err error
|
|
|
|
results []*node
|
|
|
|
cursor int
|
|
|
|
values map[*node][]match
|
|
|
|
keys map[*node][]match
|
|
|
|
}
|
|
|
|
|
2023-09-15 07:16:36 +00:00
|
|
|
func newSearch() *search {
|
2023-09-14 21:25:52 +00:00
|
|
|
return &search{
|
|
|
|
results: make([]*node, 0),
|
|
|
|
values: make(map[*node][]match),
|
|
|
|
keys: make(map[*node][]match),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 19:30:17 +00:00
|
|
|
type match struct {
|
|
|
|
start, end int
|
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
2023-09-15 07:03:35 +00:00
|
|
|
type piece struct {
|
2023-09-14 19:45:51 +00:00
|
|
|
b []byte
|
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
2023-09-15 07:03:35 +00:00
|
|
|
func splitBytesByIndexes(b []byte, indexes []match) []piece {
|
|
|
|
out := make([]piece, 0, 1)
|
2023-09-14 19:30:17 +00:00
|
|
|
pos := 0
|
|
|
|
for _, pair := range indexes {
|
2023-09-15 07:03:35 +00:00
|
|
|
out = append(out, piece{safeSlice(b, pos, pair.start), -1})
|
|
|
|
out = append(out, piece{safeSlice(b, pair.start, pair.end), pair.index})
|
2023-09-14 19:45:51 +00:00
|
|
|
pos = pair.end
|
2023-09-14 19:30:17 +00:00
|
|
|
}
|
2023-09-15 07:03:35 +00:00
|
|
|
out = append(out, piece{safeSlice(b, pos, len(b)), -1})
|
2023-09-14 19:30:17 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2023-09-14 19:45:51 +00:00
|
|
|
func splitIndexesToChunks(chunks [][]byte, indexes [][]int, searchIndex int) (chunkIndexes [][]match) {
|
|
|
|
chunkIndexes = make([][]match, len(chunks))
|
2023-09-14 19:30:17 +00:00
|
|
|
|
2023-09-14 19:45:51 +00:00
|
|
|
for index, idx := range indexes {
|
2023-09-14 19:30:17 +00:00
|
|
|
position := 0
|
|
|
|
for i, chunk := range chunks {
|
|
|
|
// If start index lies in this chunk
|
|
|
|
if idx[0] < position+len(chunk) {
|
|
|
|
// Calculate local start and end for this chunk
|
|
|
|
localStart := idx[0] - position
|
|
|
|
localEnd := idx[1] - position
|
|
|
|
|
|
|
|
// If the end index also lies in this chunk
|
|
|
|
if idx[1] <= position+len(chunk) {
|
2023-09-14 19:45:51 +00:00
|
|
|
chunkIndexes[i] = append(chunkIndexes[i], match{start: localStart, end: localEnd, index: searchIndex + index})
|
2023-09-14 19:30:17 +00:00
|
|
|
break
|
|
|
|
} else {
|
|
|
|
// If the end index is outside this chunk, split the index
|
2023-09-14 19:45:51 +00:00
|
|
|
chunkIndexes[i] = append(chunkIndexes[i], match{start: localStart, end: len(chunk), index: searchIndex + index})
|
2023-09-14 19:30:17 +00:00
|
|
|
|
|
|
|
// Adjust the starting index for the next chunk
|
|
|
|
idx[0] = position + len(chunk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
position += len(chunk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|