-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.go
223 lines (183 loc) · 6.11 KB
/
extractor.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
package gfirestore
import (
"context"
"errors"
"fmt"
"cloud.google.com/go/datastore"
"github.com/zpiroux/geist/entity"
)
type extractor struct {
id string
spec *entity.Spec
sinkConfig SinkConfig
client FirestoreClient
defaultNamespace string
}
type QueryType int
const (
Unknown QueryType = iota
KeyValue
CompositeKeyValue
All
)
// CompositeKey works as a SQL 'WHERE key1 = value1 and key2 = value2 ...' for all props in a stream
// spec with index=true. Due to the simple nature of GEIST GET /streams/.../events?... API,
// only string values are supported. A more full-fledged query API should be provided by a separate
// query service, so no need to support complex queries in GEIST.
type Query struct {
Type QueryType
Namespace string
Kind string
EntityName string
CompositeKey []entity.KeyValueFilter
}
func newExtractor(
spec *entity.Spec,
id string,
client FirestoreClient,
defaultNamespace string) (*extractor, error) {
if isNil(client) {
return nil, errors.New("client cannot be nil")
}
sinkConfig, err := NewSinkConfig(spec)
if err != nil {
return nil, err
}
var e = extractor{
spec: spec,
sinkConfig: sinkConfig,
id: id,
client: client,
defaultNamespace: defaultNamespace,
}
return &e, nil
}
func (e *extractor) StreamExtract(
ctx context.Context,
reportEvent entity.ProcessEventFunc,
err *error,
retryable *bool) {
*err = errors.New("not applicable")
}
// Currently only supporting Sink specs with a single Datastore "table" (kind)
func (e *extractor) ExtractFromSink(ctx context.Context, query entity.ExtractorQuery, result *[]*entity.Transformed) (error, bool) {
if len(e.sinkConfig.Kinds) == 0 {
return errors.New("need at least one Kind specified in Sink config"), false
}
return e.extractFromSink(ctx, e.convertQueryToNative(query), result)
}
func (e *extractor) convertQueryToNative(query entity.ExtractorQuery) Query {
var nativeQuery Query
switch query.Type {
case entity.QueryTypeAll:
nativeQuery.Type = All
nativeQuery.Namespace = e.spec.Namespace
nativeQuery.Kind = e.sinkConfig.Kinds[0].Name
case entity.QueryTypeKeyValue:
nativeQuery.Type = KeyValue
nativeQuery.Namespace = e.spec.Namespace
nativeQuery.Kind = e.sinkConfig.Kinds[0].Name
nativeQuery.EntityName = query.Key
case entity.QueryTypeCompositeKeyValue:
nativeQuery.Type = CompositeKeyValue
nativeQuery.Namespace = e.spec.Namespace
nativeQuery.Kind = e.sinkConfig.Kinds[0].Name
nativeQuery.CompositeKey = query.CompositeKey
}
return nativeQuery
}
// Currently only supporting Sink specs with a single Datastore "table" (kind)
// TODO: check if result should be changed from the current recreated transformed or as was needed
// in BigTable extractFromSink where the actual table format was returned.
func (e *extractor) extractFromSink(ctx context.Context, query Query, result *[]*entity.Transformed) (error, bool) {
log.Debugf(e.lgprfx()+"extractFromSink with query '%+v'", query)
switch query.Type {
case KeyValue:
var props datastore.PropertyList
key := datastore.NameKey(query.Kind, query.EntityName, nil)
key.Namespace = query.Namespace
if err := e.client.Get(ctx, key, &props); err != nil {
return err, true
}
transformed, err, retryable := e.recreateTransformed(&props)
if err != nil {
return err, retryable
}
*result = append(*result, transformed)
return nil, false
case All:
return e.getAll(ctx, createDatastoreQuery(query), result)
case CompositeKeyValue:
return e.getAll(ctx, createDatastoreQuery(query), result)
}
return errors.New("not yet supported"), false
}
func createDatastoreQuery(query Query) *datastore.Query {
dsq := datastore.NewQuery(query.Kind).Namespace(query.Namespace)
if query.Type == CompositeKeyValue {
for _, filter := range query.CompositeKey {
dsq = dsq.FilterField(filter.Key, "=", filter.Value)
}
}
return dsq
}
func (e *extractor) getAll(ctx context.Context, query *datastore.Query, result *[]*entity.Transformed) (error, bool) {
var propLists []datastore.PropertyList
log.Infof(e.lgprfx()+"GetAll query with query: %+v", *query)
_, err := e.client.GetAll(ctx, query, &propLists)
if err != nil {
return err, false
}
for _, props := range propLists {
transformed, err, retryable := e.recreateTransformed(&props)
if err != nil {
return err, retryable
}
*result = append(*result, transformed)
}
return nil, false
}
func (e *extractor) recreateTransformed(props *datastore.PropertyList) (*entity.Transformed, error, bool) {
transformed := entity.NewTransformed()
for _, prop := range *props {
id, err := e.getSpecIdFromPropName(prop.Name)
if err != nil {
return nil, err, false
}
transformed.Data[id] = prop.Value
}
return transformed, nil, true
}
func (e *extractor) getSpecIdFromPropName(name string) (string, error) {
// The properties are stored with Name as defined in the Sink part of the Spec.
// Only supporting one firestore "table" for now per spec (Kinds[0]).
for _, prop := range e.sinkConfig.Kinds[0].Properties {
if prop.Name == name {
return prop.Id, nil
}
}
return "", fmt.Errorf("specId not found for property name: %s", name)
}
// Currently, this function requires the caller to know how to handle Datastore specific storage structures.
// Instead, for generic repository usage, hiding away Datastore specifics, use ExtractFromSink().
// TODO: Add support for q := datastore.NewQuery("Entity").Filter("A =", 12).Limit(1) (added partly in ExtractFromSink())
func (e *extractor) Extract(ctx context.Context, query entity.ExtractorQuery, result any) (error, bool) {
q := e.convertQueryToNative(query)
switch q.Type {
case All:
dsq := datastore.NewQuery(q.Kind)
dsq = dsq.Namespace(q.Namespace)
if _, err := e.client.GetAll(ctx, dsq, result); err != nil {
return err, false
}
return nil, false
}
return errors.New("not yet supported"), false
}
func (e *extractor) SendToSource(ctx context.Context, eventData any) (string, error) {
log.Error(e.lgprfx() + "not applicable")
return "", nil
}
func (e *extractor) lgprfx() string {
return "[xfirestore.extractor:" + e.id + "] "
}