plus

package
v0.9.5 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ModuloHex

type ModuloHex struct {
	Bits int
}

ModuloHex shards tables by hex remainder of the shard key. Table name format: base_XX where XX is lowercase hex (2 digits). Bits=4 → 16 tables (_00.._0f), Bits=6 → 64 (_00.._3f), Bits=8 → 256 (_00.._ff).

func (ModuloHex) MatchPattern

func (m ModuloHex) MatchPattern(base string) string

func (ModuloHex) ResolveTableName

func (m ModuloHex) ResolveTableName(base string, shardValue any) string

type NestedSet

type NestedSet[T any] struct {
	// contains filtered or unexported fields
}

NestedSet provides Nested Set model operations for tree-structured data. The model T must have Lft and Rgt fields (int type) for left/right values.

func NewNestedSet

func NewNestedSet[T any](tbl *goent.Table[T], lftField, rgtField string) *NestedSet[T]

NewNestedSet creates a new NestedSet instance for the given table. lftField and rgtField are the column names for left/right values.

func (*NestedSet[T]) BuildTree

func (ns *NestedSet[T]) BuildTree(records []*T) *TreeNode[T]

BuildTree builds a tree structure from flat records sorted by Lft value.

func (*NestedSet[T]) DeleteSubtree

func (ns *NestedSet[T]) DeleteSubtree(node *T) error

DeleteSubtree removes a node and all its descendants.

func (*NestedSet[T]) FlattenTree

func (ns *NestedSet[T]) FlattenTree(root *TreeNode[T], start int) ([]*T, int)

FlattenTree converts a tree back to flat records with updated Lft/Rgt values.

func (*NestedSet[T]) GetAncestors

func (ns *NestedSet[T]) GetAncestors(node *T) ([]*T, error)

GetAncestors returns all ancestor nodes from root to parent.

func (*NestedSet[T]) GetChildren

func (ns *NestedSet[T]) GetChildren(node *T, depth int) ([]*T, error)

GetChildren returns descendant nodes within specified depth. depth=0 returns self, depth>0 limits levels, depth<0 returns all descendants.

func (*NestedSet[T]) GetDescendants

func (ns *NestedSet[T]) GetDescendants(node *T) ([]*T, error)

GetDescendants returns all descendant nodes (not including self).

func (*NestedSet[T]) GetLevel

func (ns *NestedSet[T]) GetLevel(node *T) (int, error)

GetLevel returns the depth/level of a node in the tree (root = 1).

func (*NestedSet[T]) GetParent

func (ns *NestedSet[T]) GetParent(node *T) (*T, error)

GetParent returns the direct parent node of the given node.

func (*NestedSet[T]) GetParents

func (ns *NestedSet[T]) GetParents(node *T) ([]*T, error)

GetParents returns all ancestor nodes from root to direct parent.

func (*NestedSet[T]) InsertAsFirstChild

func (ns *NestedSet[T]) InsertAsFirstChild(parent *T, child *T) error

InsertAsFirstChild inserts a new node as the first child of parent.

func (*NestedSet[T]) InsertAsLastChild

func (ns *NestedSet[T]) InsertAsLastChild(parent *T, child *T) error

InsertAsLastChild inserts a new node as the last child of parent.

func (*NestedSet[T]) InsertAsNextSibling

func (ns *NestedSet[T]) InsertAsNextSibling(sibling *T, node *T) error

InsertAsNextSibling inserts a new node as the next sibling of sibling.

func (*NestedSet[T]) InsertAsPrevSibling

func (ns *NestedSet[T]) InsertAsPrevSibling(sibling *T, node *T) error

InsertAsPrevSibling inserts a new node as the previous sibling of sibling.

func (*NestedSet[T]) IsDescendant

func (ns *NestedSet[T]) IsDescendant(ancestor, potentialChild *T) bool

IsDescendant checks if potentialChild is a descendant of ancestor.

func (*NestedSet[T]) IsLeafNode

func (ns *NestedSet[T]) IsLeafNode(node *T) bool

IsLeafNode checks if a node has no children.

func (*NestedSet[T]) PrintTree

func (ns *NestedSet[T]) PrintTree(root *TreeNode[T]) string

PrintTree prints tree structure with indentation for debugging.

type PageResult

type PageResult[T any] struct {
	Data  []*T
	Total int64
	Page  int
	Size  int
}

PageResult holds paginated query results across sharded tables.

type ShardedTable

type ShardedTable[T any] struct {
	// contains filtered or unexported fields
}

ShardedTable provides automatic table routing based on a shard key. It manages multiple sub-tables lazily and routes operations to the correct one.

func NewShardedTable

func NewShardedTable[T any](db *goent.DB, baseName, shardKey string,
	strategy ShardingStrategy, cache *TableCache) *ShardedTable[T]

NewShardedTable creates a new ShardedTable instance.

func (*ShardedTable[T]) BaseName

func (s *ShardedTable[T]) BaseName() string

BaseName returns the base table name (without suffix).

func (*ShardedTable[T]) Cache

func (s *ShardedTable[T]) Cache() *TableCache

Cache returns the table metadata cache.

func (*ShardedTable[T]) CountAll

func (s *ShardedTable[T]) CountAll() (int64, error)

