linkedlist

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBucketNotFound may be returned if a no Bolt bucket was found
	ErrBucketNotFound = errors.New("Bucket not found")

	// ErrKeyNotFound will be returned if the key was not found in a HashMap or KeyValue struct
	ErrKeyNotFound = errors.New("Key not found")

	// ErrDoesNotExist will be returned if an element was not found. Used in List, Set, HashMap and KeyValue.
	ErrDoesNotExist = errors.New("Does not exist")

	// ErrExistsInSet is only returned if an element is added to a Set, but it already exists
	ErrExistsInSet = errors.New("Element already exists in set")

	// ErrInvalidID is only returned if adding an element to a HashMap that contains a colon (:)
	ErrInvalidID = errors.New("Element ID can not contain \":\"")

	// ErrFoundIt is only used internally, for breaking out of Bolt DB style for-loops
	ErrFoundIt = errors.New("Found it")
)

Functions

This section is empty.

Types

type Item

type Item struct {
	Data simplebolt.StoredData
}

Item is the element of the linked list returned by Front(), Back(), Next(), Prev(), and all the Getters.

It enables access to the underlying data in Bolt for getting an updating it.

It can be used to traverse the linked list across every node of the data structure, by calling Prev(), Next() and any of the Getter methods. To retrieve, change or delete the underlying data, the Data field has the corresponding methods.

func (*Item) Next

func (i *Item) Next() (next *Item)

Next returns the next item pointed to by the current linked list item.

It should be called after Front() or any Getter method. Otherwise always returns nil.

Note that it panics if the item is an invalid linked list item, i.e. its Data field has been modified or not returned by one of the linked list methods.

func (*Item) Prev

func (i *Item) Prev() (prev *Item)

Prev returns the previous item pointed to by the current linked list item.

It should be called after Back() or any Getter method. Otherwise always returns nil.

Note that it panics if the item is an invalid linked list item, i.e. its Data field has been modified or not returned by one of the linked list methods.

type LinkedList

type LinkedList boltBucket

LinkedList is a doubly linked list. It is persisted using etcd-io/bbolt's b+tree as its underlying data structure but with a doubly linked list-like behaviour

func New

func New(db *simplebolt.Database, id string) (*LinkedList, error)

New returns a new doubly linkedlist with the given id as its identifier

func (*LinkedList) Back

func (ll *LinkedList) Back() (i *Item, err error)

Back returns the element at the back of the linked list. It returns a nil item if the list is empty.

It may return an error in case of:

bbolt.View() error

proto.Unmarshal() error

func (*LinkedList) Front

func (ll *LinkedList) Front() (i *Item, err error)

Front returns the element at the front of the linked list. Returns a nil item if the list is empty.

It may return an error in case of:

bbolt.View() error

proto.Unmarshal() error

func (*LinkedList) Get

func (ll *LinkedList) Get(val []byte) (*Item, error)

Get compares val with the value of every single node in the linked list, using bytes.Equal(). If it finds that v and the value of some node are equal, according to its criteria, then Get returns the item containing the value of the stored data.

Note that you must provide a []byte with a value in exactly the same format as the stored data in the linked list. For a more flexible criteria on the equality of the given value and the value in the stored data, see GetFunc.

If Get can't find any match, it returns an nil item and a nil error.

It may return an error due to a failed call to ll.Front(). It also returns either an "Empty list" error when called on a list with no elements, or an "Empty val" error when called with a nil []byte val. In all the cases, the returned item is nil.

Note that both Get and GetFunc always return the first match, if any. If you inserted multiple copies of the same data into the same linked list and you want to retrieve them, you must call GetNext or GetNextFunc sucesively after a call either to Get, GetFunc, GetNext or GetNextFunc, passing in the item returned by one of these methods.

func (*LinkedList) GetFunc

func (ll *LinkedList) GetFunc(val interface{}, equal func(a interface{}, b []byte) bool) (*Item, error)

GetFunc compares val with the value of every single node in the linked list, using the provided func to compare the given value and the value of the stored data. That way, you can define the criteria of equality between the two values that suits your data available at some point in time.

If GetFunc can't find any matches, it returns an nil item and a nil error.

It may return an error due to a failed call to ll.Front(). It also returns either an "Empty list" error when called on a list with no elements, an "Empty val" error when called with a nil interface{} val or an "Empty comparing function" when called with a nil function to compare. In all the cases, the returned item is nil.

Note that both Get and GetFunc always return the first match, if any. If you inserted multiple copies of the same data into the same linked list and you want to retrieve them, you must call GetNext or GetNextFunc successively after a call either to Get, GetFunc, GetNext or GetNextFunc, passing in the item returned by one of these methods.

For an example on the usage, see example/linkedlist/main.go

