levelqueue

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: MIT Imports: 8 Imported by: 0

README

levelqueue

Level queue is a simple queue golang library base on go-leveldb.

Installation

go get code.forgejo.org/forgejo/levelqueue

Usage

queue, err := levelqueue.Open("./queue")

err = queue.RPush([]byte("test"))

// pop an element from left of the queue
data, err = queue.LPop()

// if handle success, element will be pop, otherwise it will be keep
queue.LHandle(func(dt []byte) error{
    return nil
})

You can now create a Set from a leveldb:

set, err := levelqueue.OpenSet("./set")

added, err:= set.Add([]byte("member1"))

has, err := set.Has([]byte("member1"))

members, err := set.Members()

removed, err := set.Remove([]byte("member1"))

And you can create a UniqueQueue from a leveldb:

queue, err := levelqueue.OpenUnique("./queue")

err := queue.RPush([]byte("member1"))

err = queue.LPush([]byte("member1"))
// Will return ErrAlreadyInQueue

// and so on.

Creating Queues, UniqueQueues and Sets from already open DB

If you have an already open DB you can create these from this using the NewQueue, NewUniqueQueue and NewSet functions.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound means no elements in queue
	ErrNotFound = errors.New("no key found")

	ErrAlreadyInQueue = errors.New("value already in queue")
)

Functions

This section is empty.

Types

type Queue

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

Queue defines a queue struct

func NewQueue

func NewQueue(db *leveldb.DB, prefix []byte, closeUnderlyingDB bool) (*Queue, error)

NewQueue creates a queue from a db. The keys will be prefixed with prefix and at close the db will be closed as per closeUnderlyingDB

func Open

func Open(dataDir string) (*Queue, error)

Open opens a queue from the db path or creates a queue if it doesn't exist. The keys will not be prefixed by default

func (*Queue) Close

func (queue *Queue) Close() error

Close closes the queue (and the underlying db is set to closeUnderlyingDB)

func (*Queue) LHandle

func (queue *Queue) LHandle(h func([]byte) error) error

LHandle receives a user callback function to handle the left element of the queue, if function return nil, then delete the element, otherwise keep the element.

func (*Queue) LPop

func (queue *Queue) LPop() ([]byte, error)

LPop pop a data from left of queue

func (*Queue) LPush

func (queue *Queue) LPush(data []byte) error

LPush pushes a data from left of queue

func (*Queue) Len

func (queue *Queue) Len() int64

Len returns the length of the queue

func (*Queue) RHandle

func (queue *Queue) RHandle(h func([]byte) error) error

RHandle receives a user callback function to handle the right element of the queue, if function return nil, then delete the element, otherwise keep the element.

func (*Queue) RPop

func (queue *Queue) RPop() ([]byte, error)

RPop pop a data from right of queue

func (*Queue) RPush

func (queue *Queue) RPush(data []byte) error

RPush pushes a data from right of queue

type Set

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

Set defines a set struct

func NewSet

func NewSet(db *leveldb.DB, prefix []byte, closeUnderlyingDB bool) (*Set, error)

NewSet creates a set from a db. The keys will be prefixed with prefix and at close the db will be closed as per closeUnderlyingDB

func OpenSet

func OpenSet(dataDir string) (*Set, error)

OpenSet opens a set from the db path or creates a set if it doesn't exist. The keys will be prefixed with "set-" by default

func (*Set) Add

func (set *Set) Add(value []byte) (bool, error)

Add adds a member string to a key set, returns true if the member was not already present

func (*Set) Close

func (set *Set) Close() error

Close closes the set (and the underlying db if set to closeUnderlyingDB)

func (*Set) Has

func (set *Set) Has(value []byte) (bool, error)

Has returns if the member is in the set

func (*Set) Members

func (set *Set) Members() ([][]byte, error)

Members returns the current members of the set

func (*Set) Remove

func (set *Set) Remove(value []byte) (bool, error)

Remove removes a member from the set, returns true if the member was present

type UniqueQueue

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

UniqueQueue defines an unique queue struct

func NewUniqueQueue

func NewUniqueQueue(db *leveldb.DB, queuePrefix []byte, setPrefix []byte, closeUnderlyingDB bool) (*UniqueQueue, error)

NewUniqueQueue creates a new unique queue from a db. The queue keys will be prefixed with queuePrefix and the set keys with setPrefix and at close the db will be closed as per closeUnderlyingDB

func OpenUnique

func OpenUnique(dataDir string) (*UniqueQueue, error)

OpenUnique opens an unique queue from the db path or creates a set if it doesn't exist. The keys in the queue portion will not be prefixed, and the set keys will be prefixed with "set-"

func (*UniqueQueue) Close

func (queue *UniqueQueue) Close() error

Close closes the queue (and the underlying DB if set to closeUnderlyingDB)

func (*UniqueQueue) Has

func (queue *UniqueQueue) Has(data []byte) (bool, error)

Has checks whether the data is already in the queue

func (*UniqueQueue) LHandle

func (queue *UniqueQueue) LHandle(h func([]byte) error) error

LHandle receives a user callback function to handle the left element of the queue, if the function returns nil, then delete the element, otherwise keep the element.

func (*UniqueQueue) LPop

func (queue *UniqueQueue) LPop() ([]byte, error)

LPop pops data from left of the queue

func (*UniqueQueue) LPush

func (queue *UniqueQueue) LPush(data []byte) error

LPush pushes data to the left of the queue

func (*UniqueQueue) LPushFunc

func (queue *UniqueQueue) LPushFunc(data []byte, fn func() error) error

LPushFunc pushes data to the left of the queue and calls the callback if it is added

func (*UniqueQueue) Len

func (queue *UniqueQueue) Len() int64

Len returns the length of the queue

func (*UniqueQueue) RHandle

func (queue *UniqueQueue) RHandle(h func([]byte) error) error

RHandle receives a user callback function to handle the right element of the queue, if the function returns nil, then delete the element, otherwise keep the element.

func (*UniqueQueue) RPop

func (queue *UniqueQueue) RPop() ([]byte, error)

RPop pop data from the right of the queue

func (*UniqueQueue) RPush

func (queue *UniqueQueue) RPush(data []byte) error

RPush pushes data to the right of the queue

func (*UniqueQueue) RPushFunc

func (queue *UniqueQueue) RPushFunc(data []byte, fn func() error) error

RPushFunc pushes data to the right of the queue and calls the callback if is added

Jump to

Keyboard shortcuts

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