Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Custom cache #947

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions storage/filesystem/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,27 @@ import (
type ObjectStorage struct {
options Options

// deltaBaseCache is an object cache uses to cache delta's bases when
deltaBaseCache cache.Object

dir *dotgit.DotGit
index map[plumbing.Hash]idxfile.Index
}

// NewObjectStorage creates a new ObjectStorage with the given .git directory.
func NewObjectStorage(dir *dotgit.DotGit) (ObjectStorage, error) {
func NewObjectStorage(dir *dotgit.DotGit) *ObjectStorage {
return NewObjectStorageWithOptions(dir, Options{})
}

// NewObjectStorageWithOptions creates a new ObjectStorage with the given .git
// directory and sets its options.
func NewObjectStorageWithOptions(
dir *dotgit.DotGit,
ops Options,
) (ObjectStorage, error) {
s := ObjectStorage{
options: ops,
deltaBaseCache: cache.NewObjectLRUDefault(),
dir: dir,
func NewObjectStorageWithOptions(dir *dotgit.DotGit, ops Options) *ObjectStorage {
s := &ObjectStorage{
options: ops,
dir: dir,
}
if s.options.Cache == nil {
s.options.Cache = cache.NewObjectLRUDefault()
}

return s, nil
return s
}

func (s *ObjectStorage) requireIndex() error {
Expand Down Expand Up @@ -181,10 +177,7 @@ func (s *ObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (p
// Create a new object storage with the DotGit(s) and check for the
// required hash object. Skip when not found.
for _, dg := range dotgits {
o, oe := NewObjectStorage(dg)
if oe != nil {
continue
}
o := NewObjectStorage(dg)
enobj, enerr := o.EncodedObject(t, h)
if enerr != nil {
continue
Expand Down Expand Up @@ -295,9 +288,13 @@ func (s *ObjectStorage) decodeObjectAt(
idx idxfile.Index,
offset int64,
) (plumbing.EncodedObject, error) {
if s.options.Cache == nil {
s.options.Cache = cache.NewObjectLRUDefault()
}

hash, err := idx.FindHash(offset)
if err == nil {
obj, ok := s.deltaBaseCache.Get(hash)
obj, ok := s.options.Cache.Get(hash)
if ok {
return obj, nil
}
Expand All @@ -307,12 +304,7 @@ func (s *ObjectStorage) decodeObjectAt(
return nil, err
}

var p *packfile.Packfile
if s.deltaBaseCache != nil {
p = packfile.NewPackfileWithCache(idx, s.dir.Fs(), f, s.deltaBaseCache)
} else {
p = packfile.NewPackfile(idx, s.dir.Fs(), f)
}
p := packfile.NewPackfileWithCache(idx, s.dir.Fs(), f, s.options.Cache)

return p.GetByOffset(offset)
}
Expand Down Expand Up @@ -418,7 +410,7 @@ func (s *ObjectStorage) buildPackfileIters(t plumbing.ObjectType, seen map[plumb
if err != nil {
return nil, err
}
return newPackfileIter(s.dir.Fs(), pack, t, seen, s.index[h], s.deltaBaseCache)
return newPackfileIter(s.dir.Fs(), pack, t, seen, s.index[h], s.options.Cache)
},
}, nil
}
Expand Down
20 changes: 6 additions & 14 deletions storage/filesystem/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ var _ = Suite(&FsSuite{})

func (s *FsSuite) TestGetFromObjectFile(c *C) {
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit()
o, err := NewObjectStorage(dotgit.New(fs))
c.Assert(err, IsNil)
o := NewObjectStorage(dotgit.New(fs))

expected := plumbing.NewHash("f3dfe29d268303fc6e1bbce268605fc99573406e")
obj, err := o.EncodedObject(plumbing.AnyObject, expected)
Expand All @@ -38,8 +37,7 @@ func (s *FsSuite) TestGetFromObjectFile(c *C) {
func (s *FsSuite) TestGetFromPackfile(c *C) {
fixtures.Basic().ByTag(".git").Test(c, func(f *fixtures.Fixture) {
fs := f.DotGit()
o, err := NewObjectStorage(dotgit.New(fs))
c.Assert(err, IsNil)
o := NewObjectStorage(dotgit.New(fs))

expected := plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
obj, err := o.EncodedObject(plumbing.AnyObject, expected)
Expand All @@ -50,8 +48,7 @@ func (s *FsSuite) TestGetFromPackfile(c *C) {

func (s *FsSuite) TestGetFromPackfileMultiplePackfiles(c *C) {
fs := fixtures.ByTag(".git").ByTag("multi-packfile").One().DotGit()
o, err := NewObjectStorage(dotgit.New(fs))
c.Assert(err, IsNil)
o := NewObjectStorage(dotgit.New(fs))

expected := plumbing.NewHash("8d45a34641d73851e01d3754320b33bb5be3c4d3")
obj, err := o.getFromPackfile(expected, false)
Expand All @@ -67,8 +64,7 @@ func (s *FsSuite) TestGetFromPackfileMultiplePackfiles(c *C) {
func (s *FsSuite) TestIter(c *C) {
fixtures.ByTag(".git").ByTag("packfile").Test(c, func(f *fixtures.Fixture) {
fs := f.DotGit()
o, err := NewObjectStorage(dotgit.New(fs))
c.Assert(err, IsNil)
o := NewObjectStorage(dotgit.New(fs))

iter, err := o.IterEncodedObjects(plumbing.AnyObject)
c.Assert(err, IsNil)
Expand All @@ -88,8 +84,7 @@ func (s *FsSuite) TestIterWithType(c *C) {
fixtures.ByTag(".git").Test(c, func(f *fixtures.Fixture) {
for _, t := range objectTypes {
fs := f.DotGit()
o, err := NewObjectStorage(dotgit.New(fs))
c.Assert(err, IsNil)
o := NewObjectStorage(dotgit.New(fs))

iter, err := o.IterEncodedObjects(t)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -271,10 +266,7 @@ func BenchmarkGetObjectFromPackfile(b *testing.B) {
for _, f := range fixtures.Basic() {
b.Run(f.URL, func(b *testing.B) {
fs := f.DotGit()
o, err := NewObjectStorage(dotgit.New(fs))
if err != nil {
b.Fatal(err)
}
o := NewObjectStorage(dotgit.New(fs))

for i := 0; i < b.N; i++ {
expected := plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
Expand Down
12 changes: 6 additions & 6 deletions storage/filesystem/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package filesystem

import (
"gopkg.in/src-d/go-git.v4/plumbing/cache"
"gopkg.in/src-d/go-git.v4/storage/filesystem/dotgit"

"gopkg.in/src-d/go-billy.v4"
Expand All @@ -24,6 +25,9 @@ type Storage struct {

// Options holds configuration for the storage.
type Options struct {
// Cache is an object cache used to cache deltas.
Cache cache.Object

// ExclusiveAccess means that the filesystem is not modified externally
// while the repo is open.
ExclusiveAccess bool
Expand All @@ -44,16 +48,11 @@ func NewStorageWithOptions(
}

dir := dotgit.NewWithOptions(fs, dirOps)
o, err := NewObjectStorageWithOptions(dir, ops)
if err != nil {
return nil, err
}

return &Storage{
fs: fs,
dir: dir,

ObjectStorage: o,
ObjectStorage: ObjectStorage{options: ops, dir: dir},
ReferenceStorage: ReferenceStorage{dir: dir},
IndexStorage: IndexStorage{dir: dir},
ShallowStorage: ShallowStorage{dir: dir},
Expand All @@ -67,6 +66,7 @@ func (s *Storage) Filesystem() billy.Filesystem {
return s.fs
}

// Init initializes .git directory
func (s *Storage) Init() error {
return s.dir.Initialize()
}