-
Notifications
You must be signed in to change notification settings - Fork 3
/
memory.go
241 lines (205 loc) · 6.77 KB
/
memory.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
/*
* Copyright © 2020. TIBCO Software Inc.
* This file is subject to the license terms contained
* in the license file that is distributed with this file.
*/
package table
import (
"os"
)
func NewInMenmory(properties map[string]interface{}) (*InMemoryTable, error) {
myTable := &InMemoryTable{
pKey: properties["pKey"].([]string),
indices: make([][]string, 0),
theMap: make(map[CompositKey]*Record),
theIndexedKey: make(map[CompositKey]map[CompositKey][]CompositKey),
tableSchema: properties["tableSchema"].(*Schema),
}
indexible := properties["indices"].([]string)
for index := 0; index < len(indexible); index++ {
myTable.GenerateKeys(indexible, make([]string, index+1), 0, len(indexible)-1, 0, index+1)
}
return myTable, nil
}
type InMemoryTable struct {
pKey []string
indices [][]string
tableSchema *Schema
theMap map[CompositKey]*Record
theIndexedKey map[CompositKey]map[CompositKey][]CompositKey
}
func (this *InMemoryTable) AddIndex(keyName []string) bool {
key, _ := ConstructKey(keyName, nil)
this.indices = append(this.indices, keyName)
this.theIndexedKey[key] = make(map[CompositKey][]CompositKey)
log.Info("(AddIndex) table after add index: indices = ", this.indices, ", table = ", this.theMap)
return true
}
func (this *InMemoryTable) RemoveIndex(keyName []string) bool {
key, _ := ConstructKey(keyName, nil)
this.theMap[key] = nil
return true
}
func (this *InMemoryTable) GetPkeyNames() []string {
return this.pKey
}
func (this *InMemoryTable) GetAll() ([]*Record, bool) {
records := make([]*Record, 0)
for _, record := range this.theMap {
records = append(records, record)
}
return records, false
}
func (this *InMemoryTable) Get(searchKey []string, data map[string]interface{}) ([]*Record, bool) {
log.Info("(Get) searchKey : ", searchKey)
log.Info("(Get) data : ", data)
log.Info("(Get) pKey : ", this.pKey)
log.Info("(Get) theMap : ", this.theMap)
///////// Need to be fixed ////////
dumpTable := true
for _, key := range data {
if "*" != key.(string) {
dumpTable = false
break
}
}
if dumpTable {
return this.GetAll()
}
//////////////////////////////////
records := make([]*Record, 0)
pKeyHash, pKeyValueHash := ConstructKey(this.pKey, data)
searchKeyHash, searchKeyValueHash := ConstructKey(searchKey, data)
log.Info("(Get) pKeyValueHash : ", pKeyValueHash)
log.Info("(Get) searchKeyValueHash : ", searchKeyValueHash)
searchByPKey := true
if searchKeyHash == pKeyHash {
log.Info("(Get) Get by primary key !")
record := this.theMap[pKeyValueHash]
if nil != record {
records = append(records, record)
}
} else {
log.Info("(Get) Get by indexed search key !")
searchByPKey = false
pKeyValueHashs := this.theIndexedKey[searchKeyHash][searchKeyValueHash]
if nil != pKeyValueHashs {
newPKeyValueHashs := make([]CompositKey, 0)
for _, pKeyValueHash := range pKeyValueHashs {
if nil != this.theMap[pKeyValueHash] {
records = append(records, this.theMap[pKeyValueHash])
newPKeyValueHashs = append(newPKeyValueHashs, pKeyValueHash)
}
}
this.theIndexedKey[searchKeyHash][searchKeyValueHash] = newPKeyValueHashs
}
}
return records, searchByPKey
}
func (this *InMemoryTable) Insert(data map[string]interface{}) (*Record, *Record) {
log.Info("(Insert) data : ", data)
log.Info("(Insert) pKey : ", this.pKey)
log.Info("(Insert) theMap before : ", this.theMap)
_, pKeyValueHash := ConstructKey(this.pKey, data)
record := this.theMap[pKeyValueHash]
var oldRecord *Record
if nil != record {
oldRecord = record.Clone()
record = nil
} else {
// Create new record
record = &Record{}
for _, fieldInfo := range *this.tableSchema.DataSchemas() {
(*record)[fieldInfo["Name"].(string)] = data[fieldInfo["Name"].(string)]
}
this.theMap[pKeyValueHash] = record
// Indexing record
for _, index := range this.indices {
indexHash, indexValueHash := ConstructKey(index, data)
pKeyValueHashs := this.theIndexedKey[indexHash][indexValueHash]
if nil != pKeyValueHashs {
this.theIndexedKey[indexHash][indexValueHash] = append(this.theIndexedKey[indexHash][indexValueHash], pKeyValueHash)
} else {
this.theIndexedKey[indexHash][indexValueHash] = []CompositKey{pKeyValueHash}
}
}
}
log.Info("(Insert) theMap after : ", this.theMap)
return record, oldRecord
}
func (this *InMemoryTable) Upsert(data map[string]interface{}) (*Record, *Record) {
log.Info("(Upsert) data : ", data)
log.Info("(Upsert) pKey : ", this.pKey)
log.Info("(Upsert) theMap before : ", this.theMap)
_, pKeyValueHash := ConstructKey(this.pKey, data)
record := this.theMap[pKeyValueHash]
var oldRecord *Record
if nil != record {
oldRecord = record.Clone()
for _, fieldInfo := range *this.tableSchema.DataSchemas() {
fieldName := fieldInfo["Name"].(string)
fieldValue := data[fieldName]
if nil != fieldValue {
(*record)[fieldName] = fieldValue
}
}
} else {
// Create new record
record = &Record{}
for _, fieldInfo := range *this.tableSchema.DataSchemas() {
(*record)[fieldInfo["Name"].(string)] = data[fieldInfo["Name"].(string)]
}
this.theMap[pKeyValueHash] = record
// Indexing record
for _, index := range this.indices {
indexHash, indexValueHash := ConstructKey(index, data)
pKeyValueHashs := this.theIndexedKey[indexHash][indexValueHash]
if nil != pKeyValueHashs {
this.theIndexedKey[indexHash][indexValueHash] = append(this.theIndexedKey[indexHash][indexValueHash], pKeyValueHash)
} else {
this.theIndexedKey[indexHash][indexValueHash] = []CompositKey{pKeyValueHash}
}
}
}
log.Info("(Upsert) theMap after : ", this.theMap)
return record, oldRecord
}
func (this *InMemoryTable) Delete(data map[string]interface{}) *Record {
log.Info("(Delete) data : ", data)
log.Info("(Delete) pKey : ", this.pKey)
log.Info("(Delete) theMap before : ", this.theMap)
_, pKeyValueHash := ConstructKey(this.pKey, data)
record := this.theMap[pKeyValueHash]
if nil != record {
delete(this.theMap, pKeyValueHash)
}
log.Info("(Delete) theMap after : ", this.theMap)
return record
}
func (this *InMemoryTable) RowCount() int {
return len(this.theMap)
}
func (this *InMemoryTable) Load(file *os.File) {
}
func (this *InMemoryTable) SaveSchema(file *os.File) {
}
func (this *InMemoryTable) SaveData(file *os.File) {
}
func (this *InMemoryTable) GenerateKeys(arr []string, data []string, start int, end int, index int, r int) {
log.Info("(GenerateKeys) GenerateKeys, index = ", index, ", r = ", r, ", arr", arr)
if index == r {
log.Info("(GenerateKeys) GenerateKeys, data = ", data)
key := make([]string, 0)
for j := 0; j < r; j++ {
key = append(key, data[j])
}
this.AddIndex(key)
return
}
i := start
for i <= end && end-i+1 >= r-index {
data[index] = arr[i]
this.GenerateKeys(arr, data, i+1, end, index+1, r)
i += 1
}
}