-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx.go
140 lines (125 loc) · 2.55 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
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
package nodis
import (
"time"
"github.com/diiyw/nodis/ds"
)
type Tx struct {
store *store
lockedMetas []*metadata
}
func newTx(store *store) *Tx {
return &Tx{
store: store,
lockedMetas: make([]*metadata, 0),
}
}
func (tx *Tx) lockKey(key string) *metadata {
tx.store.mu.RLock()
m, ok := tx.store.metadata.Get(key)
tx.store.mu.RUnlock()
if ok {
tx.lockMeta(m)
return m
}
return newMetadata(ds.NewKey(key, 0), true)
}
func (tx *Tx) rLockKey(key string) *metadata {
tx.store.mu.RLock()
m, ok := tx.store.metadata.Get(key)
tx.store.mu.RUnlock()
if ok {
tx.rLockMeta(m)
return m
}
return newMetadata(ds.NewKey(key, 0), false)
}
func (tx *Tx) lockMeta(m *metadata) {
m.Lock()
m.writeable = true
tx.lockedMetas = append(tx.lockedMetas, m)
m.count.Add(1)
}
func (tx *Tx) rLockMeta(m *metadata) {
m.RLock()
tx.lockedMetas = append(tx.lockedMetas, m)
m.count.Add(1)
}
func (tx *Tx) storeMeta(m *metadata) {
tx.store.mu.Lock()
tx.store.metadata.Set(m.key.Name, m)
tx.store.mu.Unlock()
}
func (tx *Tx) resetMeta(m *metadata, newFn func() ds.Value) {
m.count.Store(0)
m.value = nil
if newFn != nil {
m.setValue(newFn())
}
m.state = KeyStateNormal
}
func (tx *Tx) newStoredMetadata(m *metadata, newFn func() ds.Value) *metadata {
tx.store.mu.Lock()
value := newFn()
m.setValue(value)
m.state |= KeyStateModified
tx.lockMeta(m)
tx.store.metadata.Set(m.key.Name, m)
tx.store.mu.Unlock()
return m
}
func (tx *Tx) delKey(key string) {
tx.store.mu.Lock()
tx.store.metadata.Delete(key)
tx.store.mu.Unlock()
}
func (tx *Tx) writeKey(key string, newFn func() ds.Value) *metadata {
m := tx.lockKey(key)
if m.isOk() {
if m.expired(time.Now().UnixMilli()) {
tx.resetMeta(m, newFn)
return m
}
// not expired
if m.value != nil {
return m
}
// if not found in memory, read from storage
v, err := tx.store.ss.Get(m.key)
if err != nil {
tx.resetMeta(m, newFn)
return m
}
m.setValue(v)
return m
}
if newFn == nil {
return m
}
return tx.newStoredMetadata(m, newFn)
}
func (tx *Tx) readKey(key string) *metadata {
m := tx.rLockKey(key)
if m.isOk() {
if m.expired(time.Now().UnixMilli()) {
return newMetadata(m.key, false)
}
// not expired
if m.value != nil {
return m
}
// if not found in memory, read from storage
v, err := tx.store.ss.Get(m.key)
if err != nil {
return newMetadata(m.key, false)
}
m.setValue(v)
return m
}
return newMetadata(m.key, false)
}
func (tx *Tx) commit() {
for _, meta := range tx.lockedMetas {
meta.commit()
}
tx.lockedMetas = tx.lockedMetas[:0]
}