mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-12-01 13:34:23 +01:00
deps
This commit is contained in:
1 parent
4137d3cfc0
commit
744ff2f876
382 files changed
+94483
No files matched your search
+26
@@ -0,0 +1,26 @@
|
||||
package cache
|
||||
|
||||
import "gopkg.in/src-d/go-git.v4/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()
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4/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()
|
||||
}
|
||||
|
||||
key := obj.Hash()
|
||||
if ee, ok := c.cache[key]; ok {
|
||||
c.ll.MoveToFront(ee)
|
||||
ee.Value = obj
|
||||
return
|
||||
}
|
||||
|
||||
objSize := FileSize(obj.Size())
|
||||
|
||||
if objSize > c.MaxSize {
|
||||
return
|
||||
}
|
||||
|
||||
for c.actualSize+objSize > c.MaxSize {
|
||||
last := c.ll.Back()
|
||||
lastObj := last.Value.(plumbing.EncodedObject)
|
||||
lastSize := FileSize(lastObj.Size())
|
||||
|
||||
c.ll.Remove(last)
|
||||
delete(c.cache, lastObj.Hash())
|
||||
c.actualSize -= lastSize
|
||||
}
|
||||
|
||||
ee := c.ll.PushFront(obj)
|
||||
c.cache[key] = ee
|
||||
c.actualSize += objSize
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package cache
|
||||
|
||||
import "gopkg.in/src-d/go-git.v4/plumbing"
|
||||
|
||||
// queue is a basic FIFO queue based on a circular list that resize as needed.
|
||||
type queue struct {
|
||||
elements []plumbing.Hash
|
||||
size int
|
||||
head int
|
||||
tail int
|
||||
count int
|
||||
}
|
||||
|
||||
// newQueue returns a queue with the specified initial size
|
||||
func newQueue(size int) *queue {
|
||||
return &queue{
|
||||
elements: make([]plumbing.Hash, size),
|
||||
size: size,
|
||||
}
|
||||
}
|
||||
|
||||
// Push adds a node to the queue.
|
||||
func (q *queue) Push(h plumbing.Hash) {
|
||||
if q.head == q.tail && q.count > 0 {
|
||||
elements := make([]plumbing.Hash, len(q.elements)+q.size)
|
||||
copy(elements, q.elements[q.head:])
|
||||
copy(elements[len(q.elements)-q.head:], q.elements[:q.head])
|
||||
q.head = 0
|
||||
q.tail = len(q.elements)
|
||||
q.elements = elements
|
||||
}
|
||||
q.elements[q.tail] = h
|
||||
q.tail = (q.tail + 1) % len(q.elements)
|
||||
q.count++
|
||||
}
|
||||
|
||||
// Pop removes and returns a Hash from the queue in first to last order.
|
||||
func (q *queue) Pop() plumbing.Hash {
|
||||
if q.count == 0 {
|
||||
return plumbing.ZeroHash
|
||||
}
|
||||
node := q.elements[q.head]
|
||||
q.head = (q.head + 1) % len(q.elements)
|
||||
q.count--
|
||||
return node
|
||||
}
|
||||
Reference in new issue
Block a user