/
map_store.go
83 lines (68 loc) · 1.99 KB
/
map_store.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
package storage
import (
"encoding/binary"
"strconv"
"sync"
"github.com/pkg/errors"
)
// MapStore Represents an instance of the in-memory storage.
type MapStore struct {
started bool
items map[string]string
lock sync.Mutex
}
var binlogFileKeyString = "binlogfile"
var binlogPositionKeyString = "binlogpos"
// Open Opens the storage.
func (store *MapStore) Open(filename string) error {
store.items = make(map[string]string)
store.started = true
return nil
}
// IsStarted Returns whether the store is started.
func (store *MapStore) IsStarted() bool {
return store.started
}
// Close Closes the storage.
func (store *MapStore) Close() {
}
// GetBinlogPosition Returns the persisted binlog position.
func (store *MapStore) GetBinlogPosition() (*BinlogInformation, error) {
binlogfile := store.items[binlogFileKeyString]
binlogposString := store.items[binlogPositionKeyString]
binlogpos, err := strconv.ParseUint(binlogposString, 10, 32)
if err != nil {
return nil, err
}
return &BinlogInformation{File: binlogfile, Position: uint32(binlogpos)}, nil
}
// SetBinlogPosition Sets and persists the current binlog position.
func (store *MapStore) SetBinlogPosition(binlogInfo *BinlogInformation) error {
fileBuffer := []byte(binlogInfo.File)
positionBuffer := make([]byte, 4)
binary.LittleEndian.PutUint32(positionBuffer, binlogInfo.Position)
err := store.Set(fileBuffer, positionBuffer)
if err != nil {
return err
}
return nil
}
// Get Gets the value associated with the specified key.
func (store *MapStore) Get(key []byte) ([]byte, error) {
value, found := store.items[string(key)]
if !found {
return nil, errors.New("Key not found")
}
return []byte(value), nil
}
// Set Sets the specified value associated with the specified key.
func (store *MapStore) Set(key []byte, value []byte) error {
store.lock.Lock()
store.items[string(key)] = string(value)
store.lock.Unlock()
return nil
}
// Commit Commits the current transaction.
func (store *MapStore) Commit() error {
return nil
}