CountAll counts total rows across all matched sub-tables.

func (*ShardedTable[T]) DeleteByPK

func (s *ShardedTable[T]) DeleteByPK(shardValue any) error

DeleteByPK deletes a row by primary key from the sub-table for the given shard value.

func (*ShardedTable[T]) EachTable

func (s *ShardedTable[T]) EachTable(fn func(*goent.Table[T]) error) error

EachTable iterates over all matched sub-tables and calls fn for each.

func (*ShardedTable[T]) FindByPK

func (s *ShardedTable[T]) FindByPK(shardValue any, pk any) (*T, error)

FindByPK finds a row by primary key in the sub-table for the given shard value.

func (*ShardedTable[T]) FindOne

func (s *ShardedTable[T]) FindOne(shardValue any, filters ...goent.Condition) (*T, error)

FindOne finds a single row matching filters in the sub-table for the given shard value.

func (*ShardedTable[T]) Insert

func (s *ShardedTable[T]) Insert(row *T) error

Insert inserts a row into the correct sub-table determined by the shard key value.

func (*ShardedTable[T]) RefreshCache

func (s *ShardedTable[T]) RefreshCache(ctx context.Context) error

RefreshCache refreshes the table metadata cache from the database.

func (*ShardedTable[T]) ResolveTable

func (s *ShardedTable[T]) ResolveTable(shardValue any) *goent.Table[T]

ResolveTable resolves and returns the sub-table for the given shard value.

func (*ShardedTable[T]) SelectAll

func (s *ShardedTable[T]) SelectAll() ([]*T, error)

SelectAll queries all matched sub-tables and returns combined results.

func (*ShardedTable[T]) SelectPage

func (s *ShardedTable[T]) SelectPage(page, size int) (*PageResult[T], error)

SelectPage performs paginated query across all matched sub-tables. It collects results from each table, sorts by ID, then applies pagination.

func (*ShardedTable[T]) Strategy

func (s *ShardedTable[T]) Strategy() ShardingStrategy

Strategy returns the sharding strategy.

func (*ShardedTable[T]) Update

func (s *ShardedTable[T]) Update(shardValue any, pairs ...goent.Pair) error

Update updates a row in the correct sub-table by primary key.

type ShardingStrategy

type ShardingStrategy interface {
	ResolveTableName(base string, shardValue any) string
	MatchPattern(base string) string
}

ShardingStrategy defines how to resolve a shard table name from a shard value.

type TableCache

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

TableCache caches existing table names from the database. It avoids repeated information_schema queries on every cross-table operation.

func NewTableCache

func NewTableCache(ttl time.Duration) *TableCache

NewTableCache creates a new TableCache with the given TTL.

func (*TableCache) Add

func (c *TableCache) Add(tables ...string)

Add adds table names to the cache.

func (*TableCache) Exists

func (c *TableCache) Exists(table string) bool

Exists returns true if the table is in the cache.

func (*TableCache) IsExpired

func (c *TableCache) IsExpired() bool

IsExpired returns true if the cache TTL has passed.

func (*TableCache) Match

func (c *TableCache) Match(pattern string) []string

Match returns sorted table names matching the glob pattern.

func (*TableCache) Refresh

func (c *TableCache) Refresh(ctx context.Context, db *goent.DB) error

Refresh loads all table names from the database into the cache.

func (*TableCache) Remove

func (c *TableCache) Remove(tables ...string)

Remove removes table names from the cache.

func (*TableCache) Size

func (c *TableCache) Size() int

Size returns the number of cached table names.

type TimeGranularity

type TimeGranularity int

TimeGranularity defines time-based sharding granularity.

const (
	ByDay   TimeGranularity = iota // base_YYYYMMDD
	ByMonth                        // base_YYYYMM
	ByYear                         // base_YYYY
)

func (TimeGranularity) MatchPattern

func (g TimeGranularity) MatchPattern(base string) string

func (TimeGranularity) ResolveTableName

func (g TimeGranularity) ResolveTableName(base string, shardValue any) string

type TreeNode

type TreeNode[T any] struct {
	Data     T
	Children []*TreeNode[T]
	Parent   *TreeNode[T]
	Lft      int
	Rgt      int
	Depth    int
}

TreeNode represents a node in a nested set tree structure.

func (*TreeNode[T]) AddChild

func (n *TreeNode[T]) AddChild(child *TreeNode[T])

AddChild appends a child node and sets the parent reference.

func (*TreeNode[T]) IsLeaf

func (n *TreeNode[T]) IsLeaf() bool

IsLeaf returns true if this node has no children.

func (*TreeNode[T]) IsRoot

func (n *TreeNode[T]) IsRoot() bool

IsRoot returns true if this node has no parent.

func (*TreeNode[T]) WalkBreadthFirst

func (n *TreeNode[T]) WalkBreadthFirst(fn func(*TreeNode[T]))

WalkBreadthFirst traverses the tree breadth-first.

func (*TreeNode[T]) WalkDepthFirst

func (n *TreeNode[T]) WalkDepthFirst(fn func(*TreeNode[T]))

WalkDepthFirst traverses the tree depth-first (pre-order).

Jump to

Keyboard shortcuts

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