-
Notifications
You must be signed in to change notification settings - Fork 142
/
service_store.go
158 lines (133 loc) · 4.67 KB
/
service_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
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
/*
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/storage/ast"
"github.com/openziti/storage/boltz"
"github.com/openziti/ziti/controller/xt"
"github.com/openziti/ziti/controller/xt_smartrouting"
"go.etcd.io/bbolt"
"time"
)
const (
EntityTypeServices = "services"
FieldServiceTerminatorStrategy = "terminatorStrategy"
FieldServiceMaxIdleTime = "maxIdleTime"
)
type Service struct {
boltz.BaseExtEntity
Name string `json:"name"`
MaxIdleTime time.Duration `json:"maxIdleTime"`
TerminatorStrategy string `json:"terminatorStrategy"`
}
func (entity *Service) GetEntityType() string {
return EntityTypeServices
}
func (entity *Service) GetName() string {
return entity.Name
}
type ServiceStore interface {
boltz.EntityStore[*Service]
boltz.EntityStrategy[*Service]
GetNameIndex() boltz.ReadIndex
FindByName(tx *bbolt.Tx, name string) (*Service, error)
}
func newServiceStore(stores *stores) *serviceStoreImpl {
store := &serviceStoreImpl{}
store.baseStore = baseStore[*Service]{
stores: stores,
BaseStore: boltz.NewBaseStore(NewStoreDefinition[*Service](store)),
}
store.InitImpl(store)
return store
}
type serviceStoreImpl struct {
baseStore[*Service]
indexName boltz.ReadIndex
terminatorsSymbol boltz.EntitySetSymbol
}
func (store *serviceStoreImpl) initializeLocal() {
store.AddExtEntitySymbols()
symbolName := store.AddSymbol(FieldName, ast.NodeTypeString)
store.indexName = store.AddUniqueIndex(symbolName)
store.AddSymbol(FieldServiceTerminatorStrategy, ast.NodeTypeString)
store.terminatorsSymbol = store.AddFkSetSymbol(EntityTypeTerminators, store.stores.terminator)
}
func (store *serviceStoreImpl) initializeLinked() {
}
func (store *serviceStoreImpl) GetNameIndex() boltz.ReadIndex {
return store.indexName
}
func (store *serviceStoreImpl) NewEntity() *Service {
return &Service{}
}
func (store *serviceStoreImpl) FillEntity(entity *Service, bucket *boltz.TypedBucket) {
entity.LoadBaseValues(bucket)
entity.Name = bucket.GetStringOrError(FieldName)
entity.TerminatorStrategy = bucket.GetStringWithDefault(FieldServiceTerminatorStrategy, "")
entity.MaxIdleTime = time.Duration(bucket.GetInt64WithDefault(FieldServiceMaxIdleTime, 0))
}
func (store *serviceStoreImpl) PersistEntity(entity *Service, ctx *boltz.PersistContext) {
entity.SetBaseValues(ctx)
ctx.SetString(FieldName, entity.Name)
ctx.SetInt64(FieldServiceMaxIdleTime, int64(entity.MaxIdleTime))
if entity.TerminatorStrategy == "" {
entity.TerminatorStrategy = xt_smartrouting.Name
}
_, changed := ctx.GetAndSetString(FieldServiceTerminatorStrategy, entity.TerminatorStrategy)
if changed {
strategy, err := xt.GlobalRegistry().GetStrategy(entity.TerminatorStrategy)
if err != nil {
ctx.Bucket.SetError(err)
return
}
if !ctx.IsCreate {
serviceStore := ctx.Store.(*serviceStoreImpl)
terminators, err := serviceStore.getTerminators(ctx.Bucket.Tx(), entity.Id)
if !ctx.Bucket.SetError(err) {
event := xt.NewStrategyChangeEvent(entity.Id, nil, terminators, nil, nil)
ctx.Bucket.SetError(strategy.HandleTerminatorChange(event))
}
}
}
}
func (store *serviceStoreImpl) FindByName(tx *bbolt.Tx, name string) (*Service, error) {
id := store.indexName.Read(tx, []byte(name))
if id != nil {
entity, _, err := store.FindById(tx, string(id))
return entity, err
}
return nil, nil
}
func (store *serviceStoreImpl) DeleteById(ctx boltz.MutateContext, id string) error {
terminatorIds := store.GetRelatedEntitiesIdList(ctx.Tx(), id, EntityTypeTerminators)
for _, terminatorId := range terminatorIds {
if err := store.stores.terminator.DeleteById(ctx, terminatorId); err != nil {
return err
}
}
return store.BaseStore.DeleteById(ctx, id)
}
func (store *serviceStoreImpl) getTerminators(tx *bbolt.Tx, serviceId string) ([]xt.Terminator, error) {
var terminators []xt.Terminator
for _, tId := range store.GetRelatedEntitiesIdList(tx, serviceId, EntityTypeTerminators) {
terminator, _, err := store.stores.terminator.FindById(tx, tId)
if err != nil {
return nil, err
}
if terminator != nil {
terminators = append(terminators, terminator)
}
}
return terminators, nil
}