aerospike

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

README

Aerospike

Import path: github.com/InsideGallery/core/db/aerospike

This package provides Aerospike connection helpers, namespace-scoped record helpers, and small core-owned contracts for callers that should not depend on Aerospike SDK types at their own boundaries.

Main APIs

  • ConnectionConfig and GetConnectionConfigFromEnv read Aerospike connection settings.
  • NewConnection and NewConnectionFromEnv create *aerospike.Client values.
  • ConnectionRegistry stores named clients and can close all registered clients.
  • NamespaceStore is the core-owned record contract for PutRecord, GetRecord, and DeleteRecord.
  • NamespaceInstance implements NamespaceStore and also exposes legacy SDK-shaped namespace methods.
  • Key, PutOptions, GetOptions, DeleteOptions, Record, RecordResult, and Result are the core-owned request/result types.
  • NewValue and NewBin adapt common Go values to Aerospike values and bins.
  • HLLBin, MaxIndexBits, and MaxAllowedMinhashBits are shared HLL defaults.
  • ErrConnectionIsNotSet is returned when a named client is missing from a registry.

The legacy Aerospike and Namespace interfaces, package-level Set, Get, Default, and SDK-shaped NamespaceInstance methods remain for compatibility. Prefer NamespaceStore and the core-owned option/result types for new call sites.

Usage

package example

import (
	"context"

	"github.com/InsideGallery/core/db/aerospike"
)

func saveUser(ctx context.Context) error {
	namespace, err := aerospike.NewNamespaceInstance("app", aerospike.EnvPrefix)
	if err != nil {
		return err
	}

	_, err = namespace.PutRecord(ctx, aerospike.PutOptions{
		Key: aerospike.Key{Set: "users", Value: "42"},
		Bins: map[string]any{
			"name": "Ada",
		},
	})

	return err
}

For explicit client lifecycle management, create a client with NewConnection or NewConnectionFromEnv, or keep named clients in a ConnectionRegistry. ConnectionRegistry.Close closes registered clients and clears the registry.

Configuration

GetConnectionConfigFromEnv(prefix) uppercases the prefix and reads:

  • <PREFIX>_HOST, default 127.0.0.1
  • <PREFIX>_PORT, default 3000
  • <PREFIX>_HOSTS, optional host list used instead of host/port when non-empty
  • <PREFIX>_USERNAME and <PREFIX>_PASSWORD, optional internal-auth credentials
  • <PREFIX>_CONNECTION_QUEUE_SIZE, default 1000

EnvPrefix is AEROSPIKE, so the default variables are AEROSPIKE_HOST, AEROSPIKE_PORT, and related names. CreateClientPolicy enables internal auth only when both username and password are set.

Operational Notes

Setup(DefaultBufferArchitecture()) is called during package initialization to disable Aerospike buffer architecture flags for int64 compatibility. Tests cover that Setup is idempotent and ignores nil flag dependencies.

Documentation

Overview

Package aerospike provides Aerospike client and namespace helpers.

New code should depend on core-owned operation contracts where possible:

import "github.com/InsideGallery/core/db/aerospike"

Use NamespaceStore with Key, PutOptions, GetOptions, DeleteOptions, Record, RecordResult, and Result for record operations that should not expose Aerospike SDK types. Use NewConnection or ConnectionRegistry.Default when the consuming application owns client lifecycle explicitly.

Compatibility: the legacy Aerospike and Namespace interfaces, NamespaceInstance SDK-shaped methods, and package-level Default connection helper remain available for existing consumers.

Index

Constants

View Source
const (
	HLLBin = "hll"

	MaxIndexBits          = 13 // between 4 and 16
	MaxAllowedMinhashBits = 8  // between 4 and 51

)
View Source
const (
	EnvPrefix = "AEROSPIKE"
)

EnvPrefix environment prefix for mongodb config

Variables

View Source
var (
	ErrConnectionIsNotSet = errors.New("connection is not set")
)

All kind of errors for aerospike

Functions

func CreateClientPolicy

