This repository was archived by the owner on Sep 11, 2020. It is now read-only.
File tree 4 files changed +66
-24
lines changed
4 files changed +66
-24
lines changed Original file line number Diff line number Diff line change @@ -45,19 +45,23 @@ func (c *BufferLRU) Put(key int64, slice []byte) {
45
45
c .ll = list .New ()
46
46
}
47
47
48
+ bufSize := FileSize (len (slice ))
48
49
if ee , ok := c .cache [key ]; ok {
50
+ oldBuf := ee .Value .(buffer )
51
+ // in this case bufSize is a delta: new size - old size
52
+ bufSize -= FileSize (len (oldBuf .Slice ))
49
53
c .ll .MoveToFront (ee )
50
54
ee .Value = buffer {key , slice }
51
- return
55
+ } else {
56
+ if bufSize > c .MaxSize {
57
+ return
58
+ }
59
+ ee := c .ll .PushFront (buffer {key , slice })
60
+ c .cache [key ] = ee
52
61
}
53
62
54
- objSize := FileSize (len (slice ))
55
-
56
- if objSize > c .MaxSize {
57
- return
58
- }
59
-
60
- for c .actualSize + objSize > c .MaxSize {
63
+ c .actualSize += bufSize
64
+ for c .actualSize > c .MaxSize {
61
65
last := c .ll .Back ()
62
66
lastObj := last .Value .(buffer )
63
67
lastSize := FileSize (len (lastObj .Slice ))
@@ -66,10 +70,6 @@ func (c *BufferLRU) Put(key int64, slice []byte) {
66
70
delete (c .cache , lastObj .Key )
67
71
c .actualSize -= lastSize
68
72
}
69
-
70
- ee := c .ll .PushFront (buffer {key , slice })
71
- c .cache [key ] = ee
72
- c .actualSize += objSize
73
73
}
74
74
75
75
// Get returns a buffer by its key. It marks the buffer as used. If the buffer
Original file line number Diff line number Diff line change 1
1
package cache
2
2
3
3
import (
4
+ "bytes"
4
5
"sync"
5
6
6
7
. "gopkg.in/check.v1"
@@ -38,6 +39,28 @@ func (s *BufferSuite) TestPutSameBuffer(c *C) {
38
39
}
39
40
}
40
41
42
+ func (s * ObjectSuite ) TestPutSameBufferWithDifferentSize (c * C ) {
43
+ aBuffer := []byte ("a" )
44
+ bBuffer := []byte ("bbb" )
45
+ cBuffer := []byte ("ccccc" )
46
+ dBuffer := []byte ("ddddddd" )
47
+
48
+ cache := NewBufferLRU (7 * Byte )
49
+ cache .Put (1 , aBuffer )
50
+ cache .Put (1 , bBuffer )
51
+ cache .Put (1 , cBuffer )
52
+ cache .Put (1 , dBuffer )
53
+
54
+ c .Assert (cache .MaxSize , Equals , 7 * Byte )
55
+ c .Assert (cache .actualSize , Equals , 7 * Byte )
56
+ c .Assert (cache .ll .Len (), Equals , 1 )
57
+
58
+ buf , ok := cache .Get (1 )
59
+ c .Assert (bytes .Equal (buf , dBuffer ), Equals , true )
60
+ c .Assert (FileSize (len (buf )), Equals , 7 * Byte )
61
+ c .Assert (ok , Equals , true )
62
+ }
63
+
41
64
func (s * BufferSuite ) TestPutBigBuffer (c * C ) {
42
65
for _ , o := range s .c {
43
66
o .Put (1 , s .bBuffer )
Original file line number Diff line number Diff line change @@ -42,20 +42,24 @@ func (c *ObjectLRU) Put(obj plumbing.EncodedObject) {
42
42
c .ll = list .New ()
43
43
}
44
44
45
+ objSize := FileSize (obj .Size ())
45
46
key := obj .Hash ()
46
47
if ee , ok := c .cache [key ]; ok {
48
+ oldObj := ee .Value .(plumbing.EncodedObject )
49
+ // in this case objSize is a delta: new size - old size
50
+ objSize -= FileSize (oldObj .Size ())
47
51
c .ll .MoveToFront (ee )
48
52
ee .Value = obj
49
- return
50
- }
51
-
52
- objSize := FileSize (obj .Size ())
53
-
54
- if objSize > c .MaxSize {
55
- return
53
+ } else {
54
+ if objSize > c .MaxSize {
55
+ return
56
+ }
57
+ ee := c .ll .PushFront (obj )
58
+ c .cache [key ] = ee
56
59
}
57
60
58
- for c .actualSize + objSize > c .MaxSize {
61
+ c .actualSize += objSize
62
+ for c .actualSize > c .MaxSize {
59
63
last := c .ll .Back ()
60
64
lastObj := last .Value .(plumbing.EncodedObject )
61
65
lastSize := FileSize (lastObj .Size ())
@@ -64,10 +68,6 @@ func (c *ObjectLRU) Put(obj plumbing.EncodedObject) {
64
68
delete (c .cache , lastObj .Hash ())
65
69
c .actualSize -= lastSize
66
70
}
67
-
68
- ee := c .ll .PushFront (obj )
69
- c .cache [key ] = ee
70
- c .actualSize += objSize
71
71
}
72
72
73
73
// Get returns an object by its hash. It marks the object as used. If the object
Original file line number Diff line number Diff line change @@ -45,6 +45,25 @@ func (s *ObjectSuite) TestPutSameObject(c *C) {
45
45
}
46
46
}
47
47
48
+ func (s * ObjectSuite ) TestPutSameObjectWithDifferentSize (c * C ) {
49
+ const hash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
50
+
51
+ cache := NewObjectLRU (7 * Byte )
52
+ cache .Put (newObject (hash , 1 * Byte ))
53
+ cache .Put (newObject (hash , 3 * Byte ))
54
+ cache .Put (newObject (hash , 5 * Byte ))
55
+ cache .Put (newObject (hash , 7 * Byte ))
56
+
57
+ c .Assert (cache .MaxSize , Equals , 7 * Byte )
58
+ c .Assert (cache .actualSize , Equals , 7 * Byte )
59
+ c .Assert (cache .ll .Len (), Equals , 1 )
60
+
61
+ obj , ok := cache .Get (plumbing .NewHash (hash ))
62
+ c .Assert (obj .Hash (), Equals , plumbing .NewHash (hash ))
63
+ c .Assert (FileSize (obj .Size ()), Equals , 7 * Byte )
64
+ c .Assert (ok , Equals , true )
65
+ }
66
+
48
67
func (s * ObjectSuite ) TestPutBigObject (c * C ) {
49
68
for _ , o := range s .c {
50
69
o .Put (s .bObject )
You can’t perform that action at this time.
0 commit comments