func (*LinkedList) GetNext

func (ll *LinkedList) GetNext(val []byte, mark *Item) (*Item, error)

GetNext compares val with the value of every single node in the linked list, starting from the next item of the element pointed to by mark, using bytes.Equal(). If it finds that v and the value of some node are equal, according to its criteria, then Get returns the item containing the value of the stored data.

If GetNext can't find any match, it returns an nil item and a nil error.

It may return an error due to a failed call to bbolt.View. It returns either an "Empty list" error when called on a list with no elements, an "Empty val" error when called with a nil val to get, or an "Empty mark" error when called with a nil mark to begin from. In all the cases the item returned is nil.

Note that you must pass in a []byte with a value in exactly the same format as the stored data in the linked list. For a more flexible criteria on the equality of the given value and the value in the stored data, see GetFunc and GetNextFunc.

func (*LinkedList) GetNextFunc

func (ll *LinkedList) GetNextFunc(val interface{}, mark *Item, equal func(a interface{}, b []byte) bool) (*Item, error)

GetNextFunc compares val with the value of every single node in the linked list, starting from the next item of the element pointed to by mark, using the provided function. If it finds that v and the value of some node are equal, according to its criteria, then GetNextFunc returns the item containing the value of the stored data.

If GetNextFunc can't find any matches, it returns an nil item and a nil error.

It returns either an "Empty val" error when called with a nil []byte val, an "Empty mark" error when called with a nil beginning mark, an "Invalid mark" error when the passed item is not a linked list item or belongs to another linked list, or an "Empty comparing function" error when called with a nil function to compare.

For an example on the usage, see example/linkedlist/main.go

func (*LinkedList) InsertAfter

func (ll *LinkedList) InsertAfter(data []byte, mark *Item) error

InsertAfter inserts the given data after the element pointed to by the given mark, so that all the pointers involving the new data and its siblings gets updated.

The element at which the given mark points to must belong to the same linkedlist as the linkedlist at which the method is being called. Otherwise, it returns an "Invalid mark: linkedlists are not equal" error.

It returns a "Nil mark" error in case of a nil mark argument, an "Empty list" error in case of being called on a list with no elements, and an "Invalid mark" error in case of passing an Item that wasn't returned by one of the linkedlist methods.

Other errors returned may be due to Bolt read/write or serialization/deserialization of the data operations fail.

func (*LinkedList) InsertBefore

func (ll *LinkedList) InsertBefore(data []byte, mark *Item) error

InsertBefore inserts the given data after the element pointed to by the given mark, so that all the pointers involving the new data and its siblings gets updated.

The element at which the given mark points to must belong to the same linkedlist as the linkedlist at which the method is being called. Otherwise, it returns an "Invalid mark: linkedlists are not equal" error.

It returns a "Nil mark" error in case of a nil mark argument, an "Empty list" error in case of being called on a list with no elements, and an "Invalid mark" error in case of passing an Item that wasn't returned by one of the linkedlist methods.

Other errors returned may be due to Bolt read/write or serialization/deserialization of the data operations fail.

func (*LinkedList) MoveToBack

func (ll *LinkedList) MoveToBack(it *Item) error

MoveToBack moves the element pointed to by the given Item to the back of the linked list.

The element being moved must belong to the linkedlist at which it is being moved. Otherwise, this method returns an "Invalid move" error.

It returns a "Nil item" error in case of a nil Item argument, an "Empty list" error in case of being called on a list with no elements, and an "Invalid item" error in case of passing an Item that wasn't returned by one of the linkedlist methods.

Other errors returned may be due to Bolt read/write or serialization/deserialization of the data operation fail.

func (*LinkedList) MoveToFront

func (ll *LinkedList) MoveToFront(it *Item) error

MoveToFront moves the element pointed to by the given Item to the front of the linked list.

The element being moved must belong to the linkedlist at which it is being moved. Otherwise, this method returns an "Invalid move" error.

It returns a "Nil item" error in case of a nil Item argument, an "Empty list" error in case of being called on a list with no elements, and an "Invalid item" error in case of passing an item that wasn't returned by one of the linkedlist methods.

Other errors returned may be due to Bolt read/write or serialization/deserialization of the data operation fail.

func (*LinkedList) PushBack

func (ll *LinkedList) PushBack(data []byte) error

PushBack inserts data at the end of the doubly linked list. Returns an "Empty data" error if data is nil. It also may fail if either bbolt operations or protocol buffer serialization/deserialization fail

func (*LinkedList) PushFront

func (ll *LinkedList) PushFront(data []byte) error

PushFront inserts data at the beginning of the doubly linked list. Returns an "Empty data" error if data is nil. It also may fail if either bbolt operations or protocol buffer serialization/deserialization fail

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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