diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2017-12-29 22:18:02 +0100 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2017-12-29 22:59:22 +0100 |
commit | 83e6dd9ac97d5f2faf1528228dfcb4fdf47cb3f7 (patch) | |
tree | d31a8827718c3a058967e929b619ac436e2eae45 | |
parent | b01cef071c5e95f73e756c3d039e2ddf1a6ed3df (diff) | |
download | petrific-83e6dd9ac97d5f2faf1528228dfcb4fdf47cb3f7.tar.gz petrific-83e6dd9ac97d5f2faf1528228dfcb4fdf47cb3f7.tar.bz2 petrific-83e6dd9ac97d5f2faf1528228dfcb4fdf47cb3f7.zip |
Fix caching
-rw-r--r-- | backup/backup.go | 2 | ||||
-rw-r--r-- | backup/caching_test.go | 62 | ||||
-rw-r--r-- | fs/memory.go | 54 |
3 files changed, 91 insertions, 27 deletions
diff --git a/backup/backup.go b/backup/backup.go index 84fb554..5e4ffc1 100644 --- a/backup/backup.go +++ b/backup/backup.go @@ -108,7 +108,7 @@ func (proc writeDirProcess) writeDir( switch c.Type() { case fs.FFile: - mtime, file_id, ok := pcache.PathUpdated(abspath) + mtime, file_id, ok := pcache.PathUpdated(abspath + "/" + c.Name()) if !ok || mtime.Before(c.ModTime()) { // According to cache the file was changed diff --git a/backup/caching_test.go b/backup/caching_test.go new file mode 100644 index 0000000..6dad62b --- /dev/null +++ b/backup/caching_test.go @@ -0,0 +1,62 @@ +package backup + +import ( + "code.laria.me/petrific/cache" + "code.laria.me/petrific/fs" + "code.laria.me/petrific/objects" + "code.laria.me/petrific/storage/memory" + "testing" + "time" +) + +func TestCacheMTime(t *testing.T) { + c := cache.NewFileCache("") // location doesn't matter here, just use empty string + st := memory.NewMemoryStorage() + filesys := fs.NewMemoryFSRoot("/foo") + + file, err := filesys.CreateChildFile("bar", false) + if err != nil { + t.Fatal(err) + } + + want := file.ModTime() + if _, err := WriteDir(st, "/foo", filesys, c); err != nil { + t.Fatal(err) + } + + have, _, ok := c.PathUpdated("/foo/bar") + if !ok { + t.Fatal("cache doesn't know anything about /foo/bar") + } + + if !have.Equal(want) { + t.Errorf("Unexpected cache time for /foo/bar (want=%s, have=%s)", want, have) + } +} + +func TestCacheRetrieve(t *testing.T) { + c := cache.NewFileCache("") // location doesn't matter here, just use empty string + st := memory.NewMemoryStorage() + filesys := fs.NewMemoryFSRoot("/foo") + + if err := st.Set(objid_emptyfile, objects.OTFile, obj_emptyfile); err != nil { + t.Fatalf("could not set empty file object: %s", err) + } + + file, err := filesys.CreateChildFile("bar", false) + if err != nil { + t.Fatal(err) + } + mfile := file.(*fs.MemfsFile) + + mtime := file.ModTime().Add(1 * time.Hour) + c.SetPathUpdated("/foo/bar", mtime, objid_emptyfile) + + if _, err := WriteDir(st, "/foo", filesys, c); err != nil { + t.Fatal(err) + } + + if mfile.HasBeenRead { + t.Error("/foo/bar has been read by WriteDir") + } +} diff --git a/fs/memory.go b/fs/memory.go index 2bc11dc..49982cd 100644 --- a/fs/memory.go +++ b/fs/memory.go @@ -9,7 +9,7 @@ import ( ) type memfsBase struct { - parent *memfsDir + parent *MemfsDir name string exec bool mtime time.Time @@ -33,41 +33,43 @@ func (b memfsBase) Delete() error { return nil } -type memfsFile struct { +type MemfsFile struct { memfsBase - content *bytes.Buffer + content *bytes.Buffer + HasBeenRead bool } -func (memfsFile) Type() FileType { return FFile } +func (MemfsFile) Type() FileType { return FFile } -func (f memfsFile) Open() (io.ReadCloser, error) { +func (f *MemfsFile) Open() (io.ReadCloser, error) { return f, nil } -func (f memfsFile) OpenWritable() (io.WriteCloser, error) { +func (f MemfsFile) OpenWritable() (io.WriteCloser, error) { return f, nil } -func (f memfsFile) Read(p []byte) (int, error) { +func (f *MemfsFile) Read(p []byte) (int, error) { + f.HasBeenRead = true return f.content.Read(p) } -func (f memfsFile) Write(p []byte) (int, error) { +func (f MemfsFile) Write(p []byte) (int, error) { return f.content.Write(p) } -func (memfsFile) Close() error { +func (MemfsFile) Close() error { return nil } -type memfsDir struct { +type MemfsDir struct { memfsBase children map[string]memfsChild } -func (memfsDir) Type() FileType { return FDir } +func (MemfsDir) Type() FileType { return FDir } -func (d memfsDir) Readdir() ([]File, error) { +func (d MemfsDir) Readdir() ([]File, error) { l := make([]File, 0, len(d.children)) for _, f := range d.children { @@ -77,7 +79,7 @@ func (d memfsDir) Readdir() ([]File, error) { return l, nil } -func (d memfsDir) GetChild(name string) (File, error) { +func (d MemfsDir) GetChild(name string) (File, error) { c, ok := d.children[name] if !ok { return nil, os.ErrNotExist @@ -85,7 +87,7 @@ func (d memfsDir) GetChild(name string) (File, error) { return c, nil } -func (d memfsDir) createChildBase(name string, exec bool) memfsBase { +func (d MemfsDir) createChildBase(name string, exec bool) memfsBase { return memfsBase{ parent: &d, name: name, @@ -94,8 +96,8 @@ func (d memfsDir) createChildBase(name string, exec bool) memfsBase { } } -func (d memfsDir) CreateChildFile(name string, exec bool) (RegularFile, error) { - child := memfsFile{ +func (d MemfsDir) CreateChildFile(name string, exec bool) (RegularFile, error) { + child := MemfsFile{ memfsBase: d.createChildBase(name, exec), content: new(bytes.Buffer), } @@ -103,8 +105,8 @@ func (d memfsDir) CreateChildFile(name string, exec bool) (RegularFile, error) { return &child, nil } -func (d memfsDir) CreateChildDir(name string) (Dir, error) { - child := memfsDir{ +func (d MemfsDir) CreateChildDir(name string) (Dir, error) { + child := MemfsDir{ memfsBase: d.createChildBase(name, true), children: make(map[string]memfsChild), } @@ -112,8 +114,8 @@ func (d memfsDir) CreateChildDir(name string) (Dir, error) { return &child, nil } -func (d memfsDir) CreateChildSymlink(name string, target string) (Symlink, error) { - child := memfsSymlink{ +func (d MemfsDir) CreateChildSymlink(name string, target string) (Symlink, error) { + child := MemfsSymlink{ memfsBase: d.createChildBase(name, false), target: target, } @@ -121,11 +123,11 @@ func (d memfsDir) CreateChildSymlink(name string, target string) (Symlink, error return &child, nil } -func (d *memfsDir) deleteChild(name string) { +func (d *MemfsDir) deleteChild(name string) { delete(d.children, name) } -func (d *memfsDir) RenameChild(oldname, newname string) error { +func (d *MemfsDir) RenameChild(oldname, newname string) error { c, ok := d.children[oldname] if !ok { return os.ErrNotExist @@ -140,7 +142,7 @@ func (d *memfsDir) RenameChild(oldname, newname string) error { } func NewMemoryFSRoot(name string) Dir { - return &memfsDir{ + return &MemfsDir{ memfsBase: memfsBase{ parent: nil, name: name, @@ -151,13 +153,13 @@ func NewMemoryFSRoot(name string) Dir { } } -type memfsSymlink struct { +type MemfsSymlink struct { memfsBase target string } -func (memfsSymlink) Type() FileType { return FSymlink } +func (MemfsSymlink) Type() FileType { return FSymlink } -func (s memfsSymlink) Readlink() (string, error) { +func (s MemfsSymlink) Readlink() (string, error) { return s.target, nil } |