-
Notifications
You must be signed in to change notification settings - Fork 570
/
versioned_snapshot.go
43 lines (39 loc) · 1.01 KB
/
versioned_snapshot.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
package snapshoter
import (
"encoding/gob"
"fmt"
"io"
"time"
)
// VersionedSnapshot stores the version and gob serialized form of the snapshot
// Provides a read and write methods to serialize and deserialize the gob format of the snapshot.
// Including a version provides compatibility check
type VersionedSnapshot struct {
Version int
Ser []byte
}
func (s *VersionedSnapshot) WriteSnapshot(w io.Writer, snapshot Snapshot) error {
byteContents, err := snapshot.Serialize()
if err != nil {
return err
}
s.Version = snapshot.GetVersion()
s.Ser = byteContents
enc := gob.NewEncoder(w)
return enc.Encode(s)
}
func (s *VersionedSnapshot) ReadSnapshot(r io.Reader) (Snapshot, error) {
err := gob.NewDecoder(r).Decode(s)
if err != nil {
return nil, err
}
if s.Version == 1 {
snapShotV1 := SnapshotV1{LastTimes: map[string]*time.Time{}}
err = snapShotV1.Deserialize(s.Ser)
if err != nil {
return nil, err
}
return &snapShotV1, nil
}
return nil, fmt.Errorf("unsupported version %v", s.Version)
}