-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
137 lines (124 loc) · 3.31 KB
/
config.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
package ds
import (
"fmt"
"go.etcd.io/bbolt"
)
// Config describes ds table configuration
type Config struct {
Fields []Field
TypeOf string
PrimaryKey string
Indexes []string
Uniques []string
LastInsertIndex uint64
Version int
}
func getTableOptions(tablePath string) (*Options, error) {
t, err := bbolt.Open(tablePath, 0644, nil)
if err != nil {
return nil, err
}
defer t.Close()
var optionsData []byte
err = t.View(func(tx *bbolt.Tx) error {
configBucket := tx.Bucket(configKey)
optionsData = configBucket.Get(optionsKey)
return nil
})
if err != nil {
return nil, err
}
if len(optionsData) == 0 {
return nil, nil
}
return gobDecodeOptions(optionsData)
}
func (table *Table) getConfig(tx *bbolt.Tx) (*Config, error) {
configData := tx.Bucket(configKey).Get(configKey)
if configData == nil {
table.log.Error("No config present for table")
return nil, fmt.Errorf("no config present for table")
}
config, err := gobDecodeConfig(configData)
if err != nil {
table.log.Error("Error decoding table config: %s", err.Error())
return nil, err
}
return config, nil
}
func (config *Config) update(tx *bbolt.Tx) error {
bucket := tx.Bucket(configKey)
data, err := gobEncode(*config)
if err != nil {
return err
}
return bucket.Put(configKey, data)
}
func (table *Table) initializeConfig(tx *bbolt.Tx, force bool) error {
configBucket, err := tx.CreateBucketIfNotExists(configKey)
if err != nil {
table.log.Error("Error creating config bucket: %s", err.Error())
return err
}
configData := configBucket.Get(configKey)
if configData == nil {
// New Table
config := Config{
Fields: table.getFields(),
TypeOf: table.typeOf.Name(),
PrimaryKey: table.primaryKey,
Indexes: table.indexes,
Uniques: table.uniques,
LastInsertIndex: 0,
Version: currentDSSchemaVersion,
}
data, err := gobEncode(config)
if err != nil {
return err
}
if err := configBucket.Put(configKey, data); err != nil {
return err
}
} else {
// Existing Table
config, err := gobDecodeConfig(configData)
if err != nil {
return err
}
if err := compareFields(table.getFields(), config.Fields); err != nil {
table.log.Error("%s", err.Error())
return err
}
if config.Version > currentDSSchemaVersion {
table.log.Error("Unable to register existing table %s as it's from a newer version of DS. %d > %d", table.Name, config.Version, currentDSSchemaVersion)
return fmt.Errorf("table is from newer version of ds")
}
table.log.Debug("TypeOf matches")
if !force && config.PrimaryKey != table.primaryKey {
table.log.Error("Cannot change primary key of table")
return fmt.Errorf("cannot change primary key of table")
}
table.log.Debug("PrimaryKey matches")
}
optionsData := configBucket.Get(optionsKey)
if optionsData == nil {
data, err := gobEncode(table.options)
if err != nil {
return err
}
if err := configBucket.Put(optionsKey, data); err != nil {
return err
}
} else {
options, err := gobDecodeOptions(optionsData)
if err != nil {
return err
}
if !options.compare(table.options) {
table.log.Error("Cannot change options of existing table")
return fmt.Errorf("cannot change options of existing table")
}
table.log.Debug("Config matches")
}
return nil
}