-
Notifications
You must be signed in to change notification settings - Fork 3
/
series.go
341 lines (296 loc) · 8.4 KB
/
series.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
// Copyright 2024 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// 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.
package greptime
import (
"fmt"
"time"
greptimepb "github.com/GreptimeTeam/greptime-proto/go/greptime/v1"
)
type column struct {
typ greptimepb.ColumnDataType
semantic greptimepb.SemanticType
}
func checkColumnEquality(key string, col1, col2 column) error {
if col1.typ != col2.typ {
return fmt.Errorf("the type of '%s' does not match: '%v' and '%v'", key, col1.typ, col2.typ)
}
if col1.semantic != col2.semantic {
return fmt.Errorf("tag and field MUST NOT contain same key: %q", key)
}
return nil
}
// Series represents one row of data you want to insert into GreptimeDB.
// - Tag fields are the index columns, which helps you to query data efficiently
// - Field fields are the value columns, which are used for value
// - Timestamp field is the timestamp column, which is required
//
// you do not need to create schema in advance, it will be created based on Series.
// But once the schema is created, [Client] has no ability to alert it.
type Series struct {
orders []string
columns map[string]column
vals map[string]any
timestamp time.Time // required for inserting
}
// GetTagsAndFields get all column names from metric, except timestamp column
func (s *Series) GetTagsAndFields() []string {
dst := make([]string, len(s.orders))
copy(dst, s.orders)
return dst
}
// Get helps to get value of specified column. The second return value
// indicates if the key was present in Series
func (s *Series) Get(key string) (any, bool) {
val, exist := s.vals[key]
return val, exist
}
// GetUint helps to get uint64 type of the specified key. It can retrieve the following type:
// - uint64
// - uint32
// - uint16
// - uint8
// - uint
//
// if you want uint32 instead of uint64, you can do it like:
//
// if v, ok := s.GetUint(key); ok {
// val := uint32(v)
// }
func (s *Series) GetUint(key string) (uint64, bool) {
val, exist := s.Get(key)
if !exist {
return 0, exist
}
switch t := val.(type) {
case uint8:
return uint64(t), true
case uint16:
return uint64(t), true
case uint32:
return uint64(t), true
case uint64:
return t, true
case uint:
return uint64(t), true
default:
return 0, false
}
}
// GetInt helps to get int64 type of the specified key. It can retrieve the following type:
// - int64
// - int32
// - int16
// - int8
// - int
//
// if you want int32 instead of int64, you can do it like:
//
// if v, ok := s.GetInt(key); ok {
// val := int32(v)
// }
func (s *Series) GetInt(key string) (int64, bool) {
val, exist := s.Get(key)
if !exist {
return 0, exist
}
switch t := val.(type) {
case int8:
return int64(t), true
case int16:
return int64(t), true
case int32:
return int64(t), true
case int64:
return t, true
case int:
return int64(t), true
default:
return 0, false
}
}
// GetFloat helps to get float64 type of the specified key. It can retrieve the following type:
// - float64
// - float32
//
// if you want float32 instead of float64, you can do it like:
//
// if v, ok := s.GetFloat(key); ok {
// val := float32(v)
// }
func (s *Series) GetFloat(key string) (float64, bool) {
val, exist := s.Get(key)
if !exist {
return 0, exist
}
switch t := val.(type) {
case float32:
return float64(t), true
case float64:
return t, true
default:
return 0, false
}
}
func (s *Series) GetBool(key string) (bool, bool) {
val, exist := s.Get(key)
if !exist {
return false, exist
}
v, ok := val.(bool)
return v, ok
}
func (s *Series) GetString(key string) (string, bool) {
val, exist := s.Get(key)
if !exist {
return "", exist
}
v, ok := val.(string)
return v, ok
}
func (s *Series) GetBytes(key string) ([]byte, bool) {
val, exist := s.Get(key)
if !exist {
return nil, exist
}
v, ok := val.([]byte)
return v, ok
}
func (s *Series) GetTimestamp(key string) (time.Time, bool) {
val, exist := s.Get(key)
if !exist {
return time.Time{}, exist
}
v, ok := val.(time.Time)
return v, ok
}
func (s *Series) add(name string, val any, semantic greptimepb.SemanticType) error {
key, err := toColumnName(name)
if err != nil {
return err
}
if s.columns == nil {
s.columns = map[string]column{}
}
v, err := convert(val)
if err != nil {
return fmt.Errorf("add tag err: %w", err)
}
newCol := column{
typ: v.typ,
semantic: semantic,
}
if col, seen := s.columns[key]; seen {
if err := checkColumnEquality(key, col, newCol); err != nil {
return err
}
}
s.columns[key] = newCol
s.orders = append(s.orders, key)
if s.vals == nil {
s.vals = map[string]any{}
}
s.vals[key] = v.val
return nil
}
// AddTag prepare tag column, and old value will be replaced if same tag is set.
// the length of key CAN NOT be longer than 100.
// If you want to constrain the column type, you can directly use like:
// - [Series.AddFloatTag]
// - [Series.AddIntTag]
// - ...
func (s *Series) AddTag(key string, val any) error {
return s.add(key, val, greptimepb.SemanticType_TAG)
}
// AddFloatTag helps to constrain the key to be float64 type, if you want to
// add float32 tag instead of float64, you can do it like:
//
// var i float32 = 1.0
// return s.AddFloatTag("memory", float64(i))
func (s *Series) AddFloatTag(key string, val float64) error {
return s.AddTag(key, val)
}
// AddIntTag helps to constrain the key to be int64 type, if you want to
// add int32 tag instead of int64, you can do it like:
//
// var i int32 = 1
// return s.AddIntTag("account", int64(i))
func (s *Series) AddIntTag(key string, val int64) error {
return s.AddTag(key, val)
}
// AddUintTag helps to constrain the key to be uint64 type, if you want to
// add uint32 tag instead of uint64, you can do it like:
//
// var i uint32 = 1
// return s.AddUintTag("account", uint64(i))
func (s *Series) AddUintTag(key string, val uint64) error {
return s.AddTag(key, val)
}
// AddBoolTag helps to constrain the key to be bool type
func (s *Series) AddBoolTag(key string, val bool) error {
return s.AddTag(key, val)
}
// AddStringTag helps to constrain the key to be string type
func (s *Series) AddStringTag(key string, val string) error {
return s.AddTag(key, val)
}
// AddBytesTag helps to constrain the key to be []byte type
func (s *Series) AddBytesTag(key string, val []byte) error {
return s.AddTag(key, val)
}
// AddField prepare field column, and old value will be replaced if same field is set.
// the length of key CAN NOT be longer than 100
func (s *Series) AddField(key string, val any) error {
return s.add(key, val, greptimepb.SemanticType_FIELD)
}
// AddFloatField helps to constrain the key to be float64 type, if you want to
// add float32 tag instead of float64, you can do it like:
//
// var i float32 = 1.0
// return s.AddFloatField("memory", float64(i))
func (s *Series) AddFloatField(key string, val float64) error {
return s.AddField(key, val)
}
// AddIntField helps to constrain the key to be int64 type, if you want to
// add int32 tag instead of int64, you can do it like:
//
// var i int32 = 1
// return s.AddIntField("account", int64(i))
func (s *Series) AddIntField(key string, val int64) error {
return s.AddField(key, val)
}
// AddUintField helps to constrain the key to be uint64 type, if you want to
// add uint32 tag instead of uint64, you can do it like:
//
// var i uint32 = 1
// return s.AddUintField("account", uint64(i))
func (s *Series) AddUintField(key string, val uint64) error {
return s.AddField(key, val)
}
// AddBoolField helps to constrain the key to be bool type
func (s *Series) AddBoolField(key string, val bool) error {
return s.AddField(key, val)
}
// AddStringField helps to constrain the key to be string type
func (s *Series) AddStringField(key string, val string) error {
return s.AddField(key, val)
}
// AddBytesField helps to constrain the key to be []byte type
func (s *Series) AddBytesField(key string, val []byte) error {
return s.AddField(key, val)
}
// SetTimestamp is required
func (s *Series) SetTimestamp(t time.Time) error {
s.timestamp = t
return nil
}