forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_documents_command.go
166 lines (134 loc) · 3.47 KB
/
get_documents_command.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
package ravendb
import (
"net/http"
"strconv"
)
var (
_ RavenCommand = &GetDocumentsCommand{}
)
type GetDocumentsCommand struct {
RavenCommandBase
_id string
_ids []string
_includes []string
_metadataOnly bool
_startWith string
_matches string
_start int
_pageSize int
_exclude string
_startAfter string
Result *GetDocumentsResult
}
func NewGetDocumentsCommand(ids []string, includes []string, metadataOnly bool) (*GetDocumentsCommand, error) {
if len(ids) == 0 {
return nil, newIllegalArgumentError("Please supply at least one id")
}
cmd := &GetDocumentsCommand{
RavenCommandBase: NewRavenCommandBase(),
_includes: includes,
_metadataOnly: metadataOnly,
_start: -1,
_pageSize: -1,
}
if len(ids) == 1 {
cmd._id = ids[0]
} else {
cmd._ids = ids
}
cmd.IsReadRequest = true
return cmd, nil
}
func NewGetDocumentsCommandFull(startWith string, startAfter string, matches string, exclude string, start int, pageSize int, metadataOnly bool) (*GetDocumentsCommand, error) {
if startWith == "" {
return nil, newIllegalArgumentError("startWith cannot be null")
}
return &GetDocumentsCommand{
RavenCommandBase: NewRavenCommandBase(),
_startWith: startWith,
_startAfter: startAfter,
_matches: matches,
_exclude: exclude,
_start: start,
_pageSize: pageSize,
_metadataOnly: metadataOnly,
}, nil
}
func (c *GetDocumentsCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
url := node.URL + "/databases/" + node.Database + "/docs?"
if c._start > 0 {
url += "&start=" + strconv.Itoa(c._start)
}
if c._pageSize > 0 {
url += "&pageSize=" + strconv.Itoa(c._pageSize)
}
if c._metadataOnly {
url += "&metadataOnly=true"
}
if c._startWith != "" {
url += "&startsWith="
url += urlUtilsEscapeDataString(c._startWith)
if c._matches != "" {
url += "&matches="
url += c._matches
}
if c._exclude != "" {
url += "&exclude="
url += c._exclude
}
if c._startAfter != "" {
url += "&startAfter="
url += c._startAfter
}
}
for _, include := range c._includes {
url += "&include="
url += include
}
if c._id != "" {
url += "&id="
url += urlUtilsEscapeDataString(c._id)
} else if len(c._ids) > 0 {
return c.prepareRequestWithMultipleIds(url)
}
return newHttpGet(url)
}
func (c *GetDocumentsCommand) prepareRequestWithMultipleIds(url string) (*http.Request, error) {
uniqueIds := stringArrayCopy(c._ids)
uniqueIds = stringArrayRemoveDuplicatesNoCase(uniqueIds)
totalLen := 0
for _, s := range uniqueIds {
totalLen += len(s)
}
// if it is too big, we drop to POST (note that means that we can't use the HTTP cache any longer)
// we are fine with that, requests to load > 1024 items are going to be rare
isGet := totalLen < 1024
if isGet {
for _, s := range uniqueIds {
url += "&id=" + urlUtilsEscapeDataString(s)
}
return newHttpGet(url)
}
calculateHash := c.calculateHash(uniqueIds)
url += "&loadHash="
url += calculateHash
m := map[string]interface{}{
"Ids": uniqueIds,
}
d, err := jsonMarshal(m)
panicIf(err != nil, "jsonMarshal() failed with %s", err)
return NewHttpPost(url, d)
}
func (c *GetDocumentsCommand) calculateHash(uniqueIds []string) string {
hasher := &HashCalculator{}
for _, x := range uniqueIds {
hasher.write(x)
}
return hasher.getHash()
}
func (c *GetDocumentsCommand) SetResponse(response []byte, fromCache bool) error {
if len(response) == 0 {
return nil
}
return jsonUnmarshal(response, &c.Result)
}