func CreateClientPolicy(cfg *ConnectionConfig) *aero.ClientPolicy

CreateClientPolicy creates policy for production

func Default deprecated

func Default(names ...string) (*aero.Client, error)

Default return default AerospikeInterface

Deprecated: use NewConnection or ConnectionRegistry.Default with explicit config.

func Get deprecated

func Get(name string) (*aero.Client, error)

Get return aerospike connection

Deprecated: use ConnectionRegistry.Get on an explicit registry.

func NewBin

func NewBin(name string, value interface{}) *aero.Bin

func NewConnection added in v1.1.0

func NewConnection(config *ConnectionConfig) (*aero.Client, error)

NewConnection creates an Aerospike client from explicit config.

func NewConnectionFromEnv added in v1.1.0

func NewConnectionFromEnv(prefix string) (*aero.Client, error)

NewConnectionFromEnv creates an Aerospike client from an explicit environment prefix.

func NewValue

func NewValue(value interface{}) aero.Value

func Set deprecated

func Set(name string, r *aero.Client)

Set set global connection

Deprecated: use ConnectionRegistry.Set on an explicit registry.

func Setup added in v1.1.0

func Setup(architecture BufferArchitecture)

Setup disables Aerospike buffer architecture flags for int64 compatibility.

Types

type Aerospike deprecated

type Aerospike interface {
	IsConnected() bool
	GetNodeNames() []string
	Put(*aerospike.WritePolicy, *aerospike.Key, aerospike.BinMap) aerospike.Error
	PutBins(*aerospike.WritePolicy, *aerospike.Key, ...*aerospike.Bin) aerospike.Error
	Query(*aerospike.QueryPolicy, *aerospike.Statement) (*aerospike.Recordset, aerospike.Error)

	CreateIndex(*aerospike.WritePolicy,
		string,
		string,
		string,
		string,
		aerospike.IndexType,
	) (*aerospike.IndexTask, aerospike.Error)

	DropIndex(
		policy *aerospike.WritePolicy,
		namespace string,
		setName string,
		indexName string,
	) aerospike.Error

	CreateComplexIndex(
		*aerospike.WritePolicy,
		string,
		string,
		string,
		string,
		aerospike.IndexType,
		aerospike.IndexCollectionType,
		...*aerospike.CDTContext,
	) (*aerospike.IndexTask, aerospike.Error)

	Stats() (map[string]interface{}, aerospike.Error)
	Get(*aerospike.BasePolicy, *aerospike.Key, ...string) (*aerospike.Record, aerospike.Error)
	Operate(*aerospike.WritePolicy, *aerospike.Key, ...*aerospike.Operation) (*aerospike.Record, aerospike.Error)
	Delete(
		policy *aerospike.WritePolicy,
		key *aerospike.Key,
	) (bool, aerospike.Error)
	BatchDelete(
		policy *aerospike.BatchPolicy,
		deletePolicy *aerospike.BatchDeletePolicy,
		keys []*aerospike.Key,
	) ([]*aerospike.BatchRecord, aerospike.Error)
	BatchGet(*aerospike.BatchPolicy, []*aerospike.Key, ...string) ([]*aerospike.Record, aerospike.Error)
	RegisterUDF(
		policy *aerospike.WritePolicy,
		udfBody []byte,
		serverPath string,
		language aerospike.Language,
	) (*aerospike.RegisterTask, aerospike.Error)
	RegisterUDFFromFile(
		policy *aerospike.WritePolicy,
		clientPath string,
		serverPath string,
		language aerospike.Language,
	) (*aerospike.RegisterTask, aerospike.Error)
	ExecuteUDF(
		policy *aerospike.QueryPolicy,
		statement *aerospike.Statement,
		packageName string,
		functionName string,
		functionArgs ...aerospike.Value,
	) (*aerospike.ExecuteTask, aerospike.Error)
	QueryAggregate(
		policy *aerospike.QueryPolicy,
		statement *aerospike.Statement,
		packageName, functionName string,
		functionArgs ...aerospike.Value,
	) (*aerospike.Recordset, aerospike.Error)
	Exists(policy *aerospike.BasePolicy, key *aerospike.Key) (bool, aerospike.Error)

	Truncate(policy *aerospike.InfoPolicy, namespace, set string, beforeLastUpdate *time.Time) aerospike.Error
	BatchOperate(policy *aerospike.BatchPolicy, records []aerospike.BatchRecordIfc) aerospike.Error
}

