-
Notifications
You must be signed in to change notification settings - Fork 353
/
dbfs.go
293 lines (262 loc) · 7.66 KB
/
dbfs.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
package service
import (
"encoding/base64"
"encoding/json"
"github.com/databrickslabs/databricks-terraform/client/model"
"log"
"net/http"
)
// DBFSAPI exposes the DBFS API
type DBFSAPI struct {
Client DBApiClient
}
// Create creates a file in DBFS given data string in base64
func (a DBFSAPI) Create(path string, overwrite bool, data string) (err error) {
byteArr, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return err
}
byteChunks := split(byteArr, 1e6)
handle, err := a.createHandle(path, overwrite)
if err != nil {
return err
}
defer func() {
err = a.closeHandle(handle)
}()
for _, byteChunk := range byteChunks {
b64Data := base64.StdEncoding.EncodeToString(byteChunk)
err := a.addBlock(b64Data, handle)
if err != nil {
return err
}
}
return err
}
// Read returns the contents of a file in DBFS as a base64 encoded string
func (a DBFSAPI) Read(path string) (string, error) {
var bytesFetched []byte
fetchLoop := true
offSet := int64(0)
length := int64(1e6)
for fetchLoop {
bytesRead, bytes, err := a.read(path, offSet, length)
if err != nil {
return "", err
}
if bytesRead == 0 || bytesRead < length {
fetchLoop = false
}
bytesFetched = append(bytesFetched, bytes...)
offSet = offSet + length
}
resp := base64.StdEncoding.EncodeToString(bytesFetched)
return resp, nil
}
// Copy copies a file given a source location and a target location and a provided client for source location
func (a DBFSAPI) Copy(src string, tgt string, client *DBApiClient, overwrite bool) error {
handle, err := a.createHandle(tgt, overwrite)
if err != nil {
return err
}
defer func() {
err = a.closeHandle(handle)
}()
fetchLoop := true
offSet := int64(0)
length := int64(1e6)
for fetchLoop {
var api DBFSAPI
if client == nil {
api = a
} else {
api = client.DBFS()
}
bytesRead, b64String, err := api.ReadString(src, offSet, length)
if err != nil {
return err
}
log.Println(bytesRead)
if bytesRead == 0 || bytesRead < length {
fetchLoop = false
}
err = a.addBlock(b64String, handle)
if err != nil {
return err
}
offSet = offSet + length
}
return err
}
// Move moves the file between DBFS locations via DBFS api
func (a DBFSAPI) Move(src string, tgt string) error {
moveRequest := struct {
SourcePath string `json:"source_path,omitempty" url:"source_path,omitempty"`
DestinationPath string `json:"destination_path,omitempty" url:"destination_path,omitempty"`
}{
SourcePath: src,
DestinationPath: tgt,
}
_, err := a.Client.performQuery(http.MethodPost, "/dbfs/move", "2.0", nil, moveRequest, nil)
return err
}
// Delete deletes a file in DBFS via API
func (a DBFSAPI) Delete(path string, recursive bool) error {
deleteRequest := struct {
Path string `json:"path,omitempty" url:"path,omitempty"`
Recursive bool `json:"recursive,omitempty" url:"recursive,omitempty"`
}{
Path: path,
Recursive: recursive,
}
_, err := a.Client.performQuery(http.MethodPost, "/dbfs/delete", "2.0", nil, deleteRequest, nil)
return err
}
// ReadString reads a "block" of data in DBFS given a offset and length as a base64 encoded string
func (a DBFSAPI) ReadString(path string, offset, length int64) (int64, string, error) {
var readBytes struct {
BytesRead int64 `json:"bytes_read,omitempty" url:"bytes_read,omitempty"`
Data string `json:"data,omitempty" url:"data,omitempty"`
}
readRequest := struct {
Path string `json:"path,omitempty" url:"path,omitempty"`
Offset int64 `json:"offset,omitempty" url:"offset,omitempty"`
Length int64 `json:"length,omitempty" url:"length,omitempty"`
}{
Path: path,
Offset: offset,
Length: length,
}
resp, err := a.Client.performQuery(http.MethodGet, "/dbfs/read", "2.0", nil, readRequest, nil)
if err != nil {
return readBytes.BytesRead, readBytes.Data, err
}
err = json.Unmarshal(resp, &readBytes)
return readBytes.BytesRead, readBytes.Data, err
}
func (a DBFSAPI) read(path string, offset, length int64) (int64, []byte, error) {
bytesRead, data, err := a.ReadString(path, offset, length)
if err != nil {
return bytesRead, nil, err
}
dataBytes, err := base64.StdEncoding.DecodeString(data)
return bytesRead, dataBytes, err
}
// Status returns the status of a file in DBFS
func (a DBFSAPI) Status(path string) (model.FileInfo, error) {
var fileInfo model.FileInfo
statusRequest := struct {
Path string `json:"path,omitempty" url:"path,omitempty"`
}{
Path: path,
}
resp, err := a.Client.performQuery(http.MethodGet, "/dbfs/get-status", "2.0", nil, statusRequest, nil)
if err != nil {
return fileInfo, err
}
err = json.Unmarshal(resp, &fileInfo)
return fileInfo, err
}
// List returns a list of files in DBFS and the recursive flag lets you recursively list files
func (a DBFSAPI) List(path string, recursive bool) ([]model.FileInfo, error) {
if recursive {
var paths []model.FileInfo
err := a.recursiveAddPaths(path, &paths)
if err != nil {
return nil, err
}
return paths, err
}
return a.list(path)
}
func (a DBFSAPI) recursiveAddPaths(path string, pathList *[]model.FileInfo) error {
fileInfoList, err := a.list(path)
if err != nil {
return err
}
for _, v := range fileInfoList {
if !v.IsDir {
*pathList = append(*pathList, v)
} else if v.IsDir {
err := a.recursiveAddPaths(v.Path, pathList)
if err != nil {
return err
}
}
}
return nil
}
func (a DBFSAPI) list(path string) ([]model.FileInfo, error) {
var dbfsList struct {
Files []model.FileInfo `json:"files,omitempty" url:"files,omitempty"`
}
listRequest := struct {
Path string `json:"path,omitempty" url:"path,omitempty"`
}{}
listRequest.Path = path
resp, err := a.Client.performQuery(http.MethodGet, "/dbfs/list", "2.0", nil, listRequest, nil)
if err != nil {
return dbfsList.Files, err
}
err = json.Unmarshal(resp, &dbfsList)
return dbfsList.Files, err
}
// Mkdirs makes the directories in DBFS include the parent paths
func (a DBFSAPI) Mkdirs(path string) error {
mkDirsRequest := struct {
Path string `json:"path,omitempty" url:"path,omitempty"`
}{}
mkDirsRequest.Path = path
_, err := a.Client.performQuery(http.MethodPost, "/dbfs/mkdirs", "2.0", nil, mkDirsRequest, nil)
return err
}
func (a DBFSAPI) createHandle(path string, overwrite bool) (int64, error) {
var handle struct {
Handle int64 `json:"handle,omitempty" url:"handle,omitempty"`
}
createDBFSHandleRequest := struct {
Path string `json:"path,omitempty" url:"path,omitempty"`
Overwrite bool `json:"overwrite,omitempty" url:"overwrite,omitempty"`
}{
Path: path,
Overwrite: overwrite,
}
resp, err := a.Client.performQuery(http.MethodPost, "/dbfs/create", "2.0", nil, createDBFSHandleRequest, nil)
if err != nil {
return handle.Handle, err
}
err = json.Unmarshal(resp, &handle)
return handle.Handle, err
}
func (a DBFSAPI) addBlock(data string, handle int64) error {
var addDBFSBlockRequest = struct {
Data string `json:"data,omitempty" url:"data,omitempty"`
Handle int64 `json:"handle,omitempty" url:"handle,omitempty"`
}{
Data: data,
Handle: handle,
}
_, err := a.Client.performQuery(http.MethodPost, "/dbfs/add-block", "2.0", nil, addDBFSBlockRequest, nil)
return err
}
func (a DBFSAPI) closeHandle(handle int64) error {
closeHandleRequest := struct {
Handle int64 `json:"handle,omitempty" url:"handle,omitempty"`
}{
Handle: handle,
}
_, err := a.Client.performQuery(http.MethodPost, "/dbfs/close", "2.0", nil, closeHandleRequest, nil)
return err
}
func split(buf []byte, lim int) [][]byte {
var chunk []byte
chunks := make([][]byte, 0, len(buf)/lim+1)
for len(buf) >= lim {
chunk, buf = buf[:lim], buf[lim:]
chunks = append(chunks, chunk)
}
if len(buf) > 0 {
chunks = append(chunks, buf[:])
}
return chunks
}