forked from csimplestring/delta-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
history_manager.go
264 lines (227 loc) · 6.81 KB
/
history_manager.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package deltago
import (
"io"
"math"
"github.com/barweiss/go-tuple"
"github.com/csimplestring/delta-go/action"
"github.com/csimplestring/delta-go/errno"
"github.com/csimplestring/delta-go/internal/util"
"github.com/csimplestring/delta-go/internal/util/filenames"
"github.com/csimplestring/delta-go/store"
"github.com/samber/mo"
)
type historyManager struct {
logStore store.Store
}
func (h *historyManager) getCommitInfo(version int64) (*action.CommitInfo, error) {
iter, err := h.logStore.Read(filenames.DeltaFile(h.logStore.Root(), version))
if err != nil {
return nil, err
}
defer iter.Close()
var c *action.CommitInfo
for v, err := iter.Next(); err == nil; v, err = iter.Next() {
action, err := action.FromJson(v)
if err != nil {
return nil, err
}
if singleAction := action.Wrap(); singleAction != nil && singleAction.CommitInfo != nil {
c = singleAction.CommitInfo
break
}
}
if err != nil && err != io.EOF {
return nil, err
}
if c == nil {
return &action.CommitInfo{Version: &version}, nil
} else {
return c.Copy(version), nil
}
}
func (h *historyManager) checkVersionExists(versionToCkeck int64, sr *SnapshotReader) error {
earliestVersion, err := h.getEarliestReproducibleCommitVersion()
if err != nil {
return err
}
s, err := sr.update()
if err != nil {
return err
}
latestVersion := s.Version()
if versionToCkeck < earliestVersion || versionToCkeck > latestVersion {
return errno.VersionNotExist(versionToCkeck, earliestVersion, latestVersion)
}
return nil
}
func (h *historyManager) getActiveCommitAtTime(sr *SnapshotReader, timestamp int64,
canReturnLastCommit bool, mustBeRecreatable bool, canReturnEarliestCommit bool) (*commit, error) {
timeInMill := timestamp
var earliestVersion int64
var err error
if mustBeRecreatable {
earliestVersion, err = h.getEarliestReproducibleCommitVersion()
} else {
earliestVersion, err = h.getEarliestDeltaFile()
}
if err != nil {
return nil, err
}
s, err := sr.update()
if err != nil {
return nil, err
}
latestVersion := s.Version()
commits, err := h.getCommits(h.logStore, h.logStore.Root(), earliestVersion, latestVersion+1)
if err != nil {
return nil, err
}
commit := h.getLastCommitBeforeTimestamp(commits, timeInMill).OrElse(commits[0])
commitTs := commit.timestamp
if commit.timestamp > timeInMill && !canReturnEarliestCommit {
return nil, errno.TimestampEarlierThanTableFirstCommit(timeInMill, commitTs)
} else if commit.timestamp < timeInMill && commit.version == latestVersion && !canReturnLastCommit {
return nil, errno.TimestampLaterThanTableLastCommit(timeInMill, commitTs)
}
return commit, nil
}
func (h *historyManager) getEarliestDeltaFile() (int64, error) {
version0 := filenames.DeltaFile(h.logStore.Root(), 0)
iter, err := h.logStore.ListFrom(version0)
if err != nil {
return 0, err
}
defer iter.Close()
var earliestVersionOpt *store.FileMeta
for v, err := iter.Next(); err == nil; v, err = iter.Next() {
if filenames.IsDeltaFile(v.Path()) {
earliestVersionOpt = v
break
}
}
if err != nil && err != io.EOF {
return 0, err
}
if earliestVersionOpt == nil {
return 0, errno.NoHistoryFound(h.logStore.Root())
}
return filenames.DeltaVersion(earliestVersionOpt.Path()), nil
}
func (h *historyManager) getEarliestReproducibleCommitVersion() (int64, error) {
iter, err := h.logStore.ListFrom(filenames.DeltaFile(h.logStore.Root(), 0))
if err != nil {
return 0, err
}
defer iter.Close()
var files []*store.FileMeta
for f, err := iter.Next(); err == nil; f, err = iter.Next() {
if filenames.IsCheckpointFile(f.Path()) || filenames.IsDeltaFile(f.Path()) {
files = append(files, f)
}
}
if err != nil && err != io.EOF {
return 0, err
}
checkpointMap := make(map[tuple.T2[int64, int]]int)
smallestDeltaVersion := int64(math.MaxInt64)
lastCompleteCheckpoint := mo.None[int64]()
for _, f := range files {
nextFilePath := f.Path()
if filenames.IsDeltaFile(nextFilePath) {
version := filenames.DeltaVersion(nextFilePath)
if version == 0 {
return version, nil
}
smallestDeltaVersion = util.MinInt64(version, smallestDeltaVersion)
if lastCompleteCheckpoint.IsPresent() && lastCompleteCheckpoint.MustGet() >= smallestDeltaVersion {
return lastCompleteCheckpoint.MustGet(), nil
}
} else if filenames.IsCheckpointFile(nextFilePath) {
checkpointVersion := filenames.CheckpointVersion(nextFilePath)
parts := filenames.NumCheckpointParts(nextFilePath)
if parts.IsAbsent() {
lastCompleteCheckpoint = mo.Some(checkpointVersion)
} else {
numParts := parts.OrElse(1)
key := tuple.New2(checkpointVersion, numParts)
preCount := util.GetMapValue(checkpointMap, key, 0)
if numParts == preCount+1 {
lastCompleteCheckpoint = mo.Some(checkpointVersion)
}
checkpointMap[key] = preCount + 1
}
}
}
if lastCompleteCheckpoint.IsPresent() && lastCompleteCheckpoint.MustGet() >= smallestDeltaVersion {
return lastCompleteCheckpoint.MustGet(), nil
} else if smallestDeltaVersion < math.MaxInt64 {
return 0, errno.NoReproducibleHistoryFound(h.logStore.Root())
} else {
return 0, errno.NoHistoryFound(h.logStore.Root())
}
}
func (h *historyManager) getLastCommitBeforeTimestamp(commits []*commit, timeInMill int64) mo.Option[*commit] {
var i int
for i = len(commits) - 1; i >= 0; i-- {
if commits[i].timestamp <= timeInMill {
break
}
}
if i < 0 {
return mo.None[*commit]()
}
return mo.Some(commits[i])
}
func (h *historyManager) getCommits(logStore store.Store, logPath string, start int64, end int64) ([]*commit, error) {
iter, err := logStore.ListFrom(filenames.DeltaFile(logPath, start))
if err != nil {
return nil, err
}
defer iter.Close()
var commits []*commit
for f, err := iter.Next(); err == nil; f, err = iter.Next() {
if filenames.IsDeltaFile(f.Path()) {
c := &commit{version: filenames.DeltaVersion(f.Path()), timestamp: f.TimeModified().UnixMilli()}
if c.version < end {
commits = append(commits, c)
} else {
break
}
}
}
if err != nil && err != io.EOF {
return nil, err
}
return commits, nil
}
// func (h *historyManager) monotonizeCommitTimestamps(commits []action.CommitMarker) []action.CommitMarker {
// i := 0
// length := len(commits)
// for i < length-1 {
// prevTimestamp := commits[i].GetTimestamp()
// if commits[i].GetVersion() > commits[i+1].GetVersion() {
// panic("Unordered commits provided.")
// }
// if prevTimestamp >= commits[i+1].GetTimestamp() {
// commits[i+1] = commits[i+1].WithTimestamp(prevTimestamp + 1)
// }
// i += 1
// }
// return commits
// }
type commit struct {
version int64
timestamp int64
}
func (c *commit) GetTimestamp() int64 {
return c.timestamp
}
func (c *commit) WithTimestamp(timestamp int64) *commit {
return &commit{
version: c.version,
timestamp: timestamp,
}
}
func (c *commit) GetVersion() int64 {
return c.version
}