-
Notifications
You must be signed in to change notification settings - Fork 16
/
batch_command.go
234 lines (191 loc) · 5.38 KB
/
batch_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package ravendb
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"strconv"
"strings"
)
var (
_ RavenCommand = &BatchCommand{}
)
// BatchCommand represents batch command
type BatchCommand struct {
RavenCommandBase
conventions *DocumentConventions
commands []ICommandData
attachmentStreams []io.Reader
options *BatchOptions
Result *JSONArrayResult
transactionMode int
disableAtomicDocumentWrites *bool
raftUniqueRequestId string
}
// newBatchCommand returns new BatchCommand
func newBatchCommand(conventions *DocumentConventions, commands []ICommandData, options *BatchOptions, transactionMode int, disableAtomicDocumentWrites *bool) (*BatchCommand, error) {
if conventions == nil {
return nil, newIllegalStateError("conventions cannot be nil")
}
if len(commands) == 0 {
return nil, newIllegalStateError("commands cannot be empty")
}
raftId, err := "", error(nil)
if transactionMode == TransactionMode_ClusterWide {
raftId, err = RaftId()
if err != nil {
return nil, err
}
}
cmd := &BatchCommand{
RavenCommandBase: NewRavenCommandBase(),
commands: commands,
options: options,
conventions: conventions,
transactionMode: transactionMode,
disableAtomicDocumentWrites: disableAtomicDocumentWrites,
raftUniqueRequestId: raftId,
}
for i := 0; i < len(commands); i++ {
command := commands[i]
if putAttachmentCommandData, ok := command.(*PutAttachmentCommandData); ok {
stream := putAttachmentCommandData.getStream()
for _, existingStream := range cmd.attachmentStreams {
if stream == existingStream {
return nil, throwStreamAlready()
}
}
cmd.attachmentStreams = append(cmd.attachmentStreams, stream)
}
}
return cmd, nil
}
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}
func (c *BatchCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
url := node.URL + "/databases/" + node.Database + "/bulk_docs"
url = c.appendOptions(url)
var a []interface{}
for _, cmd := range c.commands {
el, err := cmd.serialize(c.conventions)
if err != nil {
return nil, err
}
a = append(a, el)
}
v := map[string]interface{}{
"Commands": a,
}
if c.transactionMode == TransactionMode_ClusterWide {
v["TransactionMode"] = "ClusterWide"
}
js, err := jsonMarshal(v)
if err != nil {
return nil, err
}
if len(c.attachmentStreams) == 0 {
return NewHttpPost(url, js)
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
err = writer.WriteField("main", string(js))
if err != nil {
return nil, err
}
nameCounter := 1
for _, stream := range c.attachmentStreams {
name := "attachment" + strconv.Itoa(nameCounter)
nameCounter++
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"`, escapeQuotes(name)))
h.Set("Command-Type", "AttachmentStream")
// Note: Java seems to set those by default
h.Set("Content-Type", "application/octet-stream")
h.Set("Content-Transfer-Encoding", "binary")
part, err2 := writer.CreatePart(h)
if err2 != nil {
return nil, err2
}
_, err = io.Copy(part, stream)
if err != nil {
return nil, err
}
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := newHttpPostReader(url, body)
if err != nil {
return nil, err
}
contentType := writer.FormDataContentType()
req.Header.Set("Content-Type", contentType)
return req, nil
}
func (c *BatchCommand) SetResponse(response []byte, fromCache bool) error {
if len(response) == 0 {
return newIllegalStateError("Got null response from the server after doing a batch, something is very wrong. Probably a garbled response.")
}
return jsonUnmarshal(response, &c.Result)
}
func (c *BatchCommand) appendOptions(sb string) string {
_options := c.options
if _options == nil && c.transactionMode == TransactionMode_SingleNode {
return sb
}
sb += "?"
if c.transactionMode == TransactionMode_ClusterWide {
if c.disableAtomicDocumentWrites != nil {
sb += "&disableAtomicDocumentWrites="
if *c.disableAtomicDocumentWrites == false {
sb += "false"
} else {
sb += "true"
}
}
sb += "&raft-request-id=" + c.raftUniqueRequestId
if _options == nil {
return sb
}
}
if _options.waitForReplicas {
ts := durationToTimeSpan(_options.waitForReplicasTimeout)
sb += "&waitForReplicasTimeout=" + ts
if _options.throwOnTimeoutInWaitForReplicas {
sb += "&throwOnTimeoutInWaitForReplicas=true"
}
sb += "&numberOfReplicasToWaitFor="
if _options.majority {
sb += "majority"
} else {
sb += strconv.Itoa(_options.numberOfReplicasToWaitFor)
}
}
if _options.waitForIndexes {
ts := durationToTimeSpan(_options.waitForIndexesTimeout)
sb += "&waitForIndexesTimeout=" + ts
if _options.throwOnTimeoutInWaitForIndexes {
sb += "&waitForIndexThrow=true"
} else {
sb += "&waitForIndexThrow=false"
}
for _, specificIndex := range _options.waitForSpecificIndexes {
sb += "&waitForSpecificIndex=" + specificIndex
}
}
return sb
}
func (c *BatchCommand) Close() error {
// no-op
return nil
}
// Note: in Java is in PutAttachmentCommandHelper.java
func throwStreamAlready() error {
return newIllegalStateError("It is forbidden to re-use the same InputStream for more than one attachment. Use a unique InputStream per put attachment command.")
}