From b0762f5ce16e33efcd1e95782a68421bf3d2ae75 Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 13 May 2024 00:12:55 -0700 Subject: [PATCH] allow meta merging --- pkg/blockstore/blockstore.go | 14 ++++++++++++-- pkg/blockstore/blockstore_test.go | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/pkg/blockstore/blockstore.go b/pkg/blockstore/blockstore.go index 95514f4a8..36cde7e47 100644 --- a/pkg/blockstore/blockstore.go +++ b/pkg/blockstore/blockstore.go @@ -167,7 +167,7 @@ func (s *BlockStore) ListFiles(ctx context.Context, blockId string) ([]*BlockFil return files, nil } -func (s *BlockStore) WriteMeta(ctx context.Context, blockId string, name string, meta FileMeta) error { +func (s *BlockStore) WriteMeta(ctx context.Context, blockId string, name string, meta FileMeta, merge bool) error { file, ok := s.getFileFromCache(blockId, name) if !ok { dbFile, err := dbGetBlockFile(ctx, blockId, name) @@ -186,7 +186,17 @@ func (s *BlockStore) WriteMeta(ctx context.Context, blockId string, name string, return } newFileEntry := entry.copyOrCreateFileEntry(file) - newFileEntry.File.Meta = meta + if merge { + for k, v := range meta { + if v == nil { + delete(newFileEntry.File.Meta, k) + continue + } + newFileEntry.File.Meta[k] = v + } + } else { + newFileEntry.File.Meta = meta + } entry.FileEntry = newFileEntry entry.FileEntry.File.ModTs = time.Now().UnixMilli() entry.Version++ diff --git a/pkg/blockstore/blockstore_test.go b/pkg/blockstore/blockstore_test.go index 4f4dec6a8..d963dd8cd 100644 --- a/pkg/blockstore/blockstore_test.go +++ b/pkg/blockstore/blockstore_test.go @@ -104,7 +104,7 @@ func TestSetMeta(t *testing.T) { if GBS.getCacheSize() != 0 { t.Errorf("cache size mismatch -- should have 0 entries after create") } - err = GBS.WriteMeta(ctx, blockId, "testfile", map[string]any{"a": 5, "b": "hello"}) + err = GBS.WriteMeta(ctx, blockId, "testfile", map[string]any{"a": 5, "b": "hello", "q": 8}, false) if err != nil { t.Fatalf("error setting meta: %v", err) } @@ -115,8 +115,20 @@ func TestSetMeta(t *testing.T) { if file == nil { t.Fatalf("file not found") } - checkMapsEqual(t, map[string]any{"a": 5, "b": "hello"}, file.Meta, "meta") + checkMapsEqual(t, map[string]any{"a": 5, "b": "hello", "q": 8}, file.Meta, "meta") if GBS.getCacheSize() != 1 { t.Errorf("cache size mismatch") } + err = GBS.WriteMeta(ctx, blockId, "testfile", map[string]any{"a": 6, "c": "world", "d": 7, "q": nil}, true) + if err != nil { + t.Fatalf("error setting meta: %v", err) + } + file, err = GBS.Stat(ctx, blockId, "testfile") + if err != nil { + t.Fatalf("error stating file: %v", err) + } + if file == nil { + t.Fatalf("file not found") + } + checkMapsEqual(t, map[string]any{"a": 6, "b": "hello", "c": "world", "d": 7}, file.Meta, "meta") }