forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 9
/
bulkapi.go
149 lines (125 loc) · 3.07 KB
/
bulkapi.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
package elasticsearch
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"strings"
)
// MetaBuilder creates meta data for bulk requests
type MetaBuilder func(interface{}) interface{}
type bulkRequest struct {
requ *http.Request
}
type bulkResult struct {
raw []byte
}
// Bulk performs many index/delete operations in a single API call.
// Implements: http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
func (conn *Connection) Bulk(
index, docType string,
params map[string]string, body []interface{},
) (*QueryResult, error) {
return conn.BulkWith(index, docType, params, nil, body)
}
// BulkWith creates a HTTP request containing a bunch of operations and send
// them to Elasticsearch. The request is retransmitted up to max_retries before
// returning an error.
func (conn *Connection) BulkWith(
index string,
docType string,
params map[string]string,
metaBuilder MetaBuilder,
body []interface{},
) (*QueryResult, error) {
if len(body) == 0 {
return nil, nil
}
enc := conn.encoder
enc.Reset()
if err := bulkEncode(enc, metaBuilder, body); err != nil {
return nil, err
}
requ, err := newBulkRequest(conn.URL, index, docType, params, enc)
if err != nil {
return nil, err
}
_, result, err := conn.sendBulkRequest(requ)
if err != nil {
return nil, err
}
return readQueryResult(result.raw)
}
func newBulkRequest(
urlStr string,
index, docType string,
params map[string]string,
body bodyEncoder,
) (*bulkRequest, error) {
path, err := makePath(index, docType, "_bulk")
if err != nil {
return nil, err
}
url := makeURL(urlStr, path, "", params)
var reader io.Reader
if body != nil {
reader = body.Reader()
}
requ, err := http.NewRequest("POST", url, reader)
if err != nil {
return nil, err
}
if body != nil {
body.AddHeader(&requ.Header)
}
return &bulkRequest{
requ: requ,
}, nil
}
func (r *bulkRequest) Reset(body bodyEncoder) {
bdy := body.Reader()
rc, ok := bdy.(io.ReadCloser)
if !ok && body != nil {
rc = ioutil.NopCloser(bdy)
}
switch v := bdy.(type) {
case *bytes.Buffer:
r.requ.ContentLength = int64(v.Len())
case *bytes.Reader:
r.requ.ContentLength = int64(v.Len())
case *strings.Reader:
r.requ.ContentLength = int64(v.Len())
}
r.requ.Header = http.Header{}
r.requ.Body = rc
body.AddHeader(&r.requ.Header)
}
func (conn *Connection) sendBulkRequest(requ *bulkRequest) (int, bulkResult, error) {
status, resp, err := conn.execHTTPRequest(requ.requ)
if err != nil {
return status, bulkResult{}, err
}
result, err := readBulkResult(resp)
return status, result, err
}
func readBulkResult(obj []byte) (bulkResult, error) {
return bulkResult{obj}, nil
}
func bulkEncode(out bulkWriter, metaBuilder MetaBuilder, body []interface{}) error {
if metaBuilder == nil {
for _, obj := range body {
if err := out.AddRaw(obj); err != nil {
debugf("Failed to encode message: %s", err)
return err
}
}
} else {
for _, obj := range body {
meta := metaBuilder(obj)
if err := out.Add(meta, obj); err != nil {
debugf("Failed to encode event (dropping event): %s", err)
}
}
}
return nil
}