container

package
v0.1.48 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Copyright 2018-2019 The logrange Authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CLElement

type CLElement struct {
	Val interface{}
	// contains filtered or unexported fields
}

CLElement struct describes a circular list element. It contains references to previous and next elements. By default every non-nil element forms a circular list which contains only one element, which refers to itself.

func NewCLElement

func NewCLElement() *CLElement

NewCLElement returns new CLElement which refers to itself

func (*CLElement) Append

func (cle *CLElement) Append(chain *CLElement) *CLElement

Append adds chain to the list after the cle

func (*CLElement) Len

func (cle *CLElement) Len() int

Len returns number of elements in the Circular list. It has O(N) complexity.

func (*CLElement) Next

func (cle *CLElement) Next() *CLElement

Next returns next element (which comes after cle)

func (*CLElement) Prev

func (cle *CLElement) Prev() *CLElement

Prev returns previous element (which comes before cle)

func (*CLElement) TearOff

func (cle *CLElement) TearOff(e *CLElement) *CLElement

TearOff removes e from the list which probably heads by cle. It returns new head, which is cle.next, if cle is e, or the cle itself otherwise

type Lru

type Lru struct {
	// contains filtered or unexported fields
}

Lru is "Least Recently Used" container, which keeps key-value pairs in it. Lru supports auto-removing discipline which can be triggered by the container size, an element expiration timeout or both. It sorts stored elements by their recently used time, so most recently used are stored at head of the list, but auto-removing procedure works from the bottom, so it removes longest not-used element first.

Lru should be properly synchronized in case of it is used from multiple goroutines.

func NewLru

func NewLru(maxSize int64, to time.Duration, cback LruDeleteCallback) *Lru

NewLru creates new Lru container with maximum size maxSize, and maximum time 'to' an element can stay in the cache. cback is a function which is invoked when an element is pulled out of the cache. It can be nil

Timeout 'to' could be 0, what means don't use it at all

func (*Lru) Clear

func (l *Lru) Clear(cb bool)

Clear removes all elements from the container. It will notify listeners about any element which is being deleted if cb == true.

func (*Lru) Delete

func (l *Lru) Delete(k interface{})

Delete removes the value by its key 'k' from the Lru. If the container has callbacks, it will notify listeners about the operation

func (*Lru) DeleteNoCallback

func (l *Lru) DeleteNoCallback(k interface{})

DeleteNoCallback removes the value by its key 'k' from the Lru. No notifications will be done.

func (*Lru) Get

func (l *Lru) Get(k interface{}) *LruValue

Get returns the *LruValue by its key, and removes it from the collection. The resulted value could be valid till ANY other call to the Lru, and its value is undefined after that. Please use the value as soon as you get it and never cache or pass it through to other functions.

func (*Lru) GetData

func (l *Lru) GetData() map[interface{}]interface{}

GetData returns underlying container data for the LRU. The returned value is a key-value map

func (*Lru) Iterate

func (l *Lru) Iterate(f LruCallback)

Iterate is the container visitor which walks over the elements in LRU order. It calls f() for every key-value pair and continues until the f() returns false, or all elements are visited.

Note: the modifications of the container must not allowed in the f. It means f MUST not use the Lru functions.

func (*Lru) Len

func (l *Lru) Len() int

Len returns number or elements that are in the container

func (*Lru) Peek

func (l *Lru) Peek(k interface{}) *LruValue

Peek works the same way like Get() does, but it doesn't remove the element from the Lru

func (*Lru) Put

func (l *Lru) Put(k, v interface{}, size int64)

Put places new value v with the key k, considering the size of the element as 'size'. Some elements can be pull out from the Lru due to their timeouts or if the collection size will exceed the max value.

func (*Lru) Size

func (l *Lru) Size() int64

Size returns current size

func (*Lru) SweepByTime

func (l *Lru) SweepByTime() time.Time

SweepByTime walks through the container values and removes that are expired

type LruCallback

type LruCallback func(k, v interface{}) bool

type LruDeleteCallback

type LruDeleteCallback func(k, v interface{})

type LruValue

type LruValue struct {
	// contains filtered or unexported fields
}

LruValue represents a value, stored in Lru. The object is returned by Lru.Get() function.

func (*LruValue) Key

func (v *LruValue) Key() interface{}

func (*LruValue) Size

func (v *LruValue) Size() int64