Aerospike is the legacy Aerospike SDK-shaped client contract.

Deprecated: use NamespaceStore and core-owned option/result types for new code.

type BufferArchitecture added in v1.1.0

type BufferArchitecture struct {
	Arch64Bits *bool
	Arch32Bits *bool
}

BufferArchitecture exposes Aerospike buffer architecture flags as explicit dependencies.

func DefaultBufferArchitecture added in v1.1.0

func DefaultBufferArchitecture() BufferArchitecture

DefaultBufferArchitecture returns the Aerospike package buffer architecture flags.

type ConnectionConfig

type ConnectionConfig struct {
	Host                string   `env:"_HOST" envDefault:"127.0.0.1"`
	Username            string   `env:"_USERNAME" envDefault:""`
	Password            string   `env:"_PASSWORD" envDefault:""`
	Hosts               []string `env:"_HOSTS" envDefault:""`
	Port                int      `env:"_PORT" envDefault:"3000"`
	ConnectionQueueSize int      `env:"_CONNECTION_QUEUE_SIZE" envDefault:"1000"`
}

ConnectionConfig contains required data for mongo

func GetConnectionConfigFromEnv

func GetConnectionConfigFromEnv(prefix string) (*ConnectionConfig, error)

GetConnectionConfigFromEnv return aerospike configs bases on environment variables

type ConnectionRegistry added in v1.1.0

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

ConnectionRegistry owns Aerospike clients for explicit application composition.

func NewConnectionRegistry added in v1.1.0

func NewConnectionRegistry() *ConnectionRegistry

NewConnectionRegistry creates an isolated Aerospike connection registry.

func (*ConnectionRegistry) Close added in v1.1.0

func (r *ConnectionRegistry) Close() error

Close closes all clients stored in this registry and clears it.

func (*ConnectionRegistry) Default added in v1.1.0

func (r *ConnectionRegistry) Default(name string, config *ConnectionConfig) (*aero.Client, error)

Default returns or creates a registry-scoped Aerospike client from explicit config.

func (*ConnectionRegistry) Get added in v1.1.0

func (r *ConnectionRegistry) Get(name string) (*aero.Client, error)

Get returns an Aerospike client from this registry.

func (*ConnectionRegistry) Set added in v1.1.0

func (r *ConnectionRegistry) Set(name string, client *aero.Client)

Set stores an Aerospike client in this registry.

type DeleteOptions added in v1.1.0

type DeleteOptions struct {
	Key Key
}

DeleteOptions is the core-owned input for deleting a record.

type GetOptions added in v1.1.0

type GetOptions struct {
	Key      Key
	BinNames []string
}

GetOptions is the core-owned input for reading a record.

type Key added in v1.1.0

type Key struct {
	Set   string
	Value any
}

Key is a core-owned Aerospike record identity.

type Mapper

type Mapper interface {
	ToMap() map[string]interface{}
}

type Namespace deprecated

