-
Notifications
You must be signed in to change notification settings - Fork 7
/
data.go
132 lines (102 loc) · 3.23 KB
/
data.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
package data
import (
// "fmt"
)
// column settings are used to store information about the columns - spcifically name, type and
// index, i.e. the order in which they are encountered in the csv.
type columnSettings struct {
colType string
colIndex uint64 // no way there's this many columns, but better safe than sorry
bIdCol bool
}
var cols map[string]columnSettings // the map key is the column name
// this represents the database to be stored
// the parent array are columns, the arrays are the data elems i.e. rows
// NOTE(@adam-hanna): is a map a better way of implementing this?
var dataSet [][]interface{}
// make the primary key map
// assumes primary key is a string
// NOTE(@adam-hanna): change this to accept different types
var primeKeyDictionary map[string]uint64
func InitializeDataset() {
primeKeyDictionary = make(map[string]uint64)
}
func InitializeColumnSettings(columns []interface{}, idField string) {
dataSet = make([][]interface{}, len(columns))
cols = make(map[string]columnSettings)
for idx := range columns {
for key, val := range columns[idx].(map[string]interface{}) {
fieldName := key
fieldTypeString := val.(string)
// set the values to the column map
cols[fieldName] = columnSettings{fieldTypeString, uint64(idx), fieldName == idField}
// initalize the column
// NOTE(@adam-hanna): is this necessary?
dataSet[idx] = make([]interface{}, 0)
}
}
}
func GetKeyByIdx(idx uint64) string {
return dataSet[GetIdFieldIdx()][idx].(string)
}
func GetRecordIdxByKey(key string) uint64 {
return primeKeyDictionary[key]
}
func GetFullRowOfDataByKey(key string) map[string]interface{} {
return GetFullRowOfDataByIdx(primeKeyDictionary[key])
}
func GetFullRowOfDataByIdx(idx uint64) map[string]interface{} {
temp := make(map[string]interface{})
for key, val := range cols {
temp[key] = dataSet[val.colIndex][idx]
}
return temp
}
func GetDataPointByKey(colName string, key string) interface{} {
return GetDataPointByIdx(colName, primeKeyDictionary[key])
}
func GetDataPointByIdx(colName string, idx uint64) interface{} {
return dataSet[GetColIndexByName(colName)][idx]
}
func GetColTypeByName(colName string) string {
return cols[colName].colType
}
func GetColIndexByName(colName string) uint64 {
return cols[colName].colIndex
}
func GetColValsByName(colName string) []interface{} {
return GetColValsByIdx(GetColIndexByName(colName))
}
func GetColValsByIdx(colIdx uint64) []interface{} {
return dataSet[colIdx]
}
func GetIdFieldName() string {
for key, val := range cols {
if val.bIdCol {
return key
}
}
// NOTE(@adam-hanna): fix this! but don't want to return two vals (i.e. error)?
return "not found"
}
func GetIdFieldIdx() uint64 {
for _, val := range cols {
if val.bIdCol {
return val.colIndex
}
}
// NOTE(@adam-hanna): fix this! but don't want to return two vals (i.e. error)?
return uint64(0)
}
func CountRecords() uint64 {
return uint64(len(dataSet[0]))
}
func SetData(newData []interface{}) {
idFieldIdx := GetIdFieldIdx()
for key := range newData {
dataSet[key] = append(dataSet[key], newData[key])
}
// add the location to the primeKey dictionary
// NOTE(@adam-hanna): check for dupe ids?
primeKeyDictionary[newData[idFieldIdx].(string)] = uint64(len(dataSet[0]) - 1)
}