forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraven_command.go
196 lines (158 loc) · 5.43 KB
/
raven_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package ravendb
import (
"io"
"io/ioutil"
"net/http"
)
var (
_ RavenCommand = &RavenCommandBase{}
)
// RavenCommand defines interface for server commands
type RavenCommand interface {
// those are meant to be over-written
CreateRequest(node *ServerNode) (*http.Request, error)
SetResponse(response []byte, fromCache bool) error
SetResponseRaw(response *http.Response, body io.Reader) error
Send(client *http.Client, req *http.Request) (*http.Response, error)
// for all other functions, get access to underlying RavenCommandBase
GetBase() *RavenCommandBase
}
type RavenCommandBase struct {
StatusCode int
ResponseType RavenCommandResponseType
CanCache bool
CanCacheAggressively bool
// if true, can be cached
IsReadRequest bool
FailedNodes map[*ServerNode]error
}
func NewRavenCommandBase() RavenCommandBase {
res := RavenCommandBase{
ResponseType: RavenCommandResponseTypeObject,
CanCache: true,
CanCacheAggressively: true,
}
return res
}
func (c *RavenCommandBase) GetBase() *RavenCommandBase {
return c
}
func (c *RavenCommandBase) SetResponse(response []byte, fromCache bool) error {
if c.ResponseType == RavenCommandResponseTypeEmpty || c.ResponseType == RavenCommandResponseTypeRaw {
return throwInvalidResponse()
}
return newUnsupportedOperationError(c.ResponseType + " command must override the SetResponse method which expects response with the following type: " + c.ResponseType)
}
func (c *RavenCommandBase) SetResponseRaw(response *http.Response, stream io.Reader) error {
panicIf(true, "When "+c.ResponseType+" is set to Raw then please override this method to handle the response. ")
return nil
}
func (c *RavenCommandBase) CreateRequest(node *ServerNode) (*http.Request, error) {
panicIf(true, "CreateRequest must be over-written by all types")
return nil, nil
}
func throwInvalidResponse() error {
return newIllegalStateError("Invalid response")
}
func (c *RavenCommandBase) Send(client *http.Client, req *http.Request) (*http.Response, error) {
rsp, err := client.Do(req)
return rsp, err
}
func (c *RavenCommandBase) urlEncode(value string) string {
return urlEncode(value)
}
func ensureIsNotNullOrString(value string, name string) error {
if value == "" {
return newIllegalArgumentError("%s cannot be null or empty", name)
}
return nil
}
// Note: unused
func (c *RavenCommandBase) isFailedWithNode(node *ServerNode) bool {
if c.FailedNodes == nil {
return false
}
_, ok := c.FailedNodes[node]
return ok
}
// Note: in Java Raven.processResponse is virtual.
// That's impossible in Go, so we replace with stand-alone function that dispatches based on type
func ravenCommand_processResponse(cmd RavenCommand, cache *httpCache, response *http.Response, url string) (responseDisposeHandling, error) {
if cmdHead, ok := cmd.(*HeadDocumentCommand); ok {
return cmdHead.ProcessResponse(cache, response, url)
}
if cmdHead, ok := cmd.(*HeadAttachmentCommand); ok {
return cmdHead.processResponse(cache, response, url)
}
if cmdGet, ok := cmd.(*GetAttachmentCommand); ok {
return cmdGet.processResponse(cache, response, url)
}
if cmdQuery, ok := cmd.(*QueryStreamCommand); ok {
return cmdQuery.processResponse(cache, response, url)
}
if cmdStream, ok := cmd.(*StreamCommand); ok {
return cmdStream.processResponse(cache, response, url)
}
c := cmd.GetBase()
if response.Body == nil {
return responseDisposeHandlingAutomatic, nil
}
statusCode := response.StatusCode
if c.ResponseType == RavenCommandResponseTypeEmpty || statusCode == http.StatusNoContent {
return responseDisposeHandlingAutomatic, nil
}
if c.ResponseType == RavenCommandResponseTypeObject {
contentLength := response.ContentLength
if contentLength == 0 {
return responseDisposeHandlingAutomatic, nil
}
// we intentionally don't dispose the reader here, we'll be using it
// in the command, any associated memory will be released on context reset
js, err := ioutil.ReadAll(response.Body)
if err != nil {
return responseDisposeHandlingAutomatic, err
}
if cache != nil {
c.cacheResponse(cache, url, response, js)
}
err = cmd.SetResponse(js, false)
return responseDisposeHandlingAutomatic, err
}
err := cmd.SetResponseRaw(response, response.Body)
return responseDisposeHandlingAutomatic, err
}
func (c *RavenCommandBase) cacheResponse(cache *httpCache, url string, response *http.Response, responseJson []byte) {
if !c.CanCache {
return
}
changeVector := gttpExtensionsGetEtagHeader(response)
if changeVector == nil {
return
}
cache.set(url, changeVector, responseJson)
}
// Note: unused
func (c *RavenCommandBase) addChangeVectorIfNotNull(changeVector *string, request *http.Request) {
if changeVector != nil {
request.Header.Add("If-Match", `"`+*changeVector+`"`)
}
}
func (c *RavenCommandBase) onResponseFailure(response *http.Response) {
// Note: it looks like it's meant to be virtual but there are no
// over-rides in Java code
}
// Note: hackish solution due to lack of generics
// Returns OperationIDReuslt for commands that have it as a result
// When new command returning OperationIDResult are added, we must extend it
func getCommandOperationIDResult(cmd RavenCommand) *OperationIDResult {
switch c := cmd.(type) {
case *CompactDatabaseCommand:
return c.Result
case *PatchByQueryCommand:
return c.Result
case *DeleteByIndexCommand:
return c.Result
}
panicIf(true, "called on a command %T that doesn't return OperationIDResult", cmd)
return nil
}