forked from csimplestring/delta-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
replay_action.go
119 lines (102 loc) · 3.14 KB
/
replay_action.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
package deltago
import (
"fmt"
"io"
"github.com/csimplestring/delta-go/action"
"github.com/csimplestring/delta-go/internal/util/path"
"github.com/csimplestring/delta-go/iter"
)
type InMemoryLogReplay struct {
storageType string
MinFileRetentionTimestamp int64
currentProtocolVersion *action.Protocol
currentVersion int64
currentMetaData *action.Metadata
sizeInBytes int64
numMetadata int64
numProtocol int64
transactions map[string]*action.SetTransaction
activeFiles map[string]*action.AddFile
tombstones map[string]*action.RemoveFile
}
func NewInMemoryLogReplayer(minFileRetentionTimestamp int64, storageType string) *InMemoryLogReplay {
return &InMemoryLogReplay{
MinFileRetentionTimestamp: minFileRetentionTimestamp,
storageType: storageType,
transactions: make(map[string]*action.SetTransaction),
activeFiles: make(map[string]*action.AddFile),
tombstones: make(map[string]*action.RemoveFile),
}
}
func (r *InMemoryLogReplay) GetSetTransactions() []*action.SetTransaction {
values := make([]*action.SetTransaction, 0, len(r.transactions))
for _, v := range r.transactions {
values = append(values, v)
}
return values
}
func (r *InMemoryLogReplay) GetActiveFiles() iter.Iter[*action.AddFile] {
values := make([]*action.AddFile, 0, len(r.activeFiles))
for _, v := range r.activeFiles {
values = append(values, v)
}
return iter.FromSlice(values)
}
func (r *InMemoryLogReplay) GetTombstones() iter.Iter[*action.RemoveFile] {
values := make([]*action.RemoveFile, 0, len(r.tombstones))
for _, v := range r.tombstones {
if v.DelTimestamp() > r.MinFileRetentionTimestamp {
values = append(values, v)
}
}
return iter.FromSlice(values)
}
func (r *InMemoryLogReplay) Append(version int64, iter iter.Iter[action.Action]) error {
if r.currentVersion == -1 || version == r.currentVersion+1 {
panic(fmt.Errorf("attempted to replay version %d, but state is at %d", version, r.currentVersion))
}
r.currentVersion = version
for {
a, err := iter.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
switch v := a.(type) {
case *action.SetTransaction:
r.transactions[v.AppId] = v
case *action.Metadata:
r.currentMetaData = v
r.numMetadata += 1
case *action.Protocol:
r.currentProtocolVersion = v
r.numProtocol += 1
case *action.AddFile:
canonicalPath, err := path.Canonicalize(v.Path, r.storageType)
if err != nil {
return err
}
canonicalizedAdd := v.Copy(false, canonicalPath)
r.activeFiles[canonicalPath] = canonicalizedAdd
delete(r.tombstones, canonicalPath)
r.sizeInBytes += canonicalizedAdd.Size
case *action.RemoveFile:
canonicalPath, err := path.Canonicalize(v.Path, r.storageType)
if err != nil {
return err
}
canonicalizedRemove := v.Copy(false, canonicalPath)
if removeFile, ok := r.activeFiles[canonicalPath]; ok {
delete(r.activeFiles, canonicalPath)
r.sizeInBytes -= removeFile.Size
}
r.tombstones[canonicalPath] = canonicalizedRemove
default:
{
}
}
}
return iter.Close()
}