dao

package
v0.0.0-...-d13da26 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserDuplicate = errors.New("邮箱冲突")
	ErrUserNotFound  = gorm.ErrRecordNotFound
)
View Source
var ErrRecordNotFound = gorm.ErrRecordNotFound

Functions

func InitTable

func InitTable(db *gorm.DB) error

Types

type Collection

type Collection struct {
	Id   int64  `gorm:"primaryKey,autoIncrement"`
	Name string `gorm:"type=varchar(1024)"`
	Uid  int64  `gorm:""`

	Ctime int64
	Utime int64
}

Collection 收藏夹

type CollectionItem

type CollectionItem struct {
	Cid   int64
	Cname string
	BizId int64
	Biz   string
}

type DBProvider

type DBProvider func() *gorm.DB

type GORMInteractiveDAO

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

func (*GORMInteractiveDAO) BatchIncrReadCnt

func (dao *GORMInteractiveDAO) BatchIncrReadCnt(ctx context.Context, bizs []string, ids []int64) error

biz = a, bizid = 1 biz = a, bizid = 1 biz = a, bizid = 1 biz = a bizId = 2 biz = a bizId = 2 biz = a bizId = 2 biz = a bizId = 2

func (*GORMInteractiveDAO) DeleteLikeInfo

func (dao *GORMInteractiveDAO) DeleteLikeInfo(ctx context.Context, biz string, bizId, uid int64) error

func (*GORMInteractiveDAO) Get

func (dao *GORMInteractiveDAO) Get(ctx context.Context, biz string, bizId int64) (Interactive, error)

func (*GORMInteractiveDAO) GetCollectionInfo

func (dao *GORMInteractiveDAO) GetCollectionInfo(ctx context.Context, biz string, bizId, uid int64) (UserCollectionBiz, error)

func (*GORMInteractiveDAO) GetItems

func (dao *GORMInteractiveDAO) GetItems() ([]CollectionItem, error)

func (*GORMInteractiveDAO) GetLikeInfo

func (dao *GORMInteractiveDAO) GetLikeInfo(ctx context.Context, biz string, bizId, uid int64) (UserLikeBiz, error)

func (*GORMInteractiveDAO) IncrReadCnt

func (dao *GORMInteractiveDAO) IncrReadCnt(ctx context.Context,
	biz string, bizId int64) error

IncrReadCnt 是一个插入或者更新语义

func (*GORMInteractiveDAO) InsertCollectionBiz

func (dao *GORMInteractiveDAO) InsertCollectionBiz(ctx context.Context,
	cb UserCollectionBiz) error

InsertCollectionBiz 插入收藏记录,并且更新计数

func (*GORMInteractiveDAO) InsertLikeInfo

func (dao *GORMInteractiveDAO) InsertLikeInfo(ctx context.Context,
	biz string, bizId, uid int64) error

type GORMJobDAO

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

func (*GORMJobDAO) Preempt

func (g *GORMJobDAO) Preempt(ctx context.Context, refreshInterval time.Duration) (Job, error)

func (*GORMJobDAO) Release

func (g *GORMJobDAO) Release(ctx context.Context, id int64) error

func (*GORMJobDAO) Stop

func (g *GORMJobDAO) Stop(ctx context.Context, id int64) error

func (*GORMJobDAO) UpdateNextTime

func (g *GORMJobDAO) UpdateNextTime(ctx context.Context, id int64, next time.Time) error

func (*GORMJobDAO) UpdateUtime

func (g *GORMJobDAO) UpdateUtime(ctx context.Context, id int64) error

type GORMUserDAO

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

func (*GORMUserDAO) FindByEmail

func (dao *GORMUserDAO) FindByEmail(ctx context.Context, email string) (User, error)

func (*GORMUserDAO) FindById

func (dao *GORMUserDAO) FindById(ctx context.Context, id int64) (User, error)

func (*GORMUserDAO) FindByPhone

func (dao *GORMUserDAO) FindByPhone(ctx context.Context, phone string) (User, error)

