Skip to content

Commit

Permalink
return dirty pages tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
myxo committed May 18, 2024
1 parent 8e1883d commit f895534
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 10 additions & 1 deletion memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,19 @@ func (f *FakeFile) pwrite(b []byte, off int64) (n int, err error) {
}
n = copy(f.data.buff[off:], b)

//f.data.dyrtyPages = append(f.data.dyrtyPages, interval{from: off, to: off + int64(n)})
f.appendDirtyPage(off, off+int64(n))
return n, nil
}

func (f *FakeFile) appendDirtyPage(from int64, to int64) {
if !f.data.fs.trackDirtyPages {
return
}

// TODO: want to add some optimization to less allocation (e.g. for situation then we write sequentially)
f.data.dyrtyPages = append(f.data.dyrtyPages, interval{from: from, to: to})
}

func (f *FakeFile) WriteString(s string) (n int, err error) {
b := unsafe.Slice(unsafe.StringData(s), len(s))
return f.Write(b)
Expand Down
11 changes: 8 additions & 3 deletions memory_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
)

type FakeFS struct {
inodes map[string]*mockData
workDir string
inodes map[string]*mockData
workDir string
trackDirtyPages bool
}

var _ FS = &FakeFS{}
Expand All @@ -36,6 +37,10 @@ func NewMemoryFs() *FakeFS {
return fs
}

func (f *FakeFS) TrackDirtyPages() {
f.trackDirtyPages = true
}

func (f *FakeFS) Create(path string) (*File, error) {
return f.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
}
Expand Down Expand Up @@ -219,7 +224,7 @@ func (f *FakeFS) ReadDir(name string) ([]os.DirEntry, error) {
defer fp.Close()

dirs, err := fp.ReadDir(-1)
slices.SortFunc(dirs, func (a os.DirEntry, b os.DirEntry) int { return cmp.Compare(a.Name(), b.Name())})
slices.SortFunc(dirs, func(a os.DirEntry, b os.DirEntry) int { return cmp.Compare(a.Name(), b.Name()) })
return dirs, err
}

Expand Down

0 comments on commit f895534

Please sign in to comment.