-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_writer.go
83 lines (79 loc) · 2.26 KB
/
stream_writer.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
package batch
import (
"context"
"encoding/json"
"fmt"
"reflect"
es "github.com/core-go/elasticsearch"
"github.com/elastic/go-elasticsearch/v8"
)
type StreamWriter[T any] struct {
client *elasticsearch.Client
index string
idx int
Map func(T)
isPointer bool
FieldMap []es.FieldMap
batch []Model
batchSize int
}
func NewStreamWriter[T any](client *elasticsearch.Client, index string, batchSize int, opts ...func(T)) *StreamWriter[T] {
return NewStreamWriterWithIdName[T](client, index, batchSize, "", opts...)
}
func NewStreamWriterWithIdName[T any](client *elasticsearch.Client, index string, batchSize int, idFieldName string, opts ...func(T)) *StreamWriter[T] {
var t T
modelType := reflect.TypeOf(t)
isPointer := false
if modelType.Kind() == reflect.Ptr {
modelType = modelType.Elem()
isPointer = true
}
var idx int
if len(idFieldName) == 0 {
idx, _, _ = es.FindIdField(modelType)
if idx < 0 {
panic("Require Id field of " + modelType.Name() + " struct define _id bson tag.")
}
} else {
idx, _ = es.FindFieldByName(modelType, idFieldName)
if idx < 0 {
panic(fmt.Sprintf("%s struct requires id field which id name is '%s'", modelType.Name(), idFieldName))
}
}
idField := modelType.Field(idx)
if idField.Type.String() != "string" {
panic(fmt.Sprintf("%s type of %s struct must be string", modelType.Field(idx).Name, modelType.Name()))
}
var mp func(T)
if len(opts) >= 1 {
mp = opts[0]
}
return &StreamWriter[T]{client: client, index: index, idx: idx, FieldMap: es.BuildMap(modelType), batch: make([]Model, 0), batchSize: batchSize, Map: mp, isPointer: isPointer}
}
func (w *StreamWriter[T]) Write(ctx context.Context, obj T) error {
if w.Map != nil {
w.Map(obj)
}
vo := reflect.ValueOf(obj)
if w.isPointer {
vo = reflect.Indirect(vo)
}
id := vo.Field(w.idx).Interface().(string)
body := es.BuildBody(obj, w.FieldMap)
model := Model{Id: id, Body: body}
data, err := json.Marshal(model.Body)
if err != nil {
return err
}
model.Data = string(data)
w.batch = append(w.batch, model)
if len(w.batch) >= w.batchSize {
return w.Flush(ctx)
}
return nil
}
func (w *StreamWriter[T]) Flush(ctx context.Context) error {
_, err := SaveBatch(ctx, w.client, w.index, w.batch)
w.batch = make([]Model, 0)
return err
}