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

Commit e9247ce

Browse files
authored
Merge pull request #720 from jfontan/improvement/cache-delete-more-than-one-object
plumbing: cache, modify cache to delete more than one item to free space
2 parents 4d43799 + 0f6c06d commit e9247ce

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

plumbing/cache/object_lru.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,18 @@ func (c *ObjectLRU) Put(obj plumbing.EncodedObject) {
5151

5252
objSize := FileSize(obj.Size())
5353

54-
if objSize >= c.MaxSize {
54+
if objSize > c.MaxSize {
5555
return
5656
}
5757

58-
if c.actualSize+objSize > c.MaxSize {
58+
for c.actualSize+objSize > c.MaxSize {
5959
last := c.ll.Back()
6060
lastObj := last.Value.(plumbing.EncodedObject)
6161
lastSize := FileSize(lastObj.Size())
6262

6363
c.ll.Remove(last)
6464
delete(c.cache, lastObj.Hash())
6565
c.actualSize -= lastSize
66-
67-
if c.actualSize+objSize > c.MaxSize {
68-
return
69-
}
7066
}
7167

7268
ee := c.ll.PushFront(obj)

plumbing/cache/object_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type ObjectSuite struct {
1919
bObject plumbing.EncodedObject
2020
cObject plumbing.EncodedObject
2121
dObject plumbing.EncodedObject
22+
eObject plumbing.EncodedObject
2223
}
2324

2425
var _ = Suite(&ObjectSuite{})
@@ -28,6 +29,7 @@ func (s *ObjectSuite) SetUpTest(c *C) {
2829
s.bObject = newObject("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 3*Byte)
2930
s.cObject = newObject("cccccccccccccccccccccccccccccccccccccccc", 1*Byte)
3031
s.dObject = newObject("dddddddddddddddddddddddddddddddddddddddd", 1*Byte)
32+
s.eObject = newObject("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 2*Byte)
3133

3234
s.c = make(map[string]Object)
3335
s.c["two_bytes"] = NewObjectLRU(2 * Byte)
@@ -70,6 +72,24 @@ func (s *ObjectSuite) TestPutCacheOverflow(c *C) {
7072
c.Assert(obj, NotNil)
7173
}
7274

75+
func (s *ObjectSuite) TestEvictMultipleObjects(c *C) {
76+
o := s.c["two_bytes"]
77+
78+
o.Put(s.cObject)
79+
o.Put(s.dObject) // now cache is full with two objects
80+
o.Put(s.eObject) // this put should evict all previous objects
81+
82+
obj, ok := o.Get(s.cObject.Hash())
83+
c.Assert(ok, Equals, false)
84+
c.Assert(obj, IsNil)
85+
obj, ok = o.Get(s.dObject.Hash())
86+
c.Assert(ok, Equals, false)
87+
c.Assert(obj, IsNil)
88+
obj, ok = o.Get(s.eObject.Hash())
89+
c.Assert(ok, Equals, true)
90+
c.Assert(obj, NotNil)
91+
}
92+
7393
func (s *ObjectSuite) TestClear(c *C) {
7494
for _, o := range s.c {
7595
o.Put(s.aObject)

plumbing/format/packfile/decoder.go

+2
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ func (d *Decoder) fillOFSDeltaObjectContent(obj plumbing.EncodedObject, offset i
407407
if err != nil {
408408
return 0, err
409409
}
410+
411+
d.cachePut(base)
410412
}
411413

412414
obj.SetType(base.Type())

0 commit comments

Comments
 (0)