forked from csimplestring/delta-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
285 lines (230 loc) · 8.27 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
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
}
// 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
}
// 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
}
// 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")
}
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() {
if filenames.IsDeltaFile(f.Path()) {
deltaPaths = append(deltaPaths, f.Path())
}
}
if err != nil && err != io.EOF {
return nil, err
}
lastSeenVersion := startVersion - 1
versionLogs := make([]VersionLog, len(deltaPaths))
for i, deltaPath := range deltaPaths {
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
}