keyspace

package
v0.0.0-...-29d77a8 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package keyspace provides a logical directory tree abstraction over FDB.

KeySpace defines a schema of named directories with typed keys. KeySpacePath navigates the tree to produce FDB-ready tuples and subspaces.

Matches Java's com.apple.foundationdb.record.provider.foundationdb.keyspace package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Directory

type Directory struct {
	Name     string
	KeyType  KeyType
	Value    any          // constant value, or nil for any-value
	Resolver ResolverFunc // optional resolver for value transformation
	// contains filtered or unexported fields
}

Directory is a node in the KeySpace tree. Each directory has a name, a key type, optional constant value, and optional children.

Matches Java's KeySpaceDirectory.

func NewConstantDirectory

func NewConstantDirectory(name string, keyType KeyType, value any) *Directory

NewConstantDirectory creates a directory node with a fixed constant value.

func NewDirectory

func NewDirectory(name string, keyType KeyType) *Directory

NewDirectory creates a new directory node that accepts any value of the given type.

func ResolverDirectory

func ResolverDirectory(name string, resolver LocatableResolver) *Directory

ResolverDirectory creates a Directory node whose values are resolved through a LocatableResolver — strings in, int64 values stored in FDB.

Input must be a string (the logical name). Output stored in FDB is int64. Matches Java's DirectoryLayerDirectory.isValueValid which rejects non-strings.

The declared KeyType is LONG because that's how the value is stored, but input validation accepts only strings (matching Java).

func (*Directory) AddSubdirectories

func (d *Directory) AddSubdirectories(children ...*Directory) *Directory

AddSubdirectories adds multiple child directories in one call. Returns the parent for chaining. Panics on duplicate names.

func (*Directory) AddSubdirectory

func (d *Directory) AddSubdirectory(child *Directory) *Directory

AddSubdirectory adds a child directory. Returns the parent for chaining. Panics if a child with the same name already exists.

func (*Directory) Depth

func (d *Directory) Depth() int

Depth returns the distance from this directory to the root (0 for root). Matches Java's KeySpaceDirectory.depth().

func (*Directory) FindChildForValue

func (d *Directory) FindChildForValue(value any) *Directory