type Namespace interface {
	Put(*aerospike.WritePolicy, string, interface{}, aerospike.BinMap) aerospike.Error
	PutBins(policy *aerospike.WritePolicy, setName string, value interface{}, bins ...*aerospike.Bin) aerospike.Error
	Query(*aerospike.QueryPolicy, *aerospike.Statement) (*aerospike.Recordset, aerospike.Error)

	CreateIndex(
		policy *aerospike.WritePolicy,
		set string,
		indexName string,
		fieldName string,
		indexType aerospike.IndexType,
	) (*aerospike.IndexTask, aerospike.Error)

	DropIndex(
		policy *aerospike.WritePolicy,
		setName string,
		indexName string,
	) aerospike.Error

	CreateComplexIndex(
		policy *aerospike.WritePolicy,
		set string,
		indexName string,
		field string,
		indexType aerospike.IndexType,
		collectionType aerospike.IndexCollectionType,
		ctx ...*aerospike.CDTContext,
	) (*aerospike.IndexTask, aerospike.Error)

	Stats() (map[string]interface{}, aerospike.Error)
	Get(
		policy *aerospike.BasePolicy,
		setName string,
		value interface{},
		binNames ...string,
	) (*aerospike.Record, aerospike.Error)
	Operate(
		policy *aerospike.WritePolicy,
		setName string,
		value interface{},
		ops ...*aerospike.Operation,
	) (*aerospike.Record, aerospike.Error)
	Delete(
		policy *aerospike.WritePolicy,
		setName string,
		value interface{},
	) (bool, aerospike.Error)
	BatchDelete(
		policy *aerospike.BatchPolicy,
		deletePolicy *aerospike.BatchDeletePolicy,
		values []SetKey,
	) ([]*aerospike.BatchRecord, aerospike.Error)
	BatchGet(*aerospike.BatchPolicy, []SetKey, ...string) ([]*aerospike.Record, aerospike.Error)
	QueryAggregate(
		policy *aerospike.QueryPolicy,
		statement *aerospike.Statement,
		packageName, functionName string,
		functionArgs ...aerospike.Value,
	) (*aerospike.Recordset, aerospike.Error)
	Exists(*aerospike.BasePolicy, string, interface{}) (bool, aerospike.Error)

	Truncate(policy *aerospike.InfoPolicy, set string, beforeLastUpdate *time.Time) aerospike.Error
	GetConnection() Aerospike
	GetNamespace() string
	BatchOperate(policy *aerospike.BatchPolicy, records []aerospike.BatchRecordIfc) aerospike.Error
}

Namespace is the legacy namespace contract that exposes Aerospike SDK types.

Deprecated: use NamespaceStore and core-owned option/result types for new code.

type NamespaceInstance

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

func NewNamespaceInstance

func NewNamespaceInstance(namespace string, names ...string) (*NamespaceInstance, error)

func (*NamespaceInstance) BatchDelete

func (ni *NamespaceInstance) BatchDelete(
	policy *aero.BatchPolicy,
	deletePolicy *aero.BatchDeletePolicy,
	values []SetKey,
) ([]*aero.BatchRecord, aero.Error)

func (*NamespaceInstance) BatchGet

func (ni *NamespaceInstance) BatchGet(
	policy *aero.BatchPolicy,
	values []SetKey,
	names ...string,
) ([]*aero.Record, aero.Error)

func (*NamespaceInstance) BatchOperate

func (ni *NamespaceInstance) BatchOperate(policy *aero.BatchPolicy, records []aero.BatchRecordIfc) aero.Error

func (*NamespaceInstance) CreateComplexIndex

func (ni *NamespaceInstance) CreateComplexIndex(
	policy *aero.WritePolicy,
	setName string,
	indexName string,
	binName string,
	indexType aero.IndexType,
	indexCollectionType aero.IndexCollectionType,
	ctx ...*aero.CDTContext,
) (*aero.IndexTask, aero.Error)

func (*NamespaceInstance) CreateIndex

func (ni *NamespaceInstance) CreateIndex(
	policy *aero.WritePolicy,
	setName string,
	indexName string,
	binName string,
	indexType aero.IndexType,
) (*aero.IndexTask, aero.Error)

func (*NamespaceInstance) Delete

func (ni *NamespaceInstance) Delete(policy *aero.WritePolicy, setName string, value interface{}) (bool, aero.Error)

func (*NamespaceInstance) DeleteRecord added in v1.1.0

func (ni *NamespaceInstance) DeleteRecord(ctx context.Context, options DeleteOptions) (Result, error)

DeleteRecord deletes a record through a core-owned input and result shape.

func (*NamespaceInstance) DropIndex