func (*LruValue) TouchedAt

func (v *LruValue) TouchedAt() time.Time

func (*LruValue) Val

func (v *LruValue) Val() interface{}

type RingBuffer

type RingBuffer struct {
	// contains filtered or unexported fields
}

RingBuffer - the ring buffer with fixed capacity. The container has head and tail. It provides operations that allows to manipulate the data stored in the buffer.

Handle with caution! The buffer doesn't free elements, and doesn't nilling them intentionally. It was made with an intention to minimize memory allocations for the stored elements. It is the buffer's user responsibility to free and nillify stored values.

func NewRingBuffer

func NewRingBuffer(size int) *RingBuffer

NewRingBuffer - returns new ring buffer with size elements reserved

func (*RingBuffer) AdvanceHead

func (rb *RingBuffer) AdvanceHead() interface{}

AdvanceHead - advances head and reduce the buffer size to 1 (head) element. It returns the element, which was at head, before the operation

func (*RingBuffer) AdvanceTail

func (rb *RingBuffer) AdvanceTail() interface{}

AdvanceTail - moves the tail and increases the current buffer size by 1. if the buffer size reaches the maximum capacity, it will return head element, moving the head and tail both to 1 position.

func (*RingBuffer) At

func (rb *RingBuffer) At(i int) interface{}

At - returns element at the index i, countin from the head. Will panic if the index is out of bounds

func (*RingBuffer) Capacity

func (rb *RingBuffer) Capacity() int

Capacity - returns the buffer capacity

func (*RingBuffer) Clear

func (rb *RingBuffer) Clear()

Clear - drops the buffer size to 0

func (*RingBuffer) Head

func (rb *RingBuffer) Head() interface{}

Head - returns head's element. Will panic if size of the RingBuffer is 0

func (*RingBuffer) IsFull

func (rb *RingBuffer) IsFull() bool

IsFull - returns true if the buffer is full Len() == Capacity()

func (*RingBuffer) Len

func (rb *RingBuffer) Len() int

Len - returns current buffer size

func (*RingBuffer) Push

func (rb *RingBuffer) Push(v interface{}) interface{}

Push - places value v at the tail. It will increase the buffer size, or pops head element if the buffer's capacity is reached. The previos element stored at the new tail position is returned. After the operation tail points to the new value v.

func (*RingBuffer) Set

func (rb *RingBuffer) Set(i int, v interface{})

Set - assign value v for the element i, counting from the head.

func (*RingBuffer) Tail

func (rb *RingBuffer) Tail() interface{}

Tail - returns tail's element. Will panic if size of the RingBuffer is 0

type Timeseries

type Timeseries struct {
	// contains filtered or unexported fields
}

Timeseries is a structure, which keeps time-series in the number of chained buckets. This is a time-sliding window which can be used for smoothing out an observed scalar value.

func NewTimeseries

func NewTimeseries(bktDur, tsDur time.Duration, newValF TsNewValueF) *Timeseries

NewTimeseries same as NewTimeseriesWithClock, but provides system time.Now() for discovering current time.

func NewTimeseriesWithClock

func NewTimeseriesWithClock(bktDur, tsDur time.Duration, newValF TsNewValueF, clck TsClockNowF) *Timeseries

NewTimeseriesWithClock constructs new Timeseries value. Expects to receive the bucket size(bktDur in time duration), the time-series in time duration in the tsDur parameter, the newValF allows to create new scalar values and the clck is a function which allows to discover current time

func (*Timeseries) Add

func (ts *Timeseries) Add(val TsValue)

func (*Timeseries) StartTime

func (ts *Timeseries) StartTime() time.Time

func (*Timeseries) Total

func (ts *Timeseries) Total() TsValue

type TsClockNowF

type TsClockNowF func() time.Time

TsClockNowF a function whic returns current time

type TsInt

type TsInt int

TsInt implements TsValue for int

func (TsInt) Add

func (ti TsInt) Add(val TsValue) TsValue

func (TsInt) Sub

func (ti TsInt) Sub(val TsValue) TsValue

type TsNewValueF

type TsNewValueF func() TsValue

TsNewValueF a function which constructs a TsValue

type TsValue

type TsValue interface {
	Add(val TsValue) TsValue
	Sub(val TsValue) TsValue
}

TsValue is an interface which represents an immutable scalar value

func NewTsInt

func NewTsInt() TsValue

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL