forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk_insert_operation.go
354 lines (295 loc) · 9.07 KB
/
bulk_insert_operation.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package ravendb
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strconv"
"strings"
)
// Note: the implementation details are different from Java
// We take advantage of a pipe: a read end is passed as io.Reader
// to the request. A write end is what we use to write to the request.
var _ RavenCommand = &BulkInsertCommand{}
type BulkInsertCommand struct {
RavenCommandBase
_stream io.Reader
_id int
useCompression bool
Result *http.Response
}
func NewBulkInsertCommand(id int, stream io.Reader, useCompression bool) *BulkInsertCommand {
cmd := &BulkInsertCommand{
RavenCommandBase: NewRavenCommandBase(),
_stream: stream,
_id: id,
useCompression: useCompression,
}
return cmd
}
func (c *BulkInsertCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
url := node.GetUrl() + "/databases/" + node.GetDatabase() + "/bulk_insert?id=" + strconv.Itoa(c._id)
// TODO: implement compression. It must be attached to the writer
//message.setEntity(useCompression ? new GzipCompressingEntity(_stream) : _stream)
return NewHttpPostReader(url, c._stream)
}
func (c *BulkInsertCommand) SetResponse(response []byte, fromCache bool) error {
return NewNotImplementedException("Not implemented")
}
// TODO: port this. Currenlty send is not over-rideable
/*
CloseableHttpResponse send(CloseableHttpClient client, HttpRequestBase request) throws IOException {
try {
return super.send(client, request)
} catch (Exception e) {
_stream.errorOnRequestStart(e)
throw e
}
}
*/
type BulkInsertOperation struct {
_generateEntityIdOnTheClient *GenerateEntityIdOnTheClient
_requestExecutor *RequestExecutor
_bulkInsertExecuteTask *CompletableFuture
_reader *io.PipeReader
_currentWriter *io.PipeWriter
_first bool
_operationId int
useCompression bool
_concurrentCheck atomicInteger
_conventions *DocumentConventions
err error
Command *BulkInsertCommand
}
func NewBulkInsertOperation(database string, store *IDocumentStore) *BulkInsertOperation {
re := store.GetRequestExecutorWithDatabase(database)
f := func(entity Object) string {
return re.GetConventions().GenerateDocumentId(database, entity)
}
reader, writer := io.Pipe()
res := &BulkInsertOperation{
_conventions: store.GetConventions(),
_requestExecutor: re,
_generateEntityIdOnTheClient: NewGenerateEntityIdOnTheClient(re.GetConventions(), f),
_reader: reader,
_currentWriter: writer,
_operationId: -1,
_first: true,
}
return res
}
func (o *BulkInsertOperation) IsUseCompression() bool {
return o.useCompression
}
func (o *BulkInsertOperation) SetUseCompression(useCompression bool) {
o.useCompression = useCompression
}
func (o *BulkInsertOperation) throwBulkInsertAborted(e error, flushEx error) error {
err := error(o.getExceptionFromOperation())
if err == nil {
err = e
}
if err == nil {
err = flushEx
}
return NewBulkInsertAbortedException("Failed to execute bulk insert, error: %s", err)
}
func (o *BulkInsertOperation) getExceptionFromOperation() *BulkInsertAbortedException {
stateRequest := NewGetOperationStateCommand(o._requestExecutor.GetConventions(), o._operationId)
err := o._requestExecutor.ExecuteCommand(stateRequest)
if err != nil {
return nil // TODO: return an error?
}
if result, ok := stateRequest.Result["Result"]; ok {
if result, ok := result.(ObjectNode); ok {
typ, _ := jsonGetAsString(result, "$type")
if strings.HasPrefix(typ, "Raven.Client.Documents.Operations.OperationExceptionResult") {
errStr, _ := jsonGetAsString(result, "Error")
return NewBulkInsertAbortedException(errStr)
}
}
}
return nil
}
func (o *BulkInsertOperation) WaitForId() error {
if o._operationId != -1 {
return nil
}
bulkInsertGetIdRequest := NewGetNextOperationIdCommand()
o.err = o._requestExecutor.ExecuteCommand(bulkInsertGetIdRequest)
if o.err != nil {
return o.err
}
o._operationId = bulkInsertGetIdRequest.Result
return nil
}
func (o *BulkInsertOperation) StoreWithID(entity Object, id string, metadata *IMetadataDictionary) error {
if !o._concurrentCheck.compareAndSet(0, 1) {
return NewIllegalStateException("Bulk Insert Store methods cannot be executed concurrently.")
}
defer o._concurrentCheck.set(0)
// early exit if we failed previously
if o.err != nil {
return o.err
}
err := BulkInsertOperation_verifyValidId(id)
if err != nil {
return err
}
o.err = o.WaitForId()
if o.err != nil {
return o.err
}
o.err = o.ensureCommand()
if o.err != nil {
return o.err
}
if o._bulkInsertExecuteTask.IsCompletedExceptionally() {
_, err := o._bulkInsertExecuteTask.Get()
panicIf(err == nil, "err should not be nil")
return o.throwBulkInsertAborted(err, nil)
}
if metadata == nil {
metadata = &MetadataAsDictionary{}
}
if !metadata.ContainsKey(Constants_Documents_Metadata_COLLECTION) {
collection := o._requestExecutor.GetConventions().GetCollectionName(entity)
if collection != "" {
metadata.Put(Constants_Documents_Metadata_COLLECTION, collection)
}
}
if !metadata.ContainsKey(Constants_Documents_Metadata_RAVEN_GO_TYPE) {
goType := o._requestExecutor.GetConventions().GetGoTypeName(entity)
if goType != "" {
metadata.Put(Constants_Documents_Metadata_RAVEN_GO_TYPE, goType)
}
}
documentInfo := NewDocumentInfo()
documentInfo.metadataInstance = metadata
jsNode := EntityToJson_convertEntityToJson(entity, documentInfo)
var b bytes.Buffer
if o._first {
b.WriteByte('[')
o._first = false
} else {
b.WriteByte(',')
}
m := map[string]interface{}{}
m["Id"] = id
m["Type"] = "PUT"
m["Document"] = jsNode
d, err := json.Marshal(m)
if err != nil {
return err
}
b.Write(d)
_, o.err = o._currentWriter.Write(b.Bytes())
if o.err != nil {
err = o.getExceptionFromOperation()
if err != nil {
o.err = err
return o.err
}
// TODO:
//o.err = o.throwOnUnavailableStream()
return o.err
}
return o.err
}
func (o *BulkInsertOperation) ensureCommand() error {
if o.Command != nil {
return nil
}
bulkCommand := NewBulkInsertCommand(o._operationId, o._reader, o.useCompression)
panicIf(o._bulkInsertExecuteTask != nil, "already started _bulkInsertExecuteTask")
o._bulkInsertExecuteTask = NewCompletableFuture()
go func() {
err := o._requestExecutor.ExecuteCommand(bulkCommand)
if err != nil {
o._bulkInsertExecuteTask.CompleteExceptionally(err)
} else {
o._bulkInsertExecuteTask.Complete(nil)
}
}()
o.Command = bulkCommand
return nil
}
func (o *BulkInsertOperation) Abort() error {
if o._operationId == -1 {
return nil // nothing was done, nothing to kill
}
err := o.WaitForId()
if err != nil {
return err
}
command := NewKillOperationCommand(strconv.Itoa(o._operationId))
err = o._requestExecutor.ExecuteCommand(command)
//o._currentWriter.Close()
if err != nil {
return NewBulkInsertAbortedException("%s", "Unable to kill ths bulk insert operation, because it was not found on the server.")
}
o._currentWriter.CloseWithError(NewBulkInsertAbortedException("killed operation"))
return nil
}
func (o *BulkInsertOperation) Close() error {
if o._operationId == -1 {
// closing without calling a single Store.
return nil
}
d := []byte{']'}
_, err := o._currentWriter.Write(d)
errClose := o._currentWriter.Close()
if o._bulkInsertExecuteTask != nil {
_, err2 := o._bulkInsertExecuteTask.Get()
if err2 != nil && err == nil {
err = o.throwBulkInsertAborted(err, errClose)
}
}
if err != nil {
o.err = err
return err
}
return nil
}
func (o *BulkInsertOperation) Store(entity Object) (string, error) {
return o.StoreWithMetadata(entity, nil)
}
func (o *BulkInsertOperation) StoreWithMetadata(entity Object, metadata *IMetadataDictionary) (string, error) {
var id string
if metadata == nil || !metadata.ContainsKey(Constants_Documents_Metadata_ID) {
id = o.GetId(entity)
} else {
idVal, ok := metadata.Get(Constants_Documents_Metadata_ID)
panicIf(!ok, "didn't find %s key in meatadata", Constants_Documents_Metadata_ID)
id = idVal.(string)
}
return id, o.StoreWithID(entity, id, metadata)
}
func (o *BulkInsertOperation) GetId(entity Object) string {
idRef, ok := o._generateEntityIdOnTheClient.tryGetIdFromInstance(entity)
if ok {
return idRef
}
idRef = o._generateEntityIdOnTheClient.generateDocumentKeyForStorage(entity)
// set id property if it was null
o._generateEntityIdOnTheClient.trySetIdentity(entity, idRef)
return idRef
}
func (o *BulkInsertOperation) throwOnUnavailableStream(id string, innerEx error) error {
// TODO: not sure how this translates
//_streamExposerContent.errorOnProcessingRequest(new BulkInsertAbortedException("Write to stream failed at document with id " + id, innerEx))
_, err := o._bulkInsertExecuteTask.Get()
if err != nil {
return ExceptionsUtils_unwrapException(err)
}
return nil
}
func BulkInsertOperation_verifyValidId(id string) error {
if stringIsEmpty(id) {
return NewIllegalStateException("Document id must have a non empty value")
}
if strings.HasSuffix(id, "|") {
return NewUnsupportedOperationException("Document ids cannot end with '|', but was called with %s", id)
}
return nil
}