func (*GORMUserDAO) FindByWechat

func (dao *GORMUserDAO) FindByWechat(ctx context.Context, openID string) (User, error)

func (*GORMUserDAO) Insert

func (dao *GORMUserDAO) Insert(ctx context.Context, u User) error

type Interactive

type Interactive struct {
	Id int64 `gorm:"primaryKey,autoIncrement"`
	// 业务标识符
	// 同一个资源,在这里应该只有一行
	// 也就是说我要在 bizId 和 biz 上创建联合唯一索引
	// 1. bizId, biz。优先选择这个,因为 bizId 的区分度更高
	// 2. biz, bizId。如果有 WHERE biz = xx 这种查询条件(不带 bizId)的,就只能这种
	//
	// 联合索引的列的顺序:查询条件,区分度
	// 这个名字无所谓
	BizId int64 `gorm:"uniqueIndex:biz_id_type"`
	// 我这里biz 用的是 string,有些公司枚举使用的是 int 类型
	// 0-article
	// 1- xxx
	// 默认是 BLOB/TEXT 类型
	Biz string `gorm:"uniqueIndex:biz_id_type;type:varchar(128)"`
	// 这个是阅读计数
	ReadCnt    int64
	LikeCnt    int64
	CollectCnt int64
	Ctime      int64
	Utime      int64
}

Interactive 正常来说,一张主表和与它有关联关系的表会共用一个DAO, 所以我们就用一个 DAO 来操作 假如说我要查找点赞数量前 100 的, SELECT * FROM (SELECT biz, biz_id, COUNT(*) as cnt FROM `interactives` GROUP BY biz, biz_id) ORDER BY cnt LIMIT 100 实时查找,性能贼差,上面这个语句,就是全表扫描, 高性能,我不要求准确性 面试标准答案:用 zset 但是,面试标准答案不够有特色,烂大街了 你可以考虑别的方案 1. 定时计算 1.1 定时计算 + 本地缓存 2. 优化版的 zset,定时筛选 + 实时 zset 计算 还要别的方案你们也可以考虑

type InteractiveDAO

type InteractiveDAO interface {
	IncrReadCnt(ctx context.Context, biz string, bizId int64) error
	InsertLikeInfo(ctx context.Context, biz string, bizId, uid int64) error
	GetLikeInfo(ctx context.Context, biz string, bizId, uid int64) (UserLikeBiz, error)
	DeleteLikeInfo(ctx context.Context, biz string, bizId, uid int64) error
	Get(ctx context.Context, biz string, bizId int64) (Interactive, error)
	InsertCollectionBiz(ctx context.Context, cb UserCollectionBiz) error
	GetCollectionInfo(ctx context.Context, biz string, bizId, uid int64) (UserCollectionBiz, error)
	BatchIncrReadCnt(ctx context.Context, bizs []string, ids []int64) error
}

func NewGORMInteractiveDAO

func NewGORMInteractiveDAO(db *gorm.DB) InteractiveDAO

type InteractiveV1

type InteractiveV1 struct {
	Id    int64 `gorm:"primaryKey,autoIncrement"`
	BizId int64
	Biz   string
	// 这个是阅读计数
	Cnt int64
	// 阅读数/点赞数/收藏数
	CntType string
	Ctime   int64
	Utime   int64
}

InteractiveV1 对写更友好 Interactive 对读更加友好

type Job

type Job struct {
	Id       int64 `gorm:"primaryKey,autoIncrement"`
	Cfg      string
	Executor string
	Name     string `gorm:"unique"`

	// 第一个问题:哪些任务可以抢?哪些任务已经被人占着?哪些任务永远不会被运行
	// 用状态来标记
	Status int

	// 另外一个问题,定时任务,我怎么知道,已经到时间了呢?
	// NextTime 下一次被调度的时间
	// next_time <= now 这样一个查询条件
	// and status = 0
	// 要建立索引
	// 更加好的应该是 next_time 和 status 的联合索引
	NextTime int64 `gorm:"index"`
	// cron 表达式
	Cron string

	Version int

	// 创建时间,毫秒数
	Ctime int64
	// 更新时间,毫秒数
	Utime int64
}

