-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtree.go
184 lines (143 loc) · 4.2 KB
/
btree.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
package streedb
import (
"cmp"
"github.com/google/btree"
)
type EntryFilterKind int
const (
PrimaryIndexFilterKind EntryFilterKind = iota
SecondaryIndexFilterKind
)
type EntryFilter interface {
Filter(Indexer) bool
Kind() EntryFilterKind
}
func AlwaysTrueIndexFilter() EntryFilter {
return &alwaysTrueIndexFilter{}
}
type alwaysTrueIndexFilter struct{}
func (a *alwaysTrueIndexFilter) Filter(c Indexer) bool {
return true
}
func (a *alwaysTrueIndexFilter) Kind() EntryFilterKind {
return PrimaryIndexFilterKind
}
func PrimaryIndexFilter(pIdx string) EntryFilter {
return &primaryIndexFilter{pIdx: pIdx}
}
type primaryIndexFilter struct{ pIdx string }
func (p *primaryIndexFilter) Filter(c Indexer) bool {
return p.pIdx == "" || c.PrimaryIndex() == p.pIdx
}
func (p *primaryIndexFilter) Kind() EntryFilterKind {
return PrimaryIndexFilterKind
}
func SecondaryIndexFilter[O cmp.Ordered](sIdx string) EntryFilter {
return &secondaryIndexFilter[O]{sIdx: sIdx}
}
type secondaryIndexFilter[O cmp.Ordered] struct{ sIdx string }
func (p *secondaryIndexFilter[O]) Filter(c Indexer) bool {
return p.sIdx == "" || c.SecondaryIndex() == p.sIdx
}
func (p *secondaryIndexFilter[O]) Kind() EntryFilterKind {
return SecondaryIndexFilterKind
}
func LLFComp[O cmp.Ordered, I cmp.Ordered](a, b *BtreeItem[O, I]) bool {
return a.Key < b.Key
}
func NewBtreeIndex[O cmp.Ordered, I cmp.Ordered](degree int, less btree.LessFunc[*BtreeItem[O, I]]) *BtreeIndex[O, I] {
return &BtreeIndex[O, I]{BTreeG: btree.NewG[*BtreeItem[O, I]](2, LLFComp)}
}
type BtreeItem[O cmp.Ordered, I cmp.Ordered] struct {
Key I
Val *LinkedList[O, *Fileblock[O]]
}
type BtreeIndex[O cmp.Ordered, I cmp.Ordered] struct {
*btree.BTreeG[*BtreeItem[O, I]]
}
func (b *BtreeIndex[O, I]) Get(i I) (*LinkedList[O, *Fileblock[O]], bool) {
item, found := b.BTreeG.Get(&BtreeItem[O, I]{Key: i})
if item == nil {
return nil, false
}
return item.Val, found
}
func (b *BtreeIndex[O, I]) Upsert(key I, value *Fileblock[O]) bool {
ll := &LinkedList[O, *Fileblock[O]]{}
ll.SetMin(value)
old, found := b.ReplaceOrInsert(&BtreeItem[O, I]{Key: key, Val: ll})
if !found {
return found
}
old.Val.SetMin(value)
b.ReplaceOrInsert(old)
return true
}
func (b *BtreeIndex[O, I]) AscendRangeWithFilters(min, max I, filters ...EntryFilter) (EntryIterator[O], bool, error) {
pFilters := make([]EntryFilter, 0)
for _, filter := range filters {
if filter.Kind() == PrimaryIndexFilterKind {
pFilters = append(pFilters, filter)
}
}
// When no primary index filter is provided, we provice a mock one
if len(pFilters) == 0 {
pFilters = append(pFilters, AlwaysTrueIndexFilter())
}
result, found, err := b.ascendRangeWithFilters(min, max, pFilters...)
if err != nil {
return nil, false, err
}
return newIteratorWithFilters(result, filters), found, nil
}
func (b *BtreeIndex[O, I]) ascendRangeWithFilters(min, max I, filters ...EntryFilter) ([]*Fileblock[O], bool, error) {
result := make([]*Fileblock[O], 0)
b.AscendRange(
&BtreeItem[O, I]{Key: min},
&BtreeItem[O, I]{Key: max},
func(item *BtreeItem[O, I]) bool {
for next := item.Val.head; next != nil; next = next.Next {
fileblock := next.Val
for _, filter := range filters {
if filter.Filter(fileblock) {
result = append(result, fileblock)
continue
}
}
}
return true
})
return result, len(result) > 0, nil
}
func (b *BtreeIndex[O, I]) ascendRange(pIdx, sIdx string, min, max I) ([]*Fileblock[O], bool, error) {
result := make([]*Fileblock[O], 0)
b.AscendRange(
&BtreeItem[O, I]{Key: min},
&BtreeItem[O, I]{Key: max},
func(item *BtreeItem[O, I]) bool {
for next := item.Val.head; next != nil; next = next.Next {
if pIdx == "" || next.Val.PrimaryIndex() == pIdx {
for _, c := range next.Val.Rows {
if sIdx == "" || c.SecondaryIdx == sIdx {
result = append(result, next.Val)
break
}
}
}
}
return true
})
return result, len(result) > 0, nil
}
func (b *BtreeIndex[O, I]) Remove(key I, value *Fileblock[O]) bool {
btItem, found := b.BTreeG.Get(&BtreeItem[O, I]{Key: key})
if !found {
return false
}
btItem.Val.Remove(value)
if btItem.Val.head == nil {
_, found := b.Delete(btItem)
return found
}
return true
}