forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_get_command.go
157 lines (127 loc) · 3.45 KB
/
multi_get_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
package ravendb
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
var _ RavenCommand = &MultiGetCommand{}
// MultiGetCommand represents multi get command
type MultiGetCommand struct {
RavenCommandBase
cache *httpCache
commands []*getRequest
baseURL string
Result []*GetResponse // in Java we inherit from List<GetResponse>
}
func newMultiGetCommand(cache *httpCache, commands []*getRequest) *MultiGetCommand {
cmd := &MultiGetCommand{
RavenCommandBase: NewRavenCommandBase(),
cache: cache,
commands: commands,
}
cmd.ResponseType = RavenCommandResponseTypeRaw
return cmd
}
func (c *MultiGetCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
c.baseURL = node.URL + "/databases/" + node.Database
m := map[string]interface{}{}
var requests []map[string]interface{}
for _, command := range c.commands {
v := map[string]interface{}{}
cacheKey, _ := c.getCacheKey(command)
{
item, cachedChangeVector, _ := c.cache.get(cacheKey)
headers := map[string]string{}
if cachedChangeVector != nil {
headers[headersIfNoneMatch] = "\"" + *cachedChangeVector + "\""
}
for k, v := range command.headers {
headers[k] = v
}
v["Url"] = "/databases/" + node.Database + command.url
v["Query"] = command.query
if command.method == "" {
v["Method"] = nil
} else {
v["Method"] = command.method
}
v["Headers"] = headers
if command.content != nil {
v["Content"] = command.content.writeContent()
} else {
v["Content"] = nil
}
item.close()
}
requests = append(requests, v)
}
m["Requests"] = requests
d, err := jsonMarshal(m)
if err != nil {
return nil, err
}
uri := c.baseURL + "/multi_get"
return NewHttpPost(uri, d)
}
func (c *MultiGetCommand) getCacheKey(command *getRequest) (string, string) {
uri := c.baseURL + command.getUrlAndQuery()
key := command.method + "-" + uri
return key, uri
}
type getResponseJSON struct {
Result json.RawMessage `json:"Result"`
StatusCode int `json:"StatusCode"`
Headers map[string]string `json:"Headers"`
}
type resultsJSON struct {
Results []*getResponseJSON `json:"Results"`
}
func (c *MultiGetCommand) SetResponseRaw(response *http.Response, stream io.Reader) error {
var results *resultsJSON
d, err := ioutil.ReadAll(stream)
if err != nil {
return err
}
err = jsonUnmarshal(d, &results)
if err != nil {
return err
}
for i, rsp := range results.Results {
command := c.commands[i]
var getResponse GetResponse
getResponse.StatusCode = rsp.StatusCode
getResponse.Headers = rsp.Headers
getResponse.Result = rsp.Result
c.maybeSetCache(&getResponse, command)
c.maybeReadFromCache(&getResponse, command)
c.Result = append(c.Result, &getResponse)
}
return nil
}
func (c *MultiGetCommand) maybeReadFromCache(getResponse *GetResponse, command *getRequest) {
if getResponse.StatusCode != http.StatusNotModified {
return
}
cacheKey, _ := c.getCacheKey(command)
{
cacheItem, _, cachedResponse := c.cache.get(cacheKey)
getResponse.Result = cachedResponse
cacheItem.close()
}
}
func (c *MultiGetCommand) maybeSetCache(getResponse *GetResponse, command *getRequest) {
if getResponse.StatusCode == http.StatusNotModified {
return
}
cacheKey, _ := c.getCacheKey(command)
result := getResponse.Result
if len(result) == 0 {
return
}
changeVector := gttpExtensionsGetEtagHeaderFromMap(getResponse.Headers)
if changeVector == nil {
return
}
c.cache.set(cacheKey, changeVector, result)
}