forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_command.go
182 lines (152 loc) · 4.34 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
package ravendb
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"strconv"
"strings"
)
var (
_ RavenCommand = &BatchCommand{}
)
type BatchCommand struct {
RavenCommandBase
_conventions *DocumentConventions
_commands []ICommandData
_attachmentStreams []io.Reader
_options *BatchOptions
Result *JSONArrayResult
}
func NewBatchCommand(conventions *DocumentConventions, commands []ICommandData) (*BatchCommand, error) {
return NewBatchCommandWithOptions(conventions, commands, nil)
}
func NewBatchCommandWithOptions(conventions *DocumentConventions, commands []ICommandData, options *BatchOptions) (*BatchCommand, error) {
panicIf(conventions == nil, "conventions cannot be nil")
panicIf(len(commands) == 0, "commands cannot be empty")
cmd := &BatchCommand{
RavenCommandBase: NewRavenCommandBase(),
_commands: commands,
_options: options,
_conventions: conventions,
}
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.GetUrl() + "/databases/" + node.GetDatabase() + "/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,
}
js, err := json.Marshal(v)
if err != nil {
return nil, err
}
if len(c._attachmentStreams) == 0 {
return NewHttpPost(url, js)
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
writer.WriteField("main", string(js))
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")
part, err := writer.CreatePart(h)
if err != nil {
return nil, err
}
_, 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 NewIllegalStateException("Got null response from the server after doing a batch, something is very wrong. Probably a garbled response.")
}
return json.Unmarshal(response, &c.Result)
}
func (c *BatchCommand) appendOptions(sb string) string {
_options := c._options
if _options == nil {
return sb
}
sb += "?"
if _options.isWaitForReplicas() {
ts := durationToTimeSpan(_options.getWaitForReplicasTimeout())
sb += "&waitForReplicasTimeout=" + ts
if _options.isThrowOnTimeoutInWaitForReplicas() {
sb += "&throwOnTimeoutInWaitForReplicas=true"
}
sb += "&numberOfReplicasToWaitFor="
if _options.isMajority() {
sb += "majority"
} else {
sb += strconv.Itoa(_options.getNumberOfReplicasToWaitFor())
}
}
if _options.isWaitForIndexes() {
ts := durationToTimeSpan(_options.getWaitForIndexesTimeout())
sb += "&waitForIndexesTimeout=" + ts
if _options.isThrowOnTimeoutInWaitForIndexes() {
sb += "&waitForIndexThrow=true"
} else {
sb += "&waitForIndexThrow=false"
}
for _, specificIndex := range _options.getWaitForSpecificIndexes() {
sb += "&waitForSpecificIndex=" + specificIndex
}
}
return sb
}
func (c *BatchCommand) Close() {
// empty
}
// Note: in Java is in PutAttachmentCommandHelper.java
func throwStreamAlready() error {
return NewIllegalStateException("It is forbidden to re-use the same InputStream for more than one attachment. Use a unique InputStream per put attachment command.")
}