indexes

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: Apache-2.0 Imports: 5 Imported by: 8

Documentation

Overview

Package indexes contains the most common indexes types to be used with a collections.IndexedMap. It also contains specialised helper functions to collect and query efficiently an index.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectKeyValues

func CollectKeyValues[K, V any, I iterator[K], Idx collections.Indexes[K, V]](
	ctx context.Context,
	indexedMap *collections.IndexedMap[K, V, Idx],
	iter I,
) (kvs []collections.KeyValue[K, V], err error)

CollectKeyValues collects all the keys and the values of an indexed map index iterator. The Iterator is fully consumed and closed.

func CollectValues

func CollectValues[K, V any, I iterator[K], Idx collections.Indexes[K, V]](
	ctx context.Context,
	indexedMap *collections.IndexedMap[K, V, Idx],
	iter I,
) (values []V, err error)

CollectValues collects all the values from an Index iterator and the IndexedMap. Closes the Iterator.

func ScanKeyValues

func ScanKeyValues[K, V any, I iterator[K], Idx collections.Indexes[K, V]](
	ctx context.Context,
	indexedMap *collections.IndexedMap[K, V, Idx],
	iter I,
	do func(kv collections.KeyValue[K, V]) (stop bool),
) (err error)

ScanKeyValues calls the do function on every record found, in the indexed map from the index iterator. Returning true stops the iteration. The Iterator is closed when this function exits.

func ScanValues

func ScanValues[K, V any, I iterator[K], Idx collections.Indexes[K, V]](
	ctx context.Context,
	indexedMap *collections.IndexedMap[K, V, Idx],
	iter I,
	f func(value V) (stop bool),
) error

ScanValues collects all the values from an Index iterator and the IndexedMap in a lazy way. The iterator is closed when this function exits.

func WithMultiUncheckedValue added in v0.3.0

func WithMultiUncheckedValue() func(*multiOptions)

WithMultiUncheckedValue is an option that can be passed to NewMulti to ignore index values different from '[]byte{}' and continue with the operation. This should be used only to behave nicely in case you have used values different from '[]byte{}' in your storage before migrating to collections. Refer to WithKeySetUncheckedValue for more information.

func WithReversePairUncheckedValue added in v0.3.0

func WithReversePairUncheckedValue() func(*reversePairOptions)

WithReversePairUncheckedValue is an option that can be passed to NewReversePair to ignore index values different from '[]byte{}' and continue with the operation. This should be used only if you are migrating to collections and have used a different placeholder value in your storage index keys. Refer to WithKeySetUncheckedValue for more information.

Types

type Multi

type Multi[ReferenceKey, PrimaryKey, Value any] struct {
	// contains filtered or unexported fields
}

Multi defines the most common index. It can be used to create a reference between a field of value and its primary key. Multiple primary keys can be mapped to the same reference key as the index does not enforce uniqueness constraints.

func NewMulti

func NewMulti[ReferenceKey, PrimaryKey, Value any](
	schema *collections.SchemaBuilder,
	prefix collections.Prefix,
	name string,
	refCodec codec.KeyCodec[ReferenceKey],
	pkCodec codec.KeyCodec[PrimaryKey],
	getRefKeyFunc func(pk PrimaryKey, value Value) (ReferenceKey, error),
	options ...func(*multiOptions),
) *Multi[ReferenceKey, PrimaryKey, Value]

NewMulti instantiates a new Multi instance given a schema, a Prefix, the humanized name for the index, the reference key key codec and the primary key key codec. The getRefKeyFunc is a function that given the primary key and value returns the referencing key.

func (*Multi[ReferenceKey, PrimaryKey, Value]) Iterate

func (m *Multi[ReferenceKey, PrimaryKey, Value]) Iterate(ctx context.Context, ranger collections.Ranger[collections.Pair[ReferenceKey, PrimaryKey]]) (MultiIterator[ReferenceKey, PrimaryKey], error)

func (*Multi[K1, K2, Value]) KeyCodec

