blobqueue

package module
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2023 License: MIT Imports: 2 Imported by: 3

README

go-blobqueue

GoDoc Coverage Build

About

Blobqueue provides an interface used to manipulate a queue of objects marshalled into []byte (the blob part). Two implementations are provided:

  • blobqueue/memory: a pure go native implementation based on a [][]byte
  • blobqueue/redisqueue: an implementation using a Redis list. (using Redis client go-redis/redis)

Getting started

1. Memory

This example shows how to create a basic and safe memory queue. A safe queue wraps another one to properly handles locks.

import (
	"fmt"

	"github.com/blueboardio/go-blobqueue"
	"github.com/blueboardio/go-blobqueue/memory"
)

func getStarted() {
	// unsafeQueue implements Queue so you could  use it as it is, but it's not safe against race conditions.
	unsafeQueue := memory.Queue{}

	// This queue handles locks properly.
	queue := blobqueue.SafeQueue(&unsafeQueue)
}
2. Redis

This example show how to create a Redis based queue.

import (
	"fmt"

	"github.com/blueboardio/go-blobqueue/queueredis"
	"github.com/go-redis/redis"
)

func getStarted() {
	client := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})

	// Instantiate a redis queue storing its data under the key "hello_world_queue"
	queue := queueredis.New(client, "hello_world_queue")
}

3. Usage

You can now use theses queues like so:

	queue.Push([]byte("world"))
	queue.Unshift([]byte("hello"))
	list, _ := queue.List()
	fmt.Println(list)
	// Bytes: [[104 101 108 108 111] [119 111 114 108 100]]
	// Strings: ["hello" "world"]

	first, _ := queue.Shift()
	list, _ = queue.List()
	fmt.Println(first, list)
	// Bytes: [104 101 108 108 111] [[119 111 114 108 100]]
	// Strings: "hello" ["world"]

See GoDoc for all available methods.

Testing

The package blobqueue/queuetesting provides a test suite to validate implementation of bloqueue.Queue.

Running Redis implementation test

Redis implementation depends on a Redis connection, so you'll need to have a working Redis server to pass tests. In order to connect when running tests you'll have to set these environment variables:

Name Usage Example
TEST_QUEUEREDIS_ADDR The redis host used to set connection localhost:6379
TEST_QUEUEREDIS_PWD The password to use
TEST_QUEUEREDIS_DB The database index to use 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrIndexOutOfRange = errors.New("index out of range")
	ErrQueueIsEmpty    = errors.New("empty queue")
)

These variables defines typed errors.

Functions

This section is empty.

Types

type Must added in v0.2.0

type Must struct {
	Queue Queue
}

Must wraps a Queue with methods that panic instead of returning error.

Example
q := blobqueue.Must{new(memory.Queue)}
fmt.Println(q.Len())
q.Unshift([]byte("AA"))
q.Push([]byte("BB"))
fmt.Printf("%s\n", q.List())
fmt.Println(q.Len())
fmt.Printf("%s %s\n", q.Pop(), q.Shift())
fmt.Println(q.Len())
Output:

0
[AA BB]
2
BB AA
0

func (Must) Empty added in v0.2.0

func (q Must) Empty()

Empty transforms errors from Queue.Empty into panic.

func (Must) Len added in v0.2.0

func (q Must) Len() int

Len transforms errors from Queue.Len into panic.

func (Must) List added in v0.2.0

func (q Must) List() [][]byte

List transforms errors from Queue.List into panic.

func (Must) Pop added in v0.2.0

func (q Must) Pop() []byte

Unshift transforms errors from Queue.Unshift into panic.

func (Must) Push added in v0.2.0

func (q Must) Push(val []byte)

Push transforms errors from Queue.Push into panic.

func (Must) Shift added in v0.2.0

func (q Must) Shift() []byte

Shift transforms errors from Queue.Shift into panic.

func (Must) Unshift added in v0.2.0

func (q Must) Unshift(val []byte)

Unshift transforms errors from Queue.Unshift into panic.

type Queue

type Queue interface {
	// List returns all the items of the queue as [][]byte.
	List() ([][]byte, error)

	// Push appends the queue with a new elem (val).
	Push(val []byte) error
	// Unshift add a new elem at the beggining of the queue.
	Unshift(val []byte) error

	// Pop deletes the last element of the queue, it returns this precise element.
	// If the queue is empty it returns error ErrQueueIsEmpty.
	Pop() ([]byte, error)

	// Shift deletes the first element of the queue, it returns this precise element.
	// If the queue is empty it returns error ErrQueueIsEmpty.
	Shift() ([]byte, error)

	// Len returns the length of the queue.
	Len() (int, error)
	// Empty clears the queue.
	Empty() error
}

Queue is an interface providing operations to handle queues. All methods may return an error as its implementation may have runtime failures.

func SafeQueue

func SafeQueue(q Queue) Queue

SafeQueue takes a Queue and wraps it to lock properly all operations.

Directories

Path Synopsis
queuemsgpack module
queueredis module
typedqueue module

Jump to

Keyboard shortcuts

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