forked from fuyao-w/papillon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmem_snapshot.go
More file actions
82 lines (71 loc) · 1.61 KB
/
mem_snapshot.go
File metadata and controls
82 lines (71 loc) · 1.61 KB
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
package papillon
import (
"bytes"
"io"
"sync"
)
type memSnapshot struct {
latest *memSnapshotSink
has bool
sync.Mutex
}
func NewMemSnapShot() *memSnapshot {
return &memSnapshot{}
}
type memSnapshotSink struct {
meta *SnapshotMeta
buf *bytes.Buffer
}
func (m *memSnapshotSink) Write(p []byte) (n int, err error) {
size, err := m.buf.Write(p)
m.meta.Size += int64(size)
return size, err
}
func (m *memSnapshotSink) Close() error {
return nil
}
func (m *memSnapshotSink) ID() string {
return m.meta.ID
}
func (m *memSnapshotSink) Cancel() error {
return nil
}
func (m *memSnapshot) Open(id string) (*SnapshotMeta, io.ReadCloser, error) {
m.Lock()
defer m.Unlock()
if !m.has {
return nil, nil, ErrNotExist
}
if m.latest.meta.ID != id {
return nil, nil, ErrNotExist
}
buffer := bytes.NewBuffer(m.latest.buf.Bytes())
return m.latest.meta, io.NopCloser(buffer), nil
}
func (m *memSnapshot) List() ([]*SnapshotMeta, error) {
m.Lock()
defer m.Unlock()
if !m.has {
return nil, nil
}
return []*SnapshotMeta{m.latest.meta}, nil
}
func (m *memSnapshot) Create(version SnapshotVersion, index, term uint64, configuration ClusterInfo, configurationIndex uint64, rpc RpcInterface) (SnapshotSink, error) {
m.Lock()
defer m.Unlock()
sink := memSnapshotSink{
meta: &SnapshotMeta{
Version: version,
ID: snapshotName(term, index),
Index: index,
Term: term,
Configuration: configuration,
ConfigurationIndex: configurationIndex,
Size: 0,
},
buf: &bytes.Buffer{},
}
m.has = true
m.latest = &sink
return &sink, nil
}