func (m *Multi[K1, K2, Value]) KeyCodec() codec.KeyCodec[collections.Pair[K1, K2]]

func (*Multi[ReferenceKey, PrimaryKey, Value]) MatchExact

func (m *Multi[ReferenceKey, PrimaryKey, Value]) MatchExact(ctx context.Context, refKey ReferenceKey) (MultiIterator[ReferenceKey, PrimaryKey], error)

MatchExact returns a MultiIterator containing all the primary keys referenced by the provided reference key.

func (*Multi[ReferenceKey, PrimaryKey, Value]) Reference

func (m *Multi[ReferenceKey, PrimaryKey, Value]) Reference(ctx context.Context, pk PrimaryKey, newValue Value, lazyOldValue func() (Value, error)) error

func (*Multi[ReferenceKey, PrimaryKey, Value]) Unreference

func (m *Multi[ReferenceKey, PrimaryKey, Value]) Unreference(ctx context.Context, pk PrimaryKey, getValue func() (Value, error)) error

func (*Multi[ReferenceKey, PrimaryKey, Value]) Walk

func (m *Multi[ReferenceKey, PrimaryKey, Value]) Walk(
	ctx context.Context,
	ranger collections.Ranger[collections.Pair[ReferenceKey, PrimaryKey]],
	walkFunc func(indexingKey ReferenceKey, indexedKey PrimaryKey) (stop bool, err error),
) error

type MultiIterator

type MultiIterator[ReferenceKey, PrimaryKey any] collections.KeySetIterator[collections.Pair[ReferenceKey, PrimaryKey]]

MultiIterator is just a KeySetIterator with key as Pair[ReferenceKey, PrimaryKey].

func (MultiIterator[ReferenceKey, PrimaryKey]) Close

func (i MultiIterator[ReferenceKey, PrimaryKey]) Close() error

Close closes the iterator.

func (MultiIterator[ReferenceKey, PrimaryKey]) FullKey

func (i MultiIterator[ReferenceKey, PrimaryKey]) FullKey() (collections.Pair[ReferenceKey, PrimaryKey], error)

FullKey returns the current full reference key as Pair[ReferenceKey, PrimaryKey].

func (MultiIterator[ReferenceKey, PrimaryKey]) FullKeys

func (i MultiIterator[ReferenceKey, PrimaryKey]) FullKeys() ([]collections.Pair[ReferenceKey, PrimaryKey], error)

FullKeys fully consumes the iterator and returns all the list of full reference keys.

func (MultiIterator[ReferenceKey, PrimaryKey]) Next

func (i MultiIterator[ReferenceKey, PrimaryKey]) Next()

Next advances the iterator.

func (MultiIterator[ReferenceKey, PrimaryKey]) PrimaryKey

func (i MultiIterator[ReferenceKey, PrimaryKey]) PrimaryKey() (PrimaryKey, error)

PrimaryKey returns the iterator's current primary key.

func (MultiIterator[ReferenceKey, PrimaryKey]) PrimaryKeys

func (i MultiIterator[ReferenceKey, PrimaryKey]) PrimaryKeys() ([]PrimaryKey, error)

PrimaryKeys fully consumes the iterator and returns the list of primary keys.

func (MultiIterator[ReferenceKey, PrimaryKey]) Valid

func (i MultiIterator[ReferenceKey, PrimaryKey]) Valid() bool

Valid asserts if the iterator is still valid or not.

type ReversePair

type ReversePair[K1, K2, Value any] struct {
	// contains filtered or unexported fields
}

ReversePair is an index that is used with collections.Pair keys. It indexes objects by their second part of the key. When the value is being indexed by collections.IndexedMap then ReversePair will create a relationship between the second part of the primary key and the first part.

func NewReversePair

func NewReversePair[Value, K1, K2 any](
	sb *collections.SchemaBuilder,
	prefix collections.Prefix,
	name string,
	pairCodec codec.KeyCodec[collections.Pair[K1, K2]],
	options ...func(*reversePairOptions),
) *ReversePair[K1, K2, Value]

