-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemtable.go
45 lines (37 loc) · 904 Bytes
/
memtable.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
package writer
import (
"bytes"
"io"
"time"
)
type Memtable struct {
Logs bytes.Buffer
StartTimeStamp int64
OccupiedSize int
TotalSize int
}
// NewMemtable returns a new instance of Memtable
func NewMemtable(totalSize int) Memtable {
return Memtable{
OccupiedSize: 0,
TotalSize: totalSize,
}
}
// Append appends the log in the running memtable
func (m *Memtable) Append(log []byte, timestamp time.Time) error {
if len(m.Logs.Bytes()) == 0 {
// Using Nano() because when the log throughput is seemingly high
// it will lead to overwriting segment file
m.StartTimeStamp = timestamp.UnixNano()
}
m.Logs.Write(log)
m.Logs.WriteByte(NewLine)
// +1 for new line
m.OccupiedSize += len(log) + 1
return nil
}
// Flush flushes the logs from memtable to segmentfile
func (m *Memtable) Flush(w io.Writer) error {
_, err := w.Write(m.Logs.Bytes())
return err
}