ewah

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2021 License: MIT Imports: 6 Imported by: 0

README

go-ewah GoDoc Build Status codecov Go Report Card

go-ewah is a pure Go implementation of the EWAH bitmap compression format with no dependencies other than the assertion library for the tests.

Goals

Install

go get github.com/erizocosmico/go-ewah

Usage

Read from bytes
bitmap, err := bitmap.FromBytes(somebytes, binary.BigEndian)
if err != nil {
    // check error
}

value, err := bitmap.Get(6)
if err != nil {
    // check error
}

fmt.Println(value) // either `true` or `false`
Read from a reader
bitmap, err := bitmap.FromReader(somereader, binary.BigEndian)
if err != nil {
    // check error
}
 
// the rest is the same as the previous example
Create bitmap manually

By default all bits are 0, so we only call Set(bitPos) to mark the ones. Once a bit N has been set, you can't set a bit M whose index is lower than the index of N.

b := bitmap.New()

if err := b.Set(6); err != nil {
    // handle error
}
Write bitmap
b := bitmap.New()

// fill the bitmap

w := bytes.NewBuffer(nil)
bytesWritten, err := b.Write(w, binary.BigEndian)
if err != nil {
    // handle error
}

Data format

For more details regarding the compression format, please see Section 3 of the following paper:

Daniel Lemire, Owen Kaser, Kamel Aouiche, Sorting improves word-aligned bitmap indexes. Data & Knowledge Engineering 69 (1), pages 3-28, 2010. http://arxiv.org/abs/0901.3751

Or check the Java reference implementation.

Benchmarks

$ go test . -bench=. -benchmem
goos: darwin
goarch: amd64
pkg: github.com/erizocosmico/go-ewah
BenchmarkBitmapGet-4            100000000               17.5 ns/op             0 B/op          0 allocs/op
BenchmarkBitmapWrite-4           5000000               338 ns/op              64 B/op          9 allocs/op
BenchmarkBitmapRead-4            3000000               537 ns/op             272 B/op         12 allocs/op
BenchmarkBitmapSet-4            200000000                8.14 ns/op            0 B/op          0 allocs/op
PASS
ok      github.com/erizocosmico/go-ewah 44.797s

Benchmarks run on a MacBook Pro Retina (mid-2014) with a 2,6 GHz Intel Core i5 processor running macOS High Sierra 10.13.3.

License

MIT, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidBitSet = errors.New("bitmap: attempted to set a bit before the last written bit")

ErrInvalidBitSet is returned when there is an attempt to set a bit before the last written bit.

Functions

This section is empty.

Types

type Bitmap

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

Bitmap is an EWAH-encoded bitmap. See: https://github.com/lemire/javaewah

func FromBytes

func FromBytes(b []byte, order binary.ByteOrder) (*Bitmap, error)

FromBytes creates a Bitmap from the given bytes.

func FromReader

func FromReader(r io.Reader, order binary.ByteOrder) (*Bitmap, error)

FromReader creates a Bitmap from the given reader.

func New

func New() *Bitmap

New creates a new empty bitmap.

func (*Bitmap) Bits

func (b *Bitmap) Bits() uint32

Bits returns the number of uncompressed bits in the bitmap.

func (*Bitmap) Bytes

func (b *Bitmap) Bytes() int64

Bytes returns the number of bytes taken by the compressed bitmap.

func (*Bitmap) Get

func (b *Bitmap) Get(pos int64) bool

Get returns the bit at the given position, being true 1 and false 0.

func (*Bitmap) Reset

func (b *Bitmap) Reset()

Reset clears the bitmap and sets everything to unused empty zeroes.

func (*Bitmap) Set

func (b *Bitmap) Set(pos int64) error

Set sets to 1 the bit at the given position. Take into account that bits need to be set in ascending order. Setting the 4th bit will return an error if you already set the 5th bit, for example.

func (*Bitmap) Write

func (b *Bitmap) Write(w io.Writer, order binary.ByteOrder) (n int64, err error)

Write will write the Bitmap to a writer with the following format: https://github.com/git/git/blob/master/Documentation/technical/bitmap-format.txt#L92

Jump to

Keyboard shortcuts

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