NewReversePair instantiates a new ReversePair index. NOTE: when using this function you will need to type hint: doing NewReversePair[Value]() Example: if the value of the indexed map is string, you need to do NewReversePair[string](...)

func (*ReversePair[K1, K2, Value]) Iterate

func (i *ReversePair[K1, K2, Value]) Iterate(ctx context.Context, ranger collections.Ranger[collections.Pair[K2, K1]]) (iter ReversePairIterator[K2, K1], err error)

Iterate exposes the raw iterator API.

func (*ReversePair[K1, K2, Value]) IterateRaw

func (i *ReversePair[K1, K2, Value]) IterateRaw(
	ctx context.Context, start, end []byte, order collections.Order,
) (
	iter collections.Iterator[collections.Pair[K2, K1], collections.NoValue], err error,
)

func (*ReversePair[K1, K2, Value]) KeyCodec

func (i *ReversePair[K1, K2, Value]) KeyCodec() codec.KeyCodec[collections.Pair[K2, K1]]

func (*ReversePair[K1, K2, Value]) MatchExact

func (i *ReversePair[K1, K2, Value]) MatchExact(ctx context.Context, key K2) (ReversePairIterator[K2, K1], error)

MatchExact will return an iterator containing only the primary keys starting with the provided second part of the multipart pair key.

func (*ReversePair[K1, K2, Value]) Reference

func (i *ReversePair[K1, K2, Value]) Reference(ctx context.Context, pk collections.Pair[K1, K2], _ Value, _ func() (Value, error)) error

Reference implements collections.Index

func (*ReversePair[K1, K2, Value]) Unreference

func (i *ReversePair[K1, K2, Value]) Unreference(ctx context.Context, pk collections.Pair[K1, K2], _ func() (Value, error)) error

Unreference implements collections.Index

func (*ReversePair[K1, K2, Value]) Walk

func (i *ReversePair[K1, K2, Value]) Walk(
	ctx context.Context,
	ranger collections.Ranger[collections.Pair[K2, K1]],
	walkFunc func(indexingKey K2, indexedKey K1) (stop bool, err error),
) error

type ReversePairIterator

type ReversePairIterator[K2, K1 any] collections.KeySetIterator[collections.Pair[K2, K1]]

ReversePairIterator is a helper type around a collections.KeySetIterator when used to work with ReversePair indexes iterations.

func (ReversePairIterator[K2, K1]) Close

func (m ReversePairIterator[K2, K1]) Close() error

func (ReversePairIterator[K2, K1]) FullKey

func (m ReversePairIterator[K2, K1]) FullKey() (p collections.Pair[K2, K1], err error)

func (ReversePairIterator[K2, K1]) Next

func (m ReversePairIterator[K2, K1]) Next()

func (ReversePairIterator[K2, K1]) PrimaryKey

func (m ReversePairIterator[K2, K1]) PrimaryKey() (pair collections.Pair[K1, K2], err error)

PrimaryKey returns the primary key from the index. The index is composed like a reverse pair key. So we just fetch the pair key from the index and return the reverse.

func (ReversePairIterator[K2, K1]) PrimaryKeys

func (m ReversePairIterator[K2, K1]) PrimaryKeys() (pairs []collections.Pair[K1, K2], err error)

PrimaryKeys returns all the primary keys contained in the iterator.

func (ReversePairIterator[K2, K1]) Valid

func (m ReversePairIterator[K2, K1]) Valid() bool

type Unique

type Unique[ReferenceKey, PrimaryKey, Value any] struct {
	// contains filtered or unexported fields
}

Unique identifies an index that imposes uniqueness constraints on the reference key. It creates relationships between reference and primary key of the value.

func NewUnique

