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

Commit 0e9dea1

Browse files
authored
Merge pull request #289 from ajnavarro/documentation/cache
plumbing/cache: specify units in memory size (Fix #234)
2 parents a8f1e51 + ae887c9 commit 0e9dea1

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

plumbing/cache/common.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package cache
33
import "srcd.works/go-git.v4/plumbing"
44

55
const (
6-
Byte = 1 << (iota * 10)
6+
Byte FileSize = 1 << (iota * 10)
77
KiByte
88
MiByte
99
GiByte
1010
)
1111

12+
type FileSize int64
13+
1214
type Object interface {
1315
Add(o plumbing.EncodedObject)
1416
Get(k plumbing.Hash) plumbing.EncodedObject

plumbing/cache/object.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ type ObjectFIFO struct {
1111
objects map[plumbing.Hash]plumbing.EncodedObject
1212
order *queue
1313

14-
maxSize int64
15-
actualSize int64
14+
maxSize FileSize
15+
actualSize FileSize
1616
}
1717

1818
// NewObjectFIFO returns an Object cache that keeps the newest objects that fit
1919
// into the specific memory size
20-
func NewObjectFIFO(size int64) *ObjectFIFO {
20+
func NewObjectFIFO(size FileSize) *ObjectFIFO {
2121
return &ObjectFIFO{
2222
objects: make(map[plumbing.Hash]plumbing.EncodedObject),
2323
order: newQueue(initialQueueSize),
@@ -30,7 +30,7 @@ func NewObjectFIFO(size int64) *ObjectFIFO {
3030
func (c *ObjectFIFO) Add(o plumbing.EncodedObject) {
3131
// if the size of the object is bigger or equal than the cache size,
3232
// skip it
33-
if o.Size() >= c.maxSize {
33+
if FileSize(o.Size()) >= c.maxSize {
3434
return
3535
}
3636

@@ -44,14 +44,14 @@ func (c *ObjectFIFO) Add(o plumbing.EncodedObject) {
4444
h := c.order.Pop()
4545
o := c.objects[h]
4646
if o != nil {
47-
c.actualSize -= o.Size()
47+
c.actualSize -= FileSize(o.Size())
4848
delete(c.objects, h)
4949
}
5050
}
5151

5252
c.objects[o.Hash()] = o
5353
c.order.Push(o.Hash())
54-
c.actualSize += o.Size()
54+
c.actualSize += FileSize(o.Size())
5555
}
5656

5757
// Get returns an object by his hash. If the object is not found in the cache, it

plumbing/cache/object_test.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,23 @@ func (s *ObjectSuite) SetUpTest(c *C) {
3232

3333
func (s *ObjectSuite) TestAdd_SameObject(c *C) {
3434
s.c.Add(s.aObject)
35-
c.Assert(s.c.actualSize, Equals, int64(1*Byte))
35+
c.Assert(s.c.actualSize, Equals, 1*Byte)
3636
s.c.Add(s.aObject)
37-
c.Assert(s.c.actualSize, Equals, int64(1*Byte))
37+
c.Assert(s.c.actualSize, Equals, 1*Byte)
3838
}
3939

4040
func (s *ObjectSuite) TestAdd_BigObject(c *C) {
4141
s.c.Add(s.bObject)
42-
c.Assert(s.c.actualSize, Equals, int64(0))
42+
c.Assert(s.c.actualSize, Equals, 0*Byte)
43+
c.Assert(s.c.actualSize, Equals, 0*KiByte)
44+
c.Assert(s.c.actualSize, Equals, 0*MiByte)
45+
c.Assert(s.c.actualSize, Equals, 0*GiByte)
4346
c.Assert(len(s.c.objects), Equals, 0)
4447
}
4548

4649
func (s *ObjectSuite) TestAdd_CacheOverflow(c *C) {
4750
s.c.Add(s.aObject)
48-
c.Assert(s.c.actualSize, Equals, int64(1*Byte))
51+
c.Assert(s.c.actualSize, Equals, 1*Byte)
4952
s.c.Add(s.cObject)
5053
c.Assert(len(s.c.objects), Equals, 2)
5154
s.c.Add(s.dObject)
@@ -58,18 +61,18 @@ func (s *ObjectSuite) TestAdd_CacheOverflow(c *C) {
5861

5962
func (s *ObjectSuite) TestClear(c *C) {
6063
s.c.Add(s.aObject)
61-
c.Assert(s.c.actualSize, Equals, int64(1*Byte))
64+
c.Assert(s.c.actualSize, Equals, 1*Byte)
6265
s.c.Clear()
63-
c.Assert(s.c.actualSize, Equals, int64(0))
66+
c.Assert(s.c.actualSize, Equals, 0*Byte)
6467
c.Assert(s.c.Get(s.aObject.Hash()), IsNil)
6568
}
6669

6770
type dummyObject struct {
6871
hash plumbing.Hash
69-
size int64
72+
size FileSize
7073
}
7174

72-
func newObject(hash string, size int64) plumbing.EncodedObject {
75+
func newObject(hash string, size FileSize) plumbing.EncodedObject {
7376
return &dummyObject{
7477
hash: plumbing.NewHash(hash),
7578
size: size,
@@ -79,7 +82,7 @@ func newObject(hash string, size int64) plumbing.EncodedObject {
7982
func (d *dummyObject) Hash() plumbing.Hash { return d.hash }
8083
func (*dummyObject) Type() plumbing.ObjectType { return plumbing.InvalidObject }
8184
func (*dummyObject) SetType(plumbing.ObjectType) {}
82-
func (d *dummyObject) Size() int64 { return d.size }
85+
func (d *dummyObject) Size() int64 { return int64(d.size) }
8386
func (*dummyObject) SetSize(s int64) {}
8487
func (*dummyObject) Reader() (io.ReadCloser, error) { return nil, nil }
8588
func (*dummyObject) Writer() (io.WriteCloser, error) { return nil, nil }

0 commit comments

Comments
 (0)