-
Notifications
You must be signed in to change notification settings - Fork 4
/
ephemeral.go
84 lines (70 loc) · 1.39 KB
/
ephemeral.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
package wal
import (
"bytes"
)
type ephemeralWAL struct {
ch chan Entry
idx uint64
}
// NewEphemeral returns a memory-backed WriteAheadLogger
func NewEphemeral() WriteAheadLogger {
return &ephemeralWAL{
ch: make(chan Entry, 1000),
}
}
func (e *ephemeralWAL) Index() uint64 {
return e.idx
}
func (e *ephemeralWAL) AppendBytes(entries [][]byte, crc []uint32) (uint64, uint64, error) {
idxStart := e.idx
for _, entry := range entries {
e.ch <- Entry{
Body: entry,
ID: e.idx,
}
e.idx++
}
return idxStart, e.idx - 1, nil
}
func (e *ephemeralWAL) Append(entries []EntryWriterTo) (uint64, uint64, error) {
var entriesBytes [][]byte
for _, entry := range entries {
var b bytes.Buffer
entry.WriteTo(&b)
entriesBytes = append(entriesBytes, b.Bytes())
}
return e.AppendBytes(entriesBytes, nil)
}
func (e *ephemeralWAL) GetCursor(idx uint64) (Cursor, error) {
return ephemeralCursor{
readCh: e.ch,
}, nil
}
func (e *ephemeralWAL) Close() error {
close(e.ch)
return nil
}
func (e *ephemeralWAL) Delete() error {
return e.Empty()
}
func (e *ephemeralWAL) Empty() error {
for {
select {
case <-e.ch:
default:
return nil
}
}
}
type ephemeralCursor struct {
readCh chan Entry
}
func (c ephemeralCursor) ReadCh() <-chan Entry {
return c.readCh
}
func (c ephemeralCursor) Reset() (uint64, error) {
return 0, nil
}
func (c ephemeralCursor) Close() error {
return nil
}