forked from couchbase/cbgt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfg_mem.go
182 lines (157 loc) · 3.82 KB
/
cfg_mem.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
// Copyright (c) 2014 Couchbase, 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
// 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 cbgt
import (
"fmt"
"math"
"sync"
)
const (
CFG_CAS_FORCE = math.MaxUint64
)
// CfgMem is a local-only, memory-only implementation of Cfg
// interface that's useful for development and testing.
type CfgMem struct {
m sync.Mutex
CASNext uint64
Entries map[string]*CfgMemEntry
subscriptions map[string][]chan<- CfgEvent // Keyed by key.
}
// CfgMemEntry is a CAS-Val pairing tracked by CfgMem.
type CfgMemEntry struct {
CAS uint64
Val []byte
rev interface{}
}
// NewCfgMem returns an empty CfgMem instance.
func NewCfgMem() *CfgMem {
return &CfgMem{
CASNext: 1,
Entries: make(map[string]*CfgMemEntry),
subscriptions: make(map[string][]chan<- CfgEvent),
}
}
func (c *CfgMem) Get(key string, cas uint64) (
[]byte, uint64, error) {
c.m.Lock()
defer c.m.Unlock()
entry, exists := c.Entries[key]
if !exists {
return nil, 0, nil
}
if cas != 0 && cas != entry.CAS {
return nil, 0, &CfgCASError{}
}
val := make([]byte, len(entry.Val))
copy(val, entry.Val)
return val, entry.CAS, nil
}
func (c *CfgMem) GetRev(key string, cas uint64) (
interface{}, error) {
c.m.Lock()
defer c.m.Unlock()
entry, exists := c.Entries[key]
if exists {
if cas == 0 || cas == entry.CAS {
return entry.rev, nil
}
return nil, &CfgCASError{}
}
return nil, nil
}
func (c *CfgMem) SetRev(key string, cas uint64, rev interface{}) error {
c.m.Lock()
defer c.m.Unlock()
entry, exists := c.Entries[key]
if cas == 0 || (exists && cas == entry.CAS) {
entry.rev = rev
c.Entries[key] = entry
return nil
}
return &CfgCASError{}
}
func (c *CfgMem) Set(key string, val []byte, cas uint64) (
uint64, error) {
c.m.Lock()
defer c.m.Unlock()
prevEntry, exists := c.Entries[key]
switch {
case cas == 0:
if exists {
return 0, fmt.Errorf("cfg_mem: entry already exists, key: %s", key)
}
case cas == CFG_CAS_FORCE:
break
default:
if !exists || cas != prevEntry.CAS {
return 0, &CfgCASError{}
}
}
nextEntry := &CfgMemEntry{
CAS: c.CASNext,
Val: make([]byte, len(val)),
}
copy(nextEntry.Val, val)
c.Entries[key] = nextEntry
c.CASNext += 1
c.fireEvent(key, nextEntry.CAS, nil)
return nextEntry.CAS, nil
}
func (c *CfgMem) Del(key string, cas uint64) error {
c.m.Lock()
defer c.m.Unlock()
if cas != 0 {
entry, exists := c.Entries[key]
if !exists || cas != entry.CAS {
return &CfgCASError{}
}
}
delete(c.Entries, key)
c.fireEvent(key, 0, nil)
return nil
}
func (c *CfgMem) Subscribe(key string, ch chan CfgEvent) error {
c.m.Lock()
defer c.m.Unlock()
a, exists := c.subscriptions[key]
if !exists || a == nil {
a = make([]chan<- CfgEvent, 0)
}
c.subscriptions[key] = append(a, ch)
return nil
}
func (c *CfgMem) FireEvent(key string, cas uint64, err error) {
c.m.Lock()
c.fireEvent(key, cas, err)
c.m.Unlock()
}
func (c *CfgMem) fireEvent(key string, cas uint64, err error) {
for _, c := range c.subscriptions[key] {
go func(c chan<- CfgEvent) {
c <- CfgEvent{
Key: key, CAS: cas, Error: err,
}
}(c)
}
}
func (c *CfgMem) Refresh() error {
c.m.Lock()
defer c.m.Unlock()
for key := range c.subscriptions {
entry, exists := c.Entries[key]
if exists && entry != nil {
c.fireEvent(key, entry.CAS, nil)
} else {
c.fireEvent(key, 0, nil)
}
}
return nil
}