diff options
-rw-r--r-- | plumbing/cache/common.go | 4 | ||||
-rw-r--r-- | plumbing/cache/object.go | 12 | ||||
-rw-r--r-- | plumbing/cache/object_test.go | 21 |
3 files changed, 21 insertions, 16 deletions
diff --git a/plumbing/cache/common.go b/plumbing/cache/common.go index 33fb2bc..8da5fb8 100644 --- a/plumbing/cache/common.go +++ b/plumbing/cache/common.go @@ -3,12 +3,14 @@ package cache import "srcd.works/go-git.v4/plumbing" const ( - Byte = 1 << (iota * 10) + Byte FileSize = 1 << (iota * 10) KiByte MiByte GiByte ) +type FileSize int64 + type Object interface { Add(o plumbing.EncodedObject) Get(k plumbing.Hash) plumbing.EncodedObject diff --git a/plumbing/cache/object.go b/plumbing/cache/object.go index 47e390b..f1af4e3 100644 --- a/plumbing/cache/object.go +++ b/plumbing/cache/object.go @@ -11,13 +11,13 @@ type ObjectFIFO struct { objects map[plumbing.Hash]plumbing.EncodedObject order *queue - maxSize int64 - actualSize int64 + maxSize FileSize + actualSize FileSize } // NewObjectFIFO returns an Object cache that keeps the newest objects that fit // into the specific memory size -func NewObjectFIFO(size int64) *ObjectFIFO { +func NewObjectFIFO(size FileSize) *ObjectFIFO { return &ObjectFIFO{ objects: make(map[plumbing.Hash]plumbing.EncodedObject), order: newQueue(initialQueueSize), @@ -30,7 +30,7 @@ func NewObjectFIFO(size int64) *ObjectFIFO { func (c *ObjectFIFO) Add(o plumbing.EncodedObject) { // if the size of the object is bigger or equal than the cache size, // skip it - if o.Size() >= c.maxSize { + if FileSize(o.Size()) >= c.maxSize { return } @@ -44,14 +44,14 @@ func (c *ObjectFIFO) Add(o plumbing.EncodedObject) { h := c.order.Pop() o := c.objects[h] if o != nil { - c.actualSize -= o.Size() + c.actualSize -= FileSize(o.Size()) delete(c.objects, h) } } c.objects[o.Hash()] = o c.order.Push(o.Hash()) - c.actualSize += o.Size() + c.actualSize += FileSize(o.Size()) } // Get returns an object by his hash. If the object is not found in the cache, it diff --git a/plumbing/cache/object_test.go b/plumbing/cache/object_test.go index 7d00970..e0d601a 100644 --- a/plumbing/cache/object_test.go +++ b/plumbing/cache/object_test.go @@ -32,20 +32,23 @@ func (s *ObjectSuite) SetUpTest(c *C) { func (s *ObjectSuite) TestAdd_SameObject(c *C) { s.c.Add(s.aObject) - c.Assert(s.c.actualSize, Equals, int64(1*Byte)) + c.Assert(s.c.actualSize, Equals, 1*Byte) s.c.Add(s.aObject) - c.Assert(s.c.actualSize, Equals, int64(1*Byte)) + c.Assert(s.c.actualSize, Equals, 1*Byte) } func (s *ObjectSuite) TestAdd_BigObject(c *C) { s.c.Add(s.bObject) - c.Assert(s.c.actualSize, Equals, int64(0)) + c.Assert(s.c.actualSize, Equals, 0*Byte) + c.Assert(s.c.actualSize, Equals, 0*KiByte) + c.Assert(s.c.actualSize, Equals, 0*MiByte) + c.Assert(s.c.actualSize, Equals, 0*GiByte) c.Assert(len(s.c.objects), Equals, 0) } func (s *ObjectSuite) TestAdd_CacheOverflow(c *C) { s.c.Add(s.aObject) - c.Assert(s.c.actualSize, Equals, int64(1*Byte)) + c.Assert(s.c.actualSize, Equals, 1*Byte) s.c.Add(s.cObject) c.Assert(len(s.c.objects), Equals, 2) s.c.Add(s.dObject) @@ -58,18 +61,18 @@ func (s *ObjectSuite) TestAdd_CacheOverflow(c *C) { func (s *ObjectSuite) TestClear(c *C) { s.c.Add(s.aObject) - c.Assert(s.c.actualSize, Equals, int64(1*Byte)) + c.Assert(s.c.actualSize, Equals, 1*Byte) s.c.Clear() - c.Assert(s.c.actualSize, Equals, int64(0)) + c.Assert(s.c.actualSize, Equals, 0*Byte) c.Assert(s.c.Get(s.aObject.Hash()), IsNil) } type dummyObject struct { hash plumbing.Hash - size int64 + size FileSize } -func newObject(hash string, size int64) plumbing.EncodedObject { +func newObject(hash string, size FileSize) plumbing.EncodedObject { return &dummyObject{ hash: plumbing.NewHash(hash), size: size, @@ -79,7 +82,7 @@ func newObject(hash string, size int64) plumbing.EncodedObject { func (d *dummyObject) Hash() plumbing.Hash { return d.hash } func (*dummyObject) Type() plumbing.ObjectType { return plumbing.InvalidObject } func (*dummyObject) SetType(plumbing.ObjectType) {} -func (d *dummyObject) Size() int64 { return d.size } +func (d *dummyObject) Size() int64 { return int64(d.size) } func (*dummyObject) SetSize(s int64) {} func (*dummyObject) Reader() (io.ReadCloser, error) { return nil, nil } func (*dummyObject) Writer() (io.WriteCloser, error) { return nil, nil } |