bitseq

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2015 License: Apache-2.0, BSD-2-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package bitseq provides a structure and utilities for representing long bitmask as sequence of run-lenght encoded blocks. It operates direclty on the encoded representation, it does not decode/encode.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckIfAvailable

func CheckIfAvailable(head *Sequence, ordinal int) (int, int, error)

CheckIfAvailable checks if the bit correspondent to the specified ordinal is unset If the ordinal is beyond the Sequence limits, a negative response is returned

func GetFirstAvailable

func GetFirstAvailable(head *Sequence) (int, int, error)

GetFirstAvailable looks for the first unset bit in passed mask

Types

type Handle

type Handle struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Handle contains the sequece representing the bitmask and its identifier

func NewHandle

func NewHandle(app string, ds datastore.DataStore, id string, numElements uint32) (*Handle, error)

NewHandle returns a thread-safe instance of the bitmask handler

func (*Handle) Bits

func (h *Handle) Bits() uint32

Bits returns the length of the bit sequence

func (*Handle) CheckIfAvailable

func (h *Handle) CheckIfAvailable(ordinal int) (int, int, error)

CheckIfAvailable checks if the bit correspondent to the specified ordinal is unset If the ordinal is beyond the Sequence limits, a negative response is returned

func (*Handle) Destroy

func (h *Handle) Destroy()

Destroy removes from the datastore the data belonging to this handle

func (*Handle) Exists

func (h *Handle) Exists() bool

Exists method is true if this object has been stored in the DB.

func (*Handle) FromByteArray

func (h *Handle) FromByteArray(ba []byte) error

FromByteArray reads his handle's data from a byte array

func (*Handle) GetFirstAvailable

func (h *Handle) GetFirstAvailable() (int, int, error)

GetFirstAvailable returns the byte and bit position of the first unset bit

func (*Handle) Index

func (h *Handle) Index() uint64

Index returns the latest DB Index as seen by this object

func (*Handle) Key

func (h *Handle) Key() []string

Key provides the Key to be used in KV Store

func (*Handle) KeyPrefix

func (h *Handle) KeyPrefix() []string

KeyPrefix returns the immediate parent key that can be used for tree walk

func (*Handle) PushReservation

func (h *Handle) PushReservation(bytePos, bitPos int, release bool) error

PushReservation pushes the bit reservation inside the bitmask.

func (*Handle) SetIndex

func (h *Handle) SetIndex(index uint64)

SetIndex method allows the datastore to store the latest DB Index into this object

func (*Handle) SetValue

func (h *Handle) SetValue(value []byte) error

SetValue unmarshals the data from the KV store

func (*Handle) ToByteArray

func (h *Handle) ToByteArray() ([]byte, error)

ToByteArray converts this handle's data into a byte array

func (*Handle) Unselected

func (h *Handle) Unselected() uint32

Unselected returns the number of bits which are not selected

func (*Handle) Value

func (h *Handle) Value() []byte

Value marshals the data to be stored in the KV store

type Sequence

type Sequence struct {
	Block uint32    // block representing 4 byte long allocation bitmask
	Count uint32    // number of consecutive blocks
	Next  *Sequence // next sequence
}

Sequence reresents a recurring sequence of 32 bits long bitmasks

func NewSequence

func NewSequence(numElements uint32) *Sequence

NewSequence returns a sequence initialized to represent a bitmaks of numElements bits

func PushReservation

func PushReservation(bytePos, bitPos int, head *Sequence, release bool) *Sequence

PushReservation pushes the bit reservation inside the bitmask. Given byte and bit positions, identify the sequence (current) which holds the block containing the affected bit. Create a new block with the modified bit according to the operation (allocate/release). Create a new Sequence containing the new Block and insert it in the proper position. Remove current sequence if empty. Check if new Sequence can be merged with neighbour (previous/Next) sequences.

Identify "current" Sequence containing block:

[prev seq] [current seq] [Next seq]

Based on block position, resulting list of sequences can be any of three forms:

Block position                        Resulting list of sequences

A) Block is first in current: [prev seq] [new] [modified current seq] [Next seq] B) Block is last in current: [prev seq] [modified current seq] [new] [Next seq] C) Block is in the middle of current: [prev seq] [curr pre] [new] [curr post] [Next seq]

func (*Sequence) Equal

func (s *Sequence) Equal(o *Sequence) bool

Equal checks if this sequence is equal to the passed one

func (*Sequence) FromByteArray

func (s *Sequence) FromByteArray(data []byte) error

FromByteArray construct the sequence from the byte array TODO (aboch): manage network/host order stuff

func (*Sequence) GetAvailableBit

func (s *Sequence) GetAvailableBit() (bytePos, bitPos int)

GetAvailableBit returns the position of the first unset bit in the bitmask represented by this sequence

func (*Sequence) GetCopy

func (s *Sequence) GetCopy() *Sequence

GetCopy returns a copy of the linked list rooted at this node

func (*Sequence) String

func (s *Sequence) String() string

String returns a string representation of the block sequence starting from this block

func (*Sequence) ToByteArray

func (s *Sequence) ToByteArray() ([]byte, error)

ToByteArray converts the sequence into a byte array TODO (aboch): manage network/host order stuff

Jump to

Keyboard shortcuts

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