bytesort

package module
v0.0.0-...-3c6f839 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2017 License: MIT Imports: 4 Imported by: 0

README

bytesort

GoDoc Build Status Coverage Status Go Report Card

Package bytesort encodes common Go types as binary/byte slices that are bytewise sortable.

The output is intended for binary/bytewise comparison and sorting. More specifically for creating the keys used in indexes of key value stores.

Install

go get github.com/nochso/bytesort

Usage

Full documentation is available at godoc.org.

Output example
vv := []interface{}{
	"abc",
	int16(math.MinInt16),
	int16(0),
	int16(math.MaxInt16),
	false,
	true,
}
for _, v := range vv {
	b, err := bytesort.Encode(v)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("% 8X %-6T %#v\n", b, v, v)
}
// Output:
// 61 62 63 string "abc"
//    00 00 int16  -32768
//    80 00 int16  0
//    FF FF int16  32767
//       00 bool   false
//       01 bool   true

Use bytes.Compare and sort.Slice to sort a slice of []byte:

input := [][]byte{ ... }
sort.Slice(s, func(i, j int) bool {
	return bytes.Compare(s[i], s[j]) < 0
})

Using sort.Sort on structs that implement sort.Interface might be faster.

sort.Search might also be of interest.

Change log and versioning

This project adheres to Semantic Versioning.

See the CHANGELOG for a full history of releases.

License

MIT.

Documentation

Overview

Package bytesort encodes common types as binary/byte slices that are bytewise sortable.

The output is intended for binary/bytewise comparison and sorting. More specifically for creating the keys used in indexes in a bolt DB.

Use bytes.Compare and sort.Slice to sort [][]byte:

sort.Slice(s, func(i, j int) bool {
	return bytes.Compare(s[i], s[j]) < 0
})

sort.Search might also be of interest.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(v interface{}) (b []byte, err error)

Encode a value as a byte slice that is bytewise/binary-sortable.

Any results for the same type are sortable using a bytewise/binary comparison. A correct Sort order is not guaranteed when comparing different types.

The length is the same for values of the same type. Except types string and []byte as they vary in length.

Sortability is the only requirement. None of the encodings retain any type information because decoding of binary back into a value is out of scope.

The following types are supported:

bool
float32 float64
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64
string    (case-sensitive)
time.Time (normalised to UTC)
[]byte    (copied)
Example
package main

import (
	"fmt"
	"math"

	"github.com/nochso/bytesort"
)

func main() {
	vv := []interface{}{
		"abc",
		int16(math.MinInt16),
		int16(0),
		int16(math.MaxInt16),
		false,
		true,
	}
	for _, v := range vv {
		b, err := bytesort.Encode(v)
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Printf("% 8X %-6T %#v\n", b, v, v)
	}
}
Output:

61 62 63 string "abc"
   00 00 int16  -32768
   80 00 int16  0
   FF FF int16  32767
      00 bool   false
      01 bool   true

Types

type Encoder

type Encoder interface {
	// EncodeSortable encodes the receiver as a byte slice in a byte sortable way.
	EncodeSortable() ([]byte, error)
}

Encoder is the interface used for encoding custom types.

Source Files

  • bytesort.go

Jump to

Keyboard shortcuts

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