func (ni *NamespaceInstance) DropIndex(
	policy *aero.WritePolicy,
	setName string,
	indexName string,
) aero.Error

func (*NamespaceInstance) Exists

func (ni *NamespaceInstance) Exists(policy *aero.BasePolicy, setName string, value interface{}) (bool, aero.Error)

func (*NamespaceInstance) Get

func (ni *NamespaceInstance) Get(
	policy *aero.BasePolicy,
	setName string,
	value interface{},
	binNames ...string,
) (*aero.Record, aero.Error)

func (*NamespaceInstance) GetConnection

func (ni *NamespaceInstance) GetConnection() *aero.Client

func (*NamespaceInstance) GetNamespace

func (ni *NamespaceInstance) GetNamespace() string

func (*NamespaceInstance) GetRecord added in v1.1.0

func (ni *NamespaceInstance) GetRecord(ctx context.Context, options GetOptions) (RecordResult, error)

GetRecord reads a record through a core-owned input and result shape.

func (*NamespaceInstance) Operate

func (ni *NamespaceInstance) Operate(
	policy *aero.WritePolicy,
	setName string,
	value interface{},
	ops ...*aero.Operation,
) (*aero.Record, aero.Error)

func (*NamespaceInstance) Put

func (ni *NamespaceInstance) Put(
	policy *aero.WritePolicy,
	setName string,
	value interface{},
	bins aero.BinMap,
) aero.Error

func (*NamespaceInstance) PutBins

func (ni *NamespaceInstance) PutBins(
	policy *aero.WritePolicy,
	setName string,
	value interface{},
	bins ...*aero.Bin,
) aero.Error

func (*NamespaceInstance) PutRecord added in v1.1.0

func (ni *NamespaceInstance) PutRecord(ctx context.Context, options PutOptions) (Result, error)

PutRecord writes a record through a core-owned input and result shape.

func (*NamespaceInstance) Query

func (ni *NamespaceInstance) Query(policy *aero.QueryPolicy, stmt *aero.Statement) (*aero.Recordset, aero.Error)

func (*NamespaceInstance) QueryAggregate

func (ni *NamespaceInstance) QueryAggregate(
	policy *aero.QueryPolicy,
	statement *aero.Statement,
	packageName, functionName string,
	functionArgs ...aero.Value,
) (*aero.Recordset, aero.Error)

func (*NamespaceInstance) Stats

func (ni *NamespaceInstance) Stats() (map[string]interface{}, aero.Error)

func (*NamespaceInstance) Truncate

func (ni *NamespaceInstance) Truncate(policy *aero.InfoPolicy, set string, beforeLastUpdate *time.Time) aero.Error

type NamespaceStore added in v1.1.0

type NamespaceStore interface {
	PutRecord(ctx context.Context, options PutOptions) (Result, error)
	GetRecord(ctx context.Context, options GetOptions) (RecordResult, error)
	DeleteRecord(ctx context.Context, options DeleteOptions) (Result, error)
}

NamespaceStore is the core-owned namespace contract for new consumers.

type PutOptions added in v1.1.0

type PutOptions struct {
	Key  Key
	Bins map[string]any
}

PutOptions is the core-owned input for writing a record.

type Record added in v1.1.0

type Record struct {
	Key        Key
	Bins       map[string]any
	Generation uint32
	Expiration uint32
}

Record is the core-owned Aerospike record result.

type RecordResult added in v1.1.0

type RecordResult struct {
	Found  bool
	Record Record
}

RecordResult reports a record lookup result.

type Result added in v1.1.0

type Result struct {
	Affected int64
	Deleted  bool
}

Result reports a write/delete operation result.

type SetKey

type SetKey struct {
	Key interface{}
	Set string
}

Directories

Path Synopsis
mocks
Package mock_entity is a generated GoMock package.
Package mock_entity is a generated GoMock package.
Package mock_aerospike is a generated GoMock package.
Package mock_aerospike is a generated GoMock package.

Jump to

Keyboard shortcuts

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