-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemabuilder.go
67 lines (57 loc) · 1.61 KB
/
schemabuilder.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
package edb
import (
"fmt"
"reflect"
)
type TableBuilder[Row, Key any] struct {
tbl *Table
}
func DefineTable[Row, Key any](scm *Schema, name string, f func(b *TableBuilder[Row, Key])) *Table {
scm.init()
rowPtrType := reflect.TypeFor[*Row]()
if rowPtrType.Elem().Kind() != reflect.Struct {
panic(fmt.Sprintf("DefineTable(%s): Row must be a struct", name))
}
tbl := &Table{
schema: scm,
name: name,
latestSchemaVer: 1,
buck: makeBucketName(name),
rowTypePtr: rowPtrType,
rowType: rowPtrType.Elem(),
rowInfo: reflectTypeWithoutCache(rowPtrType),
valueEnc: defaultValueEncoding,
keyStringSep: "|",
indicesByName: make(map[string]*Index),
indexer: nopIndexer,
}
tbl.keyType = tbl.rowInfo.keyField.Type
tbl.keyEnc = flatEncodingOf(tbl.keyType)
scm.addTable(tbl)
tbl.zeroKey = tbl.keyEnc.encode(nil, reflect.Zero(tbl.keyType))
b := TableBuilder[Row, Key]{
tbl: tbl,
}
f(&b)
return tbl
}
func (b *TableBuilder[Row, Key]) Indexer(f func(row *Row, ib *IndexBuilder)) {
b.tbl.indexer = func(row any, ib *IndexBuilder) {
f(row.(*Row), ib)
}
}
func (b *TableBuilder[Row, Key]) Migrate(f func(tx *Tx, row *Row, oldVer uint64)) {
b.tbl.migrator = func(tx *Tx, row any, oldVer uint64) {
f(tx, row.(*Row), oldVer)
}
}
func (b *TableBuilder[Row, Key]) AddIndex(idx *Index) {
b.tbl.AddIndex(idx)
}
func (b *TableBuilder[Row, Key]) SetSchemaVersion(ver uint64) {
b.tbl.latestSchemaVer = ver
}
func (b *TableBuilder[Row, Key]) SuppressContentWhenLogging() {
b.tbl.suppressContent = true
}
func nopIndexer(row any, ib *IndexBuilder) {}