-
Notifications
You must be signed in to change notification settings - Fork 20
/
types.go
406 lines (350 loc) · 11.1 KB
/
types.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
Copyright 2018 Iguazio Systems Ltd.
Licensed under the Apache License, Version 2.0 (the "License") with
an addition restriction as set forth herein. You may not use this
file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
In addition, you may not use the software for any purposes that are
illegal under applicable law, and the grant of the foregoing license
under the Apache 2.0 license is conditioned upon your compliance with
such restriction.
*/
package frames
import (
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"github.com/v3io/frames/pb"
)
// DType is data type
type DType pb.DType
// Possible data types
var (
BoolType = DType(pb.DType_BOOLEAN)
FloatType = DType(pb.DType_FLOAT)
IntType = DType(pb.DType_INTEGER)
StringType = DType(pb.DType_STRING)
TimeType = DType(pb.DType_TIME)
NullType = DType(pb.DType_NULL)
)
type SaveMode int
func (mode SaveMode) GetNginxModeName() string {
switch mode {
case ErrorIfTableExists:
return ""
case OverwriteTable:
return ""
case UpdateItem:
return "CreateOrReplaceAttributes"
case OverwriteItem:
return "OverWriteAttributes"
case CreateNewItemsOnly:
return "CreateNewItemOnly"
default:
return ""
}
}
func (mode SaveMode) String() string {
switch mode {
case ErrorIfTableExists:
return "errorIfTableExists"
case OverwriteTable:
return "overwriteTable"
case UpdateItem:
return "updateItem"
case OverwriteItem:
return "overwriteItem"
case CreateNewItemsOnly:
return "createNewItemsOnly"
default:
return ""
}
}
const (
ErrorIfTableExists SaveMode = iota
OverwriteTable
UpdateItem
OverwriteItem
CreateNewItemsOnly
)
// Column is a data column
type Column interface {
Len() int // Number of elements
Name() string // Column name
DType() DType // Data type (e.g. IntType, FloatType ...)
Ints() ([]int64, error) // Data as []int64
IntAt(i int) (int64, error) // Int value at index i
Floats() ([]float64, error) // Data as []float64
FloatAt(i int) (float64, error) // Float value at index i
Strings() []string // Data as []string
StringAt(i int) (string, error) // String value at index i
Times() ([]time.Time, error) // Data as []time.Time
TimeAt(i int) (time.Time, error) // time.Time value at index i
Bools() ([]bool, error) // Data as []bool
BoolAt(i int) (bool, error) // bool value at index i
Slice(start int, end int) (Column, error) // Slice of data
CopyWithName(newName string) Column // Create a copy of the current column
}
// Frame is a collection of columns
type Frame interface {
Labels() map[string]interface{} // Label set
Names() []string // Column names
Indices() []Column // Index columns
Len() int // Number of rows
Column(name string) (Column, error) // Column by name
Slice(start int, end int) (Frame, error) // Slice of Frame
IterRows(includeIndex bool) RowIterator // Iterate over rows
IsNull(index int, colName string) bool
NullValuesMap() []*pb.NullValuesMap
}
// RowIterator is an iterator over frame rows
type RowIterator interface {
Next() bool // Advance to next row
Row() map[string]interface{} // Row as map of name->value
RowNum() int // Current row number
Indices() map[string]interface{} // MultiIndex as name->value
Err() error // Iteration error
}
// DataBackend is an interface for read/write on backend
type DataBackend interface {
// TODO: Expose name, type, config ... ?
Read(request *ReadRequest) (FrameIterator, error)
Write(request *WriteRequest) (FrameAppender, error) // TODO: use Appender for write streaming
Create(request *CreateRequest) error
Delete(request *DeleteRequest) error
Exec(request *ExecRequest) (Frame, error)
}
// FrameIterator iterates over frames
type FrameIterator interface {
Next() bool
Err() error
At() Frame
}
// FrameAppender appends frames
type FrameAppender interface {
Add(frame Frame) error
WaitForComplete(timeout time.Duration) error
Close()
}
// ReadRequest is a read/query request
type ReadRequest struct {
Proto *pb.ReadRequest
Password SecretString
Token SecretString
}
func (readRequest ReadRequest) ToMap() map[string]string {
reqMap := make(map[string]string, 10)
if readRequest.Proto.Query != "" {
reqMap["query"] = readRequest.Proto.Query
}
if len(readRequest.Proto.Columns) > 0 {
reqMap["columns"] = strings.Join(readRequest.Proto.Columns, ",")
}
if readRequest.Proto.Filter != "" {
reqMap["filter"] = readRequest.Proto.Filter
}
if readRequest.Proto.GroupBy != "" {
reqMap["groupBy"] = readRequest.Proto.GroupBy
}
if readRequest.Proto.Limit != 0 {
reqMap["limit"] = fmt.Sprintf("%v", readRequest.Proto.Limit)
}
if readRequest.Proto.MessageLimit != 0 {
reqMap["messageLimit"] = fmt.Sprintf("%v", readRequest.Proto.MessageLimit)
}
if len(readRequest.Proto.ShardingKeys) > 0 {
reqMap["shardingkeys"] = strings.Join(readRequest.Proto.ShardingKeys, ",")
}
if readRequest.Proto.SortKeyRangeStart != "" {
reqMap["sortKeyRangeStart"] = readRequest.Proto.SortKeyRangeStart
}
if readRequest.Proto.SortKeyRangeEnd != "" {
reqMap["sortKeyRangeEnd"] = readRequest.Proto.SortKeyRangeEnd
}
if readRequest.Proto.Start != "" {
reqMap["start"] = readRequest.Proto.Start
}
if readRequest.Proto.End != "" {
reqMap["end"] = readRequest.Proto.End
}
if readRequest.Proto.Step != "" {
reqMap["step"] = readRequest.Proto.Step
}
if readRequest.Proto.Aggregators != "" {
reqMap["aggregators"] = readRequest.Proto.Aggregators
}
if readRequest.Proto.AggregationWindow != "" {
reqMap["aggregationWindow"] = readRequest.Proto.AggregationWindow
}
if readRequest.Proto.Seek != "" {
reqMap["seek"] = readRequest.Proto.Seek
}
if readRequest.Proto.ShardId != "" {
reqMap["shardID"] = readRequest.Proto.ShardId
}
if readRequest.Proto.Sequence != 0 {
reqMap["sequence"] = fmt.Sprintf("%v", readRequest.Proto.Sequence)
}
return reqMap
}
// JoinStruct is a join structure
type JoinStruct = pb.JoinStruct
// WriteRequest is request for writing data
// TODO: Unite with probouf (currenly the protobuf message combines both this
// and a frame message)
type WriteRequest struct {
Session *Session
Password SecretString
Token SecretString
Backend string // backend name
Table string // Table name (path)
// Data message sent with the write request (in case of a stream multiple messages can follow)
ImmidiateData Frame
// Expression template, for update expressions generated from combining columns data with expression
Expression string
// Condition template, for update conditions generated from combining columns data with expression
Condition string
// Columns to partition the data by
PartitionKeys []string
// Will we get more message chunks (in a stream), if not we can complete
HaveMore bool
SaveMode SaveMode
}
func (writeRequest WriteRequest) ToMap() map[string]string {
reqMap := make(map[string]string)
if writeRequest.Expression != "" {
reqMap["expression"] = writeRequest.Expression
}
if writeRequest.Condition != "" {
reqMap["condition"] = writeRequest.Condition
}
if len(writeRequest.PartitionKeys) > 0 {
reqMap["partitionKeys"] = strings.Join(writeRequest.PartitionKeys, ",")
}
reqMap["saveMode"] = writeRequest.SaveMode.String()
return reqMap
}
// CreateRequest is a table creation request
type CreateRequest struct {
Proto *pb.CreateRequest
Password SecretString
Token SecretString
}
func (createRequest CreateRequest) ToMap() map[string]string {
reqMap := make(map[string]string)
if createRequest.Proto.Rate != "" {
reqMap["rate"] = createRequest.Proto.Rate
}
if createRequest.Proto.Aggregates != "" {
reqMap["aggregates"] = createRequest.Proto.Aggregates
}
if createRequest.Proto.AggregationGranularity != "" {
reqMap["aggregationGranularity"] = createRequest.Proto.AggregationGranularity
}
if createRequest.Proto.Shards != 0 {
reqMap["shards"] = fmt.Sprintf("%v", createRequest.Proto.Shards)
}
if createRequest.Proto.RetentionHours != 0 {
reqMap["retentionHours"] = fmt.Sprintf("%v", createRequest.Proto.RetentionHours)
}
return reqMap
}
// DeleteRequest is a deletion request
type DeleteRequest struct {
Proto *pb.DeleteRequest
Password SecretString
Token SecretString
}
func (deleteRequest DeleteRequest) ToMap() map[string]string {
reqMap := make(map[string]string)
if deleteRequest.Proto.Filter != "" {
reqMap["filter"] = deleteRequest.Proto.Filter
}
if deleteRequest.Proto.Start != "" {
reqMap["start"] = deleteRequest.Proto.Start
}
if deleteRequest.Proto.End != "" {
reqMap["end"] = deleteRequest.Proto.End
}
if len(deleteRequest.Proto.Metrics) > 0 {
reqMap["metrics"] = strings.Join(deleteRequest.Proto.Metrics, ",")
}
return reqMap
}
// HistoryRequest is a history logs request
type HistoryRequest struct {
Proto *pb.HistoryRequest
Password SecretString
Token SecretString
}
// TableSchema is a table schema
type TableSchema = pb.TableSchema
// SchemaField represents a schema field for Avro record.
type SchemaField = pb.SchemaField
// SchemaKey is a schema key
type SchemaKey = pb.SchemaKey
// Session information
type Session = pb.Session
// Shortcut for fail/ignore
const (
IgnoreError = pb.ErrorOptions_IGNORE
FailOnError = pb.ErrorOptions_FAIL
)
type VersionRequest struct {
Proto *pb.VersionRequest
Password SecretString
Token SecretString
}
// ExecRequest is execution request
type ExecRequest struct {
Proto *pb.ExecRequest
Password SecretString
Token SecretString
}
func (executeRequest ExecRequest) ToMap() map[string]string {
reqMap := make(map[string]string)
if executeRequest.Proto.Command != "" {
reqMap["command"] = executeRequest.Proto.Command
}
if executeRequest.Proto.Expression != "" {
reqMap["expression"] = executeRequest.Proto.Expression
}
if len(executeRequest.Proto.Args) > 0 {
for k, v := range executeRequest.Proto.Args {
reqMap[fmt.Sprintf("args_%v", k)] = v.String()
}
}
return reqMap
}
// Hides a string such as a password from both plain and json logs.
type SecretString struct {
s *string
}
func InitSecretString(s string) SecretString {
return SecretString{s: &s}
}
func (s SecretString) Get() string {
return *s.s
}
func SaveModeFromString(mode string) (SaveMode, error) {
switch mode {
case "", "errorIfTableExists": // this is the default save mode
return ErrorIfTableExists, nil
case "overwriteTable":
return OverwriteTable, nil
case "updateItem":
return UpdateItem, nil
case "overwriteItem":
return OverwriteItem, nil
case "createNewItemsOnly":
return CreateNewItemsOnly, nil
default:
return -1, errors.Errorf("no save mode named '%v'", mode)
}
}