forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy_query_operation.go
77 lines (65 loc) · 1.99 KB
/
lazy_query_operation.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
69
70
71
72
73
74
75
76
77
package ravendb
var _ ILazyOperation = &LazyQueryOperation{}
// LazyQueryOperation describes server operation for lazy queries
type LazyQueryOperation struct {
_conventions *DocumentConventions
_queryOperation *queryOperation
_afterQueryExecuted []func(*QueryResult)
queryResult *QueryResult
requiresRetry bool
}
// newLazyQueryOperation returns new LazyQueryOperation
func newLazyQueryOperation(conventions *DocumentConventions, queryOperation *queryOperation, afterQueryExecuted []func(*QueryResult)) *LazyQueryOperation {
return &LazyQueryOperation{
_conventions: conventions,
_queryOperation: queryOperation,
_afterQueryExecuted: afterQueryExecuted,
}
}
// needed for ILazyOperation
func (o *LazyQueryOperation) createRequest() *getRequest {
return &getRequest{
url: "/queries",
method: "POST",
query: "?queryHash=" + o._queryOperation.indexQuery.GetQueryHash(),
content: NewIndexQueryContent(o._conventions, o._queryOperation.indexQuery),
}
}
// needed for ILazyOperation
func (o *LazyQueryOperation) getResult(result interface{}) error {
return o._queryOperation.complete(result)
}
// needed for ILazyOperation
func (o *LazyQueryOperation) getQueryResult() *QueryResult {
return o.queryResult
}
// needed for ILazyOperation
func (o *LazyQueryOperation) isRequiresRetry() bool {
return o.requiresRetry
}
// needed for ILazyOperation
func (o *LazyQueryOperation) handleResponse(response *GetResponse) error {
if response.IsForceRetry {
o.requiresRetry = true
return nil
}
var queryResult *QueryResult
if len(response.Result) != 0 {
err := jsonUnmarshal(response.Result, &queryResult)
if err != nil {
return err
}
}
return o.handleResponse2(queryResult)
}
func (o *LazyQueryOperation) handleResponse2(queryResult *QueryResult) error {
err := o._queryOperation.ensureIsAcceptableAndSaveResult(queryResult)
if err != nil {
return err
}
for _, e := range o._afterQueryExecuted {
e(queryResult)
}
o.queryResult = queryResult
return nil
}