Documentation ¶
Index ¶
- Constants
- Variables
- func WithLogHandler(handler slog.Handler) cfg.Option[Config]
- func WithLogger(logger *slog.Logger) cfg.Option[Config]
- func WithMetrics(metrics Metrics) cfg.Option[Config]
- func WithTrace(tracer trace.Tracer) cfg.Option[Config]
- func WithURI(uri string) cfg.Option[Config]
- type Attribute
- type Char
- type Config
- type Index
- func (i *Index[K, V]) Delete(ctx context.Context, keys ...K) error
- func (i *Index[K, V]) Insert(ctx context.Context, attrs ...Attribute[K, V]) error
- func (i *Index[K, V]) Search(ctx context.Context, searchTerm V) (res []Attribute[K, V], err error)
- func (i *Index[K, V]) Shutdown(_ context.Context) error
- type Indexer
- func IndexerWithLogs[K SQLType, V SQLType](indexer Indexer[K, V], handler slog.Handler) Indexer[K, V]
- func IndexerWithMetrics[K SQLType, V SQLType](indexer Indexer[K, V], m Metrics) Indexer[K, V]
- func IndexerWithTrace[K SQLType, V SQLType](indexer Indexer[K, V], tracer trace.Tracer) Indexer[K, V]
- func New[K SQLType, V SQLType](attributes []Attribute[K, V], opts ...cfg.Option[Config]) (Indexer[K, V], error)
- func NoOp[K SQLType, V SQLType]() Indexer[K, V]
- type Metrics
- type Number
- type SQLNullable
- type SQLType
Constants ¶
Variables ¶
var ( ErrZeroAttributes = errs.WithDomain(errDomain, ErrZero, ErrAttributes) ErrNotFoundKeyword = errs.WithDomain(errDomain, ErrNotFound, ErrKeyword) )
Functions ¶
func WithLogHandler ¶
WithLogHandler decorates the Indexer with a slog.Logger, using the input slog.Handler.
func WithLogger ¶
WithLogger decorates the Indexer with the input slog.Logger.
func WithMetrics ¶
WithMetrics decorates the Index with the input Metrics instance.
Types ¶
type Attribute ¶
Attribute describes an entry to be added or returned from the Index, supporting types that are compatible with the SQLite FTS feature and implementation.
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config defines optional settings in an Indexer
type Index ¶
Index exposes fast full-text search by leveraging the SQLite FTS5 feature.
ref: https://www.sqlite.org/fts5.html
This implementation, using the modernc.org's pure-Go SQLite driver, allows having a very broadly-typed and yet lightweight full-text search implementation, with optional persistence.
Effectively, the Index uses a SQLite database as a cache, where it is storing (indexed) data as key-value pairs, allowing callers to find these key-value pairs by using keywords and search expressions against this data set.
The expressions, syntax and example phrases for these queries can be found in section 3. of the reference document above; providing means of performing more complex queries over indexed data.
func NewIndex ¶
NewIndex creates an Index using the provided URI and set of Attribute.
If the provided URI is an empty string or ":memory:", the SQLite implementation will comply and run in-memory. Otherwise, the URI is treated as a database URI and validated as an OS path. The latter option allows persistence of the Index.
An error is returned if the database fails when being open, initialized, and loaded with the input Attribute.
func (*Index[K, V]) Delete ¶
Delete removes attributes in the Index, which match input K-type keys.
A database transaction is performed in order to ensure that the query is executed as quickly as possible; in case multiple items are provided as input.
func (*Index[K, V]) Insert ¶
Insert indexes new attributes in the Index, via the input Attribute's key and value content.
A database transaction is performed in order to ensure that the query is executed as quickly as possible; in case multiple items are provided as input. This is especially useful for the initial load sequence.
func (*Index[K, V]) Search ¶
Search will look for matches for the input value through the indexed terms, returning a collection of matching Attribute, which will contain both key and (full) value for that match.
This call returns an error if the underlying SQL query fails, if scanning for the results fails, or an ErrNotFoundKeyword error if there are zero results from the query.
type Indexer ¶
type Indexer[K SQLType, V SQLType] interface { // Search will look for matches for the input value through the indexed terms, returning a collection of matching // Attribute, which will contain both key and (full) value for that match. // // This call returns an error if the underlying SQL query fails, if scanning for the results fails, or an // ErrNotFoundKeyword error if there are zero results from the query. Search(ctx context.Context, searchTerm V) (res []Attribute[K, V], err error) // Insert indexes new attributes in the Indexer, via the input Attribute's key and value content. // // A database transaction is performed in order to ensure that the query is executed as quickly as possible; in case // multiple items are provided as input. This is especially useful for the initial load sequence. Insert(ctx context.Context, attrs ...Attribute[K, V]) error // Delete removes attributes in the Indexer, which match input K-type keys. // // A database transaction is performed in order to ensure that the query is executed as quickly as possible; in case // multiple items are provided as input. Delete(ctx context.Context, keys ...K) error // Shutdown gracefully closes the Indexer. Shutdown(ctx context.Context) error }
Indexer describes the actions that a full-text search index should expose. It is declared as an interface so that a no-op implementation and observability decorators can be used interchangeably through a single constructor.
An Indexer exposes full-text by registering (and tokenizing) key-value pairs of data, that can be looked-up for matches with keywords that would be found in the value part of the data. The queries return sets of matching key-value pairs.
The Indexer allows creating, reading and deleting entries from the index. This ensures that the index can perform an initial load on its own; be updated with more recent data; and also pruning certain keys if needed.
Finally, it also exposes a Shutdown method allowing a graceful shutdown of the search engine.
The underlying index in an Indexer created by this package is an Index type, which leverages the SQLite FTS5 feature allowing a fast full-text search engine out-of-the-box, either in-memory or persisted to a file.
func IndexerWithLogs ¶
func IndexerWithLogs[K SQLType, V SQLType](indexer Indexer[K, V], handler slog.Handler) Indexer[K, V]
IndexerWithLogs decorates the input Indexer with a slog.Logger using the input slog.Handler.
If the Indexer is nil, a no-op Indexer is returned. If the input slog.Handler is nil, a default text handler is created as a safe default. If the input Indexer is already a logged Indexer; then its logger's handler is replaced with this handler (input or default one).
This Indexer will not add any new functionality besides decorating the Indexer with log events.
func IndexerWithMetrics ¶
IndexerWithMetrics decorates the input Indexer with a Metrics interface.
If the Indexer is nil, a no-op Indexer is returned. If the input Metrics is nil, a default Prometheus metrics handler is created as a safe default, on port 8080. If the input Indexer is already an Indexer with Metrics; then its Metrics is replaced with this one (input or default one).
This Indexer will not add any new functionality besides decorating the Indexer with metrics registry.
func IndexerWithTrace ¶
func IndexerWithTrace[K SQLType, V SQLType](indexer Indexer[K, V], tracer trace.Tracer) Indexer[K, V]
IndexerWithTrace decorates the input Indexer with a trace.Tracer interface.
If the Indexer is nil, a no-op Indexer is returned. If the input Metrics is nil, a default Prometheus metrics handler is created as a safe default. If the input Indexer is already an Indexer with Metrics; then its Metrics is replaced with this one (input or default one).
This Indexer will not add any new functionality besides decorating the Indexer with metrics registry.
func New ¶
func New[K SQLType, V SQLType](attributes []Attribute[K, V], opts ...cfg.Option[Config]) (Indexer[K, V], error)
New creates an Indexer with the input Attribute and configuration options.
This function allows creating an Index that is intended to be decorated with a logger, metrics and / or tracing.
type Metrics ¶
type Metrics interface { IncSearchesTotal() IncSearchesFailed() ObserveSearchLatency(ctx context.Context, dur time.Duration) IncInsertsTotal() IncInsertsFailed() ObserveInsertLatency(ctx context.Context, dur time.Duration) IncDeletesTotal() IncDeletesFailed() ObserveDeleteLatency(ctx context.Context, dur time.Duration) }
type Number ¶
type Number interface { int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 }
Number is a type constraint that comprises all types that are integer or real numbers.
type SQLNullable ¶
type SQLNullable interface { sql.NullBool | sql.NullInt16 | sql.NullInt32 | sql.NullInt64 | sql.NullFloat64 | sql.NullString }
SQLNullable is a type constraint that comprises all supported sql.Null* types, in a full-text search context.
type SQLType ¶
type SQLType interface { Number | Char | SQLNullable }
SQLType is a type constraint that joins the Number, Char and SQLNullable type constraints.