forked from Jonathan-Rosenberg/delta-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
345 lines (285 loc) · 9.97 KB
/
log.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package deltago
import (
"io"
"strings"
"sync"
"github.com/csimplestring/delta-go/action"
"github.com/csimplestring/delta-go/errno"
"github.com/csimplestring/delta-go/internal/util/filenames"
"github.com/csimplestring/delta-go/iter"
"github.com/csimplestring/delta-go/store"
"github.com/rotisserie/eris"
)
// Log Represents the transaction logs of a Delta table.
// It provides APIs to access the states of a Delta table.
type Log interface {
// Snapshot the current Snapshot of the Delta table.
// You may need to call update() to access the latest snapshot if the current snapshot is stale.
Snapshot() (Snapshot, error)
Update() (Snapshot, error)
SnapshotForVersionAsOf(version int64) (Snapshot, error)
SnapshotForTimestampAsOf(timestamp int64) (Snapshot, error)
StartTransaction() (OptimisticTransaction, error)
CommitInfoAt(version int64) (*action.CommitInfo, error)
Path() string
// Get all actions starting from startVersion (inclusive) in increasing order of committed version.
// If startVersion doesn't exist, return an empty Iterator.
Changes(startVersion int64, failOnDataLoss bool) (iter.Iter[VersionLog], error)
// Returns the latest version that was committed before or at timestamp. If no version exists, returns -1. Specifically:
// if a commit version exactly matches the provided timestamp, we return it
// else, we return the latest commit version with a timestamp less than the provided one
// If the provided timestamp is less than the timestamp of any committed version, we throw an error.
VersionBeforeOrAtTimestamp(timestamp int64) (int64, error)
// Returns the latest version that was committed at or after timestamp. If no version exists, returns -1. Specifically:
// if a commit version exactly matches the provided timestamp, we return it
// else, we return the earliest commit version with a timestamp greater than the provided one
// If the provided timestamp is larger than the timestamp of any committed version, we throw an error.
VersionAtOrAfterTimestamp(timestamp int64) (int64, error)
TableExists() bool
}
func getLogPath(dataPath string) string {
logPath := strings.TrimSuffix(dataPath, "/") + "/_delta_log/"
return logPath
}
// ForTableWithStore Create a DeltaLog instance representing the table located at the provided path using the provided
// logStore (or initializing a new one if not provided).
func ForTableWithStore(dataPath string, config Config, clock Clock, ls *store.Store) (Log, error) {
logPath := getLogPath(dataPath)
deltaLogLock := &sync.Mutex{}
var logStore = ls
if logStore == nil {
s, err := store.New(logPath)
logStore = &s
if err != nil {
return nil, err
}
}
parquetReader, err := newCheckpointReader(logPath)
if err != nil {
return nil, err
}
historyManager := &historyManager{logStore: *logStore}
snaptshotManager, err := newSnapshotReader(config, parquetReader, *logStore, clock, historyManager, deltaLogLock)
if err != nil {
return nil, err
}
logImpl := &logImpl{
dataPath: dataPath,
logPath: logPath,
clock: clock,
store: *logStore,
deltaLogLock: deltaLogLock,
history: historyManager,
snapshotReader: snaptshotManager,
checkpointReader: &parquetReader,
}
return logImpl, nil
}
// ForTable Create a DeltaLog instance representing the table located at the provided path.
func ForTable(dataPath string, config Config, clock Clock) (Log, error) {
logPath := getLogPath(dataPath)
deltaLogLock := &sync.Mutex{}
var logStore store.Store
logStore, err := store.New(logPath)
if err != nil {
return nil, err
}
parquetReader, err := newCheckpointReader(logPath)
if err != nil {
return nil, err
}
historyManager := &historyManager{logStore: logStore}
snaptshotManager, err := newSnapshotReader(config, parquetReader, logStore, clock, historyManager, deltaLogLock)
if err != nil {
return nil, err
}
logImpl := &logImpl{
dataPath: dataPath,
logPath: logPath,
clock: clock,
store: logStore,
deltaLogLock: deltaLogLock,
history: historyManager,
snapshotReader: snaptshotManager,
}
return logImpl, nil
}
// func ForTableV2(config Config, clock Clock) (Log, error) {
// deltaLogLock := &sync.Mutex{}
// var logStore store.Store
// var fs store.FS
// logStore, err := configureLogStore(config)
// if err != nil {
// return nil, err
// }
// fs, err = store.GetFileSystem(logPath)
// if err != nil {
// return nil, err
// }
// parquetReader, err := newCheckpointReader(config)
// if err != nil {
// return nil, err
// }
// historyManager := &historyManager{logStore: logStore}
// snaptshotManager, err := newSnapshotReader(config, parquetReader, logStore, clock, historyManager, deltaLogLock)
// if err != nil {
// return nil, err
// }
// logImpl := &logImpl{
// dataPath: dataPath,
// logPath: logPath,
// clock: clock,
// store: logStore,
// fs: fs,
// deltaLogLock: deltaLogLock,
// history: historyManager,
// snapshotReader: snaptshotManager,
// }
// return logImpl, nil
// }
type logImpl struct {
dataPath string
logPath string
clock Clock
store store.Store
deltaLogLock *sync.Mutex
history *historyManager
snapshotReader *SnapshotReader
checkpointReader *checkpointReader
}
// Snapshot the current Snapshot of the Delta table.
// You may need to call update() to access the latest snapshot if the current snapshot is stale.
func (l *logImpl) Snapshot() (Snapshot, error) {
return l.snapshotReader.currentSnapshot.Load(), nil
}
func (l *logImpl) Update() (Snapshot, error) {
return l.snapshotReader.update()
}
func (l *logImpl) SnapshotForVersionAsOf(version int64) (Snapshot, error) {
return l.snapshotReader.getSnapshotForVersionAsOf(version)
}
func (l *logImpl) SnapshotForTimestampAsOf(timestamp int64) (Snapshot, error) {
return l.snapshotReader.getSnapshotForTimestampAsOf(timestamp)
}
func (l *logImpl) StartTransaction() (OptimisticTransaction, error) {
snapshot, err := l.snapshotReader.update()
if err != nil {
return nil, err
}
return newOptimisticTransaction(snapshot,
l.snapshotReader, l.clock, nil, l.deltaLogLock, l.store, l.logPath), nil
}
func (l *logImpl) CommitInfoAt(version int64) (*action.CommitInfo, error) {
if err := l.history.checkVersionExists(version, l.snapshotReader); err != nil {
return nil, err
}
return l.history.getCommitInfo(version)
}
func (l *logImpl) Path() string {
return l.dataPath
}
// Changes Get all actions starting from startVersion (inclusive) in increasing order of committed version.
// If startVersion doesn't exist, return an empty Iterator.
func (l *logImpl) Changes(startVersion int64, failOnDataLoss bool) (iter.Iter[VersionLog], error) {
if startVersion < 0 {
return nil, eris.Wrap(errno.ErrIllegalArgument, "invalid startVersion")
}
var fs iter.Iter[*store.FileMeta]
checkpointIncluded := true
fs, err := l.store.ListFrom(filenames.CheckpointFileSingular("", startVersion))
if err != nil {
checkpointIncluded = false
fs, err = l.store.ListFrom(filenames.DeltaFile("", startVersion))
if err != nil {
return nil, err
}
}
defer fs.Close()
var deltaPaths []string
for f, err := fs.Next(); err == nil; f, err = fs.Next() {
p := f.Path()
if checkpointIncluded && filenames.IsCheckpointFile(p) {
deltaPaths = append(deltaPaths, p)
checkpointIncluded = false
} else if filenames.IsDeltaFile(p) {
deltaPaths = append(deltaPaths, p)
}
}
if err != nil && err != io.EOF {
return nil, err
}
lastSeenVersion := startVersion - 1
versionLogs := make([]VersionLog, len(deltaPaths))
for i, deltaPath := range deltaPaths {
if filenames.IsCheckpointFile(deltaPath) {
version := filenames.CheckpointVersion(deltaPath)
versionLogs[i] = &MemOptimizedCheckpoint{
version: version,
path: deltaPath,
store: l.store,
cr: l.checkpointReader,
}
continue
}
version := filenames.DeltaVersion(deltaPath)
if failOnDataLoss && version > lastSeenVersion+1 {
return nil, errno.IllegalStateError("fail on data loss")
}
lastSeenVersion = version
versionLogs[i] = &MemOptimizedVersionLog{
version: version,
path: deltaPath,
store: l.store,
}
}
return iter.FromSlice(versionLogs), nil
}
// Returns the latest version that was committed before or at timestamp. If no version exists, returns -1. Specifically:
// if a commit version exactly matches the provided timestamp, we return it
// else, we return the latest commit version with a timestamp less than the provided one
// If the provided timestamp is less than the timestamp of any committed version, we throw an error.
func (l *logImpl) VersionBeforeOrAtTimestamp(timestamp int64) (int64, error) {
if !l.TableExists() {
return -1, nil
}
canReturnLastCommit := true
mustBeRecreatable := false
canReturnEarliestCommit := false
c, err := l.history.getActiveCommitAtTime(l.snapshotReader,
timestamp,
canReturnLastCommit,
mustBeRecreatable,
canReturnEarliestCommit)
if err != nil {
return -1, err
}
return c.version, nil
}
// Returns the latest version that was committed at or after timestamp. If no version exists, returns -1. Specifically:
// if a commit version exactly matches the provided timestamp, we return it
// else, we return the earliest commit version with a timestamp greater than the provided one
// If the provided timestamp is larger than the timestamp of any committed version, we throw an error.
func (l *logImpl) VersionAtOrAfterTimestamp(timestamp int64) (int64, error) {
if !l.TableExists() {
return -1, nil
}
canReturnLastCommit := false
mustBeRecreatable := false
canReturnEarliestCommit := true
c, err := l.history.getActiveCommitAtTime(l.snapshotReader,
timestamp,
canReturnLastCommit,
mustBeRecreatable,
canReturnEarliestCommit)
if err != nil {
return -1, err
}
if c.timestamp >= timestamp {
return c.version, nil
} else {
return c.version + 1, nil
}
}
func (l *logImpl) TableExists() bool {
return l.snapshotReader.snapshot().Version() >= 0
}