-
Notifications
You must be signed in to change notification settings - Fork 12
/
cfg_mem.go
179 lines (154 loc) · 3.62 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
// Copyright 2014-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
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
}