-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresumable_query.go
68 lines (57 loc) · 1.88 KB
/
resumable_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package vector
const (
resumableQueryPath = "/resumable-query"
resumableQueryNexPath = "/resumable-query-next"
resumableQueryEndPath = "/resumable-query-end"
)
type ResumableQueryHandle struct {
index *Index
uuid string
}
// Next fetches the next page of the query result.
func (h *ResumableQueryHandle) Next(n ResumableQueryNext) (scores []VectorScore, err error) {
nn := resumableQueryNext{
ResumableQueryNext: n,
UUID: h.uuid,
}
data, err := h.index.sendJson(buildPath(resumableQueryNexPath, defaultNamespace), nn)
if err != nil {
return
}
scores, err = parseResponse[[]VectorScore](data)
return
}
// Close stops the resumable query and releases the acquired resources.
func (h *ResumableQueryHandle) Close() (err error) {
e := resumableQueryEnd{UUID: h.uuid}
data, err := h.index.sendJson(buildPath(resumableQueryEndPath, defaultNamespace), e)
if err != nil {
return
}
_, err = parseResponse[string](data)
return
}
// ResumableQuery starts a resumable query and returns the first page of the
// result of the query for the given vector in the default namespace.
// Then, next pages of the query results can be fetched over the returned handle.
// After all the needed pages of the results are fetched, it is recommended
// to close to handle to release the acquired resources.
func (ix *Index) ResumableQuery(q ResumableQuery) (scores []VectorScore, handle *ResumableQueryHandle, err error) {
return ix.resumableQueryInternal(q, defaultNamespace)
}
func (ix *Index) resumableQueryInternal(q ResumableQuery, ns string) (scores []VectorScore, handle *ResumableQueryHandle, err error) {
data, err := ix.sendJson(buildPath(resumableQueryPath, ns), q)
if err != nil {
return
}
start, err := parseResponse[resumableQueryStart](data)
if err != nil {
return
}
scores = start.Scores
handle = &ResumableQueryHandle{
index: ix,
uuid: start.UUID,
}
return
}