-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalog.go
145 lines (118 loc) · 2.8 KB
/
catalog.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
package main
import (
"fmt"
"os"
)
// schema
// {
// "tables": [
// {
// "name": "tbl1",
// "cols": [
// {"name": "col1", "type": "string"},
// {"name": "col2", "type": "string"},
// {"name": "col3", "type": "string"}
// ]
// },
// {
// "name": "tbl2",
// "cols": [
// {"name": "col4", "type": "string"},
// {"name": "col5", "type": "string"},
// {"name": "col6", "type": "string"},
// ]
// }
// ]
// }
var catfile = "data/incdb.catalog"
func init() {
// test mode
if os.Getenv("INCDB_TEST") == "1" {
catfile = "data/test.incdb.catalog"
}
// initialize file if empty
f, err := os.OpenFile(catfile, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
fmt.Fprintf(os.Stderr, "could not read catalog 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 catalog 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 catalog file: %s\n", err)
os.Exit(1)
}
}
type CtCol struct {
Name string
Type string
}
type CtTable struct {
Name string
Cols []*CtCol
}
type Catalog struct {
Tables []*CtTable
}
func addTable(tbl string, cols []string, types []string) error {
if len(cols) != len(types) {
return fmt.Errorf("the length of cols and types must be the same")
}
if len(cols) == 0 {
return fmt.Errorf("table must have at least one column")
}
f, err := os.OpenFile(catfile, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return fmt.Errorf("open catalog file: %w", err)
}
defer f.Close()
c := Catalog{}
if err := readJsonFile(f, &c); err != nil {
return fmt.Errorf("read catalog file: %w", err)
}
for _, t := range c.Tables {
if t.Name == tbl {
return fmt.Errorf("table %s already exists in catalog", tbl)
}
}
cs := make([]*CtCol, len(cols))
for i := range cols {
if types[i] != "string" {
return fmt.Errorf("type must be 'string' but '%s'", types[i])
}
cs[i] = &CtCol{Name: cols[i], Type: types[i]}
}
c.Tables = append(c.Tables, &CtTable{
Name: tbl,
Cols: cs,
})
if err := updateJsonFile(f, &c); err != nil {
return fmt.Errorf("update catalog file: %w", err)
}
f.Sync()
return nil
}
func readCatalog(tbl string) (*CtTable, error) {
f, err := os.OpenFile(catfile, os.O_RDONLY|os.O_CREATE, 0755)
if err != nil {
return nil, fmt.Errorf("open catalog file: %w", err)
}
defer f.Close()
c := Catalog{}
if err := readJsonFile(f, &c); err != nil {
return nil, fmt.Errorf("read catalog file: %w", err)
}
for _, t := range c.Tables {
if t.Name == tbl {
return t, nil
}
}
return nil, fmt.Errorf("table '%s' not found in catalog", tbl)
}