chunk

package
v0.0.0-...-f9b9731 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Overview

Package chunk contains Chunk interface which defines functions for accessing to a chunk. Chunk is a an ordered set of records which are stored somewhere and which could be iterated over. Using the Chunk interface and the chunk Iterator, a client code can receive access to the set of records, add new ones and walk over them.

Every chunk has an unique identifier Id. The Ids are sortable, so chunks can be ordered by using their Ids

Index

Constants

This section is empty.

Variables

View Source
var (
	EmptyListener = Listener(emptyListener{})
)

Functions

func FindChunkById

func FindChunkById(sortedChunks Chunks, cid Id) int

FindChunkById returns index of chunk with cid in the sortedChunks slice. It returns -1, if there is no chunk with cid in the slice.

Types

type Chunk

type Chunk interface {
	// Chunk inherits io.Closer interface
	io.Closer

	// Id returns the chunk id which must be unique between a group of
	// chunks like a journal. Also it is sortable, so chunks with less
	// value of the id comes before the chunks with greater values
	Id() Id

	// Write allows to write records to the chunk. It expects context and
	// the iterator, which provides the records source.
	//
	// Write returns 3 values:
	// 1. the number of records written (int value)
	// 2. the number of records in the chunk after the write (uint32). The
	// 	number can be greater than value returned by Count, what indicates
	//  that not all written records are persisted yet.
	// 3. an error if any:
	// 		ErrMaxSizeReached - when the write cannot be done because of
	//				the size limits
	Write(ctx context.Context, it records.Iterator) (int, uint32, error)

	// Sync allows to flash just written data to the storage
	Sync()

	// Iterator returns a chunk.Iterator object to read records from the chunk
	Iterator() (Iterator, error)

	// Size returns the size of the chunk
	Size() int64

	// Count returns number of records in the chunk. The number can be
	// less than the Write() function returned value, cause the Count() returns
	// then number of records that can be read, but Write returns the number of
	// records written, but not confirmed to be read yet in the chunk
	Count() uint32

	// AddListener adds a listener to the chunk
	AddListener(lstnr Listener)
}

Chunk is an abstraction which represents a records storage. It has its own id, which should be sortable within a group of chunks like a journal.

Chunk cannot contain more than 2^32 records.

type Chunks

type Chunks []Chunk

type Id

type Id uint64

Id represents a chunk identifier. The NewId() function generates new unique Id.

The identifier is 64bit unsigned integer value, which consists of 2 parts. Highest part of the value contains UNIX timestamp in nanoseconds cut to 48bits, and the lowest 16bits contain a process-related hash which supposed to be unique per a process. Based on the nature of the Id generation, it is supposed that every chunk has it's own unique id and it can be ordered comparing to other chunks in the system.

func NewId

func NewId() Id

NewId generates new the host unique chunk id. The chunk IDs are sortable, lately created chunks have greater ID values than older ones.

func ParseId

func ParseId(cid string) (Id, error)

ParseCId receives cid - string representation of an Id and tries to turn it to Id. Returns an error if the string cannot be decoded.

func (Id) String

func (id Id) String() string

String returns a string representation of the chunk Id

type Iterator

type Iterator interface {
	// The Iterator ihnerits io.Closer interfase. Being called the behavior
	// of the iterator is unpredictable and it must not be used anymore
	io.Closer

	// The Iterator inherits Iterator interface
	records.Iterator

	// Pos returns the current iterator position within the chunk. It could
	// return value in [0..Chunk.Count()) range
	Pos() int64

	// SetPos sets the current position within the chunk. The pos param must be in
	// between [0..Chunk.Count()].
	//
	// Setting pos to Chunk.Count() value, causes that the Get() function will
	// return (nil, io.EOF) or the new record, which has been added to the
	// chunk after the SetPos() call, but before the Get() call.
	SetPos(pos int64) error
}

Iterator is a records.Iterator extension, which allows to read records from a Сhunk.

Chunk supposes that the records are resided in a storage. Each record has its offset or position. The position could be in the range [0..Chunk.Count()) Current position contains position of the record which could be read by Get() function. Position could be changed via Next() function or via SetPos() function.

type Listener

type Listener interface {

	// OnNewData is called when new records were added to the end
	OnNewData(c Chunk)
}

Listener an interface which can be used for receiving some chunk events (like OnNewData() notifications)

Directories

Path Synopsis
Package chunkfs contains structures and functions which allow to work with chunks stored in files.
Package chunkfs contains structures and functions which allow to work with chunks stored in files.

Jump to

Keyboard shortcuts

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