Documentation
¶
Overview ¶
Package core provides the fundamental data structures for XDB, a tuple-based database abstraction.
XDB Data Model:
XDB models data as a tree of Namespaces, Schemas, Records, and Tuples:
┌─────────────────────────────────┐
│ Namespace │
└────────────────┬────────────────┘
↓
┌─────────────────────────────────┐
│ Schema │
└────────────────┬────────────────┘
↓
┌─────────────────────────────────┐
│ Record │
└────────────────┬────────────────┘
↓
┌─────────────────────────────────┐
│ Tuple │
├─────────────────────────────────┤
│ ID | Attr | Value | Options │
└─────────────────────────────────┘
Core Types:
Tuple is the fundamental building block in XDB. Each tuple contains:
- Path: A URI identifying the record (NS + SCHEMA + ID)
- Attr: An attribute name (e.g., "name", "profile.email")
- Value: A typed value containing the actual data
Record is a group of tuples sharing the same path. Records are similar to objects, structs, or rows in a database. Records typically represent a single entity or object of domain data.
Schema defines the structure of records and groups them together. Schemas can be "strict" or "flexible" and are uniquely identified by name within a namespace.
Namespace (NS) groups one or more Schemas. Namespaces are typically used to organize schemas by domain, application, or tenant.
Schema is a definition of your domain entities and their relationships. Schemas can be "strict" or "flexible". Strict schemas enforce a predefined structure on the data, while flexible schemas allow for arbitrary data.
URI provides unique references to namespaces, schemas, records, and attributes. The general format is:
xdb:// NS [ / SCHEMA ] [ / ID ] [ #ATTRIBUTE ]
Examples:
Namespace: xdb://com.example Schema: xdb://com.example/posts Record: xdb://com.example/posts/123-456-789 Attribute: xdb://com.example/posts/123-456-789#author.id
NS identifies the namespace. SCHEMA is the schema name. ID is the record identifier ATTRIBUTE is a specific attribute of a record (supports nesting like "profile.email"). Path: NS, SCHEMA, and ID combined uniquely identify a record (URI without xdb://).
Value is a typed container supporting Go's basic types plus arrays and maps. Values provide type-safe casting methods and automatic type inference.
Example usage:
// Create tuples using the builder pattern
title := New().
NS("com.example").
Schema("posts").
ID("123-456-789").
MustTuple("title", "Hello World")
author := New().
NS("com.example").
Schema("posts").
ID("123-456-789").
MustTuple("author.id", "user-001")
// Get tuple URI
uri := title.URI() // xdb://com.example/posts/123-456-789#title
// Create records with multiple tuples
record := NewRecord("com.example", "posts", "123-456-789").
Set("title", "Hello World").
Set("author", "user-001")
Index ¶
- Variables
- type Attr
- type Builder
- func (b *Builder) Attr(attr string) *Builder
- func (b *Builder) ID(id string) *Builder
- func (b *Builder) MustRecord() *Record
- func (b *Builder) MustTuple(attr string, value any) *Tuple
- func (b *Builder) MustURI() *URI
- func (b *Builder) NS(ns string) *Builder
- func (b *Builder) Record() (*Record, error)
- func (b *Builder) Schema(schema string) *Builder
- func (b *Builder) Tuple(attr string, value any) (*Tuple, error)
- func (b *Builder) URI() (*URI, error)
- type ID
- type NS
- type Record
- func (r *Record) Get(attr string) *Tuple
- func (r *Record) GoString() string
- func (r *Record) ID() *ID
- func (r *Record) IsEmpty() bool
- func (r *Record) NS() *NS
- func (r *Record) Schema() *Schema
- func (r *Record) SchemaURI() *URI
- func (r *Record) Set(attr string, value any) *Record
- func (r *Record) Tuples() []*Tuple
- func (r *Record) URI() *URI
- type Schema
- type TID
- type Tuple
- func (t *Tuple) Attr() *Attr
- func (t *Tuple) GoString() string
- func (t *Tuple) ID() *ID
- func (t *Tuple) NS() *NS
- func (t *Tuple) Path() *URI
- func (t *Tuple) Schema() *Schema
- func (t *Tuple) SchemaURI() *URI
- func (t *Tuple) ToBool() bool
- func (t *Tuple) ToBoolArray() []bool
- func (t *Tuple) ToBytes() []byte
- func (t *Tuple) ToBytesArray() [][]byte
- func (t *Tuple) ToFloat() float64
- func (t *Tuple) ToFloatArray() []float64
- func (t *Tuple) ToInt() int64
- func (t *Tuple) ToIntArray() []int64
- func (t *Tuple) ToString() string
- func (t *Tuple) ToStringArray() []string
- func (t *Tuple) ToTime() time.Time
- func (t *Tuple) ToTimeArray() []time.Time
- func (t *Tuple) ToUint() uint64
- func (t *Tuple) ToUintArray() []uint64
- func (t *Tuple) URI() *URI
- func (t *Tuple) Value() *Value
- type Type
- type URI
- type Value
- func (v *Value) GoString() string
- func (v *Value) IsNil() bool
- func (v *Value) String() string
- func (v *Value) ToBool() bool
- func (v *Value) ToBoolArray() []bool
- func (v *Value) ToBytes() []byte
- func (v *Value) ToBytesArray() [][]byte
- func (v *Value) ToFloat() float64
- func (v *Value) ToFloatArray() []float64
- func (v *Value) ToInt() int64
- func (v *Value) ToIntArray() []int64
- func (v *Value) ToString() string
- func (v *Value) ToStringArray() []string
- func (v *Value) ToTime() time.Time
- func (v *Value) ToTimeArray() []time.Time
- func (v *Value) ToUint() uint64
- func (v *Value) ToUintArray() []uint64
- func (v *Value) Type() Type
- func (v *Value) Unwrap() any
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( TypeUnknown = newType(TIDUnknown) TypeBool = newType(TIDBoolean) TypeInt = newType(TIDInteger) TypeUnsigned = newType(TIDUnsigned) TypeFloat = newType(TIDFloat) TypeString = newType(TIDString) TypeBytes = newType(TIDBytes) TypeTime = newType(TIDTime) )
var ( // ErrUnsupportedValue is returned when a value is not supported. ErrUnsupportedValue = errors.New("[xdb/core] unsupported value") // ErrTypeMismatch is returned when a value is not of the expected type. ErrTypeMismatch = errors.New("[xdb/core] type mismatch") )
var ErrCastFailed = errors.New("[xdb/core] cast failed")
ErrCastFailed is returned when a value cannot be converted to the desired type.
var ErrInvalidAttr = errors.New("[xdb/core] invalid Attr")
ErrInvalidAttr is returned when an invalid Attr is encountered.
var ErrInvalidID = errors.New("[xdb/core] invalid ID")
ErrInvalidID is returned when an invalid ID is encountered.
var ErrInvalidNS = errors.New("[xdb/core] invalid NS")
ErrInvalidNS is returned when an invalid NS is encountered.
var ErrInvalidSchema = errors.New("[xdb/core] invalid Schema")
ErrInvalidSchema is returned when an invalid Schema is encountered.
var ErrInvalidURI = errors.New("[xdb/core] invalid URI")
ErrInvalidURI is returned when an invalid URI is encountered.
var ErrUnknownType = errors.New("[xdb/core] unknown type")
ErrUnknownType is returned when an unknown type is encountered.
Functions ¶
This section is empty.
Types ¶
type Attr ¶
type Attr struct {
// contains filtered or unexported fields
}
Attr is a name of an attribute.
func NewAttr ¶
NewAttr creates a new attribute name. Panics if the attribute name is invalid (contains characters outside a-zA-Z0-9._/-).
func ParseAttr ¶
ParseAttr parses a string into an Attr. Returns ErrInvalidAttr if the Attr is invalid.
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a fluent API for constructing URIs, Records, and Tuples. Use New() to create a new Builder, then chain methods to set components.
func New ¶
func New() *Builder
New creates a new URI Builder. The builder starts empty and each component must be set via chaining methods.
func (*Builder) MustRecord ¶
MustRecord is like Record but panics if any component is invalid.
func (*Builder) Record ¶
Record builds and returns a Record from the configured components. Returns an error if any component is invalid.
type ID ¶
type ID struct {
// contains filtered or unexported fields
}
ID is an unique identifier for a record.
func NewID ¶
NewID creates a new ID from a string. Panics if the ID is invalid (contains characters outside a-zA-Z0-9._/-).
func ParseID ¶
ParseID parses a string into an ID. Returns ErrInvalidID if the ID is invalid.
type NS ¶
type NS struct {
// contains filtered or unexported fields
}
NS identifies the namespace.
func NewNS ¶
NewNS creates a new namespace identifier. Panics if the namespace is invalid (contains characters outside a-zA-Z0-9._/-).
func ParseNS ¶
ParseNS parses a string into an NS. Returns ErrInvalidNS if the NS is invalid.
type Record ¶
type Record struct {
// contains filtered or unexported fields
}
Record is a group of tuples that share the same path (NS + SCHEMA + ID). Records are mutable and thread-safe, similar to database rows.
Example ¶
package main
import (
"fmt"
"github.com/xdb-dev/xdb/core"
)
func main() {
record := core.New().NS("com.example").
Schema("users").
ID("123").
MustRecord()
record.Set("name", "John Doe").
Set("age", 25).
Set("bio.interests", []string{"reading", "traveling", "coding"})
// Reading attributes
fmt.Println(record.Get("name").ToString())
fmt.Println(record.Get("age").ToInt())
fmt.Println(record.Get("bio.interests").ToStringArray())
fmt.Println(record.URI())
}
Output: John Doe 25 [reading traveling coding] xdb://com.example/users/123
func (*Record) Get ¶
Get retrieves the tuple for the given attribute path. Returns nil if no tuple exists for the specified attribute.
func (*Record) Set ¶
Set adds or updates a tuple in the Record with the given attribute and value. If a tuple with the same attribute already exists, it will be replaced.
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
Schema is a name of a schema.
func NewSchema ¶
NewSchema creates a new Schema. Panics if the Schema name is invalid (contains characters outside a-zA-Z0-9._/-).
func ParseSchema ¶
ParseSchema parses a string into a Schema. Returns ErrInvalidSchema if the Schema is invalid.
type TID ¶
type TID int
TID represents the type of a value.
type Tuple ¶
type Tuple struct {
// contains filtered or unexported fields
}
Tuple is the core data structure of XDB.
Tuple is an immutable data structure containing: - Path: Unique identifier for the record. - Attr: Name of the attribute. - Value: Value of the attribute.
Example ¶
package main
import (
"fmt"
"github.com/xdb-dev/xdb/core"
)
func main() {
tuple := core.New().NS("com.example").
Schema("users").
ID("123").
MustTuple("name", "John Doe")
fmt.Println(tuple.NS())
fmt.Println(tuple.Schema())
fmt.Println(tuple.ID())
fmt.Println(tuple.Attr())
fmt.Println(tuple.Value().ToString())
fmt.Println(tuple.URI())
}
Output: com.example users 123 name John Doe xdb://com.example/users/123#name
func (*Tuple) ToBoolArray ¶
ToBoolArray returns the tuple's value as a []bool.
func (*Tuple) ToBytesArray ¶
ToBytesArray returns the tuple's value as a [][]byte.
func (*Tuple) ToFloatArray ¶
ToFloatArray returns the tuple's value as a []float64.
func (*Tuple) ToIntArray ¶
ToIntArray returns the tuple's value as a []int64.
func (*Tuple) ToStringArray ¶
ToStringArray returns the tuple's value as a []string.
func (*Tuple) ToTimeArray ¶
ToTimeArray returns the tuple's value as a []time.Time.
func (*Tuple) ToUintArray ¶
ToUintArray returns the tuple's value as a []uint64.
type Type ¶
type Type struct {
// contains filtered or unexported fields
}
Type represents a value type in XDB, including scalar, array, and map types.
func NewArrayType ¶
NewArrayType returns a new array Type with the given value TID.
func NewMapType ¶
NewMapType returns a new map Type with the given key and value TIDs.
func (Type) ValueTypeID ¶
ValueTypeID returns the value TID for array and map types.
type URI ¶
type URI struct {
// contains filtered or unexported fields
}
URI is a reference to XDB data.
The general format is:
xdb:// NS [ / SCHEMA ] [ / ID ] [ #ATTRIBUTE ]
NS identifies the namespace. SCHEMA is the schema name. ID is the record identifier. ATTRIBUTE is a specific attribute of a record.
func MustParseURI ¶
MustParseURI is like ParseURI but panics if the URI is invalid.
func ParsePath ¶
ParsePath parses a path string into a URI struct. The path format is: NS[/SCHEMA][/ID][#ATTRIBUTE].
func ParseURI ¶
ParseURI parses a URI string into a URI struct. The URI format is: xdb://NS[/SCHEMA][/ID][#ATTRIBUTE]
func (*URI) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*URI) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value represents an attribute value using a tagged union. A zero Value is considered a nil value.
func NewSafeValue ¶
NewSafeValue creates a new value. Returns an error if the value is not supported.
func (*Value) ToBoolArray ¶
ToBoolArray returns the value as a []bool.
func (*Value) ToBytesArray ¶
ToBytesArray returns the value as a [][]byte.
func (*Value) ToFloatArray ¶
ToFloatArray returns the value as a []float64.
func (*Value) ToIntArray ¶
ToIntArray returns the value as a []int64.
func (*Value) ToStringArray ¶
ToStringArray returns the value as a []string.
func (*Value) ToTimeArray ¶
ToTimeArray returns the value as a []time.Time.
func (*Value) ToUintArray ¶
ToUintArray returns the value as a []uint64.