diff options
Diffstat (limited to 'vendor/github.com/allegro/bigcache/queue/bytes_queue.go')
-rw-r--r-- | vendor/github.com/allegro/bigcache/queue/bytes_queue.go | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/vendor/github.com/allegro/bigcache/queue/bytes_queue.go b/vendor/github.com/allegro/bigcache/queue/bytes_queue.go index 0285c72cd..bda737403 100644 --- a/vendor/github.com/allegro/bigcache/queue/bytes_queue.go +++ b/vendor/github.com/allegro/bigcache/queue/bytes_queue.go @@ -16,6 +16,12 @@ const ( minimumEmptyBlobSize = 32 + headerEntrySize ) +var ( + errEmptyQueue = &queueError{"Empty queue"} + errInvalidIndex = &queueError{"Index must be greater than zero. Invalid index."} + errIndexOutOfBounds = &queueError{"Index out of range"} +) + // BytesQueue is a non-thread safe queue type of fifo based on bytes array. // For every push operation index of entry is returned. It can be used to read the entry later type BytesQueue struct { @@ -162,6 +168,11 @@ func (q *BytesQueue) Get(index int) ([]byte, error) { return data, err } +// CheckGet checks if an entry can be read from index +func (q *BytesQueue) CheckGet(index int) error { + return q.peekCheckErr(index) +} + // Capacity returns number of allocated bytes for queue func (q *BytesQueue) Capacity() int { return q.capacity @@ -177,18 +188,35 @@ func (e *queueError) Error() string { return e.message } +// peekCheckErr is identical to peek, but does not actually return any data +func (q *BytesQueue) peekCheckErr(index int) error { + + if q.count == 0 { + return errEmptyQueue + } + + if index <= 0 { + return errInvalidIndex + } + + if index+headerEntrySize >= len(q.array) { + return errIndexOutOfBounds + } + return nil +} + func (q *BytesQueue) peek(index int) ([]byte, int, error) { if q.count == 0 { - return nil, 0, &queueError{"Empty queue"} + return nil, 0, errEmptyQueue } if index <= 0 { - return nil, 0, &queueError{"Index must be grater than zero. Invalid index."} + return nil, 0, errInvalidIndex } if index+headerEntrySize >= len(q.array) { - return nil, 0, &queueError{"Index out of range"} + return nil, 0, errIndexOutOfBounds } blockSize := int(binary.LittleEndian.Uint32(q.array[index : index+headerEntrySize])) |