This repository was archived by the owner on Sep 11, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +68
-22
lines changed Expand file tree Collapse file tree 4 files changed +68
-22
lines changed Original file line number Diff line number Diff line change @@ -45,19 +45,25 @@ 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 ))
53
+
49
54
c .ll .MoveToFront (ee )
50
55
ee .Value = buffer {key , slice }
51
- return
52
- }
56
+ } else {
57
+ if bufSize > c .MaxSize {
58
+ return
59
+ }
53
60
54
- objSize := FileSize (len (slice ))
55
-
56
- if objSize > c .MaxSize {
57
- return
61
+ ee := c .ll .PushFront (buffer {key , slice })
62
+ c .cache [key ] = ee
58
63
}
59
64
60
- for c .actualSize + objSize > c .MaxSize {
65
+ c .actualSize += bufSize
66
+ for c .actualSize > c .MaxSize {
61
67
last := c .ll .Back ()
62
68
lastObj := last .Value .(buffer )
63
69
lastSize := FileSize (len (lastObj .Slice ))
@@ -66,10 +72,6 @@ func (c *BufferLRU) Put(key int64, slice []byte) {
66
72
delete (c .cache , lastObj .Key )
67
73
c .actualSize -= lastSize
68
74
}
69
-
70
- ee := c .ll .PushFront (buffer {key , slice })
71
- c .cache [key ] = ee
72
- c .actualSize += objSize
73
75
}
74
76
75
77
// 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,26 @@ 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 ())
51
+
47
52
c .ll .MoveToFront (ee )
48
53
ee .Value = obj
49
- return
50
- }
51
-
52
- objSize := FileSize ( obj . Size ())
54
+ } else {
55
+ if objSize > c . MaxSize {
56
+ return
57
+ }
53
58
54
- if objSize > c . MaxSize {
55
- return
59
+ ee := c . ll . PushFront ( obj )
60
+ c . cache [ key ] = ee
56
61
}
57
62
58
- for c .actualSize + objSize > c .MaxSize {
63
+ c .actualSize += objSize
64
+ for c .actualSize > c .MaxSize {
59
65
last := c .ll .Back ()
60
66
lastObj := last .Value .(plumbing.EncodedObject )
61
67
lastSize := FileSize (lastObj .Size ())
@@ -64,10 +70,6 @@ func (c *ObjectLRU) Put(obj plumbing.EncodedObject) {
64
70
delete (c .cache , lastObj .Hash ())
65
71
c .actualSize -= lastSize
66
72
}
67
-
68
- ee := c .ll .PushFront (obj )
69
- c .cache [key ] = ee
70
- c .actualSize += objSize
71
73
}
72
74
73
75
// 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