/
table.go
236 lines (197 loc) · 5.46 KB
/
table.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.
// Package refertable for saving external data
package refertable
import (
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/spf13/cast"
)
const (
columnTypeStr = "string"
columnTypeInt = "int"
columnTypeFloat = "float"
columnTypeBool = "bool"
)
type PlReferTables interface {
Query(tableName string, colName []string, colValue []any, kGet []string) (map[string]any, bool)
updateAll(tables []referTable) (retErr error)
Stats() *ReferTableStats
}
type PlReferTablesInMemory struct {
tables map[string]*referTable
tablesName []string
updateMutex sync.Mutex
queryRWmutex sync.RWMutex
}
type ReferTableStats struct {
Name []string
Row []int
}
func (plrefer *PlReferTablesInMemory) Query(tableName string, colName []string, colValue []any,
kGet []string,
) (map[string]any, bool) {
plrefer.queryRWmutex.RLock()
defer plrefer.queryRWmutex.RUnlock()
var table *referTable
if tableName == "" {
return nil, false
}
table = plrefer.tables[tableName]
if table == nil {
return nil, false
}
var row []any
if allRow, ok := query(table.index, colName, colValue, 1); ok {
row = table.RowData[allRow[0]]
} else {
return nil, false
}
result := map[string]any{}
if len(kGet) != 0 {
for _, key := range kGet {
cIdx, ok := table.colIndex[key]
if !ok {
continue
}
result[key] = row[cIdx]
}
} else {
for k, v := range table.colIndex {
result[k] = row[v]
}
}
return result, true
}
func (plrefer *PlReferTablesInMemory) updateAll(tables []referTable) (retErr error) {
defer func() {
if err := recover(); err != nil {
retErr = fmt.Errorf("run pl: %s", err)
}
}()
plrefer.updateMutex.Lock()
defer plrefer.updateMutex.Unlock()
refTableMap := map[string]*referTable{}
tablesName := []string{}
for idx := range tables {
table := tables[idx]
if err := table.buildTableIndex(); err != nil {
return err
}
if _, ok := refTableMap[table.TableName]; !ok {
refTableMap[table.TableName] = &table
tablesName = append(tablesName, table.TableName)
}
}
plrefer.queryRWmutex.Lock()
defer plrefer.queryRWmutex.Unlock()
plrefer.tables = refTableMap
plrefer.tablesName = tablesName
return nil
}
func (plrefer *PlReferTablesInMemory) Stats() *ReferTableStats {
plrefer.queryRWmutex.RLock()
defer plrefer.queryRWmutex.RUnlock()
tableStats := ReferTableStats{}
for _, name := range plrefer.tablesName {
tableStats.Name = append(tableStats.Name, name)
tableStats.Row = append(tableStats.Row,
len(plrefer.tables[name].RowData))
}
return &tableStats
}
type referTable struct {
TableName string `json:"table_name"`
ColumnName []string `json:"column_name"`
ColumnType []string `json:"column_type"`
RowData [][]any `json:"row_data"`
// 要求 []int 中的行号递增
index map[string]map[any][]int
colIndex map[string]int
}
func (table *referTable) check() error {
if table.TableName == "" {
return fmt.Errorf("table: \"%s\", error: empty table name", table.TableName)
}
if len(table.ColumnName) != len(table.ColumnType) {
return fmt.Errorf("table: %s, error: len(table.ColumnName) != len(table.ColumnType)",
table.TableName)
}
for idx, row := range table.RowData {
if len(table.ColumnName) != len(row) {
return fmt.Errorf("table: %s, col: %d, error: len(table.ColumnName) != len(table.RowData[%d])",
table.TableName, idx, idx)
}
}
for idx, columnName := range table.ColumnName {
if columnName == "" {
return fmt.Errorf("table: %s, column: %v, index: %d, value: \"\"",
table.TableName, table.ColumnName, idx)
}
}
for idx, columnType := range table.ColumnType {
switch columnType {
case columnTypeInt, columnTypeFloat,
columnTypeBool, columnTypeStr:
default:
return fmt.Errorf("table: %s, unsupported column type: %s -> %s",
table.TableName, table.ColumnName[idx], columnType)
}
}
return nil
}
func (table *referTable) buildTableIndex() error {
if err := table.check(); err != nil {
return err
}
table.index = map[string]map[any][]int{}
table.colIndex = map[string]int{}
// 遍历行
for rowIdx, row := range table.RowData {
// 遍历列,建立索引: colName -> colValue -> []rowIndex
for colIdx := 0; colIdx < len(table.ColumnName); colIdx++ {
colName := table.ColumnName[colIdx]
if _, ok := table.index[colName]; !ok {
table.colIndex[colName] = colIdx
table.index[colName] = map[any][]int{}
}
// 列数据转换为指定类型
v, err := conv(row[colIdx], table.ColumnType[colIdx])
if err != nil {
return fmt.Errorf("table: %s, row: %d, col: %d, cast error: %w",
table.TableName, rowIdx, colIdx, err)
}
row[colIdx] = v
// column 反向索引: colValue -> []rowIndex
table.index[colName][v] = append(table.index[colName][v],
rowIdx)
}
}
return nil
}
func conv(col any, dtype string) (any, error) {
switch strings.ToLower(dtype) {
case columnTypeBool:
return cast.ToBoolE(col)
case columnTypeInt:
return cast.ToInt64E(col)
case columnTypeFloat:
return cast.ToFloat64E(col)
case columnTypeStr:
return cast.ToStringE(col)
default:
return nil, fmt.Errorf("unsupported type: %s", dtype)
}
}
func decodeJSONData(data []byte) ([]referTable, error) {
var tables []referTable
if err := json.Unmarshal(data, &tables); err != nil {
return nil, err
} else {
return tables, nil
}
}