-
Notifications
You must be signed in to change notification settings - Fork 342
/
adapter.go
160 lines (135 loc) · 4.38 KB
/
adapter.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
package transient
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"net/http"
"github.com/google/uuid"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/logging"
)
var ErrInventoryNotImplemented = errors.New("inventory feature not implemented for transient storage adapter")
type Adapter struct{}
func New() *Adapter {
return &Adapter{}
}
func (a *Adapter) Put(_ context.Context, _ block.ObjectPointer, _ int64, reader io.Reader, _ block.PutOpts) error {
_, err := io.Copy(io.Discard, reader)
return err
}
func (a *Adapter) Get(_ context.Context, obj block.ObjectPointer, expectedSize int64) (io.ReadCloser, error) {
if expectedSize < 0 {
return nil, io.ErrUnexpectedEOF
}
return io.NopCloser(&io.LimitedReader{R: rand.Reader, N: expectedSize}), nil
}
func (a *Adapter) GetPreSignedURL(_ context.Context, obj block.ObjectPointer, _ block.PreSignMode) (string, error) {
return "", nil
}
func (a *Adapter) Exists(_ context.Context, obj block.ObjectPointer) (bool, error) {
return true, nil
}
func (a *Adapter) GetRange(_ context.Context, obj block.ObjectPointer, startPosition int64, endPosition int64) (io.ReadCloser, error) {
n := endPosition - startPosition
if n < 0 {
return nil, io.ErrUnexpectedEOF
}
reader := &io.LimitedReader{
R: rand.Reader,
N: n,
}
return io.NopCloser(reader), nil
}
func (a *Adapter) GetProperties(_ context.Context, _ block.ObjectPointer) (block.Properties, error) {
return block.Properties{}, nil
}
func (a *Adapter) Remove(_ context.Context, _ block.ObjectPointer) error {
return nil
}
func (a *Adapter) Copy(_ context.Context, _, _ block.ObjectPointer) error {
return nil
}
func (a *Adapter) UploadCopyPart(_ context.Context, sourceObj, destinationObj block.ObjectPointer, uploadID string, partNumber int) (*block.UploadPartResponse, error) {
h := sha256.New()
code := h.Sum(nil)
etag := hex.EncodeToString(code)
return &block.UploadPartResponse{
ETag: etag,
}, nil
}
func (a *Adapter) UploadCopyPartRange(_ context.Context, sourceObj, destinationObj block.ObjectPointer, uploadID string, partNumber int, startPosition, endPosition int64) (*block.UploadPartResponse, error) {
n := endPosition - startPosition
if n < 0 {
return nil, io.ErrUnexpectedEOF
}
h := sha256.New()
code := h.Sum(nil)
etag := hex.EncodeToString(code)
return &block.UploadPartResponse{
ETag: etag,
}, nil
}
func (a *Adapter) Walk(_ context.Context, walkOpt block.WalkOpts, walkFn block.WalkFunc) error {
return nil
}
func (a *Adapter) CreateMultiPartUpload(_ context.Context, obj block.ObjectPointer, r *http.Request, opts block.CreateMultiPartUploadOpts) (*block.CreateMultiPartUploadResponse, error) {
uid := uuid.New()
uploadID := hex.EncodeToString(uid[:])
return &block.CreateMultiPartUploadResponse{
UploadID: uploadID,
}, nil
}
func (a *Adapter) UploadPart(_ context.Context, obj block.ObjectPointer, sizeBytes int64, reader io.Reader, uploadID string, partNumber int) (*block.UploadPartResponse, error) {
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
h := sha256.New()
_, err = h.Write(data)
if err != nil {
return nil, err
}
code := h.Sum(nil)
etag := hex.EncodeToString(code)
return &block.UploadPartResponse{
ETag: etag,
}, nil
}
func (a *Adapter) AbortMultiPartUpload(context.Context, block.ObjectPointer, string) error {
return nil
}
func (a *Adapter) CompleteMultiPartUpload(context.Context, block.ObjectPointer, string, *block.MultipartUploadCompletion) (*block.CompleteMultiPartUploadResponse, error) {
const dataSize = 1024
data := make([]byte, dataSize)
if _, err := rand.Read(data); err != nil {
return nil, err
}
h := sha256.New()
_, err := h.Write(data)
if err != nil {
return nil, err
}
code := h.Sum(nil)
codeHex := hex.EncodeToString(code)
return &block.CompleteMultiPartUploadResponse{
ETag: codeHex,
ContentLength: dataSize,
}, nil
}
func (a *Adapter) GenerateInventory(_ context.Context, _ logging.Logger, _ string, _ bool, _ []string) (block.Inventory, error) {
return nil, ErrInventoryNotImplemented
}
func (a *Adapter) BlockstoreType() string {
return block.BlockstoreTypeTransient
}
func (a *Adapter) GetStorageNamespaceInfo() block.StorageNamespaceInfo {
info := block.DefaultStorageNamespaceInfo(block.BlockstoreTypeTransient)
info.PreSignSupport = false
return info
}
func (a *Adapter) RuntimeStats() map[string]string {
return nil
}