mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-30 03:23:08 +03:00
switch to fork of go-git
This commit is contained in:
98
vendor/github.com/jesseduffield/go-git/v5/plumbing/cache/buffer_lru.go
generated
vendored
Normal file
98
vendor/github.com/jesseduffield/go-git/v5/plumbing/cache/buffer_lru.go
generated
vendored
Normal file
@ -0,0 +1,98 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// BufferLRU implements an object cache with an LRU eviction policy and a
|
||||
// maximum size (measured in object size).
|
||||
type BufferLRU struct {
|
||||
MaxSize FileSize
|
||||
|
||||
actualSize FileSize
|
||||
ll *list.List
|
||||
cache map[int64]*list.Element
|
||||
mut sync.Mutex
|
||||
}
|
||||
|
||||
// NewBufferLRU creates a new BufferLRU with the given maximum size. The maximum
|
||||
// size will never be exceeded.
|
||||
func NewBufferLRU(maxSize FileSize) *BufferLRU {
|
||||
return &BufferLRU{MaxSize: maxSize}
|
||||
}
|
||||
|
||||
// NewBufferLRUDefault creates a new BufferLRU with the default cache size.
|
||||
func NewBufferLRUDefault() *BufferLRU {
|
||||
return &BufferLRU{MaxSize: DefaultMaxSize}
|
||||
}
|
||||
|
||||
type buffer struct {
|
||||
Key int64
|
||||
Slice []byte
|
||||
}
|
||||
|
||||
// Put puts a buffer into the cache. If the buffer is already in the cache, it
|
||||
// will be marked as used. Otherwise, it will be inserted. A buffers might
|
||||
// be evicted to make room for the new one.
|
||||
func (c *BufferLRU) Put(key int64, slice []byte) {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
if c.cache == nil {
|
||||
c.actualSize = 0
|
||||
c.cache = make(map[int64]*list.Element, 1000)
|
||||
c.ll = list.New()
|
||||
}
|
||||
|
||||
bufSize := FileSize(len(slice))
|
||||
if ee, ok := c.cache[key]; ok {
|
||||
oldBuf := ee.Value.(buffer)
|
||||
// in this case bufSize is a delta: new size - old size
|
||||
bufSize -= FileSize(len(oldBuf.Slice))
|
||||
c.ll.MoveToFront(ee)
|
||||
ee.Value = buffer{key, slice}
|
||||
} else {
|
||||
if bufSize > c.MaxSize {
|
||||
return
|
||||
}
|
||||
ee := c.ll.PushFront(buffer{key, slice})
|
||||
c.cache[key] = ee
|
||||
}
|
||||
|
||||
c.actualSize += bufSize
|
||||
for c.actualSize > c.MaxSize {
|
||||
last := c.ll.Back()
|
||||
lastObj := last.Value.(buffer)
|
||||
lastSize := FileSize(len(lastObj.Slice))
|
||||
|
||||
c.ll.Remove(last)
|
||||
delete(c.cache, lastObj.Key)
|
||||
c.actualSize -= lastSize
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns a buffer by its key. It marks the buffer as used. If the buffer
|
||||
// is not in the cache, (nil, false) will be returned.
|
||||
func (c *BufferLRU) Get(key int64) ([]byte, bool) {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
ee, ok := c.cache[key]
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
c.ll.MoveToFront(ee)
|
||||
return ee.Value.(buffer).Slice, true
|
||||
}
|
||||
|
||||
// Clear the content of this buffer cache.
|
||||
func (c *BufferLRU) Clear() {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
c.ll = nil
|
||||
c.cache = nil
|
||||
c.actualSize = 0
|
||||
}
|
39
vendor/github.com/jesseduffield/go-git/v5/plumbing/cache/common.go
generated
vendored
Normal file
39
vendor/github.com/jesseduffield/go-git/v5/plumbing/cache/common.go
generated
vendored
Normal file
@ -0,0 +1,39 @@
|
||||
package cache
|
||||
|
||||
import "github.com/jesseduffield/go-git/v5/plumbing"
|
||||
|
||||
const (
|
||||
Byte FileSize = 1 << (iota * 10)
|
||||
KiByte
|
||||
MiByte
|
||||
GiByte
|
||||
)
|
||||
|
||||
type FileSize int64
|
||||
|
||||
const DefaultMaxSize FileSize = 96 * MiByte
|
||||
|
||||
// Object is an interface to a object cache.
|
||||
type Object interface {
|
||||
// Put puts the given object into the cache. Whether this object will
|
||||
// actually be put into the cache or not is implementation specific.
|
||||
Put(o plumbing.EncodedObject)
|
||||
// Get gets an object from the cache given its hash. The second return value
|
||||
// is true if the object was returned, and false otherwise.
|
||||
Get(k plumbing.Hash) (plumbing.EncodedObject, bool)
|
||||
// Clear clears every object from the cache.
|
||||
Clear()
|
||||
}
|
||||
|
||||
// Buffer is an interface to a buffer cache.
|
||||
type Buffer interface {
|
||||
// Put puts a buffer into the cache. If the buffer is already in the cache,
|
||||
// it will be marked as used. Otherwise, it will be inserted. Buffer might
|
||||
// be evicted to make room for the new one.
|
||||
Put(key int64, slice []byte)
|
||||
// Get returns a buffer by its key. It marks the buffer as used. If the
|
||||
// buffer is not in the cache, (nil, false) will be returned.
|
||||
Get(key int64) ([]byte, bool)
|
||||
// Clear clears every object from the cache.
|
||||
Clear()
|
||||
}
|
101
vendor/github.com/jesseduffield/go-git/v5/plumbing/cache/object_lru.go
generated
vendored
Normal file
101
vendor/github.com/jesseduffield/go-git/v5/plumbing/cache/object_lru.go
generated
vendored
Normal file
@ -0,0 +1,101 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"sync"
|
||||
|
||||
"github.com/jesseduffield/go-git/v5/plumbing"
|
||||
)
|
||||
|
||||
// ObjectLRU implements an object cache with an LRU eviction policy and a
|
||||
// maximum size (measured in object size).
|
||||
type ObjectLRU struct {
|
||||
MaxSize FileSize
|
||||
|
||||
actualSize FileSize
|
||||
ll *list.List
|
||||
cache map[interface{}]*list.Element
|
||||
mut sync.Mutex
|
||||
}
|
||||
|
||||
// NewObjectLRU creates a new ObjectLRU with the given maximum size. The maximum
|
||||
// size will never be exceeded.
|
||||
func NewObjectLRU(maxSize FileSize) *ObjectLRU {
|
||||
return &ObjectLRU{MaxSize: maxSize}
|
||||
}
|
||||
|
||||
// NewObjectLRUDefault creates a new ObjectLRU with the default cache size.
|
||||
func NewObjectLRUDefault() *ObjectLRU {
|
||||
return &ObjectLRU{MaxSize: DefaultMaxSize}
|
||||
}
|
||||
|
||||
// Put puts an object into the cache. If the object is already in the cache, it
|
||||
// will be marked as used. Otherwise, it will be inserted. A single object might
|
||||
// be evicted to make room for the new object.
|
||||
func (c *ObjectLRU) Put(obj plumbing.EncodedObject) {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
if c.cache == nil {
|
||||
c.actualSize = 0
|
||||
c.cache = make(map[interface{}]*list.Element, 1000)
|
||||
c.ll = list.New()
|
||||
}
|
||||
|
||||
objSize := FileSize(obj.Size())
|
||||
key := obj.Hash()
|
||||
if ee, ok := c.cache[key]; ok {
|
||||
oldObj := ee.Value.(plumbing.EncodedObject)
|
||||
// in this case objSize is a delta: new size - old size
|
||||
objSize -= FileSize(oldObj.Size())
|
||||
c.ll.MoveToFront(ee)
|
||||
ee.Value = obj
|
||||
} else {
|
||||
if objSize > c.MaxSize {
|
||||
return
|
||||
}
|
||||
ee := c.ll.PushFront(obj)
|
||||
c.cache[key] = ee
|
||||
}
|
||||
|
||||
c.actualSize += objSize
|
||||
for c.actualSize > c.MaxSize {
|
||||
last := c.ll.Back()
|
||||
if last == nil {
|
||||
c.actualSize = 0
|
||||
break
|
||||
}
|
||||
|
||||
lastObj := last.Value.(plumbing.EncodedObject)
|
||||
lastSize := FileSize(lastObj.Size())
|
||||
|
||||
c.ll.Remove(last)
|
||||
delete(c.cache, lastObj.Hash())
|
||||
c.actualSize -= lastSize
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns an object by its hash. It marks the object as used. If the object
|
||||
// is not in the cache, (nil, false) will be returned.
|
||||
func (c *ObjectLRU) Get(k plumbing.Hash) (plumbing.EncodedObject, bool) {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
ee, ok := c.cache[k]
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
c.ll.MoveToFront(ee)
|
||||
return ee.Value.(plumbing.EncodedObject), true
|
||||
}
|
||||
|
||||
// Clear the content of this object cache.
|
||||
func (c *ObjectLRU) Clear() {
|
||||
c.mut.Lock()
|
||||
defer c.mut.Unlock()
|
||||
|
||||
c.ll = nil
|
||||
c.cache = nil
|
||||
c.actualSize = 0
|
||||
}
|
Reference in New Issue
Block a user