hfile

package module
v0.0.0-...-f84b774 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2019 License: Apache-2.0 Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

HFile v1 implementation in Go

Code is mostly taken from Foursquare's Quiver project, with the following modifications:

  1. Extracted into another repository to reduce the amount of dependencies
  2. Allow reading HFile from any io.Reader, so no longer limited to an OS file.

HFile is a KV file format (borrowed from HBase, based on SSTables) used at foursquare as the storage format for our distributed KV store quiver.

HFiles are designed to be simultaneously a) easily to generate incrementally (metadata is flushed at the end) and b) easy to serve / search as-is (thanks to an index), making them friendly to pipelines that regenerate large datasets and need to bulk-load them into production regularly.

Summary of HFile format

On-disk, an HFile consists of:

  • some number of blocks,
  • an index, specifying the offset and length of each block along with its first key
  • a fixed-size, fixed-layout trailer of metadata

Each block contains some number of key-value pairs, each in the form of two 4-byte sizes i and j, followed by i bytes of key and j bytes of value.

Finding data in an Hfile

Callers likely want to construct a Reader per file, and a Scanner per request.

Scanners are stateful (see below) and are not threadsafe.

Reader

Readers read the trailer to find the index and load the index into memory. When a specific block's data is requested, the reader uses the index's offsets to find its data, decompresses it if needed and returns it.

Scanner

A scanner looks up a key by binary searching the reader's block index, comparing the firstKey until it finds the last block with a starting key less than or equal to the requested key. It then iterates through the key-value pairs of the block until it finds a matching key, or returns nothing if it finds a greater key or the end of the block.

Sorting requested keys

Scanners keep a reference to the most-recently loaded block, and their position within said block. Subsequent lookups only binary search the remaining blocks, and, when searching the current block, only search forward from the current position. This saves substantial repeated searching, but requires that keys be looked up in ascending order.

Scanners can be Reset() to return to their initial state.

Scanners keep track of the last key they looked up, and will error if a smaller key is requested.

Writer

Writers support creating an hfile by repeatedly passing key-value pairs to Write(k,v), in ascending order by key, before calling Close() to flush the index and metadata.

Writers buffer written k-v pairs until they reach blockSize, after which the next new key written will flush the block (optionally compressed) to the underlying storage and start a new one. NB: Since all k-v pairs for a given key must appear in the same block, emiting too many values for the same key, or very large values, can cause blocks to grow well beyond blockSize.

NewWriter wraps any io.WriteCloser. NewLocalWriter provides a helper that uses a local file at supplied path.

Authors

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CompressionNone = uint32(2)
View Source
var CompressionSnappy = uint32(3)
View Source
var DataMagic = []byte("DATABLK*")
View Source
var IndexMagic = []byte("IDXBLK)+")
View Source
var TrailerMagic = []byte("TRABLK\"$")

Functions

func After

func After(a, b []byte) bool

func GenerateMockHfile

func GenerateMockHfile(path string, keyCount, blockSize int, compress, progress bool) error

func GenerateMockMultiHfile

func GenerateMockMultiHfile(path string, keyCount, blockSize int, compress, progress bool) error

func MockKeyInt

func MockKeyInt(i int) []byte

func MockMultiValueInt

func MockMultiValueInt(i, k int) []byte

func MockValueForMockKey

func MockValueForMockKey(key []byte) []byte

func MockValueInt

func MockValueInt(i int) []byte

func WriteMockIntPairs

func WriteMockIntPairs(w *Writer, keyCount int, progress bool, multi bool) error

Types

type Block

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

func (*Block) IsAfter

func (b *Block) IsAfter(key []byte) bool

type FileInfo

type FileInfo struct {
	InfoFields map[string]string // Human-readable fields read from the FileInfo block.
}

type Iterator

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

func NewIterator

func NewIterator(r *Reader) *Iterator

func (*Iterator) AllForPrefixes

func (it *Iterator) AllForPrefixes(prefixes [][]byte, limit int32, lastKey []byte) (map[string][][]byte, []byte, error)

