allow meta merging

This commit is contained in:
sawka 2024-05-13 00:12:55 -07:00
parent 9bb6e27201
commit b0762f5ce1
2 changed files with 26 additions and 4 deletions

View File

@ -167,7 +167,7 @@ func (s *BlockStore) ListFiles(ctx context.Context, blockId string) ([]*BlockFil
return files, nil 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) file, ok := s.getFileFromCache(blockId, name)
if !ok { if !ok {
dbFile, err := dbGetBlockFile(ctx, blockId, name) dbFile, err := dbGetBlockFile(ctx, blockId, name)
@ -186,7 +186,17 @@ func (s *BlockStore) WriteMeta(ctx context.Context, blockId string, name string,
return return
} }
newFileEntry := entry.copyOrCreateFileEntry(file) 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 = newFileEntry
entry.FileEntry.File.ModTs = time.Now().UnixMilli() entry.FileEntry.File.ModTs = time.Now().UnixMilli()
entry.Version++ entry.Version++

View File

@ -104,7 +104,7 @@ func TestSetMeta(t *testing.T) {
if GBS.getCacheSize() != 0 { if GBS.getCacheSize() != 0 {
t.Errorf("cache size mismatch -- should have 0 entries after create") 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 { if err != nil {
t.Fatalf("error setting meta: %v", err) t.Fatalf("error setting meta: %v", err)
} }
@ -115,8 +115,20 @@ func TestSetMeta(t *testing.T) {
if file == nil { if file == nil {
t.Fatalf("file not found") 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 { if GBS.getCacheSize() != 1 {
t.Errorf("cache size mismatch") 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")
} }