Skip to content

Commit fcde04a

Browse files
committed
Add basic v1.2 snapshot writer
1 parent 9d109fe commit fcde04a

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

snapio/snapwrite.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package snapio
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"encoding/binary"
7+
"io"
8+
9+
"github.com/tinylib/msgp/msgp"
10+
"github.com/viciious/go-tarantool"
11+
)
12+
13+
type SpaceData struct {
14+
Space uint
15+
Tuples [][]interface{}
16+
}
17+
18+
func WriteV12Snapshot(fd io.Writer, data []*SpaceData) error {
19+
header := `SNAP
20+
0.12
21+
Version: 2.2.1-3-g878e2a42c
22+
Instance: d31ad582-66a6-4b18-96f7-278a7a33ad20
23+
VClock: {1: 10001}
24+
25+
`
26+
27+
w := bufio.NewWriter(fd)
28+
defer w.Flush()
29+
30+
_, err := w.WriteString(header)
31+
if err != nil {
32+
return err
33+
}
34+
35+
var lsn uint
36+
for _, s := range data {
37+
space := s.Space
38+
if space == 0 {
39+
space = 10024
40+
}
41+
for _, t := range s.Tuples {
42+
var arr []byte
43+
44+
arr = msgp.AppendMapHeader(arr, 1)
45+
arr = msgp.AppendUint(arr, tarantool.KeyLSN)
46+
arr = msgp.AppendUint(arr, uint(lsn+1))
47+
48+
arr = msgp.AppendMapHeader(arr, 2)
49+
arr = msgp.AppendUint(arr, tarantool.KeySpaceNo)
50+
arr = msgp.AppendUint(arr, uint(space))
51+
arr = msgp.AppendUint(arr, tarantool.KeyTuple)
52+
arr, _ = msgp.AppendIntf(arr, t)
53+
54+
var lenbuf []byte
55+
lenbuf = msgp.AppendUint32(lenbuf, uint32(len(arr)))
56+
57+
if err = binary.Write(w, binary.BigEndian, uint32(XRowFixedHeaderMagic)); err != nil {
58+
return err
59+
}
60+
if _, err = w.Write(lenbuf); err != nil {
61+
return err
62+
}
63+
if _, err = w.Write(bytes.Repeat([]byte{'\x00'}, XRowFixedHeaderSize-len(lenbuf)-4)); err != nil {
64+
return err
65+
}
66+
if _, err = w.Write(arr); err != nil {
67+
return err
68+
}
69+
}
70+
}
71+
72+
if err = binary.Write(w, binary.BigEndian, uint32(XRowFixedHeaderEof)); err != nil {
73+
return err
74+
}
75+
76+
return nil
77+
}

0 commit comments

Comments
 (0)