-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemaindex.go
67 lines (55 loc) · 1.3 KB
/
schemaindex.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"
"strings"
"go.etcd.io/bbolt"
)
type Index struct {
table *Table
pos int // index in table.indices, unstable across code changes
name string
buck bucketName
recType reflect.Type
keyEnc *flatEncoding
isUnique bool
filler func(row any, ib *IndexBuilder)
}
func makeIndexBucketName(name string) bucketName {
return makeBucketName("i_" + name)
}
func AddIndex[T any](name string) *Index {
recType := reflect.TypeOf((*T)(nil)).Elem()
idx := &Index{
name: name,
buck: makeIndexBucketName(name),
recType: recType,
keyEnc: flatEncodingOf(recType),
}
return idx
}
func (idx *Index) requireTable() {
if idx.table == nil {
panic(fmt.Errorf("index %q was not added to a table", idx.name))
}
}
func (idx *Index) ShortName() string {
return idx.name
}
func (idx *Index) FullName() string {
idx.requireTable()
return idx.table.name + "." + idx.name
}
func (idx *Index) Unique() *Index {
idx.isUnique = true
return idx
}
func (idx *Index) bucketIn(tableRootB *bbolt.Bucket) *bbolt.Bucket {
return nonNil(tableRootB.Bucket(idx.buck.Raw()))
}
func (idx *Index) keyTupleToString(indexKeyTup tuple) string {
return strings.Join(idx.keyEnc.tupleToStrings(indexKeyTup), "|")
}
func (idx *Index) keyType() reflect.Type {
return idx.keyEnc.typ
}