Documentation
¶
Index ¶
- type ModuloHex
- type NestedSet
- func (ns *NestedSet[T]) BuildTree(records []*T) *TreeNode[T]
- func (ns *NestedSet[T]) DeleteSubtree(node *T) error
- func (ns *NestedSet[T]) FlattenTree(root *TreeNode[T], start int) ([]*T, int)
- func (ns *NestedSet[T]) GetAncestors(node *T) ([]*T, error)
- func (ns *NestedSet[T]) GetChildren(node *T, depth int) ([]*T, error)
- func (ns *NestedSet[T]) GetDescendants(node *T) ([]*T, error)
- func (ns *NestedSet[T]) GetLevel(node *T) (int, error)
- func (ns *NestedSet[T]) GetParent(node *T) (*T, error)
- func (ns *NestedSet[T]) GetParents(node *T) ([]*T, error)
- func (ns *NestedSet[T]) InsertAsFirstChild(parent *T, child *T) error
- func (ns *NestedSet[T]) InsertAsLastChild(parent *T, child *T) error
- func (ns *NestedSet[T]) InsertAsNextSibling(sibling *T, node *T) error
- func (ns *NestedSet[T]) InsertAsPrevSibling(sibling *T, node *T) error
- func (ns *NestedSet[T]) IsDescendant(ancestor, potentialChild *T) bool
- func (ns *NestedSet[T]) IsLeafNode(node *T) bool
- func (ns *NestedSet[T]) PrintTree(root *TreeNode[T]) string
- type PageResult
- type ShardedTable
- func (s *ShardedTable[T]) BaseName() string
- func (s *ShardedTable[T]) Cache() *TableCache
- func (s *ShardedTable[T]) CountAll() (int64, error)
- func (s *ShardedTable[T]) DeleteByPK(shardValue any) error
- func (s *ShardedTable[T]) EachTable(fn func(*goent.Table[T]) error) error
- func (s *ShardedTable[T]) FindByPK(shardValue any, pk any) (*T, error)
- func (s *ShardedTable[T]) FindOne(shardValue any, filters ...goent.Condition) (*T, error)
- func (s *ShardedTable[T]) Insert(row *T) error
- func (s *ShardedTable[T]) RefreshCache(ctx context.Context) error
- func (s *ShardedTable[T]) ResolveTable(shardValue any) *goent.Table[T]
- func (s *ShardedTable[T]) SelectAll() ([]*T, error)
- func (s *ShardedTable[T]) SelectPage(page, size int) (*PageResult[T], error)
- func (s *ShardedTable[T]) Strategy() ShardingStrategy
- func (s *ShardedTable[T]) Update(shardValue any, pairs ...goent.Pair) error
- type ShardingStrategy
- type TableCache
- func (c *TableCache) Add(tables ...string)
- func (c *TableCache) Exists(table string) bool
- func (c *TableCache) IsExpired() bool
- func (c *TableCache) Match(pattern string) []string
- func (c *TableCache) Refresh(ctx context.Context, db *goent.DB) error
- func (c *TableCache) Remove(tables ...string)
- func (c *TableCache) Size() int
- type TimeGranularity
- type TreeNode
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 ¶
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 ¶
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 ¶
BuildTree builds a tree structure from flat records sorted by Lft value.
func (*NestedSet[T]) DeleteSubtree ¶
DeleteSubtree removes a node and all its descendants.
func (*NestedSet[T]) FlattenTree ¶
FlattenTree converts a tree back to flat records with updated Lft/Rgt values.
func (*NestedSet[T]) GetAncestors ¶
GetAncestors returns all ancestor nodes from root to parent.
func (*NestedSet[T]) GetChildren ¶
GetChildren returns descendant nodes within specified depth. depth=0 returns self, depth>0 limits levels, depth<0 returns all descendants.
func (*NestedSet[T]) GetDescendants ¶
GetDescendants returns all descendant nodes (not including self).
func (*NestedSet[T]) GetParents ¶
GetParents returns all ancestor nodes from root to direct parent.
func (*NestedSet[T]) InsertAsFirstChild ¶
InsertAsFirstChild inserts a new node as the first child of parent.
func (*NestedSet[T]) InsertAsLastChild ¶
InsertAsLastChild inserts a new node as the last child of parent.
func (*NestedSet[T]) InsertAsNextSibling ¶
InsertAsNextSibling inserts a new node as the next sibling of sibling.
func (*NestedSet[T]) InsertAsPrevSibling ¶
InsertAsPrevSibling inserts a new node as the previous sibling of sibling.
func (*NestedSet[T]) IsDescendant ¶
IsDescendant checks if potentialChild is a descendant of ancestor.
func (*NestedSet[T]) IsLeafNode ¶
IsLeafNode checks if a node has no children.
type PageResult ¶
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.
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) 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]) WalkBreadthFirst ¶
WalkBreadthFirst traverses the tree breadth-first.
func (*TreeNode[T]) WalkDepthFirst ¶
WalkDepthFirst traverses the tree depth-first (pre-order).