Documentation
¶
Index ¶
- func Stop() error
- type AppConfig
- type Config
- type CtrlConfig
- type DB
- func (db *DB) Close() error
- func (db *DB) CountByIndex(indexName string, kr x.KeyRange, filter x.Filter) mo.Result[int]
- func (db *DB) CountByKey(kr x.KeyRange, filter x.Filter) mo.Result[int]
- func (db *DB) Delete(key string) (bool, error)
- func (db *DB) EffectiveAuthKeys() (app_0, ctrl_0 string)
- func (db *DB) Get(key string) mo.Result[string]
- func (db *DB) Keys(keyPattern string) mo.Result[[]string]
- func (db *DB) SearchIndex(indexName string, kr x.KeyRange, filter x.Filter, desc bool) mo.Result[[]string]
- func (db *DB) SearchKey(kr x.KeyRange, filter x.Filter, desc bool) mo.Result[[]string]
- func (db *DB) Set(key string, value string) error
- func (db *DB) SetNX(key string, value string) (bool, error)
- func (db *DB) SetNXWithTtl(key string, value string, ttl time.Duration) (bool, error)
- func (db *DB) SetWithTtl(key string, value string, ttl time.Duration) error
- func (db *DB) Update(kr x.KeyRange, filter x.Filter, values ...x.Mutation) mo.Result[[]string]
- type LoggingConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AppConfig ¶ added in v0.1.15
type Config ¶ added in v0.1.15
type Config struct {
App AppConfig `yaml:"app"`
Ctrl CtrlConfig `yaml:"ctrl"`
DataPath string `yaml:"data_path"`
Logging LoggingConfig `yaml:"logging"`
}
func LoadConfig ¶ added in v0.1.15
type CtrlConfig ¶ added in v0.1.15
type CtrlConfig struct {
Bind string `yaml:"bind"`
Port int `yaml:"port"`
Auth string `yaml:"auth"`
TrustProxy bool `yaml:"trust_proxy"`
DangerBindAny bool `yaml:"danger_bind_any"`
}
func (CtrlConfig) Addr ¶ added in v0.1.15
func (c CtrlConfig) Addr() string
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a lightweight two-layer BuntDB wrapper (disk + memory). Memory layer keys are routed via the "_m_:" storageNs prefix. All typed-doc registry logic (docRegSpec / idxRegSpec maps) live in-process alongside storage so registry lookups are zero I/O.
func Start ¶
Start boots a redisx dual-port server using the redisx.yaml configuration file located in the current working directory, and eagerly registers any passed doc schemas before listeners accept traffic.
Call site idiom (zero-value string alias of your Document types):
db := server.Start(
UserDoc(""),
OrderDoc(""),
)
Schemas are registered exactly once per process lifetime, and written to the "_doc_:<storage_ns>" SSoT keys on the matching storage layer (disk or in-memory, according to Schema.Mem). Conflicting namespace registrations cause a hard process exit with a descriptive error.
When redisx.yaml is missing or unreadable (except YAML syntax errors) the zero-config defaults kick in: App 7379 on auto-selected RFC1918 bind, Ctrl 7381 on 127.0.0.1, and database file at ~/.redisx/redisx.db.
To supply a Config struct directly (e.g. from tests or harnesses) instead of loading a file, use StartWith.
func StartWith ¶ added in v0.1.17
StartWith boots a redisx dual-port server from a Go-native Config struct and eagerly registers any passed doc schemas before listeners accept traffic. It is the single Go-API counterpart to Start (which loads redisx.yaml from disk).
Config is always run through Config.validate: defaults are populated, security gates (equal auth / non-loopback ctrl / port range / duplicate ports) trigger a hard exit, and the database path is created and probed for writability before any listeners open. Passing nil is equivalent to the empty Config{} (pure system defaults).
AUTH bootstrap flow — SSoT lives ONLY inside `_auth_:app` + `_auth_:ctrl` two KV entries on the disk layer of the DB itself. There is NO per-process duplicate state: every AUTH gate round-trips to the storage layer via a buntdb View tx (BuntDB is mmap-backed; read cost is O(log n) in memory):
- cfg.App.Auth / cfg.Ctrl.Auth EMPTY (zero-value default) → BootstrapAuth(seedApp="", seedCtrl="") is called. · Both slots MISSING → generate 2× crypto/rand 128-bit hex pair, write them atomically inside a single BuntDB Update tx using SETNX-style presence-check. firstBoot=true ONLY when our write actually went through; if another caller won the race, reload winner's values and continue silently. Banner is printed ONCE. · Both slots PRESENT → silent load, zero logs. · Exactly ONE slot present → hard FATAL "_auth_ namespace corrupted" requiring operator intervention (delete partial key or wipe db file).
- cfg.App.Auth / cfg.Ctrl.Auth set explicitly (yaml or StartWith) → BootstrapAuth's seed parameters ALWAYS win. Non-empty seeds are written unconditionally to their respective storage slots, with a slog.Info when an existing DB value is being overwritten. If the resolved pair would end up equal after merge → hard error.
On boot, redisx mounts indexes exclusively from the schemas passed in: every schema declaring indexes via x.RegisterSchema(x.IndexOn(...)) is re-created as an in-process BuntDB btree index. Indexes are NEVER persisted — there is no index meta key and no reload path; what the code declares here (plus any runtime SCHEMA push, see client.RegisterSchema) is the single source of truth.
Example (harness-style direct embed):
cfg := &server.Config{
App: server.AppConfig{Bind: "127.0.0.1", Port: 7379},
Ctrl: server.CtrlConfig{Bind: "127.0.0.1", Port: 7381, DangerBindAny: true},
DataPath: "/tmp/redisx.test.db",
}
db := server.StartWith(cfg, UserDoc(""))
_ = db
defer server.Stop()
func (*DB) CountByIndex ¶ added in v0.1.24
CountByIndex returns the number of records that SearchIndex would return for the same inputs.
func (*DB) CountByKey ¶ added in v0.1.24
CountByKey returns the number of records that SearchKey would return for the same inputs.
func (*DB) Delete ¶
Delete removes key from its routed layer. Returns true if the key existed. RESP equivalent: DEL single-key form. Multi-key DEL, doc-path DEL ns <pk1> [pk2…] and the "multi-KV DEL not allowed" guard are all enforced above this layer in cmd.go delCommand.
func (*DB) EffectiveAuthKeys ¶ added in v0.1.15
EffectiveAuthKeys returns the two effective port-role-bound auth keys that are persisted on disk under `_auth_:app_0` and `_auth_:ctrl_0`. These are the keys printed on the bootstrap banner whenever any slot was generated. Empty strings are returned for slots that have not been written yet (should never happen after a successful StartWith bootstrap). SSoT = direct DB read from `_auth_` namespace, no in-memory cache.
func (*DB) Keys ¶
Keys returns storage keys matching a single-layer pinned pattern. Pattern cannot start with wildcard (resolveLayer enforces single layer). The cmd.go keysCommand 4-branch colon-routing logic sits above this call: ':' in pattern → KV-glob straight through; no ':' + bare registered ns → auto-scoped `<ns>:*`; no ':' + wildcard → ERR. RESP: KEYS.
func (*DB) SearchIndex ¶
func (db *DB) SearchIndex(indexName string, kr x.KeyRange, filter x.Filter, desc bool) mo.Result[[]string]
SearchIndex scans via a buntdb-native secondary index handle. The index name must already be mounted in-process (declared in code via x.RegisterSchema(x.IndexOn(...)) and mounted at boot); an unregistered name returns Err "index not found". Key- range layer and index ownerNs layer must match (cross-layer searches are parameter-ERR). Matched rows are optionally narrowed by filter.Eval; the SEARCHINDEX command also respects colon-routing (cmd.go searchIndexCommand — index name is always logical, KV-passthrough route is SEARCHKEY only), per the typed-doc-SSOT for registry-bound queries.
func (*DB) SearchKey ¶
SearchKey walks the raw storage-key order (no secondary index) for a key- range; used both for typed-doc queries (colon-routing doc-path, KeyRange anchor resolves to storageNs) AND for generic KV-pattern passthrough (colon- routing KV-path, caller supplies a wildcard pattern on keys that contain ':'). The cmd.go searchKeyCommand already handled the 4-branch KEYS-style colon-routing before this method is invoked; this method does NOT consult the typed-doc registry for KV-pattern forms.
func (*DB) Set ¶
Set stores value under key (unconditional overwrite). "_m_:" prefix routes to the in-memory layer. RESP equivalent: SET.
func (*DB) SetNX ¶
SetNX sets key→value only when key does NOT already exist (Redis SETNX). Returns true when the write actually happened.
func (*DB) SetNXWithTtl ¶ added in v0.1.3
SetNXWithTtl is SetNX with a positive TTL applied to newly-created keys.
func (*DB) SetWithTtl ¶
SetWithTtl sets key→value with optional positive TTL (zero/negative = no TTL, same as Set). autoTTLFromKey injects the doc TTL when the caller comes from a typed-doc SET/SETEX/SETNX handler. RESP: SET EX / SETEX.
func (*DB) Update ¶
Update is TYPED-DOC ONLY (there is no KV-style UPDATE bypass per the colon- routing SSOT; the cmd.go UPDATE handler already resolved the keyrange anchor to a single storageNs, verified it is registered (non-internal) and rejected KV-shaped bare patterns). Caller provides the key-range + filter that pins to exactly one storage layer; mutations are sjson path writes executed inside a single BuntDB Update tx so either all matched rows change or none do. Primary keys derived from KeyAttrs are re-derived post-mutation; if any pk would drift the whole tx is aborted with zero writes.