mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-01-02 18:39:05 +01:00
allow meta merging
This commit is contained in:
parent
9bb6e27201
commit
b0762f5ce1
@ -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++
|
||||
|
@ -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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user