|
| 1 | +package cnosdb |
| 2 | + |
| 3 | +type ColumnType int |
| 4 | + |
| 5 | +const ( |
| 6 | + ColumnTypeUnknown ColumnType = iota |
| 7 | + ColumnTypeTag |
| 8 | + ColumnTypeTime |
| 9 | + ColumnTypeFieldUnknown |
| 10 | + ColumnTypeFieldFloat |
| 11 | + ColumnTypeFieldInteger |
| 12 | + ColumnTypeFieldUnsigned |
| 13 | + ColumnTypeFieldBoolean |
| 14 | + ColumnTypeFieldString |
| 15 | + ColumnTypeFieldGeometry |
| 16 | +) |
| 17 | + |
| 18 | +type TimeUnit int |
| 19 | + |
| 20 | +const ( |
| 21 | + TimeUnitUnknown TimeUnit = iota |
| 22 | + TimeUnitSecond |
| 23 | + TimeUnitMillisecond |
| 24 | + TimeUnitMicrosecond |
| 25 | + TimeUnitNanosecond |
| 26 | +) |
| 27 | + |
| 28 | +type TskvTableSchema struct { |
| 29 | + Tenant string `json:"tenant"` |
| 30 | + Db string `json:"db"` |
| 31 | + Name string `json:"name"` |
| 32 | + SchemaVersion uint64 `json:"schema_version"` |
| 33 | + NextColumnID uint32 `json:"next_column_id"` |
| 34 | + Columns []TableColumn `json:"columns"` |
| 35 | + ColumnsIndex map[string]uint32 `json:"columns_index"` |
| 36 | +} |
| 37 | + |
| 38 | +type TableColumn struct { |
| 39 | + ID uint64 `json:"id"` |
| 40 | + Name string `json:"name"` |
| 41 | + ColumnType interface{} `json:"column_type"` |
| 42 | + Encoding interface{} `json:"encoding"` |
| 43 | +} |
| 44 | + |
| 45 | +type ColumnTypeUnited struct { |
| 46 | + ColumnType ColumnType |
| 47 | + TimeUnit TimeUnit |
| 48 | +} |
| 49 | + |
| 50 | +func (c *TableColumn) GetColumnTypeUnited() ColumnTypeUnited { |
| 51 | + switch columnType := c.ColumnType.(type) { |
| 52 | + case string: |
| 53 | + if columnType == "Tag" { |
| 54 | + // "column_type": "Tag" |
| 55 | + return ColumnTypeUnited{ |
| 56 | + ColumnType: ColumnTypeTag, |
| 57 | + TimeUnit: TimeUnitUnknown, |
| 58 | + } |
| 59 | + } |
| 60 | + case map[string]interface{}: |
| 61 | + if timeUnitObj := columnType["Time"]; timeUnitObj != nil { |
| 62 | + // "column_type": {"Time":"Microsecond"} |
| 63 | + if timeUnit, ok := timeUnitObj.(string); ok { |
| 64 | + timeUnitCode := TimeUnitUnknown |
| 65 | + switch timeUnit { |
| 66 | + case "Second": |
| 67 | + timeUnitCode = TimeUnitSecond |
| 68 | + case "Millisecond": |
| 69 | + timeUnitCode = TimeUnitMillisecond |
| 70 | + case "Microsecond": |
| 71 | + timeUnitCode = TimeUnitMicrosecond |
| 72 | + case "Nanosecond": |
| 73 | + timeUnitCode = TimeUnitNanosecond |
| 74 | + } |
| 75 | + return ColumnTypeUnited{ |
| 76 | + ColumnType: ColumnTypeTime, |
| 77 | + TimeUnit: timeUnitCode, |
| 78 | + } |
| 79 | + } |
| 80 | + } else if fieldTypeObj := columnType["Field"]; fieldTypeObj != nil { |
| 81 | + fieldTypeCode := ColumnTypeFieldUnknown |
| 82 | + switch fieldType := fieldTypeObj.(type) { |
| 83 | + case string: |
| 84 | + switch fieldType { |
| 85 | + case "Float": |
| 86 | + // "column_type": {"Field":"Float"} |
| 87 | + fieldTypeCode = ColumnTypeFieldFloat |
| 88 | + case "Integer": |
| 89 | + // "column_type": {"Field":"Integer"} |
| 90 | + fieldTypeCode = ColumnTypeFieldInteger |
| 91 | + case "Unsigned": |
| 92 | + // "column_type": {"Field":"Unsigned"} |
| 93 | + fieldTypeCode = ColumnTypeFieldUnsigned |
| 94 | + case "Boolean": |
| 95 | + // "column_type": {"Field":"Boolean"} |
| 96 | + fieldTypeCode = ColumnTypeFieldBoolean |
| 97 | + case "String": |
| 98 | + // "column_type": {"Field":"String"} |
| 99 | + fieldTypeCode = ColumnTypeFieldString |
| 100 | + case "Geometry": |
| 101 | + // "column_type": {"Field":"Geometry"} |
| 102 | + fieldTypeCode = ColumnTypeFieldGeometry |
| 103 | + case "Unknown": |
| 104 | + // "column_type": {"Field":"Unknown"} |
| 105 | + fieldTypeCode = ColumnTypeFieldUnknown |
| 106 | + } |
| 107 | + case map[string]interface{}: |
| 108 | + if geometryInfo := fieldType["Geometry"]; geometryInfo != nil { |
| 109 | + // "column_type": {"Field":{"Geometry":{"sub_type":"Point","srid":10}}} |
| 110 | + fieldTypeCode = ColumnTypeFieldGeometry |
| 111 | + } |
| 112 | + } |
| 113 | + return ColumnTypeUnited{ |
| 114 | + ColumnType: fieldTypeCode, |
| 115 | + TimeUnit: TimeUnitUnknown, |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return ColumnTypeUnited{ |
| 121 | + ColumnType: ColumnTypeUnknown, |
| 122 | + TimeUnit: TimeUnitUnknown, |
| 123 | + } |
| 124 | +} |
0 commit comments