shard

package
v0.0.0-...-ec6159d Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package shard implements sharded writer.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PrefixSum64Hash

type PrefixSum64Hash struct {
	hash.Hash
}

PrefixSum64Hash implements hash.Hash64 interface.

func (*PrefixSum64Hash) Sum64

func (h *PrefixSum64Hash) Sum64() uint64

Sun64 implements hash.Hash64 interface. It simply reads the first 8 bytes by big endian.

type Writer

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

Writer writes data to one of the shard based on the hash function.

Example
name, err := ioutil.TempDir("", "test")
if err != nil {
	fmt.Println(err)
	return
}
defer os.RemoveAll(name) //nolint:wsl

w := NewWriter(5, &PrefixSum64Hash{sha512.New()}, NewOSFileWriterFactory(path.Join(name, "test-")))
records := []string{
	"test0",
	"test1",
	"test2",
	"test3",
	"test4",
	"test5",
	"test6",
	"test7",
	"test1",
}

for _, rec := range records {
	if _, err := w.Write([]byte(rec)); err != nil {
		fmt.Println(err)
	}
}

w.Close()

for i := 0; i < 5; i++ {
	filename := fmt.Sprintf("test-%05d-of-00005", i)
	b, _ := ioutil.ReadFile(path.Join(name, filename))
	fmt.Printf("%s:%s\n", filename, string(b))
}
Output:

test-00000-of-00005:test0test4
test-00001-of-00005:test7
test-00002-of-00005:test5test6
test-00003-of-00005:
test-00004-of-00005:test1test2test3test1

func NewWriter

func NewWriter(n int, h hash64, wf WriterFactory) *Writer

NewWriter returns a new sharded writer.

func (*Writer) Close

func (w *Writer) Close() error

Close closes sharded writer and never return error.

func (*Writer) Write

func (w *Writer) Write(data []byte) (int, error)

Write writes data to sharded writer.

type WriterFactory

type WriterFactory func(i, n int) io.Writer

WriterFactory returns a writer for each i of n.

func NewOSFileWriterFactory

func NewOSFileWriterFactory(prefix string) WriterFactory

NewOSFileWriterFactory returns a WriterFactory that returns os.File with the filenane prefixed by prefix and the index string such as 00000-of-00100, 00001-of-00100, and so on.

Jump to

Keyboard shortcuts

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