-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
209 lines (179 loc) · 3.91 KB
/
history.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Package history provides a seekable sequence of rpg.State.
package history
import (
"bytes"
"encoding/binary"
"encoding/gob"
"github.com/BenLubar/bindiff"
"github.com/Rnoadm/rpg"
"io"
)
// See also: io.Seeker
const (
SeekStart = iota
SeekCur
SeekEnd
)
const sizeof_int64 = 64 / 8
// History is a seekable sequence of rpg.State.
type History struct {
i int64
b []byte
f io.ReadWriteSeeker
}
// NewHistory returns a new History that reads from and writes to f.
func NewHistory(f io.ReadWriteSeeker) *History {
return &History{f: f, i: -1}
}
// Seek moves the cursor to an offset from the start, end, or current position and returns
// the value at the cursor. If the error returned is io.EOF, Seek was asked to pass the start
// or end of the file. Any other non-nil error means that History is no longer safe to use.
func (h *History) Seek(offset int64, whence int) (*rpg.State, error) {
if whence == SeekCur && h.i < 0 {
if offset > 0 {
offset--
} else {
offset++
whence = SeekEnd
}
}
if whence == SeekStart || h.i < 0 {
if _, err := h.f.Seek(0, SeekStart); err != nil {
return nil, err
}
h.i, h.b = -1, nil
if err := h.seekForward(); err != nil {
return nil, err
}
}
if whence == SeekEnd {
for {
err := h.seekForward()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
}
}
for offset > 0 {
if err := h.seekForward(); err != nil {
return nil, err
}
offset--
}
for offset < 0 {
if err := h.seekReverse(); err != nil {
return nil, err
}
offset++
}
var s *rpg.State
err := gob.NewDecoder(bytes.NewReader(h.b)).Decode(&s)
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return s, err
}
func (h *History) seekForward() error {
var size int64
err := binary.Read(h.f, binary.LittleEndian, &size)
if err != nil {
return err
}
patch := make([]byte, size+sizeof_int64)
_, err = io.ReadFull(h.f, patch)
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return err
}
h.i++
h.b, err = bindiff.Forward(h.b, patch[:size])
return err
}
func (h *History) seekReverse() error {
if h.i == 0 {
return io.EOF
}
_, err := h.f.Seek(-sizeof_int64, SeekCur)
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return err
}
var size int64
err = binary.Read(h.f, binary.LittleEndian, &size)
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return err
}
_, err = h.f.Seek(-size-sizeof_int64, SeekCur)
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return err
}
patch := make([]byte, size+sizeof_int64)
_, err = io.ReadFull(h.f, patch)
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return err
}
h.i--
h.b, err = bindiff.Reverse(h.b, patch[:size])
return err
}
// Append adds s to the end of h. h.Seek(0, SeekEnd) is called before writing. A non-nil
// error means that h is no longer safe to use.
func (h *History) Append(s *rpg.State) error {
_, err := h.Seek(0, SeekEnd)
if err == io.EOF && h.i == -1 {
err = nil
}
if err != nil {
return err
}
var buf bytes.Buffer
err = gob.NewEncoder(&buf).Encode(s)
if err != nil {
return err
}
patch := bindiff.Diff(h.b, buf.Bytes(), 10)
h.i++
h.b = buf.Bytes()
err = binary.Write(h.f, binary.LittleEndian, int64(len(patch)))
if err != nil {
return err
}
n, err := h.f.Write(patch)
if err != nil {
return err
}
if n != len(patch) {
return io.ErrShortWrite
}
err = binary.Write(h.f, binary.LittleEndian, int64(len(patch)))
if err != nil {
return err
}
return nil
}
// Tell returns the current position of this History's cursor. -1 is a special state that
// is either before the start or after the end, depending on the direction of the next Seek.
func (h *History) Tell() int64 {
return h.i
}
// Reset sets the cursor to position -1, equivalent to calling NewHistory on the same
// io.ReadWriteSeeker.
func (h *History) Reset() {
h.i, h.b = -1, nil
}