func NewUnique[ReferenceKey, PrimaryKey, Value any](
	schema *collections.SchemaBuilder,
	prefix collections.Prefix,
	name string,
	refCodec codec.KeyCodec[ReferenceKey],
	pkCodec codec.KeyCodec[PrimaryKey],
	getRefKeyFunc func(pk PrimaryKey, v Value) (ReferenceKey, error),
) *Unique[ReferenceKey, PrimaryKey, Value]

NewUnique instantiates a new Unique index.

func (*Unique[ReferenceKey, PrimaryKey, Value]) Iterate

func (i *Unique[ReferenceKey, PrimaryKey, Value]) Iterate(ctx context.Context, ranger collections.Ranger[ReferenceKey]) (UniqueIterator[ReferenceKey, PrimaryKey], error)

func (*Unique[ReferenceKey, PrimaryKey, Value]) IterateRaw

func (i *Unique[ReferenceKey, PrimaryKey, Value]) IterateRaw(ctx context.Context, start, end []byte, order collections.Order) (u UniqueIterator[ReferenceKey, PrimaryKey], err error)

func (*Unique[ReferenceKey, PrimaryKey, Value]) MatchExact

func (i *Unique[ReferenceKey, PrimaryKey, Value]) MatchExact(ctx context.Context, ref ReferenceKey) (PrimaryKey, error)

func (*Unique[ReferenceKey, PrimaryKey, Value]) Reference

func (i *Unique[ReferenceKey, PrimaryKey, Value]) Reference(ctx context.Context, pk PrimaryKey, newValue Value, lazyOldValue func() (Value, error)) error

func (*Unique[ReferenceKey, PrimaryKey, Value]) Unreference

func (i *Unique[ReferenceKey, PrimaryKey, Value]) Unreference(ctx context.Context, pk PrimaryKey, getValue func() (Value, error)) error

func (*Unique[ReferenceKey, PrimaryKey, Value]) Walk

func (i *Unique[ReferenceKey, PrimaryKey, Value]) Walk(
	ctx context.Context,
	ranger collections.Ranger[ReferenceKey],
	walkFunc func(indexingKey ReferenceKey, indexedKey PrimaryKey) (stop bool, err error),
) error

type UniqueIterator

type UniqueIterator[ReferenceKey, PrimaryKey any] collections.Iterator[ReferenceKey, PrimaryKey]

UniqueIterator is an Iterator wrapper, that exposes only the functionality needed to work with Unique keys.

func (UniqueIterator[ReferenceKey, PrimaryKey]) Close

func (i UniqueIterator[ReferenceKey, PrimaryKey]) Close() error

func (UniqueIterator[ReferenceKey, PrimaryKey]) FullKey

func (i UniqueIterator[ReferenceKey, PrimaryKey]) FullKey() (collections.Pair[ReferenceKey, PrimaryKey], error)

FullKey returns the iterator's current full reference key as Pair[ReferenceKey, PrimaryKey].

func (UniqueIterator[ReferenceKey, PrimaryKey]) FullKeys

func (i UniqueIterator[ReferenceKey, PrimaryKey]) FullKeys() ([]collections.Pair[ReferenceKey, PrimaryKey], error)

func (UniqueIterator[ReferenceKey, PrimaryKey]) Next

func (i UniqueIterator[ReferenceKey, PrimaryKey]) Next()

func (UniqueIterator[ReferenceKey, PrimaryKey]) PrimaryKey

func (i UniqueIterator[ReferenceKey, PrimaryKey]) PrimaryKey() (PrimaryKey, error)

PrimaryKey returns the iterator's current primary key.

func (UniqueIterator[ReferenceKey, PrimaryKey]) PrimaryKeys

func (i UniqueIterator[ReferenceKey, PrimaryKey]) PrimaryKeys() ([]PrimaryKey, error)

PrimaryKeys fully consumes the iterator, and returns all the primary keys.

func (UniqueIterator[ReferenceKey, PrimaryKey]) Valid

func (i UniqueIterator[ReferenceKey, PrimaryKey]) Valid() bool

Jump to

Keyboard shortcuts

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