FindChildForValue returns the child directory that accepts the given value, or nil if no child matches. Constant directories are checked first (matches Java's findChildForValue which prioritizes exact constant matches over open-type matches). Used for reverse resolution.

func (*Directory) GetSubdirectories

func (d *Directory) GetSubdirectories() []*Directory

GetSubdirectories returns all child directories.

func (*Directory) GetSubdirectory

func (d *Directory) GetSubdirectory(name string) *Directory

GetSubdirectory returns a child directory by name, or nil if not found.

func (*Directory) IsConstant

func (d *Directory) IsConstant() bool

IsConstant returns true if this directory has a fixed value.

func (*Directory) IsLeaf

func (d *Directory) IsLeaf() bool

IsLeaf returns true if this directory has no children. Matches Java's KeySpaceDirectory.isLeaf().

func (*Directory) NameInTree

func (d *Directory) NameInTree() string

NameInTree returns the dot-separated path from root to this directory. Matches Java's KeySpaceDirectory.getNameInTree().

func (*Directory) Parent

func (d *Directory) Parent() *Directory

Parent returns the parent directory, or nil if this is the root. Matches Java's KeySpaceDirectory.getParent().

func (*Directory) ToPathString

func (d *Directory) ToPathString() string

ToPathString returns a slash-separated path from root to this directory. Matches Java's KeySpaceDirectory.toPathString().

func (*Directory) ToTree

func (d *Directory) ToTree() string

ToTree returns an ASCII tree representation of this directory and its subtree. Matches Java's KeySpaceDirectory.toTree().

type FDBResolver

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

FDBResolver is a persistent LocatableResolver backed by an FDB subspace. It stores name→int64 mappings directly (no DirectoryLayer complexity), providing a simpler alternative to Java's ScopedDirectoryLayer.

Storage layout under the given subspace:

subspace.Pack({"n", name}) → int64 value (big-endian)
subspace.Pack({"r", value}) → name (reverse lookup)
subspace.Pack({"c"})       → int64 counter (next value to allocate)

An in-memory cache fronts the FDB reads for hot keys. The cache is transactional-aware: newly allocated mappings are only cached after commit.

func NewFDBResolver

func NewFDBResolver(db fdb.Database, ss subspace.Subspace) *FDBResolver

NewFDBResolver creates a persistent resolver under the given subspace. The database is used for transactions; the subspace holds the mappings.

func (*FDBResolver) CacheSize

func (r *FDBResolver) CacheSize() int

CacheSize returns the number of mappings currently cached in memory.

func (*FDBResolver) InvalidateCache

func (r *FDBResolver) InvalidateCache()

InvalidateCache clears the in-memory cache. Useful for testing or when you know the resolver mappings have been externally modified.

func (*FDBResolver) Resolve

func (r *FDBResolver) Resolve(ctx context.Context, name string) (int64, error)

Resolve returns the value for name, allocating and persisting a new one if absent. Safe for concurrent use — FDB transactions handle cross-process contention.

ctx is accepted for LocatableResolver interface compliance but is NOT forwarded to the FDB transaction — db.Transact uses the database's internal context. Use NewFDBResolver with a database configured for the desired deadline.

func (*FDBResolver) ReverseLookup

func (r *FDBResolver) ReverseLookup(ctx context.Context, value int64) (string, bool, error)

ReverseLookup returns the name for a value, reading from FDB if not cached.

ctx is accepted for LocatableResolver interface compliance but is NOT forwarded to the FDB transaction — db.ReadTransact uses the database's internal context.

type KeySpace

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

KeySpace is the root of a directory tree. It holds one or more root directories.

Matches Java's KeySpace.

func NewKeySpace

func NewKeySpace(root *Directory) *KeySpace

NewKeySpace creates a new key space with the given root directory.

func (*KeySpace) Path

func (ks *KeySpace) Path(name string, value any) (*Path, error)

Path starts navigating the key space from a named root subdirectory with a value.

func (*KeySpace) PathFromTuple

func (ks *KeySpace) PathFromTuple(t tuple.Tuple) (*Path, tuple.Tuple, error)

PathFromTuple resolves a tuple back to a path by matching values against the directory tree. Returns the deepest matching path and any remaining tuple elements that didn't match a directory.

Matches Java's KeySpace.resolveFromKey / KeySpaceDirectory.pathFromKey.

func (*KeySpace) Root

func (ks *KeySpace) Root() *Directory

Root returns the root directory.

func (*KeySpace) String

func (ks *KeySpace) String() string

String returns a pretty-printed tree representation of the whole KeySpace schema.

func (*KeySpace) Validate

func (ks *KeySpace) Validate() error

Validate checks the tree for structural errors: - Constant values must match their declared key type - No nil children

type KeyType

type KeyType int

KeyType defines the FDB tuple type for a directory's key. Matches Java's KeySpaceDirectory.KeyType enum.

const (
	KeyTypeNull    KeyType = iota // nil values
	KeyTypeBytes                  // []byte
	KeyTypeString                 // string
	KeyTypeLong                   // int64 (also accepts int, int32)
	KeyTypeFloat                  // float32
	KeyTypeDouble                 // float64
	KeyTypeBoolean                // bool
	KeyTypeUUID                   // tuple.UUID
)

func (KeyType) String

func (t KeyType) String() string

String returns the name of the key type.

func (KeyType) ValidateValue

func (t KeyType) ValidateValue(v any) error

ValidateValue checks if a value is compatible with this key type.

type LocatableResolver

type LocatableResolver interface {
	// Resolve maps a name to a compact int64. Creates the mapping if absent.
	Resolve(ctx context.Context, name string) (int64, error)

	// ReverseLookup maps a value back to its name. Returns ("", false, nil)
	// if the value is not in the resolver.
	ReverseLookup(ctx context.Context, value int64) (string, bool, error)
}

LocatableResolver maps string names to compact int64 values and back. Used by DirectoryLayerDirectory for string→long key compression.

Phase 2: this interface defines the contract. Phase 3 will add ScopedDirectoryLayer that uses FDB's directory layer for persistent resolution with metadata.

Matches Java's LocatableResolver (abstract class).

type MemoryResolver

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

MemoryResolver is an in-memory LocatableResolver for testing. Not persistent — data is lost on restart. Safe for concurrent use.

func NewMemoryResolver

func NewMemoryResolver(startValue int64) *MemoryResolver

NewMemoryResolver creates an in-memory resolver starting at startValue.

func (*MemoryResolver) Resolve

func (r *MemoryResolver) Resolve(_ context.Context, name string) (int64, error)

Resolve returns the value for name, allocating a new one if needed.

func (*MemoryResolver) ReverseLookup

func (r *MemoryResolver) ReverseLookup(_ context.Context, value int64) (string, bool, error)

ReverseLookup returns the name for a value, or false if not found.

func (*MemoryResolver) Size

func (r *MemoryResolver) Size() int

Size returns the number of mappings stored.

type Path

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

Path represents a position in the key space tree with a resolved value.

Matches Java's KeySpacePath.

func (*Path) Add

func (p *Path) Add(name string, value any) (*Path, error)

Add navigates to a child directory with the given value.

func (*Path) Depth

func (p *Path) Depth() int

Depth returns the number of path elements from root to this position.

func (*Path) Directory

func (p *Path) Directory() *Directory

Directory returns the directory schema at this path position. Matches Java's KeySpacePath.getDirectory().

func (*Path) DirectoryName

func (p *Path) DirectoryName() string

DirectoryName returns the name of the current directory.

func (*Path) Equal

func (p *Path) Equal(other *Path) bool

Equal reports whether two paths reference the same directories with the same values at each level. Directories are compared by pointer identity (schema reuse) and values via recordTypeKeyEquals (reflect.DeepEqual + int type normalization, safe for []byte and other non-comparable types).

func (*Path) Flatten

func (p *Path) Flatten() []*Path

Flatten returns all path elements from root to this position, in order. Matches Java's KeySpacePath.flatten().

func (*Path) GetValue

func (p *Path) GetValue() any

Value returns the resolved value at this path position.

func (*Path) HasSubdirectory

func (p *Path) HasSubdirectory(name string) bool

HasSubdirectory returns true if a child directory with the given name exists.

func (*Path) IsSameDirectory

func (p *Path) IsSameDirectory(other *Path) bool

IsSameDirectory returns true if two paths reference the same directory in the schema tree (ignoring values).

func (*Path) ListSubdirectories

func (p *Path) ListSubdirectories() []string

ListSubdirectories returns the names of available child directories at this position.

func (*Path) Parent

func (p *Path) Parent() *Path

Parent returns the parent path, or nil if this is a root path.

func (*Path) String

func (p *Path) String() string

FullPath returns a human-readable representation of the path from root to here. E.g., "/state=CA/office_id=1234".

func (*Path) ToRange

func (p *Path) ToRange() (fdb.KeyRange, error)

ToRange returns an FDB key range that covers all entries under this path. Useful for scanning or clearing all data in a directory subtree.

func (*Path) ToSubspace

func (p *Path) ToSubspace() subspace.Subspace

ToSubspace converts this path to an FDB subspace.

func (*Path) ToTuple

func (p *Path) ToTuple() tuple.Tuple

ToTuple converts this path to an FDB tuple containing all values from root to here.

type ResolverFunc

type ResolverFunc func(value any) (any, error)

ResolverFunc resolves a directory value before storing it in the path. For example, a DirectoryLayerDirectory would resolve a string name to a compact int64 via FDB's directory layer.

The function receives the raw value and returns the resolved value. Phase 2 (LocatableResolver) will plug into this hook.

Jump to

Keyboard shortcuts

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