-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorage_table.go
530 lines (495 loc) · 9.7 KB
/
gorage_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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package Gorage
import (
"fmt"
"github.com/0x3alex/gorage/eval"
"strconv"
"strings"
)
const (
INT = 0
STRING = 1
BOOLEAN = 2
FLOAT = 3
DATE = 4
)
type Column struct {
Name string
Datatype int
}
type transaction struct {
q *queue
}
type Table struct {
Name string
Columns []Column
Rows [][]interface{}
host *Gorage
t transaction
p bool
}
func (g *Table) isDuplicate(hash uint32) bool {
for _, v := range g.Rows {
if hash == computeHash(v) {
return true
}
}
return false
}
func (g *Table) getColByType(t int) *Column {
if len(g.Columns) == 0 {
return nil
}
for _, v := range g.Columns {
if v.Datatype == t {
return &v
}
}
return nil
}
func (g *Table) sendExit() {
if g.t.q == nil {
g.t.q = newQueue(&data{
action: actionExit,
})
} else {
g.t.q.append(&data{
action: actionExit,
})
}
}
func (g *Table) getColAndIndexByName(name string) (*Column, int) {
if len(g.Columns) == 0 {
return nil, -1
}
for i, v := range g.Columns {
if name == v.Name {
if g.host.Log {
gprint("getColAndIndexByName", "Column: "+name+" found")
}
return &v, i
}
}
if g.host.Log {
gprint("getColAndIndexByName", "Column: "+name+" was not found")
}
return nil, -1
}
/*
the name is the column name
*/
func (g *Table) RemoveColumn(name string) *Table {
if !g.p {
return g.removeColumn(name)
}
c := make(chan *Table)
if g.t.q == nil {
g.t.q = newQueue(&data{
action: actionDeleteColumn,
payload: []interface{}{name},
c: c,
})
} else {
g.t.q.append(&data{
action: actionDeleteColumn,
payload: []interface{}{name},
c: c,
})
}
for {
select {
case e := <-c:
close(c)
return e
}
}
}
func (g *Table) removeColumn(name string) *Table {
c, idx := g.getColAndIndexByName(name)
if c == nil {
return g
}
g.Columns = append(g.Columns[:idx], g.Columns[idx+1:]...)
//remove cells
for i := 0; i < len(g.Rows); i++ {
g.Rows[i] = append(g.Rows[i][:idx], g.Rows[i][idx+1:]...)
}
return g
}
/*
name is the name of the column. The datatype can be choosen from the provieded and implemented datatypes (f.e. INT,STRING)
*/
func (g *Table) AddColumn(name string, datatype int) *Table {
if !g.p {
return g.addColumn(name, datatype)
}
c := make(chan *Table)
if g.t.q == nil {
g.t.q = newQueue(&data{
action: actionAddColumn,
payload: []interface{}{name, datatype},
c: c,
})
} else {
g.t.q.append(&data{
action: actionAddColumn,
payload: []interface{}{name, datatype},
c: c,
})
}
for {
select {
case e := <-c:
close(c)
return e
}
}
}
func (g *Table) addColumn(name string, datatype int) *Table {
if v, _ := g.getColAndIndexByName(name); v == nil {
g.Columns = append(g.Columns, Column{
name,
datatype,
})
if g.host.Log {
gprint("AddColumn", "Column: "+name+" added")
}
if len(g.Rows) != 0 {
if g.host.Log {
gprint("AddColumn", "Table has Rows, filling up the holes")
}
for i := 0; i < len(g.Rows); i++ {
g.Rows[i] = append(g.Rows[i], nil)
}
if g.host.Log {
gprint("AddColumn", "Filled up the holes")
}
}
} else {
if g.host.Log {
gprint("AddColumn", "Column: "+name+" was not added. Duplicate?")
}
}
return g
}
/*
f is the evaluate string. See github README.md for examples
*/
func (g *Table) Where(f string) *Table {
if !g.p {
return g.where(f)
}
c := make(chan *Table)
if g.t.q == nil {
g.t.q = newQueue(&data{
action: actionWhere,
payload: []interface{}{f},
c: c,
})
} else {
g.t.q.append(&data{
action: actionWhere,
payload: []interface{}{f},
c: c,
})
}
for {
select {
case e := <-c:
close(c)
return e
}
}
}
func (g *Table) where(f string) *Table {
res := &Table{
Name: g.Name,
Columns: g.Columns,
host: g.host,
Rows: [][]interface{}{},
}
split := strings.Split(f, " ")
for _, v := range g.Rows {
var tmp []string
for _, k := range split {
y := strings.Split(k, ":")
if len(y) > 1 {
col, colIdx := g.getColAndIndexByName(y[1])
if col == nil {
panic("Column not found")
}
switch col.Datatype {
case DATE:
if v[colIdx] == nil {
k = fmt.Sprintf("f")
}
k = fmt.Sprintf("%s", v[colIdx])
break
case STRING:
if v[colIdx] == nil {
k = fmt.Sprintf("f")
}
k = fmt.Sprintf("'%s'", v[colIdx])
break
case FLOAT, INT:
if v[colIdx] == nil {
k = fmt.Sprintf("f")
} else {
switch v[colIdx].(type) {
case float32:
println("IS THIS EVEN BEING USED ON MOST MACHINES?")
break
case float64:
k = strconv.FormatFloat(v[colIdx].(float64), 'f', -1, 64)
break
default:
k = fmt.Sprintf("%d", v[colIdx])
}
}
default:
k = fmt.Sprintf("%s", v[colIdx])
break
}
}
tmp = append(tmp, k)
}
q := strings.Join(tmp, " ")
if eval.Eval(q) {
res.Rows = append(res.Rows, v)
}
}
return res
}
/*
data is a map, where the key is the column and the interace is the value.
the datatype of the interface needs to match the datatype, which the column represents
*/
func (g *Table) Update(d map[string]interface{}) *Table {
if !g.p {
return g.update(d)
}
c := make(chan *Table)
if g.t.q == nil {
g.t.q = newQueue(&data{
action: actionUpdate,
payload: []interface{}{d},
c: c,
})
} else {
g.t.q.append(&data{
action: actionUpdate,
payload: []interface{}{d},
c: c,
})
}
for {
select {
case e := <-c:
return e
}
}
}
func (g *Table) Wait() {
for g.t.q.Head() != nil {
}
}
func (g *Table) update(data map[string]interface{}) *Table {
//g.Lock()
rtCopy := g.host.copyTable(g.Name)
var rt *Table
for i, v := range g.host.Tables {
if v.Name == g.Name {
rt = &g.host.Tables[i]
}
}
for _, v := range g.Rows {
for i, r := range rt.Rows {
if computeHash(v) != computeHash(r) {
if g.host.Log {
gprint("Update", fmt.Sprintf("Hash not matching %d != %d", computeHash(v), computeHash(r)))
}
continue
}
for key, val := range data {
c, index := rt.getColAndIndexByName(key)
if c == nil || !validateDatatype(val, *c) {
g.host.copyTableToTable(g.Name, &rtCopy)
return rt
}
rt.Rows[i][index] = val
if g.host.Log {
gprint("Update", "Updated cell")
}
}
}
}
return rt
}
/*
Deletes Rows
*/
func (g *Table) Delete() {
if !g.p {
g.delete()
return
}
c := make(chan *Table)
if g.t.q == nil {
g.t.q = newQueue(&data{
action: actionDelete,
payload: []interface{}{g},
c: c,
})
} else {
g.t.q.append(&data{
action: actionDelete,
payload: []interface{}{g},
c: c,
})
}
}
func (g *Table) delete() *Table {
realTable := g.host.FromTable(g.Name) // we need to get the table again to do persistent changes to it in memory
if realTable == nil {
panic("Table not found")
}
for idx, o := range realTable.Rows {
for _, i := range g.Rows {
if compareRows(o, i) {
if idx > len(realTable.Rows) {
return g
}
if idx == len(realTable.Rows) {
realTable.Rows = append(realTable.Rows[:idx-1])
} else {
realTable.Rows = append(realTable.Rows[:idx], realTable.Rows[idx+1:]...)
}
}
}
}
return g
}
func (g *Table) Sum(column string) float64 {
c, idx := g.getColAndIndexByName(column)
if c == nil || (c.Datatype != INT && c.Datatype != FLOAT) {
return 0
}
sum := 0.0
for _, row := range g.Rows {
v := row[idx]
if i, ok := v.(int); ok {
sum += float64(i)
} else if f, ok := v.(float64); ok {
sum += f
}
}
return sum
}
func (g *Table) Avg(column string) float64 {
return g.Sum(column) / float64(len(g.Rows))
}
/*
columns is a string array, in which the wanted columns are stored
*/
func (g *Table) Select(columns []string) *Table {
if !g.p {
return g._select(columns)
}
c := make(chan *Table)
if g.t.q == nil {
g.t.q = newQueue(&data{
action: actionSelect,
payload: []interface{}{columns},
c: c,
})
} else {
g.t.q.append(&data{
action: actionSelect,
payload: []interface{}{columns},
c: c,
})
}
for {
select {
case e := <-c:
close(c)
return e
}
}
}
func (g *Table) _select(columns []string) *Table {
var columnIdx []int
tmp := &Table{
Name: g.Name,
Columns: []Column{},
host: g.host,
Rows: [][]interface{}{},
}
for _, v := range columns {
col, i := g.getColAndIndexByName(v)
tmp.AddColumn(col.Name, col.Datatype)
columnIdx = append(columnIdx, i)
}
for _, v := range g.Rows {
var t []interface{}
for _, i := range columnIdx {
if i >= len(v) {
if g.host.Log {
gprint("Select", "temp column index is out of bounds. skipping")
}
continue
}
t = append(t, v[i])
}
tmp.Rows = append(tmp.Rows, t)
}
return tmp
}
/*
The data is the data that shall be inserted. The len(data) needs to match the number of columns.
If a cell shall be empty you can use nil.
*Remember*: You can not compare in nil value, when using the column in a where condition
*/
func (g *Table) Insert(d []interface{}) {
if !g.p {
g.insert(d)
return
}
c := make(chan *Table)
if g.t.q == nil {
g.t.q = newQueue(&data{
action: actionInsert,
payload: d,
c: c,
})
} else {
g.t.q.append(&data{
action: actionInsert,
payload: d,
c: c,
})
}
for {
select {
case <-c:
return
}
}
}
func (g *Table) insert(data []interface{}) *Table {
if len(data) != len(g.Columns) {
panic(fmt.Errorf("column count and data count are different"))
}
if !g.host.AllowDuplicated && g.isDuplicate(computeHash(data)) {
if g.host.Log {
gprint("Insert", "Data already exists in Table. Returning")
}
return g
}
for i, v := range g.Columns {
if !validateDatatype(data[i], v) {
return g
}
}
g.Rows = append(g.Rows, data)
return g
}