type JobDAO

type JobDAO interface {
	Preempt(ctx context.Context, refreshInterval time.Duration) (Job, error)
	Release(ctx context.Context, id int64) error
	UpdateUtime(ctx context.Context, id int64) error
	UpdateNextTime(ctx context.Context, id int64, next time.Time) error
	Stop(ctx context.Context, id int64) error
}

type User

type User struct {
	Id int64 `gorm:"primaryKey,autoIncrement"`
	// 全部用户唯一
	Email    sql.NullString `gorm:"unique"`
	Password string
	Nickname string

	// 唯一索引允许有多个空值
	// 但是不能有多个 ""
	Phone sql.NullString `gorm:"unique"`

	// 如果要创建联合索引,<unionid, openid>,用 openid 查询的时候不会走索引
	// <openid, unionid> 用 unionid 查询的时候,不会走索引
	// 微信的字段
	WechatUnionID sql.NullString `gorm:"type=varchar(1024)"`
	WechatOpenID  sql.NullString `gorm:"type=varchar(1024);unique"`

	// 创建时间,毫秒数
	Ctime int64
	// 更新时间,毫秒数
	Utime int64
}

User 直接对应数据库表结构 有些人叫做 entity,有些人叫做 model,有些人叫做 PO(persistent object)

type UserCollectionBiz

type UserCollectionBiz struct {
	Id int64 `gorm:"primaryKey,autoIncrement"`
	// 收藏夹 ID
	// 作为关联关系中的外键,我们这里需要索引
	Cid   int64  `gorm:"index"`
	BizId int64  `gorm:"uniqueIndex:biz_type_id_uid"`
	Biz   string `gorm:"type:varchar(128);uniqueIndex:biz_type_id_uid"`
	// 这算是一个冗余,因为正常来说,
	// 只需要在 Collection 中维持住 Uid 就可以
	Uid   int64 `gorm:"uniqueIndex:biz_type_id_uid"`
	Ctime int64
	Utime int64
}

UserCollectionBiz 收藏的东西

type UserDAO

type UserDAO interface {
	FindByEmail(ctx context.Context, email string) (User, error)
	FindById(ctx context.Context, id int64) (User, error)
	FindByPhone(ctx context.Context, phone string) (User, error)
	Insert(ctx context.Context, u User) error
	FindByWechat(ctx context.Context, openID string) (User, error)
}

func NewUserDAO

func NewUserDAO(db *gorm.DB) UserDAO

func NewUserDAOV1

func NewUserDAOV1(p DBProvider) UserDAO

type UserLikeBiz

type UserLikeBiz struct {
	Id int64 `gorm:"primaryKey,autoIncrement"`

	// 要分场景
	// 1. 如果你的场景是,用户要看看自己点赞过那些,那么 Uid 在前
	// WHERE uid =?
	// 2. 如果你的场景是,我的点赞数量,需要通过这里来比较/纠正
	// biz_id 和 biz 在前
	// select count(*) where biz = ? and biz_id = ?
	Biz   string `gorm:"uniqueIndex:uid_biz_id_type;type:varchar(128)"`
	BizId int64  `gorm:"uniqueIndex:uid_biz_id_type"`

	// 谁的操作
	Uid int64 `gorm:"uniqueIndex:uid_biz_id_type"`

	Ctime int64
	Utime int64
	// 如果这样设计,那么,取消点赞的时候,怎么办?
	// 我删了这个数据
	// 你就软删除
	// 这个状态是存储状态,纯纯用于软删除的,业务层面上没有感知
	// 0-代表删除,1 代表有效
	Status uint8
}

UserLikeBiz 命名无能,用户点赞的某个东西

Directories

Path Synopsis
Package daomocks is a generated GoMock package.
Package daomocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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