forked from robustirc/robustirc
/
compaction.go
71 lines (61 loc) · 1.52 KB
/
compaction.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
package main
import (
"encoding/base64"
"encoding/json"
"log"
"time"
"github.com/hashicorp/raft"
"github.com/robustirc/robustirc/raft_store"
"github.com/robustirc/robustirc/types"
)
type robustSnapshot struct {
firstIndex uint64
lastIndex uint64
store *raft_store.LevelDBStore
state []byte
compactionEnd time.Time
}
// Persist writes a robustSnapshot to disk, i.e. handles the
// serialization details.
func (s *robustSnapshot) Persist(sink raft.SnapshotSink) error {
stateMsg := types.RobustMessage{
Type: types.RobustState,
Data: base64.StdEncoding.EncodeToString(s.state), // TODO: find a more straight-forward way to encode this
}
stateMsgJson, err := json.Marshal(&stateMsg)
if err != nil {
return err
}
stateMsgRaftJson, err := json.Marshal(&raft.Log{
Type: raft.LogCommand,
Index: 0, // never passed to raft.
Data: stateMsgJson,
})
if err != nil {
return err
}
log.Printf("Copying non-deleted messages into snapshot\n")
snapshotBytes, err := sink.Write(stateMsgRaftJson)
if err != nil {
return err
}
iterator := s.store.GetBulkIterator(s.firstIndex, s.lastIndex+1)
defer iterator.Release()
available := iterator.First()
for available {
if err := iterator.Error(); err != nil {
return err
}
n, err := sink.Write(iterator.Value())
if err != nil {
return err
}
snapshotBytes += n
available = iterator.Next()
}
log.Printf("snapshot: wrote %d bytes", snapshotBytes)
log.Printf("Snapshot done\n")
return nil
}
func (s *robustSnapshot) Release() {
}