streamsort

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2018 License: Apache-2.0 Imports: 13 Imported by: 0

README

StreamSort

Build Status GoDoc Go Report Card License

Sort arbitrarily large data sets with a predictable amount of memory using temporary files.

Example:
import(
  "fmt"

  "github.com/bsm/streamsort"
)

func main() {
	// Init a Sorter with default options
	sorter := streamsort.New(nil)
	defer sorter.Close()

	// Append data
	sorter.Append([]byte("foo"))
	sorter.Append([]byte("bar"))
	sorter.Append([]byte("baz"))
	sorter.Append([]byte("boo"))

	// Sort and iterate
	iter, err := sorter.Sort(context.Background())
	if err != nil {
		panic(err)
	}
	defer iter.Close()

	for iter.Next() {
		fmt.Println(string(iter.Bytes()))
	}
	if err := iter.Err(); err != nil {
		panic(err)
	}

}

For more complex examples, please see our Documentation

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"

	"github.com/bsm/streamsort"
)

func main() {
	// Init a Sorter with default options
	sorter := streamsort.New(nil)
	defer sorter.Close()

	// Append data
	sorter.Append([]byte("foo"))
	sorter.Append([]byte("bar"))
	sorter.Append([]byte("baz"))
	sorter.Append([]byte("boo"))

	// Sort and iterate
	iter, err := sorter.Sort(context.Background())
	if err != nil {
		panic(err)
	}
	defer iter.Close()

	for iter.Next() {
		fmt.Println(string(iter.Bytes()))
	}
	if err := iter.Err(); err != nil {
		panic(err)
	}

}
Output:
bar
baz
boo
foo

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comparer

type Comparer interface {
	// Compare returns -1 when a is 'less than', 0 when a is 'equal to' or
	// +1' when a is 'greater than' b.
	Compare(a, b []byte) int
}

Comparer is used to compare data chunks for ordering

type ComparerFunc

type ComparerFunc func(a, b []byte) int

func (ComparerFunc) Compare

func (f ComparerFunc) Compare(a, b []byte) int

type Compression

type Compression uint8
const (
	CompressionNone Compression = iota
	CompressionGzip
)

type Iterator

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

Iterator allows to iterate over sorted outputs

func (*Iterator) Bytes

func (i *Iterator) Bytes() []byte

Bytes returns the chunk at the current position

func (*Iterator) Close

func (i *Iterator) Close() error

Close closes and releases the iterator

func (*Iterator) Err

func (i *Iterator) Err() error

Err returns the iterator error

func (*Iterator) Next

func (i *Iterator) Next() bool

Next advances the cursor to the next entry

type Options

type Options struct {
	// TempDir specifies the working directory.
	// By default standard temp is used
	TempDir string

	// Compararer defines the sort order.
	// Default: bytes.Compare
	Comparer Comparer

	// Compression is used for intermediate files.
	// Default: CompressionNone
	Compression Compression

	// MaxOpenFiles limits the number of open files; must be >1.
	// Default: 100
	MaxOpenFiles int

	// MaxMemBuffer limits the memory used for sorting
	// Default: 64M (must be at least 1M = 1024*1024)
	MaxMemBuffer int
}

Options contains sorting options

type Sorter

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

Sorter is responsible for sorting a stream

func New

func New(opt *Options) *Sorter

New inits a sorter

func (*Sorter) Append

func (s *Sorter) Append(data []byte) error

Append appends data to the sorter

func (*Sorter) Close

func (s *Sorter) Close() error

Close stops the processing and removes all temporary fnames

func (*Sorter) Sort

func (s *Sorter) Sort(ctx context.Context) (*Iterator, error)

Sort sorts the inputs and returns an interator output iterator. You must close the iterator after use.

Jump to

Keyboard shortcuts

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