-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtablefile.go
72 lines (60 loc) · 1.72 KB
/
tablefile.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
package doltswarm
import (
"context"
"fmt"
"io"
"strconv"
"time"
remotesapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/remotesapi/v1alpha1"
"github.com/nustiueudinastea/doltswarm/proto"
)
// RemoteTableFile is an implementation of a TableFile that lives in a DoltChunkStore
type RemoteTableFile struct {
client *DBClient
info *remotesapi.TableFileInfo
}
// LocationPrefix
func (rtf RemoteTableFile) LocationPrefix() string {
return ""
}
// FileID gets the id of the file
func (rtf RemoteTableFile) FileID() string {
return rtf.info.FileId
}
// NumChunks returns the number of chunks in a table file
func (rtf RemoteTableFile) NumChunks() int {
return int(rtf.info.NumChunks)
}
// Open returns an io.ReadCloser which can be used to read the bytes of a table file.
func (rtf RemoteTableFile) Open(ctx context.Context) (io.ReadCloser, uint64, error) {
if rtf.info.RefreshAfter != nil && rtf.info.RefreshAfter.AsTime().After(time.Now()) {
resp, err := rtf.client.RefreshTableFileUrl(ctx, rtf.info.RefreshRequest)
if err == nil {
rtf.info.Url = resp.Url
rtf.info.RefreshAfter = resp.RefreshAfter
}
}
response, err := rtf.client.DownloadFile(
ctx,
&proto.DownloadFileRequest{Id: rtf.info.FileId},
)
if err != nil {
return nil, 0, fmt.Errorf("client.LoadFile: %w", err)
}
md, err := response.Header()
if err != nil {
return nil, 0, fmt.Errorf("response.Header: %w", err)
}
var size uint64
if sizes := md.Get("file-size"); len(sizes) > 0 {
size, err = strconv.ParseUint(sizes[0], 10, 64)
if err != nil {
return nil, 0, fmt.Errorf("response.Header: file size header not valid: %w", err)
}
} else {
size = 0
}
r, w := io.Pipe()
go copyFileChunksFromResponse(w, response)
return r, size, nil
}