Skip to content

Commit 718c3c8

Browse files
authored
Merge pull request #5 from letterbeezps/dev
Dev
2 parents f216b21 + 87f600e commit 718c3c8

File tree

6 files changed

+460
-16
lines changed

6 files changed

+460
-16
lines changed

engine/engine.go

+13
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,25 @@ import (
88
)
99

1010
type Engine interface {
11+
// requested
1112
Get(key string) ([]byte, bool)
1213

14+
// requested
1315
Set(key string, value []byte)
1416

17+
// requested
1518
Delete(key string)
1619

20+
// option
1721
Scan(start, end internal.Bound, iter func(key string, value []byte) bool)
1822

23+
// option
1924
Reverse(start, end internal.Bound, iter func(key string, value []byte) bool)
2025

26+
// requested
2127
Iter(start, end internal.Bound) Iterator
2228

29+
// option
2330
ReverseIter(start, end internal.Bound) Iterator
2431
}
2532

@@ -181,6 +188,12 @@ func ScanTest(engine Engine, t *testing.T) {
181188
i++
182189
}
183190

191+
iterator = engine.Iter(
192+
internal.NewBound("d", internal.Include),
193+
internal.NewBound("", internal.NoBound),
194+
)
195+
assert.False(t, iterator.IsValid())
196+
184197
iterator = engine.Iter(
185198
internal.NewBound("aab", internal.Include),
186199
internal.NewBound("", internal.NoBound),

engine/memory/iterator.go

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ type MemoryIterator struct {
1515
}
1616

1717
func (iter *MemoryIterator) Value() []byte {
18+
if len(iter.V) == 0 {
19+
return []byte{}
20+
}
1821
return iter.V
1922
}
2023

engine/memory/memory.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,22 @@ func (m *Memory) Iter(start, end internal.Bound) engine.Iterator {
8989
iter := m.Data.Iter()
9090
key, value := "", []byte{}
9191
valid := true
92-
switch start.BoundType {
93-
case internal.NoBound:
92+
93+
if start.BoundType == internal.NoBound {
9494
valid = iter.First()
95-
case internal.Include, internal.Exclude:
95+
} else {
9696
valid = iter.Seek(start.Key)
97-
if valid && start.BoundType == internal.Exclude && iter.Key() == start.Key {
98-
valid = iter.Next()
99-
}
10097
}
98+
if valid && start.BoundType == internal.Exclude && iter.Key() == start.Key {
99+
valid = iter.Next()
100+
}
101+
if valid && end.BoundType == internal.Include && iter.Key() > end.Key {
102+
valid = false
103+
}
104+
if valid && end.BoundType == internal.Exclude && iter.Key() >= end.Key {
105+
valid = false
106+
}
107+
101108
if valid {
102109
key = iter.Key()
103110
value = iter.Value()
@@ -111,6 +118,7 @@ func (m *Memory) Iter(start, end internal.Bound) engine.Iterator {
111118
}
112119
}
113120

121+
// todo : fix bug
114122
func (m *Memory) ReverseIter(start, end internal.Bound) engine.Iterator {
115123
iter := m.Data.Iter()
116124
key, value := "", []byte{}

mvcc/error.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package mvcc
2+
3+
import "errors"
4+
5+
var (
6+
ErrorSerialization = errors.New("serialization")
7+
)

mvcc/tx.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (tx *TX) Begin() error {
2121
tx.Lock.Lock()
2222
defer tx.Lock.Unlock()
2323

24-
txId := uint64(0)
24+
txId := uint64(1)
2525
txIdBytes, ok := tx.Engine.Get(NextTxID)
2626
if ok {
2727
txId = binary.BigEndian.Uint64(txIdBytes)
@@ -88,7 +88,7 @@ func (tx *TX) Write(key string, value []byte) error {
8888
return errors.Wrap(err, fmt.Sprintf("got bad txKey: %s", iter.Key()))
8989
}
9090
if !tx.State.IsVisible(check_id) {
91-
return errors.Wrap(err, fmt.Sprintf("serialization, cur tx: %d, exist: %d", tx.State.TxID, check_id))
91+
return errors.Wrap(ErrorSerialization, fmt.Sprintf("cur tx: %d, exist: %d", tx.State.TxID, check_id))
9292
}
9393
iter.Next()
9494
}
@@ -123,10 +123,14 @@ func (tx *TX) Commit() error {
123123
start := internal.NewBound(startKey, internal.Include)
124124
end := internal.NewBound(endKey, internal.Exclude)
125125
iter := tx.Engine.Iter(start, end)
126+
removeKeys := []string{}
126127
for iter.IsValid() {
127-
tx.Engine.Delete(iter.Key())
128+
removeKeys = append(removeKeys, iter.Key())
128129
iter.Next()
129130
}
131+
for _, key := range removeKeys {
132+
tx.Engine.Delete(key)
133+
}
130134

131135
activeKey, err := encodeTxActiveKey(tx.State.TxID)
132136
if err != nil {
@@ -145,7 +149,9 @@ func (tx *TX) RollBack() error {
145149
start := internal.NewBound(startKey, internal.Include)
146150
end := internal.NewBound(endKey, internal.Exclude)
147151
iter := tx.Engine.Iter(start, end)
152+
removeKeys := []string{}
148153
for iter.IsValid() {
154+
removeKeys = append(removeKeys, iter.Key())
149155
_, origin_key, err := decodeTxWriteKey(iter.Key())
150156
if err != nil {
151157
return errors.Wrap(err, fmt.Sprintf("decodeTxWriteKey with key: %s", iter.Key()))
@@ -154,10 +160,14 @@ func (tx *TX) RollBack() error {
154160
if err != nil {
155161
return errors.Wrap(err, fmt.Sprintf("encodeTxKey with key: %s", txKey))
156162
}
157-
tx.Engine.Delete(txKey)
163+
removeKeys = append(removeKeys, txKey)
158164
iter.Next()
159165
}
160166

167+
for _, key := range removeKeys {
168+
tx.Engine.Delete(key)
169+
}
170+
161171
activeKey, err := encodeTxActiveKey(tx.State.TxID)
162172
if err != nil {
163173
return errors.Wrap(err, fmt.Sprintf("getTxActiveKey with %d", tx.State.TxID))
@@ -188,5 +198,5 @@ func (tx *TX) Get(key string) ([]byte, error) {
188198
}
189199
iter.Next()
190200
}
191-
return nil, nil
201+
return []byte{}, nil
192202
}

0 commit comments

Comments
 (0)