-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
131 lines (108 loc) · 2.81 KB
/
schema.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
package edb
import (
"fmt"
"reflect"
"strings"
)
var indexBuilderPtrType = reflect.TypeOf((*IndexBuilder)(nil))
var anyType = reflect.TypeOf((*any)(nil)).Elem()
var dataBucket = makeBucketName("data")
type Schema struct {
Name string
tables []*Table
tablesByLowerName map[string]*Table
tablesByRowType map[reflect.Type]*Table
maps []*KVMap
}
func (scm *Schema) init() {
if scm.tablesByLowerName == nil {
scm.tablesByLowerName = make(map[string]*Table)
scm.tablesByRowType = make(map[reflect.Type]*Table)
}
}
func (scm *Schema) Include(peer *Schema) {
scm.init()
for _, tbl := range peer.tables {
if scm.tablesByLowerName[strings.ToLower(tbl.name)] != nil {
panic(fmt.Errorf("table %s is defined in multiple schemas", tbl.name))
}
if scm.tablesByRowType[tbl.rowType] != nil {
panic(fmt.Errorf("row type %v is used in multiple schemas", tbl.rowType))
}
scm.addTable(tbl)
}
}
func (scm *Schema) addTable(tbl *Table) {
lower := strings.ToLower(tbl.name)
if scm.tablesByLowerName[lower] != nil {
panic(fmt.Errorf("table %s is already defined", tbl.name))
}
if scm.tablesByRowType[tbl.rowType] != nil {
panic(fmt.Errorf("row type %v is already used", tbl.rowType))
}
tbl.pos = len(scm.tables)
scm.tables = append(scm.tables, tbl)
scm.tablesByLowerName[lower] = tbl
scm.tablesByRowType[tbl.rowType] = tbl
scm.tablesByRowType[tbl.rowTypePtr] = tbl
}
func (scm *Schema) Tables() []*Table {
return append([]*Table(nil), scm.tables...)
}
func (scm *Schema) TableNamed(name string) *Table {
return scm.tablesByLowerName[strings.ToLower(name)]
}
func (scm *Schema) TableByRowType(rt reflect.Type) *Table {
tbl := scm.tablesByRowType[rt]
if tbl == nil {
panic(fmt.Errorf("no table defined for row type %v", rt))
}
return tbl
}
func (scm *Schema) TableByRow(row any) *Table {
rt := reflect.TypeOf(row)
if rt.Kind() == reflect.Ptr && rt.Elem().Kind() == reflect.Struct {
return scm.TableByRowType(rt)
} else {
panic(fmt.Errorf("expected pointer to a table row type, got %v", rt))
}
}
type bucketName []byte
func makeBucketName(name string) bucketName {
return bucketName(name)
}
func (bn bucketName) String() string {
return string(bn)
}
func (bn bucketName) Raw() []byte {
return []byte(bn)
}
type KVMap struct {
buck bucketName
}
func AddKVMap(scm *Schema, name string) *KVMap {
mp := &KVMap{
buck: makeBucketName(name),
}
scm.maps = append(scm.maps, mp)
return mp
}
func AddSingletonKey[T any](mp *KVMap, key string) *SKey {
sk := &SKey{
mp: mp,
keyBytes: []byte(key),
valueEnc: defaultValueEncoding,
}
return sk
}
type SKey struct {
mp *KVMap
keyBytes []byte
valueEnc encodingMethod
}
func (sk *SKey) String() string {
return sk.mp.buck.String() + "." + string(sk.keyBytes)
}
func (sk *SKey) Raw() []byte {
return sk.keyBytes
}