forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_operation.go
167 lines (139 loc) · 3.87 KB
/
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package ravendb
import (
"fmt"
"reflect"
"strings"
)
// LoadOperation represents a load operation
type LoadOperation struct {
session *InMemoryDocumentSessionOperations
ids []string
includes []string
idsToCheckOnServer []string
}
func NewLoadOperation(session *InMemoryDocumentSessionOperations) *LoadOperation {
return &LoadOperation{
session: session,
}
}
func (o *LoadOperation) createRequest() (*GetDocumentsCommand, error) {
if len(o.idsToCheckOnServer) == 0 {
return nil, nil
}
if o.session.checkIfIdAlreadyIncluded(o.ids, o.includes) {
return nil, nil
}
if err := o.session.incrementRequestCount(); err != nil {
return nil, err
}
return NewGetDocumentsCommand(o.idsToCheckOnServer, o.includes, false)
}
func (o *LoadOperation) byID(id string) *LoadOperation {
if id == "" {
return o
}
if o.ids == nil {
o.ids = []string{id}
}
if o.session.IsLoadedOrDeleted(id) {
return o
}
o.idsToCheckOnServer = append(o.idsToCheckOnServer, id)
return o
}
func (o *LoadOperation) withIncludes(includes []string) *LoadOperation {
o.includes = includes
return o
}
func (o *LoadOperation) byIds(ids []string) *LoadOperation {
o.ids = stringArrayCopy(ids)
seen := map[string]struct{}{}
for _, id := range ids {
if id == "" {
continue
}
idl := strings.ToLower(id)
if _, ok := seen[idl]; ok {
continue
}
seen[idl] = struct{}{}
o.byID(id)
}
return o
}
func (o *LoadOperation) getDocument(result interface{}) error {
return o.getDocumentWithID(result, o.ids[0])
}
func (o *LoadOperation) getDocumentWithID(result interface{}, id string) error {
if id == "" {
// TODO: should return default value?
//return ErrNotFound
return nil
}
if o.session.IsDeleted(id) {
// TODO: return ErrDeleted?
//return ErrNotFound
return nil
}
doc := o.session.documentsByID.getValue(id)
if doc == nil {
doc = o.session.includedDocumentsByID[id]
}
if doc == nil {
//return ErrNotFound
return nil
}
return o.session.TrackEntityInDocumentInfo(result, doc)
}
var stringType = reflect.TypeOf("")
// TODO: also handle a pointer to a map?
func (o *LoadOperation) getDocuments(results interface{}) error {
// results must be map[string]*struct
//fmt.Printf("LoadOperation.getDocuments: results type: %T\n", results)
m := reflect.ValueOf(results)
if m.Type().Kind() != reflect.Map {
return fmt.Errorf("results should be a map[string]*struct, is %s. tp: %s", m.Type().String(), m.Type().String())
}
mapKeyType := m.Type().Key()
if mapKeyType != stringType {
return fmt.Errorf("results should be a map[string]*struct, is %s. tp: %s", m.Type().String(), m.Type().String())
}
mapElemPtrType := m.Type().Elem()
if mapElemPtrType.Kind() != reflect.Ptr {
return fmt.Errorf("results should be a map[string]*struct, is %s. tp: %s", m.Type().String(), m.Type().String())
}
mapElemType := mapElemPtrType.Elem()
if mapElemType.Kind() != reflect.Struct {
return fmt.Errorf("results should be a map[string]*struct, is %s. tp: %s", m.Type().String(), m.Type().String())
}
uniqueIds := stringArrayCopy(o.ids)
stringArrayRemove(&uniqueIds, "")
uniqueIds = stringArrayRemoveDuplicatesNoCase(uniqueIds)
for _, id := range uniqueIds {
v := reflect.New(mapElemPtrType).Interface()
err := o.getDocumentWithID(v, id)
if err != nil {
return err
}
key := reflect.ValueOf(id)
v2 := reflect.ValueOf(v).Elem() // convert *<type> to <type>
m.SetMapIndex(key, v2)
}
return nil
}
func (o *LoadOperation) setResult(result *GetDocumentsResult) {
if result == nil {
return
}
o.session.registerIncludes(result.Includes)
results := result.Results
for _, document := range results {
// TODO: Java also does document.isNull()
if document == nil {
continue
}
newDocumentInfo := getNewDocumentInfo(document)
o.session.documentsByID.add(newDocumentInfo)
}
o.session.registerMissingIncludes(result.Results, result.Includes, o.includes)
}