store

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package store is RAMen's in-memory keyspace. Keys are spread across a fixed number of shards, each guarded by its own RWMutex, so unrelated keys rarely contend for the same lock. Expiry is handled both lazily (on access) and by a background sweep (see expiry.go). The PRD calls for a "sharded in-process map" and to "revisit only if benchmarks show real lock contention" (§9).

Index

Constants

This section is empty.

Variables

View Source
var ErrNotInteger = errors.New("ERR value is not an integer or out of range")

ErrNotInteger is returned when a string value cannot be parsed as an int64 for INCR/DECR-style operations.

View Source
var ErrWrongType = errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")

ErrWrongType mirrors Redis' WRONGTYPE condition: an operation was attempted against a key holding a different data type.

Functions

This section is empty.

Types

type Record

type Record struct {
	Key            string
	Type           string // "string","hash","list","set","zset","vector"
	ExpireAtUnixMs int64  // 0 == no expiry
	Str            string
	Hash           map[string]string
	List           []string
	Set            []string
	ZSet           []ZMember
	Vectors        []VecRecord
	VecDim         int
}

Record is the serialisable form of a single key, used by the persist package to snapshot and restore the keyspace. Exactly one of the type-specific fields is populated according to Type.

type SetOptions

type SetOptions struct {
	TTL   time.Duration // 0 means no expiry
	HasEx bool          // an EX/PX flag was supplied
	NX    bool          // only set if the key does not exist
	XX    bool          // only set if the key already exists
}

SetOptions controls SET behaviour (EX/PX/NX/XX flags).

type Store

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

Store is the full keyspace, safe for concurrent use.

func New

func New() *Store

New returns an empty store.

func (*Store) Append

func (s *Store) Append(key, val string) (int, error)

Append concatenates val to the string at key (creating it if absent) and returns the new length.

func (*Store) DBSize

func (s *Store) DBSize() int

DBSize returns the number of live keys across all shards.

func (*Store) Del

func (s *Store) Del(keys ...string) int

Del removes the given keys, returning the number actually deleted.

func (*Store) Exists

func (s *Store) Exists(keys ...string) int

Exists reports how many of the given keys currently exist.

func (*Store) Expire

func (s *Store) Expire(key string, ttl time.Duration) bool

Expire sets a relative TTL in milliseconds on an existing key. It reports whether the key existed.

func (*Store) Export

func (s *Store) Export() []Record

Export returns a snapshot of every live key. It is safe to call while the store is serving traffic; each shard is read-locked in turn.

func (*Store) Flush

func (s *Store) Flush()

Flush removes every key.

func (*Store) Get

func (s *Store) Get(key string) (val string, ok bool, err error)

Get returns the string at key. ok is false if the key is missing.

func (*Store) GetRange

func (s *Store) GetRange(key string, start, end int64) (string, error)

GetRange returns the substring between start and end (inclusive), with Redis-style negative offsets and clamping. A missing key yields an empty string.

func (*Store) GetSet

func (s *Store) GetSet(key, val string) (old string, hadOld bool, err error)

GetSet sets key to val and returns the previous string value.

func (*Store) HDel

func (s *Store) HDel(key string, fields ...string) (int, error)

HDel removes fields and returns how many were removed. The key is dropped when its last field is deleted.

func (*Store) HGet

func (s *Store) HGet(key, field string) (string, bool, error)

HGet returns the value of a single field.

func (*Store) HGetAll

func (s *Store) HGetAll(key string) ([]string, error)

HGetAll returns all fields and values as a flat [f1,v1,f2,v2,...] slice.

func (*Store) HKeys

func (s *Store) HKeys(key string) ([]string, error)

HKeys returns the field names of the hash.

func (*Store) HLen

func (s *Store) HLen(key string) (int, error)

HLen returns the number of fields in the hash.

func (*Store) HSet

func (s *Store) HSet(key string, pairs map[string]string) (int, error)

HSet sets the given field/value pairs on the hash at key (creating it if absent) and returns the number of newly created fields.

func (*Store) HVals

func (s *Store) HVals(key string) ([]string, error)

HVals returns the values of the hash.

func (*Store) Import

func (s *Store) Import(recs []Record)

Import loads records into the store, replacing any existing data. Records whose expiry is already in the past are skipped.

func (*Store) IncrBy

func (s *Store) IncrBy(key string, delta int64) (int64, error)

IncrBy adds delta to the integer string at key (treating a missing key as 0) and returns the new value.

func (*Store) Keys

func (s *Store) Keys(pattern string) []string

Keys returns every live key matching the glob-style pattern.

func (*Store) LIndex

func (s *Store) LIndex(key string, idx int) (string, bool, error)

LIndex returns the element at index (negative counts from the tail).