func (*Iterator) Key

func (it *Iterator) Key() []byte

A copy of the current key it.key is a pointer into a buffer that may get recycled for another block, thus the copy.

func (*Iterator) Next

func (it *Iterator) Next() (bool, error)

Load a kv pair into it.key/it.value and advance the iterator. Return true if a kv pair was loaded (eg can call Key), or false if at EOF.

func (*Iterator) Release

func (it *Iterator) Release()

func (*Iterator) Reset

func (it *Iterator) Reset()

func (*Iterator) Seek

func (it *Iterator) Seek(requested []byte) (bool, error)

Position the iterator at-or-after requested key by seeking forward from current position.

If already at-or-after, this is a no-op. Otherwise, if the requested key exists, it will be positioned there, otherwise at the first key greater than the requested one. Returns true if, after positioning, the iterator is on a valid key (eg not at EOF), same as Next.

func (*Iterator) Value

func (it *Iterator) Value() []byte

A copy of the current value it.key is a pointer into a buffer that may get recycled for another block, thus the copy.

type OrderedOps

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

func (*OrderedOps) CheckIfKeyOutOfOrder

func (s *OrderedOps) CheckIfKeyOutOfOrder(key []byte) error

func (*OrderedOps) ResetState

func (s *OrderedOps) ResetState()

func (*OrderedOps) Same

func (s *OrderedOps) Same(key []byte) bool

type Reader

type Reader struct {
	FileInfo
	Trailer
	// contains filtered or unexported fields
}

func NewReader

func NewReader(data io.Reader) (*Reader, error)

func (*Reader) CalculateBloom

func (r *Reader) CalculateBloom(falsePosRate float64) error

func (*Reader) DisableBloom

func (r *Reader) DisableBloom()

func (*Reader) EnableBloom

func (r *Reader) EnableBloom()

func (*Reader) FindBlock

func (r *Reader) FindBlock(from int, key []byte) int

func (*Reader) FirstKey

func (r *Reader) FirstKey() ([]byte, error)

func (*Reader) GetBlockBuf

func (r *Reader) GetBlockBuf(i int, dst []byte) ([]byte, error)

func (*Reader) GetIterator

func (r *Reader) GetIterator() *Iterator

func (*Reader) GetScanner

func (r *Reader) GetScanner() *Scanner

func (*Reader) MightContain

func (r *Reader) MightContain(key []byte) bool

func (*Reader) PrintDebugInfo

func (r *Reader) PrintDebugInfo(out io.Writer, includeStartKeys int)

type Scanner

type Scanner struct {

	// When off, maybe be faster but may return incorrect results rather than error on out-of-order keys.
	EnforceKeyOrder bool
	OrderedOps
	// contains filtered or unexported fields
}

func NewScanner

func NewScanner(r *Reader) *Scanner

func (*Scanner) GetAll

func (s *Scanner) GetAll(key []byte) ([][]byte, error)

func (*Scanner) GetFirst

func (s *Scanner) GetFirst(key []byte) ([]byte, error, bool)

func (*Scanner) Release

func (s *Scanner) Release()

func (*Scanner) Reset

func (s *Scanner) Reset()

type Trailer

type Trailer struct {
	FileInfoOffset             uint64
	DataIndexOffset            uint64
	DataIndexCount             uint32
	MetaIndexOffset            uint64
	MetaIndexCount             uint32
	TotalUncompressedDataBytes uint64
	EntryCount                 uint32
	CompressionCodec           uint32
	// contains filtered or unexported fields
}

type Writer

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

func NewLocalWriter

func NewLocalWriter(path string, compress bool, blockSize int) (*Writer, error)

func NewWriter

func NewWriter(out io.WriteCloser, compress bool, blockSize int) (*Writer, error)

func (*Writer) Close

func (w *Writer) Close() error

func (*Writer) Write

func (w *Writer) Write(k, v []byte) error

Jump to

Keyboard shortcuts

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