-
Notifications
You must be signed in to change notification settings - Fork 8
/
namespace.go
158 lines (129 loc) · 3.72 KB
/
namespace.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 2020 Delving B.V.
//
// 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.
package memory
import (
"fmt"
"sync"
"github.com/delving/hub3/ikuzo/domain"
)
// NameSpaceStore is the default namespace.Store for namespace.Service.
//
// Note: mutations in this store are ephemeral.
type NameSpaceStore struct {
sync.RWMutex
prefix2base map[string]*domain.Namespace
base2prefix map[string]*domain.Namespace
namespaces map[string]*domain.Namespace
}
// NewNameSpaceStore creates an in-memory namespace.Store.
func NewNameSpaceStore() *NameSpaceStore {
return &NameSpaceStore{
prefix2base: make(map[string]*domain.Namespace),
base2prefix: make(map[string]*domain.Namespace),
namespaces: make(map[string]*domain.Namespace),
}
}
// Len returns the number of stored namespaces.
// Alternatives Base or Prefixes don't count towards the total.
func (ms *NameSpaceStore) Len() int {
return len(ms.namespaces)
}
// Put stores the NameSpace in the Store
func (ms *NameSpaceStore) Put(ns *domain.Namespace) error {
if ns == nil {
return fmt.Errorf("cannot store empty namespace")
}
// this implementation of Delete can never return an error
_ = ms.Delete(ns.GetID())
// if you lock earlier you get a deadlock
ms.Lock()
defer ms.Unlock()
for _, prefix := range ns.Prefixes() {
ms.prefix2base[prefix] = ns
}
for _, base := range ns.BaseURIs() {
ms.base2prefix[base] = ns
}
id := ns.GetID()
ms.namespaces[id] = ns
return nil
}
func (ms *NameSpaceStore) delete(ns *domain.Namespace) error {
return ms.Delete(ns.GetID())
}
// Delete removes a NameSpace from the store.
//
// When the Namespace is not found it returns an domain.ErrNameSpaceNotFound error.
func (ms *NameSpaceStore) Delete(id string) error {
ms.Lock()
defer ms.Unlock()
ns, ok := ms.namespaces[id]
if !ok {
return domain.ErrNameSpaceNotFound
}
delete(ms.namespaces, id)
// drop all prefixes
for _, p := range ns.Prefixes() {
_, ok := ms.prefix2base[p]
if ok {
delete(ms.prefix2base, p)
}
}
// drop all base-URIs
for _, b := range ns.BaseURIs() {
_, ok := ms.base2prefix[b]
if ok {
delete(ms.base2prefix, b)
}
}
return nil
}
func (ms *NameSpaceStore) Get(id string) (*domain.Namespace, error) {
ns, ok := ms.namespaces[id]
if !ok {
return nil, domain.ErrNameSpaceNotFound
}
return ns, nil
}
// GetWithPrefix returns a NameSpace from the store if the prefix is found.
func (ms *NameSpaceStore) GetWithPrefix(prefix string) (*domain.Namespace, error) {
ms.RLock()
defer ms.RUnlock()
ns, ok := ms.prefix2base[prefix]
if !ok {
return nil, domain.ErrNameSpaceNotFound
}
return ns, nil
}
// GetWithBase returns a NameSpace from the store if the base URI is found.
func (ms *NameSpaceStore) GetWithBase(base string) (*domain.Namespace, error) {
ms.RLock()
defer ms.RUnlock()
ns, ok := ms.base2prefix[base]
if !ok {
return nil, domain.ErrNameSpaceNotFound
}
return ns, nil
}
// List returns a list of all the stored NameSpace objects.
// An error is only returned when the underlying datastructure is unavailable.
func (ms *NameSpaceStore) List() ([]*domain.Namespace, error) {
namespaces := []*domain.Namespace{}
for _, ns := range ms.namespaces {
if ns != nil {
namespaces = append(namespaces, ns)
}
}
return namespaces, nil
}