cachefs/pkg/fs/metadata.go
2022-03-13 18:49:27 +01:00

52 lines
957 B
Go

// Copyright (C) 2022 Marius Schellenberger
package fs
import (
"os"
"sync"
)
type Metadata struct {
mu sync.RWMutex `json:"-"`
f *os.File `json:"-"`
Size int64 `json:"s"`
Chunks Chunks `json:"c"`
preload bool `json:"-"`
}
func (md *Metadata) HasChunk(off int64, n int) bool {
md.mu.RLock()
defer md.mu.RUnlock()
return md.Chunks.Exists(off, n, md.Size)
}
func (md *Metadata) AddChunk(off int64, n int) {
md.mu.Lock()
defer md.mu.Unlock()
md.Chunks.Add(off, n)
}
func (md *Metadata) ReadAt(p []byte, pos int64) (int, error) {
return md.f.ReadAt(p, pos)
}
func (md *Metadata) WriteAt(data []byte, pos int64) (int, error) {
return md.f.WriteAt(data, pos)
}
func (md *Metadata) Preload() bool {
md.mu.Lock()
defer md.mu.Unlock()
if md.preload {
return true
}
md.preload = true
return false
}
func (md *Metadata) UnlockPreload() {
md.mu.Lock()
defer md.mu.Unlock()
md.preload = false
}