Documentation
¶
Overview ¶
设计原则是当前最简单快捷开发,不考虑通用性和将来扩展要求。 除了需要排序的主键和索引需要转换为[]byte外,其他所有字段值,皆转换为字符串存储
Index ¶
- Constants
- Variables
- func ClearAllCaches()
- func ClearFieldsBytesCache()
- func ClearIndexMatchCache()
- func CombineBytes(arrays [][][]byte, sep []byte, prefix []byte) [][]byte
- func DisableFieldsBytesLRUCache()
- func EnableFieldsBytesLRUCache()
- func GetAllCacheStats() map[string]CacheStats
- func GetAnyMap() map[string]any
- func GetAnyMapPointer() *map[string]any
- func GetBatchContainer(batch storage.Batch, indexs *Indexs, tbid uint8, kvStore storage.Store) *batchContainer
- func GetMap() map[any]bool
- func GetStringSlice() []string
- func Join(fieldsBytes *map[string][]byte, fields []string) []byte
- func JoinAndToBytes(v ...string) []byte
- func MatchFields(fields []string, existFields ...string) bool
- func NewBatchContainer(batch storage.Batch, indexs *Indexs, tbid uint8, kvStore storage.Store) *batchContainer
- func PutAnyMap(m map[string]any)
- func PutAnyMapPointer(mp *map[string]any)
- func PutBatchContainer(c *batchContainer)
- func PutMap(m map[any]bool)
- func PutStringSlice(s []string)
- func ResetStringSlicePool()
- func TableToJSON(t *Table) (string, error)
- type AutoInt
- type BaseIndex
- func (bi *BaseIndex) AddFields(field ...string)
- func (bi *BaseIndex) DeleteFields(field ...string)
- func (bi *BaseIndex) GetFields() []string
- func (bi *BaseIndex) GetId() uint8
- func (bi *BaseIndex) IsUnique(key []byte) bool
- func (bi *BaseIndex) Join(fieldsBytes *map[string][]byte) []byte
- func (bi *BaseIndex) JoinPrefix(tbid uint8, val []byte) []byte
- func (bi *BaseIndex) JoinValue(fieldsBytes *map[string][]byte, tbid uint8, existFields ...string) []byte
- func (bi *BaseIndex) Len() int
- func (bi *BaseIndex) MatchFields(fields ...string) bool
- func (bi *BaseIndex) Name() string
- func (bi *BaseIndex) Parse(primaryFields []string, pkfieldTypeLen *map[string]uint8, value []byte) (*map[string][]byte, error)
- func (bi *BaseIndex) Prefix(tbid uint8) []byte
- func (bi *BaseIndex) SetName(name string) error
- func (bi *BaseIndex) UpdateFields(oldfields string, newfields string)
- type BatchInsert
- type BatchInsertImpl
- type BatchInsertImplPool
- type CacheStats
- type ConcurrentCache
- func (c *ConcurrentCache) Clear()
- func (c *ConcurrentCache) Get(key string) (interface{}, bool)
- func (c *ConcurrentCache) Set(key string, entry interface{})
- func (c *ConcurrentCache) SetEnableStats(enable bool)
- func (c *ConcurrentCache) Size() int64
- func (c *ConcurrentCache) Stats() (size, accesses, hits int64)
- type DefaultFullTextIndex
- func (dfi *DefaultFullTextIndex) JoinFullValues(fieldsBytes *map[string][]byte, tbid uint8, existFields ...string) [][]byte
- func (dfi *DefaultFullTextIndex) JoinValue(fieldsBytes *map[string][]byte, tbid uint8, existFields ...string) []byte
- func (dfi *DefaultFullTextIndex) Parse(primaryFields []string, pkfieldTypeLen *map[string]uint8, value []byte) (*map[string][]byte, error)
- func (dfi *DefaultFullTextIndex) SetFullField(field string, len int) error
- func (dfi *DefaultFullTextIndex) Tokenize(nr string, ftlen int) (tokens []string)
- type DefaultNormalIndex
- type DefaultPrimaryKey
- type Delete
- type DeleteImpl
- type DeleteImplPool
- type Export
- type ExportRecord
- type FieldsBytesPool
- type FullTextIndex
- type IDManager
- func (m *IDManager) GetCurrentID(key string) (uint8, error)
- func (m *IDManager) GetNextID(key string) (uint8, error)
- func (m *IDManager) GetOrCreateID(key string) (uint8, bool, error)
- func (m *IDManager) GetPreviousID(key string) (uint8, error)
- func (m *IDManager) ResetID(key string) error
- func (m *IDManager) SetID(key string, id uint8) error
- func (m *IDManager) UpdateKey(oldKey string, newKey string) error
- type Index
- type IndexSchema
- type Indexs
- func (i *Indexs) DeleteFields(field ...string)
- func (i *Indexs) DeleteIndex(name string) error
- func (i *Indexs) GetAllIndexNameIdMap() map[string]uint8
- func (i *Indexs) GetAllIndexes() []Index
- func (i *Indexs) GetFullTextIndexs() []FullTextIndex
- func (i *Indexs) GetIndex(name string) Index
- func (i *Indexs) GetNormalIndexs() []NormalIndex
- func (i *Indexs) Len() int
- func (i *Indexs) MatchIndex(fields ...string) Index
- func (i *Indexs) UpdateFields(oldfields string, newfields string)
- type Insert
- type InsertImpl
- type InsertImplPool
- type LRUCache
- func (c *LRUCache) Clear()
- func (c *LRUCache) Disable()
- func (c *LRUCache) Enable()
- func (c *LRUCache) Get(key string) (*map[string][]byte, bool)
- func (c *LRUCache) IsEnabled() bool
- func (c *LRUCache) Set(key string, value *map[string][]byte)
- func (c *LRUCache) Size() int64
- func (c *LRUCache) Stats() (size, accesses, hits int64)
- type NormalIndex
- type Page
- type PrimaryKey
- type Table
- func (t *Table) AutoValue() int
- func (t *Table) BatchFieldsToBytes(records []*map[string]any) []*map[string][]byte
- func (t *Table) BatchFormatRecords(records []*map[string][]byte) [][]byte
- func (t *Table) BatchInsertInc(records []*map[string]any, batchs ...storage.Batch) ([]int, error)
- func (t *Table) BatchInsertNoInc(records []*map[string]any, batchs ...storage.Batch) ([]int, error)
- func (t *Table) BatchRecordByteToAny(records []*map[string][]byte) []*map[string]any
- func (t *Table) CheckType(fields *map[string]any) error
- func (t *Table) CreateCompositeIndex(name string, fields ...string) error
- func (t *Table) CreateCompositePrimaryKey(name string, fields ...string) error
- func (t *Table) CreateIndex(index Index) error
- func (t *Table) CreatePrimaryKey(fields ...string) error
- func (t *Table) CreateSimpleIndex(name string, fields ...string) error
- func (t *Table) Delete(fields *map[string]any, batchs ...storage.Batch) error
- func (t *Table) DeleteAll() error
- func (t *Table) DropIndex(name string) error
- func (t *Table) DropPrimaryKey() error
- func (t *Table) ExportToCSV(filePath string) error
- func (t *Table) ExportToJSON(filePath string) error
- func (t *Table) ExportToSQL(filePath string) error
- func (t *Table) FieldsToBytes(fields *map[string]any) *map[string][]byte
- func (t *Table) FieldsToBytesNil(fields *map[string]any) *map[string][]byte
- func (t *Table) FieldsToBytesNilLRU(fields *map[string]any) *map[string][]byte
- func (t *Table) For() storage.Iterator
- func (t *Table) ForData() *TableIter
- func (t *Table) FormatRecord(fieldsBytes *map[string][]byte) []byte
- func (t *Table) GetAllFieldNameIdMap() map[string]uint8
- func (t *Table) GetAllFields() map[string]any
- func (t *Table) GetAllIndexNameIdMap() map[string]uint8
- func (t *Table) GetAllIndexes() []Index
- func (t *Table) GetAutoInc() int
- func (t *Table) GetAutoIncBatch(count int) int
- func (t *Table) GetField(field string) (any, bool)
- func (t *Table) GetFieldsName() []string
- func (t *Table) GetId() uint8
- func (t *Table) GetIndexByName(name string) Index
- func (t *Table) GetIndexesByField(field string) []Index
- func (t *Table) GetName() string
- func (t *Table) GetPrimary() []string
- func (t *Table) GetPrimaryFields() []string
- func (t *Table) GetPrimaryKey() PrimaryKey
- func (t *Table) GetStore() storage.Store
- func (t *Table) ImportFromCSV(filePath string, batchSize int) error
- func (t *Table) ImportFromJSON(filePath string, batchSize int) error
- func (t *Table) InitAuto()
- func (t *Table) Insert(fields *map[string]any, batchs ...storage.Batch) (currentID int, err error)
- func (t *Table) MatchIndex(fields ...string) Index
- func (t *Table) MatchIndexCached(fields []string) Index
- func (t *Table) MaxAutoValue() int
- func (t *Table) RangeForAny(funIter storage.FunIter, Start, Limit *map[string]any) (storage.Iterator, Index, error)
- func (t *Table) Read(fields *map[string]any) ([]byte, error)
- func (t *Table) ReadByBytes(key []byte) ([]byte, error)
- func (t *Table) RecordByteToAny(value *map[string][]byte) *map[string]any
- func (t *Table) ResetPrimaryFields()
- func (t *Table) Search(fields *map[string]any, ops ...util.ComparisonOperator) (*TableIter, error)
- func (t *Table) SearchRange(funIter storage.FunIter, Start, Limit *map[string]any) (*TableIter, error)
- func (t *Table) Searchs(funIter storage.FunIter, fields *map[string]any, ...) (*TableIter, error)
- func (t *Table) SetFields(fields map[string]any) error
- func (t *Table) SetIndexCacheSizeLimit(limit int)
- func (t *Table) ToSchema() *TableSchema
- func (t *Table) ToSerialization() *TableSerialization
- func (t *Table) Update(fields *map[string]any, batchs ...storage.Batch) error
- func (t *Table) UpdateFieldName(oldfield string, newfield string) error
- type TableCache
- type TableIter
- func (t *TableIter) Count() int
- func (t *TableIter) Delete(limit ...int) error
- func (t *TableIter) Exist() bool
- func (t *TableIter) ExportRecord(export ExportRecord, esc bool, limit ...int)
- func (t *TableIter) First() bool
- func (t *TableIter) ForExport(esc bool, export Export)
- func (t *TableIter) GetIndexFields() []string
- func (t *TableIter) GetIndexId() uint8
- func (t *TableIter) GetIndexName() string
- func (t *TableIter) GetPrimaryKeys(k, v []byte, fields ...string) (r any)
- func (t *TableIter) GetRecords(esc bool, limit ...int) (r record.Records)
- func (t *TableIter) JumpRange(key []byte, jumpRanges []storage.Iterator, esc bool) []byte
- func (t *TableIter) Key() []byte
- func (t *TableIter) Last() bool
- func (t *TableIter) Map(fields ...string) (data map[any]bool)
- func (t *TableIter) Match(rd *map[string]any, match []match.Match) bool
- func (t *TableIter) Next() bool
- func (t *TableIter) ParseBytes(k, v []byte) *map[string][]byte
- func (t *TableIter) ParseRecord(fieldsBytes *map[string][]byte) (rd record.Record)
- func (t *TableIter) Prev() bool
- func (t *TableIter) RecordIndexTime(startTime time.Time, searchType string)
- func (t *TableIter) Release()
- func (t *TableIter) ReleaseMap(data map[any]bool)
- func (t *TableIter) Seek(key []byte) bool
- func (t *TableIter) SetJumpRanges(jumpRanges ...storage.Iterator)
- func (t *TableIter) SetMatch(match ...match.Match)
- func (t *TableIter) SetSelects(fields ...string)
- func (t *TableIter) Update(fields *map[string]any, limit ...int) error
- func (t *TableIter) Valid() bool
- func (t *TableIter) Value() []byte
- type TableIterPool
- type TableSchema
- type TableSerialization
- type Update
- type UpdateImpl
- func (u *UpdateImpl) AddNewRecord()
- func (u *UpdateImpl) CheckParams() error
- func (u *UpdateImpl) Commit() error
- func (u *UpdateImpl) DeleteOldRecord()
- func (u *UpdateImpl) GetFieldsBytes() error
- func (u *UpdateImpl) PrepareBatch(batchs ...storage.Batch)
- func (u *UpdateImpl) PrepareUpdateFields() error
- func (u *UpdateImpl) ReadRecord() error
- func (u *UpdateImpl) Reset()
- type UpdateImplPool
- type ValueJoiner
- func (vj *ValueJoiner) GenerateCounterKey(prefix, objType string) string
- func (vj *ValueJoiner) GenerateFieldCounterKey(tableID uint8) string
- func (vj *ValueJoiner) GenerateFieldKey(tableID uint8, fieldName string) string
- func (vj *ValueJoiner) GenerateIndexCounterKey(tableID uint8) string
- func (vj *ValueJoiner) GenerateIndexKey(tableID uint8, indexName string) string
- func (vj *ValueJoiner) GenerateObjectKey(prefix, objType, name string) string
- func (vj *ValueJoiner) GenerateTableCounterKey() string
- func (vj *ValueJoiner) GenerateTableKey(tableName string) string
- func (vj *ValueJoiner) Join(parts ...string) string
Constants ¶
const ( // SysPrefix 系统前缀 SysPrefix = "sys" // 键类型常量 KeyTypeTable = "table" KeyTypeIndex = "idx" KeyTypeField = "field" // Separator 键分隔符 Separator = "-" )
常量定义
const (
// IDMaxLimit ID的最大限制(1字节最多能创建255个ID)
IDMaxLimit = 255
)
常量定义
const SPLIT = util.SPLIT //分隔符
Variables ¶
var ( //没有主键 ErrNoPrimaryKey = errors.New("no primary key") //没有记录 ErrNoUpdateFields = errors.New("no update fields") )
var GlobalBatchInsertImplPool = &BatchInsertImplPool{ pool: sync.Pool{ New: func() interface{} { return &BatchInsertImpl{} }, }, }
全局 BatchInsertImpl 对象池
var GlobalDeleteImplPool = &DeleteImplPool{ pool: sync.Pool{ New: func() interface{} { return &DeleteImpl{} }, }, }
全局 DeleteImpl 对象池
var GlobalInsertImplPool = &InsertImplPool{ pool: sync.Pool{ New: func() interface{} { return &InsertImpl{} }, }, }
全局 InsertImpl 对象池
var GlobalUpdateImplPool = &UpdateImplPool{ pool: sync.Pool{ New: func() interface{} { return &UpdateImpl{} }, }, }
全局 UpdateImpl 对象池
Functions ¶
func CombineBytes ¶
全文索引算法 CombineBytes 接收多个字节数组,使用指定分隔符返回第一个数组与其他数组的所有可能拼接组合 返回的结果是从对象池获取的,外部调用者使用后需要通过 PutBytesArray 归还到对象池
func DisableFieldsBytesLRUCache ¶
func DisableFieldsBytesLRUCache()
DisableFieldsBytesLRUCache 禁用 LRU 缓存
func EnableFieldsBytesLRUCache ¶
func EnableFieldsBytesLRUCache()
EnableFieldsBytesLRUCache 启用 LRU 缓存
func GetAnyMapPointer ¶
GetAnyMapPointer 从对象池获取一个 map[string]any 并返回其指针
func GetBatchContainer ¶
func GetBatchContainer(batch storage.Batch, indexs *Indexs, tbid uint8, kvStore storage.Store) *batchContainer
GetBatchContainer 从对象池中获取一个 batchContainer
func JoinAndToBytes ¶
func NewBatchContainer ¶
func PutAnyMapPointer ¶
PutAnyMapPointer 将 map[string]any 指针指向的对象归还到对象池
func PutBatchContainer ¶
func PutBatchContainer(c *batchContainer)
PutBatchContainer 将 batchContainer 归还到对象池
Types ¶
type AutoInt ¶
type AutoInt int64
func (*AutoInt) GetAndIncrementBy ¶ added in v1.9.3
GetAndIncrementBy 原子地获取当前值并增加指定的数量,返回增加前的值
func (*AutoInt) IncrementBy ¶
IncrementBy 批量增加指定的值,并返回增加后的值
type BaseIndex ¶
type BaseIndex struct {
// contains filtered or unexported fields
}
------------------------------------------ 基础索引结构体,包含所有索引类型共有的字段和方法
func (*BaseIndex) IsUnique ¶
IsUnique方法返回false,表示不是唯一索引。 传入参考key=JoinValue(fieldsBytes *map[string][]byte, tbid uint8, existFields ...string) []byte
func (*BaseIndex) MatchFields ¶
func (*BaseIndex) Parse ¶
func (bi *BaseIndex) Parse(primaryFields []string, pkfieldTypeLen *map[string]uint8, value []byte) (*map[string][]byte, error)
将索引的value值转换为主键map值 value=fval1+SPLIT+fval2+SPLIT+...+fieldn fields []string, value []byte, 顺序必须相同 pkfieldTypeLen,按照定长截取字段值。
func (*BaseIndex) UpdateFields ¶
修改索引字段名称
type BatchInsert ¶
type BatchInsert interface {
Insert
// 批量插入多条记录
BatchInsert(records []*map[string]any, batchs ...storage.Batch) ([]int, error)
// 批量插入多条记录,不自动生成主键
BatchInsertNoInc(batchs ...storage.Batch) ([]int, error)
// 批量提交事务
BatchCommit() error
}
批量插入接口
type BatchInsertImpl ¶ added in v1.9.3
type BatchInsertImpl struct {
InsertImpl
// contains filtered or unexported fields
}
func NewBatchInsertImpl ¶
func NewBatchInsertImpl(table *Table, records []*map[string]any) *BatchInsertImpl
NewBatchInsertImpl 创建一个新的用于批量插入的 BatchInsertImpl 实例
func (*BatchInsertImpl) BatchCommit ¶ added in v1.9.3
func (i *BatchInsertImpl) BatchCommit() error
BatchCommit 批量提交事务
func (*BatchInsertImpl) BatchInsertInc ¶ added in v1.9.3
func (i *BatchInsertImpl) BatchInsertInc(records []*map[string]any, batchs ...storage.Batch) ([]int, error)
BatchInsertInc 批量插入多条记录,自动生成主键
func (*BatchInsertImpl) BatchInsertNoInc ¶ added in v1.9.3
func (i *BatchInsertImpl) BatchInsertNoInc(batchs ...storage.Batch) ([]int, error)
批量添加不需要自动增值的记录,并且全部记录规则相同。 可用于批量插入时序数据,当表主键为时间戳时,建议使用此方法
func (*BatchInsertImpl) Reset ¶ added in v1.9.3
func (i *BatchInsertImpl) Reset()
Reset 重置 BatchInsertImpl 实例的状态
type BatchInsertImplPool ¶ added in v1.9.3
type BatchInsertImplPool struct {
// contains filtered or unexported fields
}
BatchInsertImplPool 是 BatchInsertImpl 的对象池
func (*BatchInsertImplPool) Get ¶ added in v1.9.3
func (p *BatchInsertImplPool) Get() *BatchInsertImpl
Get 从对象池中获取一个 BatchInsertImpl 实例
func (*BatchInsertImplPool) Put ¶ added in v1.9.3
func (p *BatchInsertImplPool) Put(impl *BatchInsertImpl)
Put 将 BatchInsertImpl 实例放回对象池
type CacheStats ¶
type CacheStats struct {
Size int64 // 缓存大小
Accesses int64 // 访问次数
Hits int64 // 命中次数
HitRate float64 // 命中率
}
CacheStats 缓存统计信息结构体
func GetIndexMatchCacheStats ¶
func GetIndexMatchCacheStats() CacheStats
GetIndexMatchCacheStats 获取索引匹配缓存统计信息
type ConcurrentCache ¶
type ConcurrentCache struct {
// contains filtered or unexported fields
}
ConcurrentCache 并发安全的缓存实现
func (*ConcurrentCache) Get ¶
func (c *ConcurrentCache) Get(key string) (interface{}, bool)
Get 获取缓存条目
func (*ConcurrentCache) Set ¶
func (c *ConcurrentCache) Set(key string, entry interface{})
Set 设置缓存条目
func (*ConcurrentCache) SetEnableStats ¶
func (c *ConcurrentCache) SetEnableStats(enable bool)
SetEnableStats 设置是否启用统计
func (*ConcurrentCache) Stats ¶
func (c *ConcurrentCache) Stats() (size, accesses, hits int64)
Stats 获取缓存统计信息
type DefaultFullTextIndex ¶
type DefaultFullTextIndex struct {
BaseIndex // 嵌入基础索引
// contains filtered or unexported fields
}
------------------------------------------ 默认全文索引
func DefaultFullTextIndexNew ¶
func DefaultFullTextIndexNew(name string) (*DefaultFullTextIndex, error)
func (*DefaultFullTextIndex) JoinFullValues ¶
func (dfi *DefaultFullTextIndex) JoinFullValues(fieldsBytes *map[string][]byte, tbid uint8, existFields ...string) [][]byte
put时拼接key的值 全文索引考据级别的切词算法。 只有全文索引才需要转义
func (*DefaultFullTextIndex) JoinValue ¶
func (dfi *DefaultFullTextIndex) JoinValue(fieldsBytes *map[string][]byte, tbid uint8, existFields ...string) []byte
调用基类JoinValue 对应全文索引,该函数仅作搜索前缀用。 拼接全文索引前缀,所有全文索引值都会进行转义。
func (*DefaultFullTextIndex) SetFullField ¶
func (dfi *DefaultFullTextIndex) SetFullField(field string, len int) error
指定那个字段是全文索引字段,以及索引长度
type DefaultNormalIndex ¶
type DefaultNormalIndex struct {
BaseIndex // 嵌入基础索引
}
------------------------------------------ 默认普通索引,二级索引
func DefaultNormalIndexNew ¶
func DefaultNormalIndexNew(name string) (*DefaultNormalIndex, error)
type DefaultPrimaryKey ¶
type DefaultPrimaryKey struct {
BaseIndex // 嵌入基础索引
}
------------------------------------------ 默认主键索引 组合主键时只支持固定长度的类型的组合。 字符串类型,必须指定长度。否则无法解析或存在转义问题导致bug。
func DefaultPrimaryKeyNew ¶
func DefaultPrimaryKeyNew(name string) (*DefaultPrimaryKey, error)
func (*DefaultPrimaryKey) GetID ¶
func (dpk *DefaultPrimaryKey) GetID(fieldsBytes *map[string][]byte, existFields ...string) []byte
過濾存在的字段 系統設計爲過濾key存在的字段不在value中儲存。
func (*DefaultPrimaryKey) GetfieldTypeLen ¶
func (dpk *DefaultPrimaryKey) GetfieldTypeLen(tablefields *map[string]any) *map[string]uint8
获取主键字段的总长度 组合主键时只支持固定长度的类型的组合。 字符串类型,必须指定长度。否则无法解析或存在转义问题导致bug。
func (*DefaultPrimaryKey) Parse ¶
func (dpk *DefaultPrimaryKey) Parse(fieldsid map[uint8]string, value []byte) (*map[string][]byte, error)
// // 与func (t *Table) FormatRecord(fieldsBytes *map[string][]byte) (r []byte) 相对应 // 主键索引值记录格式:field1idvalue1-field2idvalue2-...-fieldNidvalueN // Parse(fields []string, value []byte) // fieldsid map[uint8]string // id到字段名的映射的关键作用在这里。第一个字节是字段id,后面是字段值。解析方法简单。
反格式化,解析函数,将索引的value转换为记录对应的map[string][]byte
type Delete ¶
type Delete interface {
// 是否提供了主键字段
HasPrimaryKey() error
//准备删除操作的batch
PrepareBatch(batchs ...storage.Batch)
// 读取要删除的记录
ReadRecord() error
// 删除记录
DeleteRecord() error
// 提交事务
Commit() error
}
提供了一个统一的删除流程接口
type DeleteImpl ¶
type DeleteImpl struct {
// contains filtered or unexported fields
}
func NewDeleteImpl ¶
func NewDeleteImpl(table *Table, fields *map[string]any) *DeleteImpl
NewDeleteImpl 创建一个新的 DeleteImpl 实例
func (*DeleteImpl) DeleteRecord ¶ added in v1.9.3
func (d *DeleteImpl) DeleteRecord() error
func (*DeleteImpl) HasPrimaryKey ¶ added in v1.9.3
func (d *DeleteImpl) HasPrimaryKey() error
func (*DeleteImpl) PrepareBatch ¶ added in v1.9.3
func (d *DeleteImpl) PrepareBatch(batchs ...storage.Batch)
func (*DeleteImpl) ReadRecord ¶ added in v1.9.3
func (d *DeleteImpl) ReadRecord() error
type DeleteImplPool ¶
type DeleteImplPool struct {
// contains filtered or unexported fields
}
DeleteImplPool 是 DeleteImpl 的对象池
func (*DeleteImplPool) Put ¶
func (p *DeleteImplPool) Put(impl *DeleteImpl)
Put 将 DeleteImpl 实例放回对象池
type FieldsBytesPool ¶
type FieldsBytesPool struct {
// contains filtered or unexported fields
}
FieldsBytesPool 是用于管理 map[string][]byte 对象的池
var GlobalFieldsBytesPool *FieldsBytesPool
全局 FieldsBytesPool 实例
func NewFieldsBytesPool ¶
func NewFieldsBytesPool() *FieldsBytesPool
NewFieldsBytesPool 创建一个新的 FieldsBytesPool
func (*FieldsBytesPool) Get ¶
func (p *FieldsBytesPool) Get() map[string][]byte
Get 从池中获取一个 map[string][]byte 对象
func (*FieldsBytesPool) GetMapPointer ¶
func (p *FieldsBytesPool) GetMapPointer() *map[string][]byte
GetMapPointer 从池中获取一个 map[string][]byte 并返回其指针
func (*FieldsBytesPool) Put ¶
func (p *FieldsBytesPool) Put(m map[string][]byte)
Put 将 map[string][]byte 对象放回池中
func (*FieldsBytesPool) PutMapPointer ¶
func (p *FieldsBytesPool) PutMapPointer(mp *map[string][]byte)
PutMapPointer 将 map[string][]byte 指针指向的对象放回池中
type FullTextIndex ¶
type FullTextIndex interface {
Index
SetFullField(field string, len int) error
//GetFtlen() int
// 拼接全文索引值
JoinFullValues(fieldsBytes *map[string][]byte, tbid uint8, existFields ...string) [][]byte
// 分词方法
Tokenize(nr string, ftlen int) (tokens []string)
Parse(primaryFields []string, pkfieldTypeLen *map[string]uint8, value []byte) (*map[string][]byte, error)
}
------------------------------------------ 全文索引接口,嵌入基础索引接口
type IDManager ¶
type IDManager struct {
ValueJoiner // 嵌入值拼接器,用于生成系统ID键
// contains filtered or unexported fields
}
IDManager 实现ID管理功能
功能描述: 1. 根据对象键(sys-table-name ,sys-tableid-idx-name,sys-tableid-field-name)读取已有ID 2. 如果对象键不存在,则从对应类型的自动增长计数器(sys-table,sys-tableid-idx,sys-tableid-field)获取新ID 3. 支持表、索引、字段等不同类型的ID管理 4. 保证线程安全
var TableIDManager *IDManager
func (*IDManager) GetCurrentID ¶
GetCurrentID 获取指定key的当前ID值(未递增)
参数: - key: 用于自动递增的key(如sys-table, sys-index等)
返回: - uint8: 当前ID值(0-255) - error: 错误信息
func (*IDManager) GetNextID ¶
GetNextID 通过key值获取下一个自动递增的ID
参数: - key: 用于自动递增的key(如sys-table, sys-index等)
返回: - uint8: 生成的ID(0-255) - error: 错误信息,如ID已达上限
功能流程: 1. 从key中获取当前计数器值 2. 检查ID是否已达上限 3. 递增并存储计数器 4. 返回生成的ID
func (*IDManager) GetOrCreateID ¶
GetOrCreateID 获取或创建基于名称和类型的ID
参数: - name: 对象名称(如表名、索引名、字段名) - objType: 对象类型前缀(table-表,idx-索引,field-字段)
返回: - uint8: 生成或获取的ID(0-255) - bool: 是否是新创建的ID - error: 错误信息,如ID已达上限
功能流程: 1. 尝试从对象key获取已存在的ID 2. 如果ID存在,直接返回 3. 如果ID不存在,从类型计数器获取当前值 4. 检查ID是否已达上限 5. 递增并存储计数器 6. 将生成的ID存储到对象key 7. 返回生成的ID bool: 是否是新创建的ID,true表示是,false表示不是
func (*IDManager) GetPreviousID ¶
GetPreviousID 回退指定key的ID计数器,并返回回退前的ID 用于在错误时回退ID
参数: - key: 用于自动递增的key(如sys-table, sys-index等)
返回: - uint8: 回退前的ID值(0-255) - error: 错误信息,如ID已达下限(0)
func (*IDManager) ResetID ¶
ResetID 重置指定key的ID计数器为0
参数: - key: 用于自动递增的key(如sys-table, sys-index等)
返回: - error: 错误信息
type Index ¶
type Index interface {
// 添加索引字段
AddFields(field ...string)
// 获取索引字段列表
GetFields() []string
Len() int
GetId() uint8
Name() string
SetName(name string) error
//修改索引字段名称
UpdateFields(oldfields string, newfields string)
//删除索引字段
DeleteFields(field ...string)
//拼接前缀,表id+索引id
Prefix(tbid uint8) []byte
//拼接值,字段值拼接
Join(fieldsBytes *map[string][]byte) []byte
//拼接前缀+值。调用Prefix方法
JoinPrefix(tbid uint8, val []byte) []byte
// 拼接索引前缀+索引值,调用JoinPrefix,Join方法
JoinValue(fieldsBytes *map[string][]byte, tbid uint8, existFields ...string) []byte
//是否唯一key,key=JoinValue方法返回的值
IsUnique(key []byte) bool
// 匹配索引字段
MatchFields(fields ...string) bool
// contains filtered or unexported methods
}
基础索引接口,定义所有索引类型共有的方法
type IndexSchema ¶
type Indexs ¶
type Indexs struct {
// contains filtered or unexported fields
}
索引管理结构
func (*Indexs) GetAllIndexNameIdMap ¶
获取所有索引的名称和id映射
func (*Indexs) MatchIndex ¶
匹配索引,主键第一优先,其他按索引字段数量排序(字段越多,优先级越高)
func (*Indexs) UpdateFields ¶
修改所有包含字段名称的索引
type Insert ¶
type Insert interface {
//检查参数
CheckParams() error
//准备插入操作的batch
PrepareBatch(batchs ...storage.Batch)
//自动增值主键
AutoIncrement() (int, error)
// 转换字段为字节数组
FieldsToBytes(fields *map[string]any) *map[string][]byte
// 格式化记录
FormatRecord(fieldsBytes *map[string][]byte) []byte
// 添加数据到batch
AddRecord()
// 提交事务
Commit() error
}
提供了一个统一的插入流程接口 添加记录流程接口
type InsertImpl ¶
type InsertImpl struct {
// contains filtered or unexported fields
}
func NewInsertImpl ¶
func NewInsertImpl(table *Table, fields *map[string]any) *InsertImpl
NewInsertImpl 创建一个新的 InsertImpl 实例
func (*InsertImpl) AddRecord ¶ added in v1.9.3
func (i *InsertImpl) AddRecord()
func (*InsertImpl) AutoIncrement ¶
func (i *InsertImpl) AutoIncrement() (int, error)
AutoIncrement 实现自动增值主键
func (*InsertImpl) CheckParams ¶ added in v1.9.3
func (i *InsertImpl) CheckParams() error
func (*InsertImpl) PrepareBatch ¶ added in v1.9.3
func (i *InsertImpl) PrepareBatch(batchs ...storage.Batch)
PrepareBatch 准备插入操作的batch
type InsertImplPool ¶
type InsertImplPool struct {
// contains filtered or unexported fields
}
InsertImplPool 是 InsertImpl 的对象池
func (*InsertImplPool) Put ¶
func (p *InsertImplPool) Put(impl *InsertImpl)
Put 将 InsertImpl 实例放回对象池
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRUCache 基于 LRU 算法的缓存实现
func GetFieldsBytesLRUCache ¶
func GetFieldsBytesLRUCache() *LRUCache
GetFieldsBytesLRUCache 获取 LRU 缓存实例
type NormalIndex ¶
type NormalIndex interface {
Index
// 将索引的value转换为主键map值
// 由于NormalIndex完全匹配index接口,所以需要一个Tag方法来区别是否是二级索引。
Tag() bool
Parse(primaryFields []string, pkfieldTypeLen *map[string]uint8, value []byte) (*map[string][]byte, error)
}
------------------------------------------ 普通索引接口,嵌入基础索引接口
type PrimaryKey ¶
type PrimaryKey interface {
Index
// 设置主键ID
GetID(fieldsBytes *map[string][]byte, existFields ...string) []byte
GetfieldTypeLen(tablefields *map[string]any) *map[string]uint8
Parse(fieldsid map[uint8]string, value []byte) (*map[string][]byte, error)
}
------------------------------------------ 主键接口,嵌入基础索引接口
type Table ¶
type Table struct {
TableCache
// contains filtered or unexported fields
}
func FromSchema ¶
func FromSchema(schema *TableSchema) (*Table, error)
FromSchema 从TableSchema创建Table 用于从元数据反序列化Table结构体
func FromSerialization ¶
func FromSerialization(serialization *TableSerialization) (*Table, error)
FromSerialization 从TableSerialization创建Table 用于从完整序列化信息反序列化Table结构体 kvStore需要在调用此函数后重新初始化
func TableFromJSON ¶
TableFromJSON 从JSON字符串创建Table
func (*Table) BatchFieldsToBytes ¶
BatchFieldsToBytes 批量转换多个记录
func (*Table) BatchFormatRecords ¶
BatchFormatRecords 批量格式化多个记录 对于批量处理场景,此方法比多次调用 FormatRecord 更高效
func (*Table) BatchInsertInc ¶ added in v1.9.3
数据流动流程 1,外部传入 Insert(fields *map[string]any 2,// 转换字段为字节数组 fieldsBytes := t.FieldsToBytes(fields) 3,//格式化记录 record := t.FormatRecord(fieldsBytes) 4,添加更新记录 tableiter 查询功能则与上面添加的流程相反。一正一逆。
BatchInsert 批量插入多条记录 records []*map[string]any 要插入的记录列表 batchs ...storage.Batch 可选的批量操作容器 返回值:插入记录的ID列表和错误信息
func (*Table) BatchInsertNoInc ¶
BatchInsertNoInc 批量插入不需要自动增值的记录 records []*map[string]any 要插入的记录列表 batchs ...storage.Batch 可选的批量操作容器 返回值:插入记录的ID列表和错误信息 批量添加时序数据,当表主键为时间戳时,建议使用此方法
func (*Table) BatchRecordByteToAny ¶
BatchRecordByteToAny 批量转换多个记录
func (*Table) CreateCompositeIndex ¶
创建普通复合索引
func (*Table) CreateCompositePrimaryKey ¶
创建主键复合索引
func (*Table) CreateIndex ¶
func (*Table) CreatePrimaryKey ¶
创建主键索引(支持单个或多个字段)
func (*Table) CreateSimpleIndex ¶
创建普通索引(简化版,直接指定名称和字段)
func (*Table) Delete ¶
删除记录 fields *map[string]any 主键值,可能是组合主键 之前Delete的缺省参数为batchs ...storage.Batch ,支持乐观锁需要增加一个参数,故而为兼容之前的函数, 使用使用 batchs ...storage.Batch 。batch和timeout合并为一个参数组数
func (*Table) ExportToCSV ¶
ExportToCSV 将表数据导出为CSV格式
func (*Table) ExportToJSON ¶
ExportToJSON 将表数据导出为JSON格式
func (*Table) ExportToSQL ¶
ExportToSQL 将表数据导出为SQL格式
func (*Table) FieldsToBytes ¶
将数据转换为字节数组,该合适添加时用。搜索时nil值不能更改 *map[string]any ==> *map[string][]byte 与RecordByteToAny相反
func (*Table) FieldsToBytesNil ¶
将数据转换为字节数组,该合适搜索时用。搜索时nil值不能更改,否则导致结果错误
func (*Table) FieldsToBytesNilLRU ¶
FieldsToBytesNilLRU 使用 LRU 缓存的字段转换
func (*Table) GetAllFieldNameIdMap ¶
获取所有字段名称和id映射
func (*Table) GetAllFields ¶
获取所有字段值,用于添加记录时,直接复制,无需自行创建。
func (*Table) GetAllIndexNameIdMap ¶
获取所有索引的名称和id映射
func (*Table) GetAutoInc ¶
// 初始化自动增值计数器(如果需要)可能存在并发问题,需要使用锁保护
func (t *Table) initAutoCounterIfNeeded() {
if t.counter.Get() == 0 {
// 使用表级别的锁,避免全局锁导致的并发瓶颈
t.initMutex.Lock()
// 再次检查计数器是否为 0,避免重复初始化
if t.counter.Get() == 0 {
t.InitAuto()
}
t.initMutex.Unlock()
}
}
获取自动增值的值
func (*Table) GetAutoIncBatch ¶
GetAutoIncBatch 批量获取自动增值的值 count 需要获取的ID数量 返回值:第一个ID的值
func (*Table) GetIndexesByField ¶
根据字段名获取包含该字段的所有索引
func (*Table) GetPrimaryFields ¶
GetPrimaryFields 获取主键字段列表(带缓存)
func (*Table) ImportFromCSV ¶
ImportFromCSV 从CSV文件导入数据到表
func (*Table) ImportFromJSON ¶
ImportFromJSON 从JSON文件导入数据到表
func (*Table) RangeForAny ¶
func (t *Table) RangeForAny(funIter storage.FunIter, Start, Limit *map[string]any) (storage.Iterator, Index, error)
区间迭代器,用于范围搜索和*TableIter的跳跃区间
func (*Table) ReadByBytes ¶
// 从按主键数据库读取记录 func (t *Table) Read(fields *map[string]any) ([]byte, error) {
// 使用 SearchImpl
searchImpl := NewSearchImpl(t)
// 读取记录
record := searchImpl.Read(fields)
if record == nil {
GlobalSearchImplPool.Put(searchImpl)
return nil, fmt.Errorf("主键值 '%v' 的记录不存在", fields)
}
// 归还对象池
GlobalSearchImplPool.Put(searchImpl)
return record, nil
}
从按主键数据库读取记录
func (*Table) RecordByteToAny ¶
*map[string][]byte ==> *map[string]any 与FieldsToBytes相反
func (*Table) ResetPrimaryFields ¶
func (t *Table) ResetPrimaryFields()
ResetPrimaryFields 重置主键字段列表缓存
func (*Table) SearchRange ¶
func (t *Table) SearchRange(funIter storage.FunIter, Start, Limit *map[string]any) (*TableIter, error)
SearchRange 范围搜索,区间搜索 fieldname 对应索引字段名,单主键或组合主键,单索引或组合索引字段。 组合主键或索引时,Start, Limit any为最后一个字段的范围值。第一个字段必须全量匹配,也就是后缀匹配。
- 当Limit的最后一个字段值为nil时,表示搜索到该前缀的最大值
- 搜索基于索引进行,必须存在匹配的索引
该功能实现了比sql的between查询更高效强大的范围搜索
func (*Table) SetIndexCacheSizeLimit ¶
SetIndexCacheSizeLimit 设置索引缓存大小限制
func (*Table) ToSchema ¶
func (t *Table) ToSchema() *TableSchema
ToSchema 将Table转换为TableSchema 用于序列化Table结构体的元数据
func (*Table) ToSerialization ¶
func (t *Table) ToSerialization() *TableSerialization
ToSerialization 将Table转换为TableSerialization 用于完整序列化Table结构体,包括运行时状态
type TableCache ¶ added in v1.9.3
type TableCache struct {
// contains filtered or unexported fields
}
缓存结构体
type TableIter ¶
type TableIter struct {
// contains filtered or unexported fields
}
func TableIterNew ¶
func (*TableIter) ExportRecord ¶
func (t *TableIter) ExportRecord(export ExportRecord, esc bool, limit ...int)
func (*TableIter) GetPrimaryKeys ¶
提取主键值 if len(fields) == 0 ,默认是提取主键值,主键也可以是组合主键 单主键则返回原始值,组合主键则返回拼接的字符串
func (*TableIter) GetRecords ¶
遍历迭代器返回解析后的记录,包含版本号字段。 用于系统内部调用,包含版本号字段。
func (*TableIter) JumpRange ¶
// 检测跳跃区间内是否包含key // 如果包含,返回跳跃区间的结束位置 // 如果不包含,返回nil //esc true 表示顺序,false 表示倒序 场景1: 电商系统 // 商品格式:product_类别_12345 // 跳跃区间:SkipStart = []byte("product_electronics_"), SkipLimit = append([]byte("product_electronics_"), 0) // 查询除电子产品外的所有商品 场景2: 金融系统 // 交易格式:transaction_20231201_12345 // 跳跃区间:SkipStart = []byte("transaction_20231201"), SkipLimit = []byte("transaction_20231202") // 查询除2023年12月1日外的所有交易
jumpRange.First()=nil或jumpRange.Last()=nil的情况,需要特殊处理todo...(也可能不存在这样的情况)
func (*TableIter) Map ¶
将某字段的所有值转换为map[any]bool if len(fields) == 0 ,默认是提取主键值,否则提取指定字段值 用于与其他迭代器进行匹配。
func (*TableIter) Match ¶
// 检测记录是否符合Match条件 // 如果符合,返回true // 如果不符合,返回false 常见sql场景,f in (1,2,3) 或 and 等操作
func (*TableIter) ParseBytes ¶
解析k,v里所有存在的字段byte值 主键得到整个记录值,其他索引得到主键ID值
func (*TableIter) ParseRecord ¶
func (*TableIter) RecordIndexTime ¶
跟踪索引耗时
func (*TableIter) ReleaseMap ¶
func (*TableIter) SetJumpRanges ¶
func (*TableIter) SetSelects ¶
sql语句中的select f0,f1,... from table 要返回的字段
type TableIterPool ¶
type TableIterPool struct {
// contains filtered or unexported fields
}
TableIterPool 是 TableIter 对象的池
var GlobalTableIterPool *TableIterPool
全局 TableIter 对象池实例
func NewTableIterPool ¶
func NewTableIterPool() *TableIterPool
NewTableIterPool 创建一个新的 TableIter 对象池
type TableSchema ¶
type TableSchema struct {
// 表ID
ID uint8 `json:"id"`
// 表名
Name string `json:"name"`
// 字段定义,string为字段名,any为字段值示例(用于类型推断)
Fields map[string]any `json:"fields"`
// 字段ID映射,uint8为字段ID,string为字段名
FieldsID map[uint8]string `json:"fields_id"`
// 索引信息,包含所有索引的基本信息
Indexes []IndexSchema `json:"indexes"`
// 时间字段映射,标记字段是否为时间类型
TimeFields map[string]bool `json:"time_fields"`
// 主键字段列表
PrimaryFields []string `json:"primary_fields"`
}
TableSchema 是Table结构体的映射,用于序列化和反序列化 包含Table的元数据,不包含运行时状态(如counter和kvStore)
func (*TableSchema) MarshalJSON ¶
func (ts *TableSchema) MarshalJSON() ([]byte, error)
MarshalJSON 将TableSchema序列化为JSON
func (*TableSchema) UnmarshalJSON ¶
func (ts *TableSchema) UnmarshalJSON(data []byte) error
UnmarshalJSON 从JSON反序列化TableSchema
type TableSerialization ¶
type TableSerialization struct {
// TableSchema 元数据
Schema *TableSchema `json:"schema"`
// 自动增值的当前值(counter AutoInt)
CurrentAutoID int `json:"current_auto_id"`
}
func (*TableSerialization) MarshalJSON ¶
func (ts *TableSerialization) MarshalJSON() ([]byte, error)
MarshalJSON 将TableSerialization序列化为JSON
func (*TableSerialization) UnmarshalJSON ¶
func (ts *TableSerialization) UnmarshalJSON(data []byte) error
UnmarshalJSON 从JSON反序列化TableSerialization
type Update ¶
type Update interface {
//检查参数
CheckParams() error
//准备更新操作的batch
PrepareBatch(batchs ...storage.Batch)
//读取记录
ReadRecord() error
// 准备更新字段列表
PrepareUpdateFields() error
// 获取更新字段的字节表示
GetFieldsBytes() error
//删除旧记录
DeleteOldRecord() error
//添加新记录
AddNewRecord() error
// 提交事务
Commit() error
}
提供了一个统一的更新流程接口
type UpdateImpl ¶
type UpdateImpl struct {
// contains filtered or unexported fields
}
func NewUpdateImpl ¶
func NewUpdateImpl(table *Table, fields *map[string]any) *UpdateImpl
NewUpdateImpl 创建一个新的 UpdateImpl 实例
func (*UpdateImpl) AddNewRecord ¶ added in v1.9.3
func (u *UpdateImpl) AddNewRecord()
func (*UpdateImpl) CheckParams ¶ added in v1.9.3
func (u *UpdateImpl) CheckParams() error
CheckParams 检查参数是否正确
func (*UpdateImpl) DeleteOldRecord ¶ added in v1.9.3
func (u *UpdateImpl) DeleteOldRecord()
DeleteOldRecord 删除旧记录
func (*UpdateImpl) GetFieldsBytes ¶ added in v1.9.3
func (u *UpdateImpl) GetFieldsBytes() error
GetFieldsBytes 获取更新字段的字节表示
func (*UpdateImpl) PrepareBatch ¶ added in v1.9.3
func (u *UpdateImpl) PrepareBatch(batchs ...storage.Batch)
PrepareBatch 准备更新操作的batch
func (*UpdateImpl) PrepareUpdateFields ¶
func (u *UpdateImpl) PrepareUpdateFields() error
PrepareUpdateFields 准备更新字段列表
func (*UpdateImpl) ReadRecord ¶ added in v1.9.3
func (u *UpdateImpl) ReadRecord() error
ReadRecord 读取记录
type UpdateImplPool ¶
type UpdateImplPool struct {
// contains filtered or unexported fields
}
UpdateImplPool 是 UpdateImpl 的对象池
func (*UpdateImplPool) Put ¶
func (p *UpdateImplPool) Put(impl *UpdateImpl)
Put 将 UpdateImpl 实例放回对象池
type ValueJoiner ¶
type ValueJoiner struct{}
ValueJoiner 用于实现系统ID键的生成和拼接 支持生成以下格式的键: 1. 对象键:sys-table-name, sys-tableid-idx-name, sys-tableid-field-name 2. 计数器键:sys-table, sys-tableid-idx, sys-tableid-field
func (*ValueJoiner) GenerateCounterKey ¶
func (vj *ValueJoiner) GenerateCounterKey(prefix, objType string) string
GenerateCounterKey 生成计数器键 示例:sys-type
func (*ValueJoiner) GenerateFieldCounterKey ¶
func (vj *ValueJoiner) GenerateFieldCounterKey(tableID uint8) string
GenerateFieldCounterKey 生成字段计数器键 示例:sys-tableid-field
func (*ValueJoiner) GenerateFieldKey ¶
func (vj *ValueJoiner) GenerateFieldKey(tableID uint8, fieldName string) string
GenerateFieldKey 生成字段对象键 示例:sys-tableid-field-fieldName
func (*ValueJoiner) GenerateIndexCounterKey ¶
func (vj *ValueJoiner) GenerateIndexCounterKey(tableID uint8) string
GenerateIndexCounterKey 生成索引计数器键 示例:sys-tableid-idx
func (*ValueJoiner) GenerateIndexKey ¶
func (vj *ValueJoiner) GenerateIndexKey(tableID uint8, indexName string) string
GenerateIndexKey 生成索引对象键 示例:sys-tableid-idx-indexName
func (*ValueJoiner) GenerateObjectKey ¶
func (vj *ValueJoiner) GenerateObjectKey(prefix, objType, name string) string
GenerateObjectKey 生成基本对象键 示例:sys-type-name
func (*ValueJoiner) GenerateTableCounterKey ¶
func (vj *ValueJoiner) GenerateTableCounterKey() string
GenerateTableCounterKey 生成表计数器键 示例:sys-table
func (*ValueJoiner) GenerateTableKey ¶
func (vj *ValueJoiner) GenerateTableKey(tableName string) string
GenerateTableKey 生成表对象键 示例:sys-table-tableName
func (*ValueJoiner) Join ¶
func (vj *ValueJoiner) Join(parts ...string) string
Join 实现基本的值拼接功能 根据输入的参数生成拼接后的字符串
Source Files
¶
- anyMapPool.go
- autoint.go
- batchcontainer.go
- delete.go
- error.go
- fieldsBytesPool.go
- idManager.go
- index.go
- indexs.go
- insert.go
- pools.go
- searchCache.go
- searchCacheLru.go
- table.go
- tableDelete.go
- tableImportExport.go
- tableIncrement.go
- tableInsert.go
- tableIterPool.go
- tableSearch.go
- tableUpdate.go
- tableindexs.go
- tableiter.go
- tableschema.go
- update.go
- valueJoiner.go