-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathmembackstore.go
184 lines (158 loc) · 4.29 KB
/
membackstore.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package asserts
import (
"sync"
)
type memoryBackstore struct {
top memBSBranch
mu sync.RWMutex
}
type memBSNode interface {
put(assertType *AssertionType, key []string, assert Assertion) error
get(key []string, maxFormat int) (Assertion, error)
search(hint []string, found func(Assertion), maxFormat int)
}
type memBSBranch map[string]memBSNode
type memBSLeaf map[string]map[int]Assertion
func (br memBSBranch) put(assertType *AssertionType, key []string, assert Assertion) error {
key0 := key[0]
down := br[key0]
if down == nil {
if len(key) > 2 {
down = make(memBSBranch)
} else {
down = make(memBSLeaf)
}
br[key0] = down
}
return down.put(assertType, key[1:], assert)
}
func (leaf memBSLeaf) cur(key0 string, maxFormat int) (a Assertion) {
for formatnum, a1 := range leaf[key0] {
if formatnum <= maxFormat {
if a == nil || a1.Revision() > a.Revision() {
a = a1
}
}
}
return a
}
func (leaf memBSLeaf) put(assertType *AssertionType, key []string, assert Assertion) error {
key0 := key[0]
cur := leaf.cur(key0, assertType.MaxSupportedFormat())
if cur != nil {
rev := assert.Revision()
curRev := cur.Revision()
if curRev >= rev {
return &RevisionError{Current: curRev, Used: rev}
}
}
if _, ok := leaf[key0]; !ok {
leaf[key0] = make(map[int]Assertion)
}
leaf[key0][assert.Format()] = assert
return nil
}
func (br memBSBranch) get(key []string, maxFormat int) (Assertion, error) {
key0 := key[0]
down := br[key0]
if down == nil {
return nil, ErrNotFound
}
return down.get(key[1:], maxFormat)
}
func (leaf memBSLeaf) get(key []string, maxFormat int) (Assertion, error) {
key0 := key[0]
cur := leaf.cur(key0, maxFormat)
if cur == nil {
return nil, ErrNotFound
}
return cur, nil
}
func (br memBSBranch) search(hint []string, found func(Assertion), maxFormat int) {
hint0 := hint[0]
if hint0 == "" {
for _, down := range br {
down.search(hint[1:], found, maxFormat)
}
return
}
down := br[hint0]
if down != nil {
down.search(hint[1:], found, maxFormat)
}
return
}
func (leaf memBSLeaf) search(hint []string, found func(Assertion), maxFormat int) {
hint0 := hint[0]
if hint0 == "" {
for key := range leaf {
cand := leaf.cur(key, maxFormat)
if cand != nil {
found(cand)
}
}
return
}
cur := leaf.cur(hint0, maxFormat)
if cur != nil {
found(cur)
}
}
// NewMemoryBackstore creates a memory backed assertions backstore.
func NewMemoryBackstore() Backstore {
return &memoryBackstore{
top: make(memBSBranch),
}
}
func (mbs *memoryBackstore) Put(assertType *AssertionType, assert Assertion) error {
mbs.mu.Lock()
defer mbs.mu.Unlock()
internalKey := make([]string, 1+len(assertType.PrimaryKey))
internalKey[0] = assertType.Name
for i, name := range assertType.PrimaryKey {
internalKey[1+i] = assert.HeaderString(name)
}
err := mbs.top.put(assertType, internalKey, assert)
return err
}
func (mbs *memoryBackstore) Get(assertType *AssertionType, key []string, maxFormat int) (Assertion, error) {
mbs.mu.RLock()
defer mbs.mu.RUnlock()
internalKey := make([]string, 1+len(assertType.PrimaryKey))
internalKey[0] = assertType.Name
copy(internalKey[1:], key)
return mbs.top.get(internalKey, maxFormat)
}
func (mbs *memoryBackstore) Search(assertType *AssertionType, headers map[string]string, foundCb func(Assertion), maxFormat int) error {
mbs.mu.RLock()
defer mbs.mu.RUnlock()
hint := make([]string, 1+len(assertType.PrimaryKey))
hint[0] = assertType.Name
for i, name := range assertType.PrimaryKey {
hint[1+i] = headers[name]
}
candCb := func(a Assertion) {
if searchMatch(a, headers) {
foundCb(a)
}
}
mbs.top.search(hint, candCb, maxFormat)
return nil
}