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

plumbing/cache: specify units in memory size (Fix #234) #289

Merged
merged 1 commit into from
Feb 27, 2017
Merged
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
4 changes: 3 additions & 1 deletion plumbing/cache/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions plumbing/cache/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
}

Expand All @@ -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
Expand Down
21 changes: 12 additions & 9 deletions plumbing/cache/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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 }