-
Notifications
You must be signed in to change notification settings - Fork 159
/
base_store.go
125 lines (105 loc) · 3.39 KB
/
base_store.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
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package db
import (
"github.com/openziti/foundation/v2/errorz"
"github.com/openziti/storage/ast"
"github.com/openziti/storage/boltz"
"go.etcd.io/bbolt"
"strings"
)
type initializableStore interface {
boltz.Store
initializeLocal()
initializeLinked()
initializeIndexes(tx *bbolt.Tx, errorHolder errorz.ErrorHolder)
}
type Store[E boltz.ExtEntity] interface {
boltz.EntityStore[E]
initializableStore
}
type baseStore[E boltz.ExtEntity] struct {
stores *stores
*boltz.BaseStore[E]
}
func (store *baseStore[E]) addUniqueNameField() boltz.ReadIndex {
symbolName := store.AddSymbol(FieldName, ast.NodeTypeString)
return store.AddUniqueIndex(symbolName)
}
func (store *baseStore[E]) initializeIndexes(tx *bbolt.Tx, errorHolder errorz.ErrorHolder) {
store.InitializeIndexes(tx, errorHolder)
}
func (store *baseStore[E]) deleteEntityReferences(tx *bbolt.Tx, entity boltz.NamedExtEntity, rolesSymbol boltz.EntitySetSymbol) error {
idRef := entityRef(entity.GetId())
for _, policyHolderId := range store.GetRelatedEntitiesIdList(tx, entity.GetId(), rolesSymbol.GetStore().GetEntityType()) {
err := rolesSymbol.Map(tx, []byte(policyHolderId), func(ctx *boltz.MapContext) {
if ctx.ValueS() == idRef {
ctx.Delete()
}
})
if err != nil {
return err
}
}
return nil
}
func (store *baseStore[E]) getParentBucket(entity boltz.Entity, childBucket *boltz.TypedBucket) *boltz.TypedBucket {
parentBucket := store.GetParentStore().GetEntityBucket(childBucket.Tx(), []byte(entity.GetId()))
parentBucket.ErrorHolderImpl = childBucket.ErrorHolderImpl
return parentBucket
}
type NameIndexed interface {
GetNameIndex() boltz.ReadIndex
}
func (store *baseStore[E]) GetName(tx *bbolt.Tx, id string) *string {
symbol := store.GetSymbol(FieldName)
if symbol == nil {
return nil
}
_, val := symbol.Eval(tx, []byte(id))
if val != nil {
result := string(val)
return &result
}
return nil
}
func (store *baseStore[E]) getRoleAttributesCursorProvider(index boltz.SetReadIndex, values []string, semantic string) (ast.SetCursorProvider, error) {
if semantic == "" {
semantic = SemanticAllOf
}
if !isSemanticValid(semantic) {
return nil, errorz.NewFieldError("invalid semantic", FieldSemantic, semantic)
}
roles, ids, err := splitRolesAndIds(values)
if err != nil {
return nil, err
}
return func(tx *bbolt.Tx, forward bool) ast.SetCursor {
validIds := ast.NewTreeSet(forward)
for _, id := range ids {
if store.IsEntityPresent(tx, id) {
validIds.Add([]byte(id))
}
}
var rolesCursor ast.SetCursor
if strings.EqualFold(semantic, SemanticAllOf) {
rolesCursor = store.IteratorMatchingAllOf(index, roles)(tx, forward)
} else {
rolesCursor = store.IteratorMatchingAnyOf(index, roles)(tx, forward)
}
if validIds.Size() == 0 {
return rolesCursor
}
return ast.NewUnionSetCursor(rolesCursor, validIds.ToCursor(), forward)
}, nil
}