forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy_load_operation.go
140 lines (117 loc) · 3.04 KB
/
lazy_load_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package ravendb
var _ ILazyOperation = &LazyLoadOperation{}
// LazyLoadOperation represents lazy load operation
type LazyLoadOperation struct {
_session *InMemoryDocumentSessionOperations
_loadOperation *LoadOperation
_ids []string
_includes []string
hasItems bool
queryResult *QueryResult
//result interface{}
requiresRetry bool
}
// NewLazyLoadOperation returns new LazyLoadOperation
func newLazyLoadOperation(session *InMemoryDocumentSessionOperations, loadOperation *LoadOperation) *LazyLoadOperation {
return &LazyLoadOperation{
_session: session,
_loadOperation: loadOperation,
}
}
// TODO: should return an error
// needed for ILazyOperation
func (o *LazyLoadOperation) createRequest() *getRequest {
var idsToCheckOnServer []string
for _, id := range o._ids {
if !o._session.IsLoadedOrDeleted(id) {
idsToCheckOnServer = append(idsToCheckOnServer, id)
}
}
queryBuilder := "?"
if o._includes != nil {
for _, include := range o._includes {
queryBuilder += "&include="
queryBuilder += include
}
}
for _, id := range idsToCheckOnServer {
queryBuilder += "&id="
queryBuilder += urlUtilsEscapeDataString(id)
}
o.hasItems = len(idsToCheckOnServer) == 0
if o.hasItems {
// no need to hit the server
return nil
}
getRequest := &getRequest{
url: "/docs",
query: queryBuilder,
}
return getRequest
}
func (o *LazyLoadOperation) byID(id string) *LazyLoadOperation {
if id == "" {
return o
}
if o._ids == nil {
o._ids = []string{id}
}
return o
}
func (o *LazyLoadOperation) byIds(ids []string) *LazyLoadOperation {
o._ids = ids
return o
}
func (o *LazyLoadOperation) withIncludes(includes []string) *LazyLoadOperation {
o._includes = includes
return o
}
// needed for ILazyOperation
func (o *LazyLoadOperation) getResult(result interface{}) error {
if o.hasItems {
err := o._loadOperation.getDocuments(result)
if err != nil && len(o._ids) == 1 {
err = o._loadOperation.getDocument(result)
}
return err
}
if o.requiresRetry {
return nil
}
err := o._loadOperation.getDocuments(result)
// TODO: a better way to distinguish between a Load() and LoadMulti() operation
if err != nil && len(o._ids) == 1 {
err = o._loadOperation.getDocument(result)
}
return err
}
// needed for ILazyOperation
func (o *LazyLoadOperation) getQueryResult() *QueryResult {
return o.queryResult
}
// needed for ILazyOperation
func (o *LazyLoadOperation) isRequiresRetry() bool {
return o.requiresRetry
}
// needed for ILazyOperation
func (o *LazyLoadOperation) handleResponse(response *GetResponse) error {
if response.IsForceRetry {
o.requiresRetry = true
return nil
}
res := response.Result
if len(res) == 0 {
return o.handleResponse2(nil)
}
var multiLoadResult *GetDocumentsResult
err := jsonUnmarshal(res, &multiLoadResult)
if err != nil {
return err
}
return o.handleResponse2(multiLoadResult)
}
func (o *LazyLoadOperation) handleResponse2(loadResult *GetDocumentsResult) error {
o._loadOperation.setResult(loadResult)
// Note: rest delayed to getResult()
return nil
}