func (*Store) LLen

func (s *Store) LLen(key string) (int, error)

LLen returns the list length.

func (*Store) LPop

func (s *Store) LPop(key string) (string, bool, error)

LPop removes and returns the head element.

func (*Store) LPush

func (s *Store) LPush(key string, values ...string) (int, error)

LPush prepends values to the list at key.

func (*Store) LRange

func (s *Store) LRange(key string, start, stop int) ([]string, error)

LRange returns the elements in the inclusive index range [start, stop], where negative indices count from the tail (Redis semantics).

func (*Store) Persist

func (s *Store) Persist(key string) bool

Persist removes any TTL from key, returning whether a TTL was removed.

func (*Store) RPop

func (s *Store) RPop(key string) (string, bool, error)

RPop removes and returns the tail element.

func (*Store) RPush

func (s *Store) RPush(key string, values ...string) (int, error)

RPush appends values to the list at key.

func (*Store) SAdd

func (s *Store) SAdd(key string, members ...string) (int, error)

SAdd adds members to the set at key (creating it if absent) and returns how many were newly added.

func (*Store) SCard

func (s *Store) SCard(key string) (int, error)

SCard returns the set cardinality.

func (*Store) SIsMember

func (s *Store) SIsMember(key, member string) (bool, error)

SIsMember reports whether member is in the set.

func (*Store) SMembers

func (s *Store) SMembers(key string) ([]string, error)

SMembers returns all members of the set.

func (*Store) SRem

func (s *Store) SRem(key string, members ...string) (int, error)

SRem removes members and returns how many were removed; the key is dropped when emptied.

func (*Store) Set

func (s *Store) Set(key, val string, opts SetOptions) bool

Set assigns a string value to key, honouring the supplied options. It reports whether the write happened (NX/XX can suppress it).

func (*Store) SetRange

func (s *Store) SetRange(key string, offset int, val string) (int, error)

SetRange overwrites from offset with val, zero-padding past the current end. It returns the length of the string after the write.

func (*Store) StartSweeper

func (s *Store) StartSweeper(ctx context.Context, interval time.Duration)

StartSweeper runs a background goroutine that periodically scans shards and deletes expired keys. Lazy expiry already removes keys on access; the sweep reclaims memory for keys that are never read again. It stops when ctx is cancelled.

func (*Store) TTL

func (s *Store) TTL(key string) (d time.Duration, hasTTL, ok bool)

TTL returns the remaining time to live for key. ok is false when the key does not exist; hasTTL is false when the key exists but is persistent.

func (*Store) Type

func (s *Store) Type(key string) string

Type returns the Redis type name of key ("string", "hash", "list", "set", "zset", "vector") or "none" if it does not exist.

func (*Store) VCard

func (s *Store) VCard(key string) (int, error)

VCard returns the number of vectors in the collection at key.

func (*Store) VDel

func (s *Store) VDel(key, id string) (bool, error)

VDel removes a vector id from the collection at key.

func (*Store) VDim

func (s *Store) VDim(key string) (int, error)

VDim returns the dimension of the collection at key (0 if empty/missing).

func (*Store) VSearch

func (s *Store) VSearch(key string, query []float32, k int) ([]vector.Result, error)

VSearch returns the top-k nearest vectors in the collection at key.

func (*Store) VSet

func (s *Store) VSet(key, id string, vec []float32, meta string) error

VSet stores a vector under id within the collection at key.

func (*Store) ZAdd

func (s *Store) ZAdd(key string, members []ZMember) (int, error)

ZAdd sets the score for each member (creating the set if absent) and returns the number of newly added members.

func (*Store) ZCard

func (s *Store) ZCard(key string) (int, error)

ZCard returns the number of members.

func (*Store) ZRange

func (s *Store) ZRange(key string, start, stop int) ([]ZMember, error)

ZRange returns members in the inclusive rank range [start, stop] ordered by score (negative indices count from the end).

func (*Store) ZRangeByScore

func (s *Store) ZRangeByScore(key string, min, max float64) ([]ZMember, error)

ZRangeByScore returns members whose score lies in [min, max] inclusive, ordered by score.

func (*Store) ZRem

func (s *Store) ZRem(key string, members ...string) (int, error)

ZRem removes members and returns how many were removed.

func (*Store) ZScore

func (s *Store) ZScore(key, member string) (float64, bool, error)

ZScore returns the score of member.

type VecRecord

type VecRecord struct {
	ID   string
	Vec  []float32
	Meta string
}

VecRecord is one stored vector inside a vector collection.

type ZMember

type ZMember struct {
	Member string
	Score  float64
}

ZMember pairs a member with its score.

Jump to

Keyboard shortcuts

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