Storage: remove pointer on write path (#47580)

* remove pointer to contents in UpsertFileCommand

* add command to UpsertFileCommand

* fix client after API refactor
This commit is contained in:
Artur Wierzbicki 2022-04-12 16:58:09 +02:00 committed by GitHub
parent 7f5e8bb00f
commit 712b239d5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 50 deletions

View File

@ -51,9 +51,12 @@ type Paging struct {
}
type UpsertFileCommand struct {
Path string
MimeType string
Contents *[]byte
Path string
MimeType string
// Contents of an existing file won't be modified if cmd.Contents is nil
Contents []byte
// Properties of an existing file won't be modified if cmd.Properties is nil
Properties map[string]string
}

View File

@ -98,7 +98,7 @@ func (c cdkBlobStorage) Upsert(ctx context.Context, command *UpsertFileCommand)
if command.Contents == nil {
contents = make([]byte, 0)
} else {
contents = *command.Contents
contents = command.Contents
}
metadata = make(map[string]string)
@ -115,7 +115,7 @@ func (c cdkBlobStorage) Upsert(ctx context.Context, command *UpsertFileCommand)
contents = existing.Contents
if command.Contents != nil {
contents = *command.Contents
contents = command.Contents
}
if command.Properties != nil {

View File

@ -141,7 +141,7 @@ func (s dbFileStorage) Upsert(ctx context.Context, cmd *UpsertFileCommand) error
if exists {
existing.Updated = now
if cmd.Contents != nil {
contents := *cmd.Contents
contents := cmd.Contents
existing.Contents = contents
existing.MimeType = cmd.MimeType
existing.Size = int64(len(contents))
@ -154,7 +154,7 @@ func (s dbFileStorage) Upsert(ctx context.Context, cmd *UpsertFileCommand) error
} else {
contentsToInsert := make([]byte, 0)
if cmd.Contents != nil {
contentsToInsert = *cmd.Contents
contentsToInsert = cmd.Contents
}
file := &file{

View File

@ -152,7 +152,7 @@ func runTests(createCases func() []fsTestCase, t *testing.T) {
func TestFsStorage(t *testing.T) {
//skipTest := true
emptyFileBytes := make([]byte, 0)
emptyContents := make([]byte, 0)
pngImage, _ := base64.StdEncoding.DecodeString(pngImageBase64)
pngImageSize := int64(len(pngImage))
@ -164,21 +164,21 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/folder2/file.jpg",
Contents: &[]byte{},
Contents: emptyContents,
Properties: map[string]string{"prop1": "val1", "prop2": "val"},
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/file-inner.jpg",
Contents: &[]byte{},
Contents: emptyContents,
Properties: map[string]string{"prop1": "val1", "prop2": "val"},
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/file-inner2.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
queryListFiles{
@ -261,25 +261,25 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/ab/a.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/ab/a/a.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/ac/a.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/aba/a.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
queryListFiles{
@ -307,14 +307,14 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/folder2/file.jpg",
Contents: &[]byte{},
Contents: emptyContents,
Properties: map[string]string{"prop1": "val1", "prop2": "val"},
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/file-inner.jpg",
Contents: &[]byte{},
Contents: emptyContents,
Properties: map[string]string{"prop1": "val1"},
},
},
@ -370,13 +370,13 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/folder2/file.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/file-inner.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
queryListFiles{
@ -411,19 +411,19 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/a",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/b",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder2/c",
Contents: &[]byte{},
Contents: emptyContents,
},
},
queryListFiles{
@ -533,25 +533,25 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/folder2/file.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/file-inner.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folderX/folderZ/file.txt",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folderA/folderB/file.txt",
Contents: &[]byte{},
Contents: emptyContents,
},
},
queryListFolders{
@ -585,25 +585,25 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/folder2/file.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder1/file-inner.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folderX/folderZ/file.txt",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folderA/folderB/file.txt",
Contents: &[]byte{},
Contents: emptyContents,
},
},
queryListFolders{
@ -673,7 +673,7 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder/a.png",
Contents: &pngImage,
Contents: pngImage,
Properties: map[string]string{"prop1": "val1", "prop2": "val"},
},
},
@ -698,7 +698,7 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/Folder/A.png",
Contents: &emptyFileBytes,
Contents: emptyContents,
},
},
queryGet{
@ -718,7 +718,7 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/a.png",
Contents: &pngImage,
Contents: pngImage,
Properties: map[string]string{"a": "av", "b": "bv"},
},
},
@ -754,7 +754,7 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/aB.png",
Contents: &emptyFileBytes,
Contents: emptyContents,
Properties: map[string]string{"a": "av", "b": "bv"},
},
},
@ -791,7 +791,7 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/FILE.png",
Contents: &emptyFileBytes,
Contents: emptyContents,
Properties: map[string]string{"a": "av", "b": "bv"},
},
},
@ -803,13 +803,13 @@ func TestFsStorage(t *testing.T) {
fName("FILE.png"),
fProperties(map[string]string{"a": "av", "b": "bv"}),
fSize(0),
fContents(emptyFileBytes),
fContents(emptyContents),
),
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/file.png",
Contents: &pngImage,
Contents: pngImage,
},
},
queryGet{
@ -832,7 +832,7 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/FILE.png",
Contents: &emptyFileBytes,
Contents: emptyContents,
Properties: map[string]string{"a": "av", "b": "bv"},
},
},
@ -855,7 +855,7 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/file.png",
Contents: &emptyFileBytes,
Contents: emptyContents,
Properties: map[string]string{"a": "av", "b": "bv"},
},
},
@ -888,7 +888,7 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/aB/cD/eF/file.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
queryListFolders{
@ -1007,7 +1007,7 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/a/b/c/file.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
queryListFolders{
@ -1119,7 +1119,7 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/folder/dashboards/myNewFolder/file.jpg",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdDeleteFolder{
@ -1164,43 +1164,43 @@ func TestFsStorage(t *testing.T) {
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/s3/folder/dashboard.json",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/s3/folder/nested/dashboard.json",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/gitA/dashboard1.json",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/gitA/dashboard2.json",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/gitB/nested/dashboard.json",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/gitB/nested2/dashboard2.json",
Contents: &[]byte{},
Contents: emptyContents,
},
},
cmdUpsert{
cmd: UpsertFileCommand{
Path: "/gitC/nestedC/dashboardC.json",
Contents: &[]byte{},
Contents: emptyContents,
},
},
queryListFiles{

View File

@ -82,7 +82,7 @@ func (s *rootStorageDisk) Write(ctx context.Context, cmd *WriteValueRequest) (*W
}
err := s.store.Upsert(ctx, &filestorage.UpsertFileCommand{
Path: path,
Contents: &byteAray,
Contents: byteAray,
})
if err != nil {
return nil, err