-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema_impl.go
300 lines (265 loc) · 6.32 KB
/
schema_impl.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
// Copyright (C) 2022 PuzzleDB Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package document
import "strings"
// Schema format (version 1)
//
// map[uint8]any
// 0: uint8 - version
// 1: string - name
// 2: elements - []map[uint8]any
// 1: name - string
// 2: type - uint8
// 3: indexes - []map[uint8]any
// 1: name - string
// 2: type - uint8
// 3: elements - []string (element name)
const (
// SchemaVersion specifies a latest schema version.
SchemaVersion = 1
)
const (
schemaVersionIdx = 0
schemaNameIdx = 1
schemaElementsIdx = 2
schemaIndexesIdx = 3
)
type schemaMap = map[uint8]any
type schema struct {
data schemaMap
elements []Element
indexes []Index
}
// NewSchema returns a blank schema.
func NewSchema() Schema {
s := &schema{
data: schemaMap{},
elements: []Element{},
indexes: []Index{},
}
s.SetVersion(SchemaVersion)
s.data[schemaElementsIdx] = []elementMap{}
s.data[schemaIndexesIdx] = []indexMap{}
return s
}
// NewSchemaWith creates a schema from the specified object.
func NewSchemaWith(obj any) (Schema, error) {
smap, ok := schemaMapFrom(obj)
if !ok {
return nil, newErrSchemaInvalid(obj)
}
s := &schema{
data: smap,
elements: []Element{},
indexes: []Index{},
}
return s, s.updateCashes()
}
func (s *schema) updateCashes() error {
// Caches elements
ems, ok := s.elementMaps()
if !ok {
return newErrElementMapNotExist()
}
s.elements = []Element{}
for _, em := range ems {
e, err := newElementWith(em)
if err != nil {
return err
}
s.elements = append(s.elements, e)
}
// Caches indexes
ims, ok := s.indexMpas()
if !ok {
return newErrIndexMapNotExist()
}
s.indexes = []Index{}
for _, im := range ims {
i, err := newIndexWith(s, im)
if err != nil {
return err
}
s.indexes = append(s.indexes, i)
}
return nil
}
// SetVersion sets the specified version to the schema.
func (s *schema) SetVersion(ver int) {
s.data[schemaVersionIdx] = ver
}
// Version returns the schema version.
func (s *schema) Version() int {
v, ok := s.data[schemaVersionIdx]
if !ok {
return 0
}
switch ver := v.(type) {
case int:
return int(ver)
default:
return 0
}
}
// SetName sets the specified name to the schema.
func (s *schema) SetName(name string) {
s.data[schemaNameIdx] = name
}
// Name returns the schema name.
func (s *schema) Name() string {
v, ok := s.data[schemaNameIdx]
if !ok {
return ""
}
switch name := v.(type) {
case string:
return name
default:
return ""
}
}
func (s *schema) elementMaps() ([]elementMap, bool) {
v, ok := s.data[schemaElementsIdx]
if !ok {
return nil, false
}
ems, ok := schemaMapsFrom(v)
if !ok {
return nil, false
}
return ems, true
}
// AddElement adds the specified element to the schema.
func (s *schema) AddElement(elem Element) error {
ems, ok := s.elementMaps()
if !ok {
return newErrElementMapNotExist()
}
em, ok := elem.Data().(elementMap)
if !ok {
return newErrElementMapNotExist()
}
s.data[schemaElementsIdx] = append(ems, em)
// Add element to cache
s.elements = append(s.elements, elem)
return nil
}
// DropElement drops the specified element from the schema.
func (s *schema) DropElement(name string) error {
ems, ok := s.elementMaps()
if !ok {
return newErrElementMapNotExist()
}
for i, em := range ems {
emName, ok := em[elementNameIdx].(string)
if ok && strings.EqualFold(emName, name) {
s.data[schemaElementsIdx] = append(ems[:i], ems[i+1:]...)
return s.updateCashes()
}
}
return newErrElementNotExistError(name)
}
// Elements returns the schema elements.
func (s *schema) Elements() Elements {
return s.elements
}
// FindElement returns the schema elements by the specified name.
func (s *schema) FindElement(name string) (Element, error) {
es := s.Elements()
for _, e := range es {
if strings.EqualFold(e.Name(), name) {
return e, nil
}
}
return nil, newErrElementNotExistError(name)
}
func (s *schema) indexMpas() ([]indexMap, bool) {
v, ok := s.data[schemaIndexesIdx]
if !ok {
return nil, false
}
ims, ok := schemaMapsFrom(v)
if !ok {
return nil, false
}
return ims, true
}
// AddIndex adds the specified index to the schema.
func (s *schema) AddIndex(idx Index) error {
ims, ok := s.indexMpas()
if !ok {
return newErrIndexMapNotExist()
}
im, ok := idx.Data().(indexMap)
if !ok {
return newErrIndexMapNotExist()
}
s.data[schemaIndexesIdx] = append(ims, im)
// Add index to cache
s.indexes = append(s.indexes, idx)
return nil
}
// DropIndex drops the specified index from the schema.
func (s *schema) DropIndex(name string) error {
ims, ok := s.indexMpas()
if !ok {
return newErrIndexMapNotExist()
}
for i, im := range ims {
imName, ok := im[indexNameIdx].(string)
if ok && strings.EqualFold(imName, name) {
s.data[schemaIndexesIdx] = append(ims[:i], ims[i+1:]...)
return s.updateCashes()
}
}
return newErrIndexNotExist(name)
}
// Indexes returns the schema indexes.
func (s *schema) Indexes() Indexes {
return s.indexes
}
// FindIndex returns the schema index by the spacified name.
func (s *schema) FindIndex(name string) (Index, error) {
idxes := s.indexes
for _, idx := range idxes {
if strings.EqualFold(idx.Name(), name) {
return idx, nil
}
}
return nil, newErrIndexNotExist(name)
}
// PrimaryIndex returns the schema primary index.
func (s *schema) PrimaryIndex() (Index, error) {
for _, idx := range s.indexes {
if idx.Type() == PrimaryIndex {
return idx, nil
}
}
return nil, NewErrPrimaryIndexNotExist()
}
// SecondaryIndexes returns the schema secondary indexes.
func (s *schema) SecondaryIndexes() (Indexes, error) {
secIdxes := []Index{}
for _, idx := range s.indexes {
if idx.Type() != SecondaryIndex {
continue
}
secIdxes = append(secIdxes, idx)
}
return secIdxes, nil
}
// Data returns the raw representation data in memory.
func (s *schema) Data() any {
return s.data
}