options

package
v25.0.0-split-vector3 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

In Dgraph, we have the abiltity to specify an @index directive for the type vfloat. Here, we want to expand that capability to allow us to specify something like:

myAttribute @index("hnsw-euclidean","maxNeighbors":"5","maxLevels":"5","exponent":"6")

Roughly, this should be read as: I want to specify an HNSW-type vector index using euclidean distance as a metric with maxNeighbors, maxLevels, and exponent being declared as written, and with all other parameters taking on default values. (In the case of specifying an exponent, this should affect the defaults for the other options, but that is something that can be decided by the factory for the hnsw index)!

But this leads to some natural questions: How do I know what option types are allowed? And how do I interpret their types? For example, how do I know that I should interpret "5" as an integer rather than a string?

The solution will be to allow each factory to specify a set of "allowed option types". Hence, we get the AllowedOptions class, which specifies a set of named options and corresponding parsers for the given name.

Now, if we want to populate an Options instance based on the content of myAttribute @index(hnsw(metric:"euclidean",exponent:"6")),

we collect the key-value pairs: "metric":"euclidean" "exponent":"6"

And we can now invoke: allowedOpts := hnswFactory.AllowedOpts() myAttributeIndexOpts := NewOptions()

val, err := allowedOpts.GetParsedOption("exponent", "6") if err != nil { return ErrBadOptionNoBiscuit } myAttributeIndexOpts.SetOpt("exponent", val)

The final resolution of the "puzzle" in fact is to invoke allowedOpts.PopulateOptions(pairs, myAttributeIndexOpts) based on pairs being built from the collection of key-value pairs.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Float32OptParser

func Float32OptParser(optValue string) (any, error)

Float32OptParser implements OptionParser specifically translating float32 options.

func Float64OptParser

func Float64OptParser(optValue string) (any, error)

Float64OptParser implements OptionParser specifically translating float64 options.

func GetInterfaceOpt

func GetInterfaceOpt(o Options, whichOpt string) (any, bool)

GetInterfaceOpt(o, whichOpt) will retrieve the option associated with whichOpt from o. If the value is found, this returns the found value and true, otherwise, this returns nil and false. It is the caller's responsibility to make sure that the option type stored is compatible with the variable to which it is being assigned. Similarly, it is the client responsibility to handle possible default values if the expected value is not found.

func GetOpt

func GetOpt[T c.Simple](o Options, whichOpt string, defaultValue T) (T, bool, error)

GetOpt(o, whichOpt, defaultValue) will attempt to retrieve the value associated with whichOpt in o. The values returned represent the retrieved (or default) value, whether or not the value was found, and possibly an error if there was a type conversion error. If o does not contain the key whichOpt:

This returns (defaultValue, false, nil)

If o finds a value for whichOpt, but the type does not match the type for defaultValue:

This returns (defaultValue, true, A Non-Nil-Error)

If o find a value "v" for whichOpt, and the value matches the type for defaultValue:

This returns (v, true, nil).

func IntOptParser

func IntOptParser(optValue string) (any, error)

IntOptParser implements OptionParser specifically translating int options. It assumes decimal representation of the value.

func StringOptParser

func StringOptParser(optValue string) (any, error)

StringOptionParser implements OptionParser specifically translating string options (well, it actually just passes in the string value as-is with no modification, but having this allows us to simply implement the OptionParser interface.

func UintOptParser

func UintOptParser(optValue string) (any, error)

UintOptParser implement OptionParser specifically translating uint options. It assumes decimal representation of the value.

Types

type AllowedOptions

type AllowedOptions map[string]OptionParser

AllowedOptions specifies the collection of allowed option kinds, and specifies how to interpret the values. The assumption is that at some point, options are being specified by some kind of key-value pair in a string, such as: "dataWidth:8". In this case, for a given index, if we want to form the desired options, we need to know that:

  1. "dataWidth" is an allowed option
  2. how to interpret the string "8".

In this case, to an AllowedOptions type, we would add the key "dataWidth" and an IntParser as the value (or whichever data type is appropriate for the dataWidth option).

func NewAllowedOptions

func NewAllowedOptions() AllowedOptions

func (AllowedOptions) AddCustomOption

func (ao AllowedOptions) AddCustomOption(optName string, parser OptionParser) AllowedOptions

AddCustomOption(optName) will specify that optName is an allowed option, and that the values will be interpreted by the provided parser.

func (AllowedOptions) AddFloat64Option

func (ao AllowedOptions) AddFloat64Option(optName string) AllowedOptions

AddFloat64Option(optName) will specify that optName is an allowed option, and that the values will be interepreted by the standard Float64OptionParser.

func (AllowedOptions) AddIntOption

func (ao AllowedOptions) AddIntOption(optName string) AllowedOptions

AddIntOption(optName) will specify that optName is an allowed option, and that the values will be interepreted by the standard IntOptionParser.

func (AllowedOptions) AddStringOption

func (ao AllowedOptions) AddStringOption(optName string) AllowedOptions

AddStringOption(optName) will specify that optName is an allowed option, and that the values will be interepreted by the standard StringOptionParser.

func (AllowedOptions) GetOptionList

func (ao AllowedOptions) GetOptionList() []string

ao.GetOptionList() provides the list of named options for ao.

func (AllowedOptions) GetParsedOption

func (ao AllowedOptions) GetParsedOption(optName, optValue string) (any, error)

ao.GetParsedOption(optName, optValue) will parse optValue based on the parser associated with optName in ao. Note that if ao does not specify a parser for optName, this will panic!

func (AllowedOptions) ParsePair

func (ao AllowedOptions) ParsePair(pair OptionValuePair) (any, error)

ao.ParsePair(pair) is shorthand for:

ao.GetParsedOption(pair.Option, pair.Value)

func (AllowedOptions) PopulateOptions

func (ao AllowedOptions) PopulateOptions(pairs []OptionValuePair, opts Options) error

ao.PopulateOptions(pairs, opts) will use ao to interpret pairs and use the results to populate opts. This will look at each OptionValuePair in pairs, and if the value can be properly parsed, it will add the result to opts for the given option and result. If one or more of the options cannot be properly interpreted, this results in an error.

type OptionParser

type OptionParser func(optValue string) (any, error)

OptionParser translates a string to a value (assuming that the option value is well-formed). See AllowedOptions to see the purpose.

type OptionValuePair

type OptionValuePair struct {
	Option string
	Value  string
}

OptionValue pair represents the use of a particular option with a string representation of its intended value. It is expected that one might originally encounter a string representation of the value, but then might need to interpret the intended value for the option.

type Options

type Options map[string]any

An Options instance maps the various named options to their specific values. This is intended as a generic mechanism to specify construction options. Note that the value types can be mixed: i.e., we might store both an int option value and a string option value in the same map.

func NewOptions

func NewOptions() Options

NewOptions() creates a new instance of Options, allowing a client to then modify it to specify whichever packaged options desired.

func (Options) SetOpt

func (o Options) SetOpt(whichOpt string, value any) Options

SetOpt(o, whichOpt, value) will associate whichOpt with value.

func (Options) Specifies

func (o Options) Specifies(whichOpt string) bool

o.Specifies(whichOpt) returns true if whichOpt has been assigned a value in o, and false otherwise.

Jump to

Keyboard shortcuts

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