table

package
v1.65.2 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2021 License: BSD-3-Clause Imports: 11 Imported by: 62

Documentation

Overview

Package table 实现一个基于kv的关系型数据库的表格功能

Index

Constants

View Source
const (
	None = iota
	Add
	Update
	Del
)

表关联设计 指出是 添加 还是 删除 行 primary key auto 的del 需要指定 primary key

Variables

View Source
var (
	ErrEmptyPrimaryKey        = errors.New("ErrEmptyPrimaryKey")
	ErrPrimaryKey             = errors.New("ErrPrimaryKey")
	ErrIndexKey               = errors.New("ErrIndexKey")
	ErrTooManyIndex           = errors.New("ErrTooManyIndex")
	ErrTablePrefixOrTableName = errors.New("ErrTablePrefixOrTableName")
	ErrDupPrimaryKey          = errors.New("ErrDupPrimaryKey")
	ErrNilValue               = errors.New("ErrNilValue")
)

table 中的错误处理

Functions

func DecodeRow

func DecodeRow(data []byte) ([]byte, []byte, error)

DecodeRow from data

func JoinKey

func JoinKey(leftvalue, rightvalue []byte) []byte

JoinKey 两个left 和 right key 合并成一个key

Types

type Count

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

Count 计数器

func NewCount

func NewCount(prefix string, name string, kvdb db.KV) *Count

NewCount 创建一个计数器

func (*Count) Dec

func (c *Count) Dec() (num int64, err error)

Dec 减少1

func (*Count) Get

func (c *Count) Get() (int64, error)

Get count

func (*Count) Inc

func (c *Count) Inc() (num int64, err error)

Inc 增加1

func (*Count) Save

func (c *Count) Save() (kvs []*types.KeyValue, err error)

Save 保存kv

func (*Count) Set

func (c *Count) Set(i int64)

Set 这个操作要谨慎使用

type JoinData

type JoinData struct {
	Left  types.Message
	Right types.Message
}

JoinData 由left 和 right 两个数据组成

func (*JoinData) ProtoMessage

func (msg *JoinData) ProtoMessage()

ProtoMessage data

func (*JoinData) Reset

func (msg *JoinData) Reset()

Reset data

func (*JoinData) String

func (msg *JoinData) String() string

String string

type JoinMeta

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

JoinMeta left right 合成的一个meta 结构

func (*JoinMeta) CreateRow

func (tx *JoinMeta) CreateRow() *Row

CreateRow create a meta struct

func (*JoinMeta) Get

func (tx *JoinMeta) Get(key string) ([]byte, error)

Get 按照indexName 查询 indexValue

func (*JoinMeta) SetPayload

func (tx *JoinMeta) SetPayload(data types.Message) error

SetPayload 设置数据

type JoinTable

type JoinTable struct {
	*Table
	Fk string
	// contains filtered or unexported fields
}

JoinTable 是由两个表格组合成的一个表格,自动维护一个联合结构 其中主表: LeftTable 连接表: RightTable

func NewJoinTable

func NewJoinTable(left *Table, right *Table, indexes []string) (*JoinTable, error)

NewJoinTable 新建一个JoinTable

func (*JoinTable) GetData

func (join *JoinTable) GetData(primaryKey []byte) (*Row, error)

GetData rewrite get data of jointable

func (*JoinTable) GetLeft

func (join *JoinTable) GetLeft() *Table

GetLeft get left table

func (*JoinTable) GetRight

func (join *JoinTable) GetRight() *Table

GetRight get right table

func (*JoinTable) GetTable

func (join *JoinTable) GetTable(name string) (*Table, error)

GetTable get table by name

func (*JoinTable) ListIndex

func (join *JoinTable) ListIndex(indexName string, prefix []byte, primaryKey []byte, count, direction int32) (rows []*Row, err error)

ListIndex 查询jointable 数据

func (*JoinTable) MustGetTable

func (join *JoinTable) MustGetTable(name string) *Table

MustGetTable if name not exist, panic

func (*JoinTable) Save

func (join *JoinTable) Save() (kvs []*types.KeyValue, err error)

Save 重写默认的save 函数,不仅仅 Save left,right table 还要save jointable 没有update 到情况,只有del, add, 性能考虑可以加上 update 的情况 目前update 是通过 del + add 完成 left modify: del index, add new index (query right by primary) (check in cache) right modify: query all primary in left, include in cache, del index, add new index TODO: 没有修改过的数据不需要修改

type Option

type Option struct {
	Prefix  string
	Name    string
	Primary string
	Join    bool
	Index   []string
}

Option table 的选项

type Query

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

Query 列表查询结构

func (*Query) List

func (query *Query) List(indexName string, data types.Message, primaryKey []byte, count, direction int32) (rows []*Row, err error)

List 通过某个数据,查询

func (*Query) ListIndex

func (query *Query) ListIndex(indexName string, prefix []byte, primaryKey []byte, count, direction int32) (rows []*Row, err error)

ListIndex 根据索引查询列表 index 用哪个index prefix 必须要符合的前缀, 可以为空 primaryKey 开始查询的位置(不包含数据本身) count 最多取的数量 direction 方向

func (*Query) ListOne

func (query *Query) ListOne(indexName string, data types.Message, primaryKey []byte) (row *Row, err error)

ListOne 通过某个数据,查询一行

type Row

type Row struct {
	Ty      int
	Primary []byte
	Data    types.Message
	// contains filtered or unexported fields
}

Row 行操作

func (*Row) Encode

func (row *Row) Encode() ([]byte, error)

Encode row

type RowMeta

type RowMeta interface {
	CreateRow() *Row
	SetPayload(types.Message) error
	Get(key string) ([]byte, error)
}

RowMeta 定义行的操作

type Table

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

Table 定一个表格, 并且添加 primary key, index key

func NewTable

func NewTable(rowmeta RowMeta, kvdb db.KV, opt *Option) (*Table, error)

NewTable 新建一个表格 primary 可以为: auto, 由系统自动创建 index 可以为nil

func (*Table) Add

func (table *Table) Add(data types.Message) error

Add 在表格中添加一行

func (*Table) Del

func (table *Table) Del(primaryKey []byte) error

Del 在表格中删除一行(包括删除索引)

func (*Table) DelRow

func (table *Table) DelRow(data types.Message) error

DelRow 删除一行

func (*Table) GetData

func (table *Table) GetData(primaryKey []byte) (*Row, error)

GetData 根据主键获取数据

func (*Table) GetMeta

func (table *Table) GetMeta() RowMeta

GetMeta 获取meta

func (*Table) GetQuery

func (table *Table) GetQuery(kvdb db.KVDB) *Query

GetQuery 获取查询结构(允许传入 kvdb 为nil)

func (*Table) ListIndex

func (table *Table) ListIndex(indexName string, prefix []byte, primaryKey []byte, count, direction int32) (rows []*Row, err error)

ListIndex list table index

func (*Table) Replace

func (table *Table) Replace(data types.Message) error

Replace 如果有重复的,那么替换

func (*Table) Save

func (table *Table) Save() (kvs []*types.KeyValue, err error)

Save 保存表格

func (*Table) Update

func (table *Table) Update(primaryKey []byte, newdata types.Message) (err error)

Update 更新数据库

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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