partitioner

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package partitioner implements utility routines for partitioning of a single YAML file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config defines common configuration for yaml partitioning. Config must be immutable.

func NewConfig

func NewConfig(opts ...Option) (*Config, error)

NewConfig creates a new common configuration for yaml partitioning.

Example:

var shardNames = []string{
	"alpha",
	"beta",
	"gamma",
	"delta",
	"epsilon",
}

rndv, _ := hrw.New(xxhash.Sum64, shardNames...)

cfg, err := NewConfig(

WithConsistentHashing(rndv),
WithReplicasCount(1),
WithResultYamlIndent(2),
WithSplitPoint("groups.*.rules"),
WithThisShardID(-1),

) .

func (*Config) NodeNames

func (c *Config) NodeNames() []string

NodeNames returns names of nodes in the ConsistentHashing.

func (*Config) NodesCount

func (c *Config) NodesCount() int

NodesCount returns the number of nodes in the ConsistentHashing.

func (*Config) WorkDir

func (c *Config) WorkDir() string

WorkDir returns the working directory (temp dir).

type ConsistentHashing

type ConsistentHashing interface {
	// NodeNames returns the list of node names in the ConsistentHashing
	NodeNames() []string
	// NodesCount returns the number of nodes in the ConsistentHashing
	NodesCount() int
	// Get gets the most suitable node name for a key
	Get(key []byte) string
	// GetN gets N most suitable node names for a key
	GetN(key []byte, replicasCount int) map[string]struct{}
}

ConsistentHashing represent an abstract interface for consistent hashing.

type Option

type Option func(*Config) error

Option is a type for a function that accepts a pointer to an empty or minimal Config struct created in the Config constructor. Option represents Functional Options Pattern. See this article for details - https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis .

func WithConsistentHashing

func WithConsistentHashing(h ConsistentHashing) Option

WithConsistentHashing sets the field with the object that implements ConsistentHashing interface REQUIRED .

func WithReplicasCount

func WithReplicasCount(n int) Option

WithReplicasCount sets the number of replicas. Meaning how many shards will get the same data. This defaults to 1.

func WithResultYamlIndent

func WithResultYamlIndent(i int) Option

WithResultYamlIndent sets the indentation used for Yaml encoding. This defaults to 2.

func WithSplitPoint

func WithSplitPoint(s string) Option

WithSplitPoint sets the path to a yaml Node, which represents the so-called SplitPoint. YamlPartitioner dives into the YAML structure up to the given yaml Node and starts sharding items from there. Note: the SplitPoint yaml Node Kind must be either a SequenceNode (list) or a MappingNode (map). The SplitPoint must be in format "<key>", "<key>.*.<key>" REQUIRED .

func WithThisShardID

func WithThisShardID(id int) Option

WithThisShardID sets the shard id for which YamlPartitioner creates the resulting YAML(s). This defaults to -1, meaning that YamlPartitioner creates the resulting YAML for all shards.

func WithWorkingDirectory

func WithWorkingDirectory(path string) Option

WithWorkingDirectory sets the directory where. This defaults to os.TempDir() .

type Partitioner

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

Partitioner represents the structure for partitioning a given input file.

func New

func New(inputFile, commonPath string, opts ...Option) (*Partitioner, error)

New creates a new partitioner for the given inputFile. inputFile is an absolute path to a file that needs to be partitioned.

commonPath: Say inputFile is <commonPath>/<path-to-file.yml>, the result of partitioning will be written to <workDir>/<shardName>/<path-to-file.yml>.

func WithConfig

func WithConfig(cfg *Config, inputFile, commonPath string) (*Partitioner, error)

WithConfig is being used to create a new partitioner with the previously created configuration.

func (*Partitioner) Report

func (p *Partitioner) Report() string

Report returns a partitioning report.

func (*Partitioner) Reset

func (p *Partitioner) Reset()

Reset sets the partitioner to its initial state.

func (*Partitioner) Run

func (p *Partitioner) Run(ctx context.Context) error

Run performs the partitioning of a given input file accordingly to settings in the Partitioner Config.

func (*Partitioner) ShardItemsCount

func (p *Partitioner) ShardItemsCount() map[string]int

ShardItemsCount returns how many items got each shard.

Jump to

Keyboard shortcuts

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