-
Notifications
You must be signed in to change notification settings - Fork 351
/
delta.go
164 lines (145 loc) · 4.42 KB
/
delta.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
package formats
import (
"context"
"errors"
"fmt"
"net/url"
"regexp"
"github.com/Shopify/go-lua"
"github.com/aws/aws-sdk-go-v2/aws"
delta "github.com/csimplestring/delta-go"
"github.com/csimplestring/delta-go/storage"
deltaStore "github.com/csimplestring/delta-go/store"
luautil "github.com/treeverse/lakefs/pkg/actions/lua/util"
)
type storageType string
const (
s3StorageType storageType = "s3"
)
var errUnimplementedProvided = errors.New("unimplemented provider")
type DeltaClient struct {
accessProvider AccessProvider
ctx context.Context
}
func (dc *DeltaClient) fetchS3Table(repo, ref, prefix string, awsProps *storage.AWSProperties) (map[int64][]string, error) {
table, err := dc.getS3DeltaTable(repo, ref, prefix, awsProps)
if err != nil {
return nil, err
}
return dc.buildLog(table)
}
func (dc *DeltaClient) getS3DeltaTable(repo, ref, prefix string, awsProps *storage.AWSProperties) (delta.Log, error) {
config := delta.Config{StoreType: string(s3StorageType)}
u := fmt.Sprintf("lakefs://%s/%s/%s", repo, ref, prefix)
parsedURL, err := url.Parse(u)
if err != nil {
return nil, err
}
s3LogStore, err := deltaStore.NewS3CompatLogStore(awsProps, parsedURL)
if err != nil {
return nil, err
}
store := deltaStore.Store(s3LogStore)
return delta.ForTableWithStore(u, config, &delta.SystemClock{}, &store)
}
func (dc *DeltaClient) buildLog(table delta.Log) (map[int64][]string, error) {
s, err := table.Snapshot()
if err != nil {
return nil, err
}
version, err := s.EarliestVersion()
if err != nil {
return nil, err
}
versionLog, err := table.Changes(version, false)
if err != nil {
return nil, err
}
entries := make(map[int64][]string)
for entry, err := versionLog.Next(); err == nil; entry, err = versionLog.Next() {
strLog := make([]string, 0)
entryVersion := entry.Version()
actions, aErr := entry.Actions()
if aErr != nil {
return nil, aErr
}
for _, a := range actions {
aj, _ := a.Json()
strLog = append(strLog, aj)
}
entries[entryVersion] = strLog
}
return entries, nil
}
func (dc *DeltaClient) fetchTableLog(repo, ref, prefix string) (map[int64][]string, error) {
ap, _ := dc.accessProvider.GetAccessProperties()
switch access := ap.(type) {
case AWSInfo:
return dc.fetchS3Table(repo, ref, prefix, &access.AWSProps)
default:
return nil, errUnimplementedProvided
}
}
func getTable(client *DeltaClient) lua.Function {
return func(l *lua.State) int {
repo := lua.CheckString(l, 1)
ref := lua.CheckString(l, 2)
prefix := lua.CheckString(l, 3)
tableLog, err := client.fetchTableLog(repo, ref, prefix)
if err != nil {
lua.Errorf(l, "%s", err.Error())
panic("failed fetching table log")
}
luautil.DeepPush(l, tableLog)
return 1
}
}
var functions = map[string]func(client *DeltaClient) lua.Function{
"get_table": getTable,
}
// AccessProvider is used to provide different expected access properties to different storage providers
type AccessProvider interface {
GetAccessProperties() (interface{}, error)
}
type AWSInfo struct {
AWSProps storage.AWSProperties
}
func (awsI AWSInfo) GetAccessProperties() (interface{}, error) {
return awsI, nil
}
// newDelta is a factory function to create server/cloud specific Delta Lake client
// lakeFSAddr is the domain or "authority:port" of the running lakeFS server
func newDelta(ctx context.Context, lakeFSAddr string) lua.Function {
if regexp.MustCompile(`^:\d+`).MatchString(lakeFSAddr) {
// workaround in case we listen on all interfaces without specifying ip
lakeFSAddr = fmt.Sprintf("localhost%s", lakeFSAddr)
}
lakeFSAddr = fmt.Sprintf("http://%s", lakeFSAddr)
return func(l *lua.State) int {
client := newS3DeltaClient(l, ctx, lakeFSAddr)
l.NewTable()
for name, goFn := range functions {
l.PushGoFunction(goFn(client))
l.SetField(-2, name)
}
return 1
}
}
func newS3DeltaClient(l *lua.State, ctx context.Context, lakeFSAddr string) *DeltaClient {
accessKeyID := lua.CheckString(l, 1)
secretAccessKey := lua.CheckString(l, 2)
r := lua.CheckString(l, 3)
awsProps := storage.AWSProperties{
Region: r,
ForcePathStyle: true,
CredsProvider: aws.CredentialsProviderFunc(func(context.Context) (aws.Credentials, error) {
return aws.Credentials{
AccessKeyID: accessKeyID,
SecretAccessKey: secretAccessKey,
}, nil
}),
Endpoint: lakeFSAddr,
}
storage.RegisterS3CompatBucketURLOpener("lakefs", &awsProps)
return &DeltaClient{accessProvider: AWSInfo{AWSProps: awsProps}, ctx: ctx}
}