-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablespace.go
185 lines (152 loc) · 4.1 KB
/
tablespace.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
package main
import (
"fmt"
"os"
)
// schema
// {
// "tbl1": [
// {"col1": "val1", "col2": "val2", "col3": "val3"},
// {"col1": "val4", "col2": "val5", "col3": "val6"},
// {"col1": "val1", "col2": "val9", "col3": "val9"},
// ],
// "tbl2": [
// {"col4": "val1", "col5": "val2", "col6": "val3"},
// {"col4": "val4", "col5": "val5", "col6": "val6"},
// {"col4": "val1", "col5": "val9", "col6": "val9"},
// ]
// }
var datafile = "data/incdb.data"
func init() {
// test mode
if os.Getenv("INCDB_TEST") == "1" {
datafile = "data/test.incdb.data"
}
// initialize file if empty
f, err := os.OpenFile(datafile, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
fmt.Fprintf(os.Stderr, "could not read data file for initialization: %s\n", err)
os.Exit(1)
}
defer f.Close()
info, err := f.Stat()
if err != nil {
fmt.Fprintf(os.Stderr, "could not read data file stat for initialization: %s\n", err)
os.Exit(1)
}
if info.Size() != 0 {
return
}
if _, err := f.Write([]byte("{}")); err != nil {
fmt.Fprintf(os.Stderr, "could not initialize empty data file: %s\n", err)
os.Exit(1)
}
}
func readData(tbl string) ([]*Record, error) {
f, err := os.OpenFile(datafile, os.O_RDONLY|os.O_CREATE, 0755)
if err != nil {
return nil, fmt.Errorf("open tablespace file: %w", err)
}
defer f.Close()
d := map[string][]map[string]string{}
if err := readJsonFile(f, &d); err != nil {
return nil, fmt.Errorf("read file: %w", err)
}
t, ok := d[tbl]
if !ok {
return nil, fmt.Errorf("table '%s' not found", tbl)
}
tDef, err := readCatalog(tbl)
if err != nil {
return nil, fmt.Errorf("read table '%s' definition from catalog: %w", tbl, err)
}
records := make([]*Record, len(t))
for i := range t {
r := &Record{
Cols: make([]string, len(tDef.Cols)),
Types: make([]string, len(tDef.Cols)),
Vals: make([]string, len(tDef.Cols)),
}
for j := range tDef.Cols {
r.Cols[j] = tDef.Cols[j].Name
r.Types[j] = tDef.Cols[j].Type
r.Vals[j] = t[i][tDef.Cols[j].Name]
}
records[i] = r
}
return records, nil
}
func save(tbl string, cols, vals []string) error {
f, err := os.OpenFile(datafile, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return fmt.Errorf("open tablespace file: %w", err)
}
defer f.Close()
d := map[string][]map[string]string{}
if err := readJsonFile(f, &d); err != nil {
return fmt.Errorf("read file: %w", err)
}
if _, ok := d[tbl]; !ok {
return fmt.Errorf("table '%s' not found", tbl)
}
tDef, err := readCatalog(tbl)
if err != nil {
return fmt.Errorf("read catalog: %w", err)
}
r := map[string]string{}
if len(cols) != 0 {
// in case at least one column is specified, data will be saved on the given columns
for i := range cols {
givenCol := cols[i]
found := false
for j := range tDef.Cols {
colDef := tDef.Cols[j]
// fill specified column value
if colDef.Name == givenCol {
r[colDef.Name] = vals[i]
found = true
break
}
}
// Column not found in table definition
// This means the column name is incorrect
if !found {
return fmt.Errorf("column '%s' is not found in table '%s'", givenCol, tbl)
}
}
} else {
// if column is not specified, all the data must be given
if len(vals) != len(tDef.Cols) {
return fmt.Errorf("%d values must be passed according to the table definition", len(tDef.Cols))
}
for i := range tDef.Cols {
r[tDef.Cols[i].Name] = vals[i]
}
}
d[tbl] = append(d[tbl], r)
if err := updateJsonFile(f, &d); err != nil {
return fmt.Errorf("update tablespace file: %w", err)
}
f.Sync()
return nil
}
func createTable(tbl string) error {
f, err := os.OpenFile(datafile, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return fmt.Errorf("open tablespace file: %w", err)
}
defer f.Close()
d := map[string][]map[string]string{}
if err := readJsonFile(f, &d); err != nil {
return fmt.Errorf("read tablespace file: %w", err)
}
if _, ok := d[tbl]; ok {
return fmt.Errorf("table '%s' already exists", tbl)
}
d[tbl] = []map[string]string{}
if err := updateJsonFile(f, &d); err != nil {
return fmt.Errorf("update tablespace file: %w", err)
}
f.Sync()
return nil
}