summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-store/kv/state.go
blob: 20a3e951d9a1f95e8b899f5eb50988bb6141924a (plain)
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
package kv

import (
	"io"
	"sync"

	"codeberg.org/gruf/go-errors"
)

var ErrStateClosed = errors.New("store/kv: state closed")

// StateRO provides a read-only window to the store. While this
// state is active during the Read() function window, the entire
// store will be read-locked. The state is thread-safe for concurrent
// use UNTIL the moment that your supplied function to Read() returns,
// then the state has zero guarantees
type StateRO struct {
	store *KVStore
	mutex sync.RWMutex
}

func (st *StateRO) Get(key string) ([]byte, error) {
	// Get state read lock
	st.mutex.RLock()
	defer st.mutex.RUnlock()

	// Check not closed
	if st.store == nil {
		return nil, ErrStateClosed
	}

	// Pass request to store
	return st.store.get(key)
}

func (st *StateRO) GetStream(key string) (io.ReadCloser, error) {
	// Get state read lock
	st.mutex.RLock()
	defer st.mutex.RUnlock()

	// Check not closed
	if st.store == nil {
		return nil, ErrStateClosed
	}

	// Pass request to store
	return st.store.getStream(key)
}

func (st *StateRO) Has(key string) (bool, error) {
	// Get state read lock
	st.mutex.RLock()
	defer st.mutex.RUnlock()

	// Check not closed
	if st.store == nil {
		return false, ErrStateClosed
	}

	// Pass request to store
	return st.store.has(key)
}

func (st *StateRO) Release() {
	// Get state write lock
	st.mutex.Lock()
	defer st.mutex.Unlock()

	// Release the store
	if st.store != nil {
		st.store.mutex.RUnlock()
		st.store = nil
	}
}

// StateRW provides a read-write window to the store. While this
// state is active during the Update() function window, the entire
// store will be locked. The state is thread-safe for concurrent
// use UNTIL the moment that your supplied function to Update() returns,
// then the state has zero guarantees
type StateRW struct {
	store *KVStore
	mutex sync.RWMutex
}

func (st *StateRW) Get(key string) ([]byte, error) {
	// Get state read lock
	st.mutex.RLock()
	defer st.mutex.RUnlock()

	// Check not closed
	if st.store == nil {
		return nil, ErrStateClosed
	}

	// Pass request to store
	return st.store.get(key)
}

func (st *StateRW) GetStream(key string) (io.ReadCloser, error) {
	// Get state read lock
	st.mutex.RLock()
	defer st.mutex.RUnlock()

	// Check not closed
	if st.store == nil {
		return nil, ErrStateClosed
	}

	// Pass request to store
	return st.store.getStream(key)
}

func (st *StateRW) Put(key string, value []byte) error {
	// Get state read lock
	st.mutex.RLock()
	defer st.mutex.RUnlock()

	// Check not closed
	if st.store == nil {
		return ErrStateClosed
	}

	// Pass request to store
	return st.store.put(key, value)
}

func (st *StateRW) PutStream(key string, r io.Reader) error {
	// Get state read lock
	st.mutex.RLock()
	defer st.mutex.RUnlock()

	// Check not closed
	if st.store == nil {
		return ErrStateClosed
	}

	// Pass request to store
	return st.store.putStream(key, r)
}

func (st *StateRW) Has(key string) (bool, error) {
	// Get state read lock
	st.mutex.RLock()
	defer st.mutex.RUnlock()

	// Check not closed
	if st.store == nil {
		return false, ErrStateClosed
	}

	// Pass request to store
	return st.store.has(key)
}

func (st *StateRW) Delete(key string) error {
	// Get state read lock
	st.mutex.RLock()
	defer st.mutex.RUnlock()

	// Check not closed
	if st.store == nil {
		return ErrStateClosed
	}

	// Pass request to store
	return st.store.delete(key)
}

func (st *StateRW) Release() {
	// Get state write lock
	st.mutex.Lock()
	defer st.mutex.Unlock()

	// Release the store
	if st.store != nil {
		st.store.mutex.Unlock()
		st.store = nil
	}
}