Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate context to Search operation. #2117

Merged
merged 3 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions internal/core/algorithm/ngt/ngt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package ngt
import "C"

import (
"context"
"reflect"
"sync"
"unsafe"
Expand All @@ -40,7 +41,7 @@ type (
// NGT is core interface.
NGT interface {
// Search returns search result as []SearchResult
Search(vec []float32, size int, epsilon, radius float32) ([]SearchResult, error)
Search(ctx context.Context, vec []float32, size int, epsilon, radius float32) ([]SearchResult, error)

// Linear Search returns linear search result as []SearchResult
LinearSearch(vec []float32, size int) ([]SearchResult, error)
Expand Down Expand Up @@ -366,7 +367,7 @@ func (n *ngt) loadObjectSpace() error {
}

// Search returns search result as []SearchResult.
func (n *ngt) Search(vec []float32, size int, epsilon, radius float32) (result []SearchResult, err error) {
func (n *ngt) Search(ctx context.Context, vec []float32, size int, epsilon, radius float32) (result []SearchResult, err error) {
if len(vec) != int(n.dimension) {
return nil, errors.ErrIncompatibleDimensionSize(len(vec), int(n.dimension))
}
Expand Down Expand Up @@ -415,6 +416,12 @@ func (n *ngt) Search(vec []float32, size int, epsilon, radius float32) (result [
result = make([]SearchResult, rsize)

for i := range result {
select {
case <-ctx.Done():
n.PutErrorBuffer(ebuf)
return result[:i], nil
default:
}
d := C.ngt_get_result(results, C.uint32_t(i), ebuf)
if d.id == 0 && d.distance == 0 {
result[i] = SearchResult{0, 0, n.newGoError(ebuf)}
Expand Down
Loading
Loading