-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtx.go
62 lines (46 loc) · 1.31 KB
/
tx.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
package lfmap
import (
"iter"
"github.com/benbjohnson/immutable"
)
// Tx is a handle to map state usable from within transactions.
// It's methods are NOT safe for concurrent use.
type Tx[K comparable, V any] interface {
// Clears the map.
Clear()
// Deletes the key.
Delete(key K)
// Returns the value for a given key and a flag indicating whether the key
// exists. This flag distinguishes a nil value set on a key versus a
// non-existent key in the map.
Get(key K) (value V, ok bool)
// Returns the number of elements in the map.
Len() int
// Map iterator suitable for use with range keyword.
Range(yield func(key K, value V) bool)
// Updates the map setting specified key to the new value.
Set(key K, value V)
}
var _ Tx[string, string] = &tx[string, string]{}
var _ iter.Seq2[string, string] = (&tx[string, string]{}).Range
type tx[K comparable, V any] struct {
m *immutable.Map[K, V]
}
func (t *tx[K, V]) Clear() {
t.m = immutable.NewMap[K, V](newHasher[K]())
}
func (t *tx[K, V]) Delete(key K) {
t.m = t.m.Delete(key)
}
func (t *tx[K, V]) Get(key K) (value V, ok bool) {
return t.m.Get(key)
}
func (t *tx[K, V]) Len() int {
return t.m.Len()
}
func (t *tx[K, V]) Set(key K, value V) {
t.m = t.m.Set(key, value)
}
func (t *tx[K, V]) Range(yield func(key K, value V) bool) {
iterMap(t.m, yield)
}