-
Notifications
You must be signed in to change notification settings - Fork 3
/
accessor.go
223 lines (185 loc) · 5.58 KB
/
accessor.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2023 Cisco Systems, Inc. and its affiliates
//
// 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
//
// http://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.
//
// SPDX-License-Identifier: Apache-2.0
package tenancy
import (
"container/list"
"context"
"errors"
"fmt"
"github.com/cisco-open/go-lanai/pkg/redis"
r "github.com/go-redis/redis/v8"
"github.com/google/uuid"
"strings"
)
const (
errTmplNotLoaded = `tenancy is not loaded`
)
//goland:noinspection GoNameStartsWithPackageName
type TenancyAccessor struct {
cachedRootID string
rc redis.Client
}
func newAccessor(rc redis.Client) *TenancyAccessor {
return &TenancyAccessor{
cachedRootID: "",
rc: rc,
}
}
func (a *TenancyAccessor) GetParent(ctx context.Context, tenantId string) (string, error) {
if !a.IsLoaded(ctx) {
return "", errors.New(errTmplNotLoaded)
}
gteValue := BuildSpsString(tenantId, IsChildrenOfPredict)
lteValue := BuildSpsString(tenantId, IsChildrenOfPredict, RedisZsetMaxByte)
zrange := &r.ZRangeBy{Min: ZInclusive(gteValue), Max: ZInclusive(lteValue)}
cmd := a.rc.ZRangeByLex(ctx, ZsetKey, zrange)
relations := cmd.Val()
if len(relations) == 0 {
return "", nil
} else if len(relations) > 1 {
return "", errors.New(fmt.Sprintf("Tenant should only have one parent, but tenant with Id %s has %d ", tenantId, len(relations)))
} else {
p, err := GetObjectOfSpo(relations[0])
return p, err
}
}
func (a *TenancyAccessor) GetChildren(ctx context.Context, tenantId string) ([]string, error) {
if !a.IsLoaded(ctx) {
return nil, errors.New(errTmplNotLoaded)
}
gteValue := BuildSpsString(tenantId, IsParentOfPredict)
lteValue := BuildSpsString(tenantId, IsParentOfPredict, RedisZsetMaxByte)
zrange := &r.ZRangeBy{Min: ZInclusive(gteValue), Max: ZInclusive(lteValue)}
cmd := a.rc.ZRangeByLex(ctx, ZsetKey, zrange)
var children = make([]string, len(cmd.Val()))
for i, relation := range cmd.Val() {
child, err := GetObjectOfSpo(relation)
if err != nil {
return nil, err
}
children[i] = child
}
return children, nil
}
func (a *TenancyAccessor) GetAncestors(ctx context.Context, tenantId string) ([]string, error) {
if !a.IsLoaded(ctx) {
return nil, errors.New(errTmplNotLoaded)
}
var ancestors = make([]string, 0)
p, err := a.GetParent(ctx, tenantId)
for p != "" && err == nil {
ancestors = append(ancestors, p)
p, err = a.GetParent(ctx, p)
}
if err != nil {
return nil, err
}
return ancestors, nil
}
func (a *TenancyAccessor) GetDescendants(ctx context.Context, tenantId string) ([]string, error) {
if !a.IsLoaded(ctx) {
return nil, errors.New(errTmplNotLoaded)
}
descendants := make([]string, 0)
idsToVisit := list.New()
idsToVisit.PushBack(tenantId)
for idsToVisit.Len() > 0 {
cmds, err := a.rc.Pipelined(ctx, func(pipeliner r.Pipeliner) error {
for idsToVisit.Len() > 0 {
id := idsToVisit.Front()
gteValue := BuildSpsString(id.Value.(string), IsParentOfPredict)
lteValue := BuildSpsString(id.Value.(string), IsParentOfPredict, RedisZsetMaxByte)
zrange := &r.ZRangeBy{Min: ZInclusive(gteValue), Max: ZInclusive(lteValue)}
pcmd := pipeliner.ZRangeByLex(ctx, ZsetKey, zrange)
if pcmd.Err() == nil {
idsToVisit.Remove(id)
} else {
return pcmd.Err()
}
}
return nil
})
if err != nil {
return nil, err
}
var children []string
for _, c := range cmds {
for _, relation := range c.(*r.StringSliceCmd).Val() {
child, err := GetObjectOfSpo(relation)
if err != nil {
return nil, err
}
children = append(children, child)
}
}
descendants = append(descendants, children...)
for _, child := range children {
idsToVisit.PushBack(child)
}
}
return descendants, nil
}
// GetRoot will return the root tenantID.
// Because the root tenantId won't change once system is started, we can cache
// it after first successful read.
func (a *TenancyAccessor) GetRoot(ctx context.Context) (string, error) {
if !a.IsLoaded(ctx) {
return "", errors.New(errTmplNotLoaded)
}
if a.cachedRootID != "" {
return a.cachedRootID, nil
}
cmd := a.rc.Get(ctx, RootTenantKey)
if cmd.Err() != nil {
a.cachedRootID = ""
} else {
a.cachedRootID = cmd.Val()
}
return a.cachedRootID, cmd.Err()
}
func (a *TenancyAccessor) IsLoaded(ctx context.Context) bool {
cmd := a.rc.Get(ctx, StatusKey)
if cmd.Err() != nil {
return false
}
return strings.HasPrefix(cmd.Val(), STATUS_LOADED)
}
func (a *TenancyAccessor) GetTenancyPath(ctx context.Context, tenantId string) ([]uuid.UUID, error) {
current, err := uuid.Parse(tenantId)
if err != nil {
return nil, err
}
path := []uuid.UUID{current}
ancestors, err := a.GetAncestors(ctx, tenantId)
if err != nil {
return nil, err
}
for _, str := range ancestors {
id, err := uuid.Parse(str)
if err != nil {
return nil, err
}
path = append(path, id)
}
//reverse the order to that the result is root tenant id -> current tenant id
//fi is index going forward starting from 0,
//ri is index going backward starting from last element
//swap the element at ri and ri
for fi, ri := 0, len(path)-1; fi < ri; fi, ri = fi+1, ri-1 {
path[fi], path[ri] = path[ri], path[fi]
}
return path, nil
}