-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_manager.go
97 lines (84 loc) · 2.61 KB
/
schema_manager.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
package jsonpack
import (
"sync"
"github.com/pkg/errors"
)
// schemaManager manages schema instances
type schemaManager struct {
schemas sync.Map // provides thread safety map
}
// newSchemaManager returns a new schema manager instance
func newSchemaManager() *schemaManager {
return &schemaManager{}
}
// getAll returns a cloned map of schema instances
func (s *schemaManager) getAllSchemas() map[string]*Schema {
schemas := make(map[string]*Schema)
s.schemas.Range(func(key, value interface{}) bool {
schemas[key.(string)] = value.(*Schema)
return true
})
return schemas
}
// getAllSchemaDefs returns a map which contains all existed schema definitions,
// key of map it schema name, and value of map is *SchemaDef.
func (s *schemaManager) getAllSchemaDefs() map[string]*SchemaDef {
schDefs := make(map[string]*SchemaDef)
s.schemas.Range(func(key, value interface{}) bool {
schema := value.(*Schema)
schDef, err := schema.GetSchemaDef()
if err == nil {
schDefs[key.(string)] = schDef
}
return true
})
return schDefs
}
// getAllSchemaDefTexts returns a map which contains all existed schema text definitions,
// key of map it schema name, and value of map is text format of schema definition, which
// presented as []byte.
func (s *schemaManager) getAllSchemaDefTexts() map[string][]byte {
schDefTexts := make(map[string][]byte)
s.schemas.Range(func(key, value interface{}) bool {
schema := value.(*Schema)
schDefTexts[key.(string)] = schema.GetSchemaDefText()
return true
})
return schDefTexts
}
// get returns schema instance in schema manager or returns nil if schema not found.
func (s *schemaManager) get(name string) *Schema {
instance, ok := s.schemas.Load(name)
if !ok {
return nil
}
return instance.(*Schema)
}
// add a new schema or or replace existing one, compile and store it in schema manager.
// It returns error if can't not build new schema and it will not add to schemna manager.
func (s *schemaManager) add(name string, v ...interface{}) (*Schema, error) {
schema := newSchema(name, v...)
err := schema.build()
if err != nil {
return nil, err
}
s.schemas.Delete(name)
s.schemas.Store(name, schema)
return schema, nil
}
// remove schema by name in schema manager, it returns *SchemaNonExistError error
// if schema doesn't exist.
func (s *schemaManager) remove(name string) error {
_, ok := s.schemas.LoadAndDelete(name)
if !ok {
return errors.WithStack(&SchemaNonExistError{name})
}
return nil
}
// reset removes all schema instance in schema manager.
func (s *schemaManager) reset() {
s.schemas.Range(func(key, value interface{}) bool {
s.schemas.Delete(key)
return true
})
}