Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"databus_test.go",
"memcache_test.go",
"mysql_test.go",
"redis_test.go",
"search_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/service/main/favorite/conf:go_default_library",
"//app/service/main/favorite/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"databus.go",
"memcache.go",
"mysql.go",
"redis.go",
"search.go",
],
importpath = "go-common/app/service/main/favorite/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/favorite/conf:go_default_library",
"//app/service/main/favorite/model:go_default_library",
"//app/service/main/riot-search/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//library/queue/databus:go_default_library",
"//library/time:go_default_library",
"//library/xstr:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,83 @@
package dao
import (
"context"
"time"
"go-common/app/service/main/favorite/conf"
"go-common/library/cache/memcache"
"go-common/library/cache/redis"
"go-common/library/database/sql"
httpx "go-common/library/net/http/blademaster"
"go-common/library/queue/databus"
)
// Dao favorite dao.
type Dao struct {
db *sql.DB
dbRead *sql.DB
dbPush *sql.DB
mc *memcache.Pool
redis *redis.Pool
redisExpire int
mcExpire int32
jobDatabus *databus.Databus
httpClient *httpx.Client
}
// New a dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// db
db: sql.NewMySQL(c.MySQL.Fav),
dbRead: sql.NewMySQL(c.MySQL.Read),
dbPush: sql.NewMySQL(c.MySQL.Push),
// redis
redis: redis.NewPool(c.Redis.Config),
redisExpire: int(time.Duration(c.Redis.Expire) / time.Second),
// memcache
mc: memcache.NewPool(c.Memcache.Config),
mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
// databus
jobDatabus: databus.New(c.JobDatabus),
// httpclient
httpClient: httpx.NewClient(c.HTTPClient),
}
return
}
// Close close all connection.
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
if d.dbRead != nil {
d.dbRead.Close()
}
if d.redis != nil {
d.redis.Close()
}
if d.mc != nil {
d.mc.Close()
}
if d.jobDatabus != nil {
d.jobDatabus.Close()
}
}
// BeginTran crate a *sql.Tx for database transaction.
func (d *Dao) BeginTran(c context.Context) (*sql.Tx, error) {
return d.db.Begin(c)
}
// Ping check connection used in dao
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.pingRedis(c); err != nil {
return
}
if err = d.pingMC(c); err != nil {
return
}
err = d.pingMySQL(c)
return
}

View File

@@ -0,0 +1,36 @@
package dao
import (
"flag"
"go-common/app/service/main/favorite/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.community.favorite-service")
flag.Set("conf_token", "b11f25cfa8ff8d5ec5240dd98d283e10")
flag.Set("tree_id", "25111")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../cmd/favorite-service-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,197 @@
package dao
import (
"context"
"strconv"
"go-common/app/service/main/favorite/model"
)
func (d *Dao) send(c context.Context, mid int64, msg *model.Message) error {
key := strconv.FormatInt(mid, 10)
return d.jobDatabus.Send(c, key, msg)
}
// PubAddFav push the add resource event into databus.
func (d *Dao) PubSortFavs(c context.Context, tp int8, mid, fid int64, sorts []model.SortFav) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionSortFavs,
Type: tp,
Mid: mid,
Fid: fid,
SortFavs: sorts,
}
d.send(c, mid, msg)
}
// PubAddFav push the add resource event into databus.
func (d *Dao) PubAddFav(c context.Context, tp int8, mid, fid, oid int64, attr int32, ts int64, otype int8) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionAdd,
Type: tp,
Mid: mid,
Fid: fid,
Oid: oid,
FolderAttr: attr,
FTime: ts,
Otype: otype,
}
d.send(c, mid, msg)
}
// PubDelFav push the delete favorite event into databus.
func (d *Dao) PubDelFav(c context.Context, tp int8, mid, fid, oid int64, attr int32, ts int64, otype int8) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionDel,
Type: tp,
Mid: mid,
Fid: fid,
Oid: oid,
FolderAttr: attr,
FTime: ts,
Otype: otype,
}
d.send(c, mid, msg)
}
// PubInitRelationFids push the relationfids cache event into databus.
func (d *Dao) PubInitRelationFids(c context.Context, tp int8, mid int64) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionInitRelationFids,
Type: tp,
Mid: mid,
}
d.send(c, mid, msg)
}
// PubInitFolderRelations push the folder relations cache event into databus.
func (d *Dao) PubInitFolderRelations(c context.Context, tp int8, mid, fid int64) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionInitFolderRelations,
Type: tp,
Mid: mid,
Fid: fid,
}
d.send(c, mid, msg)
}
// PubInitAllFolderRelations push the folder relations cache event into databus.
func (d *Dao) PubInitAllFolderRelations(c context.Context, tp int8, mid, fid int64) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionInitAllFolderRelations,
Type: tp,
Mid: mid,
Fid: fid,
}
d.send(c, mid, msg)
}
// PubAddFolder push the add folder action event into databus.
func (d *Dao) PubAddFolder(c context.Context, typ int8, mid, fid int64, attr int32) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionFolderAdd,
Type: typ,
Mid: mid,
Fid: fid,
FolderAttr: attr,
}
d.send(c, mid, msg)
}
// PubDelFolder push the del folder action event into databus.
func (d *Dao) PubDelFolder(c context.Context, typ int8, mid, fid int64, attr int32, ts int64) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionFolderDel,
Type: typ,
Mid: mid,
Fid: fid,
FolderAttr: attr,
FTime: ts,
}
d.send(c, mid, msg)
}
// PubMultiDelFavs push the multi del fav relations event into databus.
func (d *Dao) PubMultiDelFavs(c context.Context, typ int8, mid, fid, rows int64, attr int32, oids []int64, ts int64) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionMultiDel,
Type: typ,
Mid: mid,
Fid: fid,
Affected: rows,
FolderAttr: attr,
Oids: oids,
FTime: ts,
}
d.send(c, mid, msg)
}
// PubMultiAddFavs push the multi add fav relations event into databus.
func (d *Dao) PubMultiAddFavs(c context.Context, typ int8, mid, fid, rows int64, attr int32, oids []int64, ts int64) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionMultiAdd,
Type: typ,
Mid: mid,
Fid: fid,
Affected: rows,
FolderAttr: attr,
Oids: oids,
FTime: ts,
}
d.send(c, mid, msg)
}
// PubMoveFavs push the move resources event into databus.
func (d *Dao) PubMoveFavs(c context.Context, typ int8, mid, ofid, nfid, rows int64, oids []int64, ts int64) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionMove,
Type: typ,
Mid: mid,
OldFid: ofid,
NewFid: nfid,
Affected: rows,
Oids: oids,
FTime: ts,
}
d.send(c, mid, msg)
}
// PubCopyFavs push the copy resources event into databus.
func (d *Dao) PubCopyFavs(c context.Context, typ int8, mid, ofid, nfid, rows int64, oids []int64, ts int64) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionCopy,
Type: typ,
Mid: mid,
OldFid: ofid,
NewFid: nfid,
Affected: rows,
Oids: oids,
FTime: ts,
}
d.send(c, mid, msg)
}
// PubClean push the clean video event into databus.
func (d *Dao) PubClean(c context.Context, typ int8, mid, fid, ftime int64) {
msg := &model.Message{
Field: model.FieldResource,
Action: model.ActionClean,
Type: typ,
Mid: mid,
Fid: fid,
FTime: ftime,
}
d.send(c, mid, msg)
}

View File

@@ -0,0 +1,187 @@
package dao
import (
"context"
"go-common/app/service/main/favorite/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestFavsend(t *testing.T) {
var (
c = context.TODO()
mid = int64(0)
msg = &model.Message{}
)
convey.Convey("send", t, func(ctx convey.C) {
err := d.send(c, mid, msg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavPubAddFav(t *testing.T) {
var (
c = context.TODO()
tp = int8(0)
mid = int64(0)
fid = int64(0)
oid = int64(0)
attr = int32(0)
ts = int64(0)
)
convey.Convey("PubAddFav", t, func(ctx convey.C) {
d.PubAddFav(c, tp, mid, fid, oid, attr, ts, tp)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
}
func TestFavPubDelFav(t *testing.T) {
var (
c = context.TODO()
tp = int8(0)
mid = int64(0)
fid = int64(0)
oid = int64(0)
attr = int32(0)
ts = int64(0)
)
convey.Convey("PubDelFav", t, func(ctx convey.C) {
d.PubDelFav(c, tp, mid, fid, oid, attr, ts, tp)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
}
func TestFavPubInitRelationFids(t *testing.T) {
var (
c = context.TODO()
tp = int8(0)
mid = int64(0)
)
convey.Convey("PubInitRelationFids", t, func(ctx convey.C) {
d.PubInitRelationFids(c, tp, mid)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
}
func TestFavPubInitFolderRelations(t *testing.T) {
var (
c = context.TODO()
tp = int8(0)
mid = int64(0)
fid = int64(0)
)
convey.Convey("PubInitFolderRelations", t, func(ctx convey.C) {
d.PubInitFolderRelations(c, tp, mid, fid)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
}
func TestFavPubAddFolder(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
fid = int64(0)
attr = int32(0)
)
convey.Convey("PubAddFolder", t, func(ctx convey.C) {
d.PubAddFolder(c, typ, mid, fid, attr)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
}
func TestFavPubDelFolder(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
fid = int64(0)
attr = int32(0)
ts = int64(0)
)
convey.Convey("PubDelFolder", t, func(ctx convey.C) {
d.PubDelFolder(c, typ, mid, fid, attr, ts)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
}
func TestFavPubMultiDelFavs(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
fid = int64(0)
rows = int64(0)
attr = int32(0)
oids = []int64{}
ts = int64(0)
)
convey.Convey("PubMultiDelFavs", t, func(ctx convey.C) {
d.PubMultiDelFavs(c, typ, mid, fid, rows, attr, oids, ts)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
}
func TestFavPubMultiAddFavs(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
fid = int64(0)
rows = int64(0)
attr = int32(0)
oids = []int64{}
ts = int64(0)
)
convey.Convey("PubMultiAddFavs", t, func(ctx convey.C) {
d.PubMultiAddFavs(c, typ, mid, fid, rows, attr, oids, ts)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
}
func TestFavPubMoveFavs(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
ofid = int64(0)
nfid = int64(0)
rows = int64(0)
oids = []int64{}
ts = int64(0)
)
convey.Convey("PubMoveFavs", t, func(ctx convey.C) {
d.PubMoveFavs(c, typ, mid, ofid, nfid, rows, oids, ts)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
}
func TestFavPubCopyFavs(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
ofid = int64(0)
nfid = int64(0)
rows = int64(0)
oids = []int64{}
ts = int64(0)
)
convey.Convey("PubCopyFavs", t, func(ctx convey.C) {
d.PubCopyFavs(c, typ, mid, ofid, nfid, rows, oids, ts)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
}

View File

@@ -0,0 +1,584 @@
package dao
import (
"context"
"fmt"
"go-common/app/service/main/favorite/model"
"go-common/library/cache/memcache"
"go-common/library/log"
"go-common/library/xstr"
"strconv"
)
const (
_oldFolder = "f_%d_%d_%d" // key:f_{type}_{mid}_{fid},value:{*Fodler}.pb
_folder = "f_%d_%d" // key:f_{mid%100}_{fid},value:{*Fodler}.pb
_folderStat = "ft_%d_%d" // key:f_{mid%100}_{fid},value:{*FodlerStat}.pb
_folderSort = "fst_%d_%d" // key:f_{type}_{mid},value:{fid,fid,fid...}.binary
_relationFids = "rof_%d_%d_%d" // key:rof_{type}_{mid}_{oid},value:{[]int64}.pb
_oidCount = "oc_%d_%d" // key:oc_{type}_{oid},value:int64
_batchOids = "bo_%d_%d" // key:oc_{type}_{mid},value:{[]int64}.pb
_recentOids = "rcto_%d_%d" // key:rcto_{type}_{mid},value:{[]int64}.pb
_recentRes = "rctr_%d_%d" // key:rcto_{type}_{mid},value:{[]*Resource}.pb
)
func folderMcKey(mid, fid int64) string {
return fmt.Sprintf(_folder, mid%100, fid)
}
func folderStatMcKey(mid, fid int64) string {
return fmt.Sprintf(_folderStat, mid%100, fid)
}
func fsortMcKey(typ int8, mid int64) string {
return fmt.Sprintf(_folderSort, typ, mid)
}
func relationFidsKey(typ int8, mid, oid int64) string {
return fmt.Sprintf(_relationFids, typ, mid, oid)
}
func oidCountKey(typ int8, oid int64) string {
return fmt.Sprintf(_oidCount, typ, oid)
}
func batchOidsKey(typ int8, mid int64) string {
return fmt.Sprintf(_batchOids, typ, mid)
}
func recentOidsKey(typ int8, mid int64) string {
return fmt.Sprintf(_recentOids, typ, mid)
}
func recentResKey(typ int8, mid int64) string {
return fmt.Sprintf(_recentRes, typ, mid)
}
// pingMC ping mc is ok.
func (d *Dao) pingMC(c context.Context) error {
conn := d.mc.Get(c)
defer conn.Close()
item := memcache.Item{Key: "ping", Value: []byte{1}, Expiration: d.mcExpire}
return conn.Set(&item)
}
// FolderStatsMc return folders stat by mid & fid from mc.
func (d *Dao) FolderStatsMc(c context.Context, fvmids []*model.ArgFVmid) (fs map[int64]*model.Folder, missFvmids []*model.ArgFVmid, err error) {
var (
keys = make([]string, 0, len(fvmids))
keysMap = make(map[string]*model.ArgFVmid, len(fvmids))
)
for _, v := range fvmids {
key := folderStatMcKey(v.Vmid, v.Fid)
keys = append(keys, key)
keysMap[key] = v
}
conn := d.mc.Get(c)
defer conn.Close()
items, err := conn.GetMulti(keys)
if err != nil {
log.Error("conn.GetMulti(%v) error(%v)", keys, err)
return
}
fs = make(map[int64]*model.Folder, len(items))
for _, item := range items {
stat := new(model.Folder)
if err = conn.Scan(item, stat); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
return
}
fs[keysMap[item.Key].MediaID()] = stat
delete(keysMap, item.Key)
}
for _, v := range keysMap {
missFvmids = append(missFvmids, v)
}
return
}
// SetFoldersMc add folders mc cache.
func (d *Dao) SetFoldersMc(c context.Context, vs ...*model.Folder) (err error) {
if len(vs) == 0 {
return
}
conn := d.mc.Get(c)
defer conn.Close()
for _, v := range vs {
if v == nil {
continue
}
item := &memcache.Item{Key: folderMcKey(v.Mid, v.ID), Object: v, Flags: memcache.FlagProtobuf, Expiration: d.mcExpire}
if err = conn.Set(item); err != nil {
log.Error("conn.Set(%s) error(%v)", folderMcKey(v.Mid, v.ID), err)
return
}
}
return
}
// SetFolderStatsMc add folders mc cache.
func (d *Dao) SetFolderStatsMc(c context.Context, stats map[int64]*model.Folder) (err error) {
if len(stats) == 0 {
return
}
conn := d.mc.Get(c)
defer conn.Close()
for k, v := range stats {
if v == nil {
continue
}
item := &memcache.Item{Key: folderStatMcKey(k%100, k/100), Object: v, Flags: memcache.FlagProtobuf, Expiration: d.mcExpire}
if err = conn.Set(item); err != nil {
log.Error("conn.Set(%s) error(%v)", folderStatMcKey(k%100, k/100), err)
return
}
}
return
}
// FoldersMc return folders by mid & fid from mc.
func (d *Dao) FoldersMc(c context.Context, fvmids []*model.ArgFVmid) (fs map[string]*model.Folder, missFvmids []*model.ArgFVmid, err error) {
var (
keys = make([]string, 0, len(fvmids))
keysMap = make(map[string]*model.ArgFVmid, len(fvmids))
)
for _, v := range fvmids {
key := folderMcKey(v.Vmid, v.Fid)
keys = append(keys, key)
keysMap[key] = v
}
conn := d.mc.Get(c)
defer conn.Close()
items, err := conn.GetMulti(keys)
if err != nil {
log.Error("conn.GetMulti(%v) error(%v)", keys, err)
return
}
fs = make(map[string]*model.Folder, len(items))
for _, item := range items {
folder := new(model.Folder)
if err = conn.Scan(item, folder); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
return
}
delete(keysMap, item.Key)
fvmidStr := xstr.JoinInts([]int64{folder.ID, folder.Mid})
fs[fvmidStr] = folder
}
for _, v := range keysMap {
missFvmids = append(missFvmids, v)
}
return
}
// FolderMc return folder pb from mc.
func (d *Dao) FolderMc(c context.Context, typ int8, mid, fid int64) (f *model.Folder, err error) {
var (
key = folderMcKey(mid, fid)
item *memcache.Item
conn = d.mc.Get(c)
)
defer conn.Close()
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("memcache.Get(%s) error(%v)", key, err)
}
return
}
f = new(model.Folder)
if err = conn.Scan(item, f); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
f = nil
}
return
}
// DelFolderMc delete folder mc cache.
func (d *Dao) DelFolderMc(c context.Context, typ int8, mid, fid int64) (err error) {
var (
key = folderMcKey(mid, fid)
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("conn.Delete(%s) error(%v)", key, err)
}
}
return
}
// SetFolderSortMc set folder's sort binary data to mc.
func (d *Dao) SetFolderSortMc(c context.Context, fst *model.FolderSort) (err error) {
key := fsortMcKey(fst.Type, fst.Mid)
conn := d.mc.Get(c)
defer conn.Close()
sortBin := fst.Index()
item := &memcache.Item{
Key: key,
Value: sortBin,
Expiration: d.mcExpire,
}
if err = conn.Set(item); err != nil {
log.Error("conn.Set(%s) error(%v)", key, err)
return
}
return
}
// FolderSortMc return folder sort binary from mc.
func (d *Dao) FolderSortMc(c context.Context, typ int8, mid int64) (fst *model.FolderSort, err error) {
var (
key = fsortMcKey(typ, mid)
item *memcache.Item
b []byte
conn = d.mc.Get(c)
)
defer conn.Close()
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("memcache.Get(%s) error(%v)", key, err)
}
return
}
if err = conn.Scan(item, &b); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
fst = nil
return
}
fst = &model.FolderSort{Type: typ, Mid: mid}
if err = fst.SetIndex(b); err != nil {
log.Error("fs.SetIndex(%v) error(%v)", b, err)
err = nil
fst = nil
}
return
}
// DelFolderSortMc delete folder's sort mc cache.
func (d *Dao) DelFolderSortMc(c context.Context, typ int8, mid int64) (err error) {
var (
key = fsortMcKey(typ, mid)
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("conn.Delete(%s) error(%v)", key, err)
}
}
return
}
// SetRelaitonFidsMc set fids binary data to mc.
func (d *Dao) SetRelaitonFidsMc(c context.Context, typ int8, mid, oid int64, fids []int64) (err error) {
key := relationFidsKey(typ, mid, oid)
conn := d.mc.Get(c)
defer conn.Close()
bytes := model.ToBytes(fids)
item := &memcache.Item{Key: key, Value: bytes, Flags: memcache.FlagRAW, Expiration: d.mcExpire}
if err = conn.Set(item); err != nil {
log.Error("conn.Set(%s) error(%v)", key, err)
return
}
return
}
// RelaitonFidsMc return fids from mc.
func (d *Dao) RelaitonFidsMc(c context.Context, typ int8, mid, oid int64) (fids []int64, err error) {
var (
key = relationFidsKey(typ, mid, oid)
item *memcache.Item
conn = d.mc.Get(c)
b []byte
)
defer conn.Close()
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("memcache.Get(%s) error(%v)", key, err)
}
return
}
if err = conn.Scan(item, &b); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
fids = nil
return
}
if fids, err = model.ToInt64s(b); err != nil {
log.Error("fs.SetIndex(%v) error(%v)", b, err)
err = nil
fids = nil
}
return
}
// DelRelationFidsMc delete oid's fid mc cache.
func (d *Dao) DelRelationFidsMc(c context.Context, typ int8, mid int64, oids ...int64) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
for _, oid := range oids {
key := relationFidsKey(typ, mid, oid)
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("conn.Delete(%s) error(%v)", key, err)
}
}
}
return
}
// SetOidCountMc set oid count to mc.
func (d *Dao) SetOidCountMc(c context.Context, typ int8, oid, count int64) (err error) {
var (
key = oidCountKey(typ, oid)
conn = d.mc.Get(c)
)
defer conn.Close()
bs := []byte(strconv.FormatInt(int64(count), 10))
item := &memcache.Item{Key: key, Value: bs, Flags: memcache.FlagRAW, Expiration: d.mcExpire}
if err = conn.Set(item); err != nil {
log.Error("conn.Set(%s) error(%v)", key, err)
return
}
return
}
// OidCountMc return oid count from mc.
func (d *Dao) OidCountMc(c context.Context, typ int8, oid int64) (count int64, err error) {
var (
key = oidCountKey(typ, oid)
item *memcache.Item
conn = d.mc.Get(c)
)
defer conn.Close()
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("memcache.Get(%s) error(%v)", key, err)
}
return
}
var v string
if err = conn.Scan(item, &v); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
return
}
count, err = strconv.ParseInt(v, 10, 64)
return
}
// OidsCountMc return oids's count from mc.
func (d *Dao) OidsCountMc(c context.Context, typ int8, oids []int64) (counts map[int64]int64, misOids []int64, err error) {
var (
keys = make([]string, 0, len(oids))
keysMap = make(map[string]int64, len(oids))
)
for _, oid := range oids {
key := oidCountKey(typ, oid)
keys = append(keys, key)
keysMap[key] = oid
}
conn := d.mc.Get(c)
defer conn.Close()
items, err := conn.GetMulti(keys)
if err != nil {
log.Error("conn.GetMulti(%v) error(%v)", keys, err)
return
}
counts = make(map[int64]int64, len(items))
for _, item := range items {
var v string
if err = conn.Scan(item, &v); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
return
}
var cnt int64
if cnt, err = strconv.ParseInt(v, 10, 64); err != nil {
log.Error("strconv.ParseInt(%s) error(%v)", v, err)
return
}
counts[keysMap[item.Key]] = cnt
delete(keysMap, item.Key)
}
for _, v := range keysMap {
misOids = append(misOids, v)
}
return
}
// SetOidsCountMc set oids count to mc.
func (d *Dao) SetOidsCountMc(c context.Context, typ int8, cnts map[int64]int64) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
for oid, cnt := range cnts {
bs := []byte(strconv.FormatInt(int64(cnt), 10))
item := &memcache.Item{Key: oidCountKey(typ, oid), Object: bs, Flags: memcache.FlagRAW, Expiration: d.mcExpire}
if err = conn.Set(item); err != nil {
log.Error("conn.Set(%s) error(%v)", oidCountKey(typ, oid), err)
return
}
}
return
}
// BatchOidsMc return oids from mc.
func (d *Dao) BatchOidsMc(c context.Context, typ int8, mid int64) (oids []int64, err error) {
var (
key = batchOidsKey(typ, mid)
item *memcache.Item
conn = d.mc.Get(c)
b []byte
)
defer conn.Close()
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("memcache.Get(%s) error(%v)", key, err)
}
return
}
if err = conn.Scan(item, &b); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
return
}
if oids, err = model.ToInt64s(b); err != nil {
log.Error("fs.SetIndex(%v) error(%v)", b, err)
}
return
}
// SetBatchOidsMc set oids binary data to mc.
func (d *Dao) SetBatchOidsMc(c context.Context, typ int8, mid int64, oids []int64) (err error) {
key := batchOidsKey(typ, mid)
conn := d.mc.Get(c)
defer conn.Close()
bytes := model.ToBytes(oids)
item := &memcache.Item{Key: key, Value: bytes, Flags: memcache.FlagRAW, Expiration: d.mcExpire}
if err = conn.Set(item); err != nil {
log.Error("conn.Set(%s) error(%v)", key, err)
return
}
return
}
// UserRecentOidsMc return oids from mc.
func (d *Dao) UserRecentOidsMc(c context.Context, typ int8, mid int64) (oids []int64, err error) {
var (
key = recentOidsKey(typ, mid)
item *memcache.Item
conn = d.mc.Get(c)
b []byte
)
defer conn.Close()
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("memcache.Get(%s) error(%v)", key, err)
}
return
}
if err = conn.Scan(item, &b); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
return
}
if oids, err = model.ToInt64s(b); err != nil {
log.Error("fs.SetIndex(%v) error(%v)", b, err)
}
return
}
// SetUserRecentOidsMc set oids binary data to mc.
func (d *Dao) SetUserRecentOidsMc(c context.Context, typ int8, mid int64, oids []int64) (err error) {
key := recentOidsKey(typ, mid)
conn := d.mc.Get(c)
defer conn.Close()
bytes := model.ToBytes(oids)
item := &memcache.Item{Key: key, Value: bytes, Flags: memcache.FlagRAW, Expiration: d.mcExpire}
if err = conn.Set(item); err != nil {
log.Error("conn.Set(%s) error(%v)", key, err)
return
}
return
}
// SetUserRecentOidsMc set oids binary data to mc.
func (d *Dao) SetUserRecentResourcesMc(c context.Context, typ int8, mid int64, recents []*model.Resource) (err error) {
key := recentResKey(typ, mid)
conn := d.mc.Get(c)
defer conn.Close()
item := &memcache.Item{Key: key, Object: recents, Flags: memcache.FlagJSON, Expiration: d.mcExpire}
if err = conn.Set(item); err != nil {
log.Error("conn.Set(%s) error(%v)", key, err)
return
}
return
}
// UserRecentOidsMc return oids from mc.
func (d *Dao) UserRecentResourcesMc(c context.Context, typ int8, mid int64) (recents []*model.Resource, err error) {
var (
key = recentResKey(typ, mid)
item *memcache.Item
conn = d.mc.Get(c)
)
defer conn.Close()
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("memcache.Get(%s) error(%v)", key, err)
}
return
}
recents = make([]*model.Resource, 0)
if err = conn.Scan(item, &recents); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
return
}
return
}
// DelRecentResMc delete recent oids mc cache.
func (d *Dao) DelRecentResMc(c context.Context, typ int8, mid int64) (err error) {
key := recentResKey(typ, mid)
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("conn.Delete(%s) error(%v)", key, err)
}
}
return
}
// DelRecentOidsMc delete recent oids mc cache.
func (d *Dao) DelRecentOidsMc(c context.Context, typ int8, mid int64) (err error) {
key := recentOidsKey(typ, mid)
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("conn.Delete(%s) error(%v)", key, err)
}
}
return
}

View File

@@ -0,0 +1,346 @@
package dao
import (
"context"
"testing"
"go-common/app/service/main/favorite/model"
"github.com/smartystreets/goconvey/convey"
)
func TestGETANDSetUserRecentResourcesMc(t *testing.T) {
var (
mid = int64(76)
typ = int8(2)
c = context.Background()
)
convey.Convey("folderMcKey", t, func(ctx convey.C) {
recents, err := d.UserRecentResourcesMc(c, typ, mid)
ctx.So(err, convey.ShouldBeNil)
err = d.SetUserRecentResourcesMc(c, typ, mid, recents)
ctx.So(err, convey.ShouldBeNil)
err = d.DelRecentResMc(c, typ, mid)
ctx.So(err, convey.ShouldBeNil)
})
}
func TestFavfolderMcKey(t *testing.T) {
var (
mid = int64(0)
fid = int64(0)
)
convey.Convey("folderMcKey", t, func(ctx convey.C) {
p1 := folderMcKey(mid, fid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavfsortMcKey(t *testing.T) {
var (
typ = int8(0)
mid = int64(0)
)
convey.Convey("fsortMcKey", t, func(ctx convey.C) {
p1 := fsortMcKey(typ, mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavrelationFidsKey(t *testing.T) {
var (
typ = int8(0)
mid = int64(0)
oid = int64(0)
)
convey.Convey("relationFidsKey", t, func(ctx convey.C) {
p1 := relationFidsKey(typ, mid, oid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavoidCountKey(t *testing.T) {
var (
typ = int8(0)
oid = int64(0)
)
convey.Convey("oidCountKey", t, func(ctx convey.C) {
p1 := oidCountKey(typ, oid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavbatchOidsKey(t *testing.T) {
var (
typ = int8(0)
mid = int64(0)
)
convey.Convey("batchOidsKey", t, func(ctx convey.C) {
p1 := batchOidsKey(typ, mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavpingMC(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("pingMC", t, func(ctx convey.C) {
err := d.pingMC(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavSetFoldersMc(t *testing.T) {
var (
c = context.TODO()
vs = &model.Folder{}
)
convey.Convey("SetFoldersMc", t, func(ctx convey.C) {
err := d.SetFoldersMc(c, vs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavFoldersMc(t *testing.T) {
var (
c = context.TODO()
fvmids = []*model.ArgFVmid{
&model.ArgFVmid{
Fid: 1,
Vmid: 88888894,
},
}
)
convey.Convey("FoldersMc", t, func(ctx convey.C) {
fs, missFvmids, err := d.FoldersMc(c, fvmids)
ctx.Convey("Then err should be nil.fs,missFvmids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(missFvmids, convey.ShouldNotBeNil)
ctx.So(fs, convey.ShouldNotBeNil)
})
})
}
func TestFavFolderMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
fid = int64(0)
)
convey.Convey("FolderMc", t, func(ctx convey.C) {
f, err := d.FolderMc(c, typ, mid, fid)
ctx.Convey("Then err should be nil.f should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(f, convey.ShouldNotBeNil)
})
})
}
func TestFavDelFolderMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
fid = int64(0)
)
convey.Convey("DelFolderMc", t, func(ctx convey.C) {
err := d.DelFolderMc(c, typ, mid, fid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavSetFolderSortMc(t *testing.T) {
var (
c = context.TODO()
fst = &model.FolderSort{}
)
convey.Convey("SetFolderSortMc", t, func(ctx convey.C) {
err := d.SetFolderSortMc(c, fst)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavFolderSortMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
)
convey.Convey("FolderSortMc", t, func(ctx convey.C) {
fst, err := d.FolderSortMc(c, typ, mid)
ctx.Convey("Then err should be nil.fst should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(fst, convey.ShouldNotBeNil)
})
})
}
func TestFavDelFolderSortMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
)
convey.Convey("DelFolderSortMc", t, func(ctx convey.C) {
err := d.DelFolderSortMc(c, typ, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavSetRelaitonFidsMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(1)
mid = int64(88888894)
oid = int64(1)
fids = []int64{1}
)
convey.Convey("SetRelaitonFidsMc", t, func(ctx convey.C) {
err := d.SetRelaitonFidsMc(c, typ, mid, oid, fids)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavRelaitonFidsMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(1)
mid = int64(88888894)
oid = int64(1)
)
convey.Convey("RelaitonFidsMc", t, func(ctx convey.C) {
fids, err := d.RelaitonFidsMc(c, typ, mid, oid)
ctx.Convey("Then err should be nil.fids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(fids, convey.ShouldNotBeNil)
})
})
}
func TestFavDelRelationFidsMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
oids = int64(0)
)
convey.Convey("DelRelationFidsMc", t, func(ctx convey.C) {
err := d.DelRelationFidsMc(c, typ, mid, oids)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavSetOidCountMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
oid = int64(0)
count = int64(0)
)
convey.Convey("SetOidCountMc", t, func(ctx convey.C) {
err := d.SetOidCountMc(c, typ, oid, count)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavOidCountMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
oid = int64(0)
)
convey.Convey("OidCountMc", t, func(ctx convey.C) {
count, err := d.OidCountMc(c, typ, oid)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
}
func TestFavOidsCountMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(1)
oids = []int64{1, 2, 3}
)
convey.Convey("OidsCountMc", t, func(ctx convey.C) {
counts, misOids, err := d.OidsCountMc(c, typ, oids)
ctx.Convey("Then err should be nil.counts,misOids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(misOids, convey.ShouldNotBeNil)
ctx.So(counts, convey.ShouldNotBeNil)
})
})
}
func TestFavSetOidsCountMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
cnts map[int64]int64
)
convey.Convey("SetOidsCountMc", t, func(ctx convey.C) {
err := d.SetOidsCountMc(c, typ, cnts)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavBatchOidsMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(1)
mid = int64(88888894)
)
convey.Convey("BatchOidsMc", t, func(ctx convey.C) {
_, err := d.BatchOidsMc(c, typ, mid)
ctx.Convey("Then err should be nil.oids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavSetBatchOidsMc(t *testing.T) {
var (
c = context.TODO()
typ = int8(1)
mid = int64(88888894)
oids = []int64{1, 2, 3}
)
convey.Convey("SetBatchOidsMc", t, func(ctx convey.C) {
err := d.SetBatchOidsMc(c, typ, mid, oids)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,700 @@
package dao
import (
"bytes"
"context"
"fmt"
"go-common/app/service/main/favorite/model"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_folderSharding int64 = 100 // folder by mid
_relationSharding int64 = 500 // objects in folder by mid
_usersSharding int64 = 500 // objects faved by oid
_countSharding int64 = 50 // objects count by oid
// folder table
_cntFolderSQL = "SELECT COUNT(id) FROM fav_folder_%s WHERE type=? AND mid=? AND state=0"
_folderSQL = "SELECT id,type,mid,name,cover,description,count,attr,state,ctime,mtime FROM fav_folder_%s WHERE id=? AND type=? AND mid=?"
_userFoldersSQL = "SELECT id,type,mid,name,cover,description,count,attr,state,ctime,mtime FROM fav_folder_%s WHERE type=? AND mid=? AND state=0"
_folderByNameSQL = "SELECT id,type,mid,name,cover,description,count,attr,state,ctime,mtime FROM fav_folder_%s WHERE name=? AND type=? AND mid=? AND state=0"
_folderByIdsSQL = "SELECT id,type,mid,name,cover,description,count,attr,state,ctime,mtime FROM fav_folder_%s WHERE id in (%s)"
_defFolderSQL = "SELECT id,type,mid,name,cover,description,count,attr,state,ctime,mtime FROM fav_folder_%s WHERE type=? AND mid=? AND attr&2=0"
_addFolderSQL = `INSERT INTO fav_folder_%s (type,mid,name,cover,description,count,attr,state,ctime,mtime) VALUES (?,?,?,?,?,?,?,?,?,?)
ON DUPLICATE KEY UPDATE name=?,cover=?,description=?,count=?,attr=?,state=?,ctime=?,mtime=?`
_delFolderSQL = "UPDATE fav_folder_%s SET state=1 WHERE type=? AND id=?"
_updateFolderSQL = "UPDATE fav_folder_%s SET name=?,description=?,cover=?,attr=?,state=?,mtime=? WHERE type=? AND id=?"
_upFolderNameSQL = "UPDATE IGNORE fav_folder_%s SET name=? WHERE id=?"
_upFolderAttrSQL = "UPDATE fav_folder_%s SET attr=? WHERE id=?"
// relation table
_relationSQL = "SELECT id,type,oid,mid,fid,state,ctime,mtime,sequence FROM fav_relation_%s WHERE type=? AND mid=? AND fid=? AND oid=? AND state=0"
_relationsSQL = "SELECT id,type,oid,mid,fid,state,ctime,mtime,sequence FROM fav_relation_%s FORCE INDEX(ix_fid_state_type_mtime) WHERE fid=? AND state=0 AND type=? ORDER BY mtime DESC LIMIT ?,?"
_allRelationsSQL = "SELECT id,type,oid,mid,fid,state,ctime,mtime,sequence FROM fav_relation_%s FORCE INDEX(ix_fid_state_sequence) WHERE fid=? AND state=0 ORDER BY sequence DESC LIMIT ?,?"
_relationFidsSQL = "SELECT fid FROM fav_relation_%s WHERE type=? AND mid=? AND oid=? AND state=0"
_FidsByOidsSQL = "SELECT oid,fid FROM fav_relation_%s WHERE type=? AND mid=? AND oid in (%s) AND state=0"
_cntRelationSQL = "SELECT COUNT(id) FROM fav_relation_%s FORCE INDEX(ix_fid_state_type_mtime) WHERE fid=? AND state=0 AND type=?"
_cntAllRelationSQL = "SELECT COUNT(id) FROM fav_relation_%s FORCE INDEX(ix_fid_state_sequence) WHERE fid=? AND state=0"
_addRelationSQL = "INSERT INTO fav_relation_%s (type,oid,mid,fid,state,ctime,mtime) VALUES(?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE state=?,mtime=?"
_maddRelationsSQL = "INSERT INTO fav_relation_%s (type,oid,mid,fid,state) VALUES %s ON DUPLICATE KEY UPDATE state=0"
_delRelationSQL = "UPDATE fav_relation_%s SET state=1 WHERE type=? AND fid=? AND oid=?"
_delRelationsSQL = "UPDATE fav_relation_%s SET state=1 WHERE type=? AND fid=? AND oid in (%s)"
_recentOidsSQL = "SELECT oid,type FROM fav_relation_%s FORCE INDEX(ix_fid_state_mtime) WHERE fid=? AND state=0 ORDER BY mtime DESC LIMIT 3"
_copyRelationsSQL = `INSERT IGNORE INTO fav_relation_%s (type,oid,mid,fid)
SELECT %d,oid,%d,%d FROM fav_relation_%s WHERE type=? AND mid=? AND fid=? AND oid in (%s) AND state=0
ON DUPLICATE KEY UPDATE state=0`
_batchOidsSQL = `SELECT oid FROM fav_relation_%s WHERE type=? AND mid=? AND state=0 LIMIT ?`
// users table
_cntUsersSQL = "SELECT COUNT(id) FROM fav_users_%s WHERE type=? AND oid=? AND state=0"
_usersSQL = "SELECT id,type,oid,mid,state,ctime,mtime FROM fav_users_%s WHERE type=? AND oid=? AND state=0 ORDER BY mtime DESC LIMIT ?,?"
// folderSort table
_folderSortSQL = "SELECT id,type,mid,sort,ctime,mtime FROM fav_folder_sort WHERE type=? AND mid=?"
_setFolderSortSQL = "INSERT INTO fav_folder_sort (type,mid,sort,ctime,mtime) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE sort=?,mtime=?"
// count table
_countSQL = "SELECT count FROM fav_count_%s WHERE type=? AND oid=?"
_countsSQL = "SELECT oid,count FROM fav_count_%s WHERE type=? AND oid in (%s)"
_folderStatSQL = "SELECT fid,play,fav,share from fav_folder_stat_%s WHERE fid in (%s)"
)
// folderHit hit table by the mod of mid and _folderSharding.
func folderHit(mid int64) string {
return fmt.Sprintf("%02d", mid%_folderSharding)
}
// relationHit hit table by the mod of mid and _relationSharding.
func relationHit(mid int64) string {
return fmt.Sprintf("%03d", mid%_relationSharding)
}
// usersHit hit table by the mod of oid and _userSharding.
func usersHit(oid int64) string {
return fmt.Sprintf("%03d", oid%_usersSharding)
}
// countHit hit table by the mod of oid and _countSharding.
func countHit(oid int64) string {
return fmt.Sprintf("%02d", oid%_countSharding)
}
// pingMySQL check mysql connection.
func (d *Dao) pingMySQL(c context.Context) error {
if err := d.dbRead.Ping(c); err != nil {
return err
}
if err := d.dbPush.Ping(c); err != nil {
return err
}
return d.db.Ping(c)
}
// Folder get a Folder by fid from mysql.
func (d *Dao) Folder(c context.Context, tp int8, mid, fid int64) (f *model.Folder, err error) {
f = new(model.Folder)
row := d.db.QueryRow(c, fmt.Sprintf(_folderSQL, folderHit(mid)), fid, tp, mid)
if err = row.Scan(&f.ID, &f.Type, &f.Mid, &f.Name, &f.Cover, &f.Description, &f.Count, &f.Attr, &f.State, &f.CTime, &f.MTime); err != nil {
if err == sql.ErrNoRows {
f = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// FolderByName get a Folder by name from mysql.
func (d *Dao) FolderByName(c context.Context, tp int8, mid int64, name string) (f *model.Folder, err error) {
f = &model.Folder{}
row := d.db.QueryRow(c, fmt.Sprintf(_folderByNameSQL, folderHit(mid)), name, tp, mid)
if err = row.Scan(&f.ID, &f.Type, &f.Mid, &f.Name, &f.Cover, &f.Description, &f.Count, &f.Attr, &f.State, &f.CTime, &f.MTime); err != nil {
if err == sql.ErrNoRows {
f = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// DefaultFolder get default folder from mysql.
func (d *Dao) DefaultFolder(c context.Context, tp int8, mid int64) (f *model.Folder, err error) {
f = new(model.Folder)
row := d.db.QueryRow(c, fmt.Sprintf(_defFolderSQL, folderHit(mid)), tp, mid)
if err = row.Scan(&f.ID, &f.Type, &f.Mid, &f.Name, &f.Cover, &f.Description, &f.Count, &f.Attr, &f.State, &f.CTime, &f.MTime); err != nil {
if err == sql.ErrNoRows {
f = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// AddFolder add a new favorite folder to mysql.
func (d *Dao) AddFolder(c context.Context, f *model.Folder) (fid int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_addFolderSQL, folderHit(f.Mid)), f.Type, f.Mid, f.Name, f.Cover, f.Description, f.Count, f.Attr, f.State, f.CTime, f.MTime,
f.Name, f.Cover, f.Description, f.Count, f.Attr, f.State, f.CTime, f.MTime)
if err != nil {
log.Error("db.Exec error(%v)", err)
return
}
return res.LastInsertId()
}
// UpdateFolder add a new favorite folder to mysql.
func (d *Dao) UpdateFolder(c context.Context, f *model.Folder) (fid int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_updateFolderSQL, folderHit(f.Mid)), f.Name, f.Description, f.Cover, f.Attr, f.State, f.MTime, f.Type, f.ID)
if err != nil {
log.Error("db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// UpFolderName rename user's folder name to mysql.
func (d *Dao) UpFolderName(c context.Context, typ int8, mid, fid int64, name string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_upFolderNameSQL, folderHit(mid)), name, fid)
if err != nil {
log.Error("db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// UpFolderAttr update user's folder attr to mysql.
func (d *Dao) UpFolderAttr(c context.Context, typ int8, mid, fid int64, attr int32) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_upFolderAttrSQL, folderHit(mid)), attr, fid)
if err != nil {
log.Error("db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// FolderRelations get favorite relations from mysql.
func (d *Dao) FolderRelations(c context.Context, typ int8, mid, fid int64, start, end int) (fr []*model.Favorite, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_relationsSQL, relationHit(mid)), fid, typ, start, end)
if err != nil {
log.Error("d.db.Query(%s,%d,%d,%d,%d,%d) error(%v)", fmt.Sprintf(_relationsSQL, relationHit(mid)), typ, mid, fid, start, end, err)
return
}
defer rows.Close()
fr = make([]*model.Favorite, 0)
for rows.Next() {
var r = &model.Favorite{}
if err = rows.Scan(&r.ID, &r.Type, &r.Oid, &r.Mid, &r.Fid, &r.State, &r.CTime, &r.MTime, &r.Sequence); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
if r.Type != typ || r.Mid != mid {
log.Error("dirty data relations(%d,%d,%d)", typ, mid, fid)
continue
}
fr = append(fr, r)
}
err = rows.Err()
return
}
// FolderAllRelations get favorite relations from mysql.
func (d *Dao) FolderAllRelations(c context.Context, mid, fid int64, start, end int) (fr []*model.Favorite, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_allRelationsSQL, relationHit(mid)), fid, start, end)
if err != nil {
log.Error("d.db.Query(%s,%d,%d,%d,%d,%d) error(%v)", fmt.Sprintf(_allRelationsSQL, relationHit(mid)), mid, fid, start, end, err)
return
}
defer rows.Close()
fr = make([]*model.Favorite, 0)
for rows.Next() {
var r = &model.Favorite{}
if err = rows.Scan(&r.ID, &r.Type, &r.Oid, &r.Mid, &r.Fid, &r.State, &r.CTime, &r.MTime, &r.Sequence); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
if r.Mid != mid {
log.Error("dirty data relations(%d,%d,%d)", mid, fid)
continue
}
fr = append(fr, r)
}
err = rows.Err()
return
}
// Folders get folders from mysql.
func (d *Dao) Folders(c context.Context, fvmids []*model.ArgFVmid) (fs map[string]*model.Folder, err error) {
tblMap := make(map[string][]int64, len(fvmids))
for _, fvmid := range fvmids {
folderHits := folderHit(fvmid.Vmid)
tblMap[folderHits] = append(tblMap[folderHits], fvmid.Fid)
}
fs = make(map[string]*model.Folder, len(fvmids))
for folderHit, fids := range tblMap {
fidsStr := xstr.JoinInts(fids)
var rows *sql.Rows
if rows, err = d.db.Query(c, fmt.Sprintf(_folderByIdsSQL, folderHit, fidsStr)); err != nil {
log.Error("d.db.Query(%s,%s,%s) error(%v)", _folderByIdsSQL, folderHit, fidsStr, err)
return
}
defer rows.Close()
for rows.Next() {
f := new(model.Folder)
if err = rows.Scan(&f.ID, &f.Type, &f.Mid, &f.Name, &f.Cover, &f.Description, &f.Count, &f.Attr, &f.State, &f.CTime, &f.MTime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
fmid := xstr.JoinInts([]int64{f.ID, f.Mid})
fs[fmid] = f
}
err = rows.Err()
if err != nil {
return
}
}
return
}
// RelationFidsByOid get favortied folders in relations by oid from mysql.
func (d *Dao) RelationFidsByOid(c context.Context, tp int8, mid, oid int64) (fids []int64, err error) {
var fid int64
rows, err := d.db.Query(c, fmt.Sprintf(_relationFidsSQL, relationHit(mid)), tp, mid, oid)
if err != nil {
log.Error("d.db.Query(%d,%d,%d) error(%v)", tp, mid, oid, err)
return
}
defer rows.Close()
for rows.Next() {
if err = rows.Scan(&fid); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
fids = append(fids, fid)
}
err = rows.Err()
return
}
// RelationFidsByOids get favortied folders in relations by oid from mysql.
func (d *Dao) RelationFidsByOids(c context.Context, tp int8, mid int64, oids []int64) (fidsMap map[int64][]int64, err error) {
oidsStr := xstr.JoinInts(oids)
rows, err := d.dbRead.Query(c, fmt.Sprintf(_FidsByOidsSQL, relationHit(mid), oidsStr), tp, mid)
if err != nil {
log.Error("d.db.Query(%d,%d,%d) error(%v)", mid, mid, oidsStr, err)
return
}
defer rows.Close()
var oid, fid int64
fidsMap = make(map[int64][]int64, len(oids))
for rows.Next() {
if err = rows.Scan(&oid, &fid); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
fidsMap[oid] = append(fidsMap[oid], fid)
}
err = rows.Err()
return
}
// CntRelations get favoried folders count in relation from mysql.
func (d *Dao) CntRelations(c context.Context, mid, fid int64, typ int8) (count int, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_cntRelationSQL, relationHit(mid)), fid, typ)
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// CntRelations get favoried folders count in relation from mysql.
func (d *Dao) CntAllRelations(c context.Context, mid, fid int64) (count int, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_cntAllRelationSQL, relationHit(mid)), fid)
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// FolderCnt get user's folder count.
func (d *Dao) FolderCnt(c context.Context, tp int8, mid int64) (count int, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_cntFolderSQL, folderHit(mid)), tp, mid)
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// AddFav add a favorite relation to mysql.
func (d *Dao) AddFav(c context.Context, fr *model.Favorite) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_addRelationSQL, relationHit(fr.Mid)), fr.Type, fr.Oid, fr.Mid, fr.Fid, fr.State, fr.CTime, fr.MTime, fr.State, fr.MTime)
if err != nil {
log.Error("db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// DelFav delete a favorite relation to mysql.
func (d *Dao) DelFav(c context.Context, tp int8, mid, fid, oid int64) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_delRelationSQL, relationHit(mid)), tp, fid, oid)
if err != nil {
log.Error("d.db.Exec(%d,%d,%d,%d) error(%v)", mid, tp, fid, oid, err)
return
}
return res.RowsAffected()
}
// AddRelation add a favorite relation to mysql.
func (d *Dao) AddRelation(c context.Context, fr *model.Favorite) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_addRelationSQL, relationHit(fr.Mid)), fr.Type, fr.Oid, fr.Mid, fr.Fid, fr.State, fr.CTime, fr.MTime, fr.State, fr.MTime)
if err != nil {
log.Error("db.Exec error(%v)", err)
return
}
return res.LastInsertId()
}
// Relation get a relation from mysql.
func (d *Dao) Relation(c context.Context, tp int8, mid, fid, oid int64) (m *model.Favorite, err error) {
m = &model.Favorite{}
row := d.db.QueryRow(c, fmt.Sprintf(_relationSQL, relationHit(mid)), tp, mid, fid, oid)
if err = row.Scan(&m.ID, &m.Type, &m.Oid, &m.Mid, &m.Fid, &m.State, &m.CTime, &m.MTime, &m.Sequence); err != nil {
if err == sql.ErrNoRows {
err = nil
m = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// DelRelation delete a favorite relation to mysql.
func (d *Dao) DelRelation(c context.Context, tp int8, mid, fid, oid int64) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_delRelationSQL, relationHit(mid)), tp, fid, oid)
if err != nil {
log.Error("d.db.Exec(%d,%d,%d,%d) error(%v)", mid, tp, fid, oid, err)
return
}
return res.RowsAffected()
}
// MultiDelRelations delete relations to mysql.
func (d *Dao) MultiDelRelations(c context.Context, tp int8, mid, fid int64, oids []int64) (rows int64, err error) {
if len(oids) <= 0 {
return
}
oidsStr := xstr.JoinInts(oids)
res, err := d.db.Exec(c, fmt.Sprintf(_delRelationsSQL, relationHit(mid), oidsStr), tp, fid)
if err != nil {
log.Error("d.db.Exec(%d,%d,%d,%s) error(%v)", mid, tp, fid, oidsStr, err)
return
}
return res.RowsAffected()
}
// TxMultiDelRelations delete relations to mysql.
func (d *Dao) TxMultiDelRelations(tx *sql.Tx, tp int8, mid, fid int64, oids []int64) (rows int64, err error) {
if len(oids) <= 0 {
return
}
oidsStr := xstr.JoinInts(oids)
res, err := tx.Exec(fmt.Sprintf(_delRelationsSQL, relationHit(mid), oidsStr), tp, fid)
if err != nil {
log.Error("d.db.Exec(%d,%d,%d,%s) error(%v)", mid, tp, fid, oidsStr, err)
return
}
return res.RowsAffected()
}
// MultiAddRelations insert relations to mysql.
func (d *Dao) MultiAddRelations(c context.Context, tp int8, mid, fid int64, oids []int64) (rows int64, err error) {
var buffer bytes.Buffer
valuesTpl := "(%d,%d,%d,%d,%d),"
for _, oid := range oids {
buffer.WriteString(fmt.Sprintf(valuesTpl, tp, oid, mid, fid, 0))
}
buffer.Truncate(buffer.Len() - 1)
res, err := d.db.Exec(c, fmt.Sprintf(_maddRelationsSQL, relationHit(mid), buffer.String()))
if err != nil {
log.Error("d.db.Exec(%d,%d,%d,%s) error(%v)", mid, tp, fid, oids, err)
return
}
return res.RowsAffected()
}
// DelFolder delete a folder to mysql.
func (d *Dao) DelFolder(c context.Context, tp int8, mid, fid int64) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_delFolderSQL, folderHit(mid)), tp, fid)
if err != nil {
log.Error("d.db.Exec(mid:%d,tp:%d,fid:%d) error(%v)", mid, tp, fid, err)
return
}
return res.RowsAffected()
}
// FolderStats get folders from mysql.
func (d *Dao) FolderStats(c context.Context, fvmids []*model.ArgFVmid) (fs map[int64]*model.Folder, err error) {
tblMap := make(map[int64][]int64, len(fvmids))
for _, fvmid := range fvmids {
tblMap[fvmid.Vmid] = append(tblMap[fvmid.Vmid], fvmid.Fid)
}
fs = make(map[int64]*model.Folder, len(fvmids))
for vmid, fids := range tblMap {
fidsStr := xstr.JoinInts(fids)
var rows *sql.Rows
if rows, err = d.db.Query(c, fmt.Sprintf(_folderStatSQL, folderHit(vmid), fidsStr)); err != nil {
log.Error("d.db.Query(%s,%s,%s) error(%v)", _folderStatSQL, folderHit(vmid), fidsStr, err)
return
}
defer rows.Close()
for rows.Next() {
stat := new(model.Folder)
if err = rows.Scan(&stat.ID, &stat.PlayCount, &stat.FavedCount, &stat.ShareCount); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
fs[stat.ID*100+vmid%100] = stat
}
err = rows.Err()
if err != nil {
return
}
}
return
}
// UserFolders get user's folders.
func (d *Dao) UserFolders(c context.Context, typ int8, mid int64) (fs map[int64]*model.Folder, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_userFoldersSQL, folderHit(mid)), typ, mid)
if err != nil {
log.Error("d.db.Query(mid:%d,typ:%d) error(%v)", mid, typ, err)
return
}
defer rows.Close()
fs = make(map[int64]*model.Folder)
for rows.Next() {
f := new(model.Folder)
if err = rows.Scan(&f.ID, &f.Type, &f.Mid, &f.Name, &f.Cover, &f.Description, &f.Count, &f.Attr, &f.State, &f.CTime, &f.MTime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
fs[f.ID] = f
}
err = rows.Err()
return
}
// FolderSort return user's folders sort by type & mid.
func (d *Dao) FolderSort(c context.Context, typ int8, mid int64) (fst *model.FolderSort, err error) {
var b []byte
fst = new(model.FolderSort)
row := d.db.QueryRow(c, _folderSortSQL, typ, mid)
if err = row.Scan(&fst.ID, &fst.Type, &fst.Mid, &b, &fst.CTime, &fst.MTime); err != nil {
if err == sql.ErrNoRows {
fst = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
return
}
fst.SetIndex(b)
return
}
// UpFolderSort update user's folder sort.
func (d *Dao) UpFolderSort(c context.Context, fst *model.FolderSort) (rows int64, err error) {
sort := fst.Index()
res, err := d.db.Exec(c, _setFolderSortSQL, fst.Type, fst.Mid, sort, fst.CTime, fst.MTime, sort, fst.MTime)
if err != nil {
log.Error("d.db.Exec() error(%v)", err)
return
}
return res.RowsAffected()
}
// RecentOids return user's three newest fav from a folder.
func (d *Dao) RecentOids(c context.Context, mid, fid int64, typ int8) (oids []int64, err error) {
rows, err := d.dbRead.Query(c, fmt.Sprintf(_recentOidsSQL, relationHit(mid)), fid)
if err != nil {
log.Error("d.db.Query(%d,%d) error(%v)", mid, fid, err)
return
}
defer rows.Close()
for rows.Next() {
var oid int64
var otyp int8
if err = rows.Scan(&oid, &otyp); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
if otyp == typ {
oids = append(oids, oid)
}
}
err = rows.Err()
return
}
// RecentOids return user's three newest fav from a folder.
func (d *Dao) RecentRes(c context.Context, mid, fid int64) (res []*model.Resource, err error) {
rows, err := d.dbRead.Query(c, fmt.Sprintf(_recentOidsSQL, relationHit(mid)), fid)
if err != nil {
log.Error("d.db.Query(%d,%d) error(%v)", mid, fid, err)
return
}
defer rows.Close()
for rows.Next() {
var oid int64
var typ int32
if err = rows.Scan(&oid, &typ); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
res = append(res, &model.Resource{Oid: oid, Typ: typ})
}
err = rows.Err()
return
}
// TxCopyRelations copy resources from oldfid to newfid by oids.
func (d *Dao) TxCopyRelations(tx *sql.Tx, typ int8, oldmid, mid, oldfid, newfid int64, oids []int64) (rows int64, err error) {
res, err := tx.Exec(fmt.Sprintf(_copyRelationsSQL, relationHit(mid), typ, mid, newfid, relationHit(oldmid), xstr.JoinInts(oids)), typ, oldmid, oldfid)
if err != nil {
log.Error("db.Exec() error(%v)", err)
return
}
return res.RowsAffected()
}
// CopyRelations copy resources from oldfid to newfid by oids.
func (d *Dao) CopyRelations(c context.Context, typ int8, oldmid, mid, oldfid, newfid int64, oids []int64) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_copyRelationsSQL, relationHit(mid), typ, mid, newfid, relationHit(oldmid), xstr.JoinInts(oids)), typ, oldmid, oldfid)
if err != nil {
log.Error("db.Exec() error(%v)", err)
return
}
return res.RowsAffected()
}
// CntUsers get favoried users count from mysql.
func (d *Dao) CntUsers(c context.Context, typ int8, oid int64) (count int, err error) {
row := d.dbPush.QueryRow(c, fmt.Sprintf(_cntUsersSQL, usersHit(oid)), typ, oid)
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// Users get favorite users from mysql.
func (d *Dao) Users(c context.Context, typ int8, oid int64, start, end int) (us []*model.User, err error) {
rows, err := d.dbPush.Query(c, fmt.Sprintf(_usersSQL, usersHit(oid)), typ, oid, start, end)
if err != nil {
log.Error("d.db.Query(%d,%d,%d,%d) error(%v)", typ, oid, start, end, err)
return
}
defer rows.Close()
us = make([]*model.User, 0)
for rows.Next() {
var u = new(model.User)
if err = rows.Scan(&u.ID, &u.Type, &u.Oid, &u.Mid, &u.State, &u.CTime, &u.MTime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
us = append(us, u)
}
err = rows.Err()
return
}
// OidCount get oid's fav count from mysql.
func (d *Dao) OidCount(c context.Context, typ int8, oid int64) (count int64, err error) {
row := d.dbPush.QueryRow(c, fmt.Sprintf(_countSQL, countHit(oid)), typ, oid)
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// OidsCount get oids's count from mysql.
func (d *Dao) OidsCount(c context.Context, typ int8, oids []int64) (counts map[int64]int64, err error) {
tblMap := make(map[string][]int64, len(oids))
for _, oid := range oids {
countHit := countHit(oid)
tblMap[countHit] = append(tblMap[countHit], oid)
}
counts = make(map[int64]int64, len(oids))
for countHit, oids := range tblMap {
oidsStr := xstr.JoinInts(oids)
var rows *sql.Rows
if rows, err = d.db.Query(c, fmt.Sprintf(_countsSQL, countHit, oidsStr), typ); err != nil {
log.Error("d.db.Query(%s,%d) error(%v)", fmt.Sprintf(_countsSQL, countHit, oidsStr), typ, err)
return
}
defer rows.Close()
for rows.Next() {
var oid, count int64
if err = rows.Scan(&oid, &count); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
counts[oid] = count
}
if err = rows.Err(); err != nil {
log.Error("rows.Err() error(%v)", err)
return
}
}
return
}
// BatchOids get batch oids from mysql.
func (d *Dao) BatchOids(c context.Context, typ int8, mid int64, limit int) (oids []int64, err error) {
rows, err := d.dbRead.Query(c, fmt.Sprintf(_batchOidsSQL, relationHit(mid)), typ, mid, limit)
if err != nil {
log.Error("d.db.Query(%d,%d,%d) error(%v)", typ, mid, limit, err)
return
}
defer rows.Close()
for rows.Next() {
var oid int64
if err = rows.Scan(&oid); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
oids = append(oids, oid)
}
if err = rows.Err(); err != nil {
log.Error("rows.Err() error(%v)", err)
}
return
}

View File

@@ -0,0 +1,592 @@
package dao
import (
"context"
"testing"
"go-common/app/service/main/favorite/model"
"github.com/smartystreets/goconvey/convey"
)
func TestFavfolderHit(t *testing.T) {
var (
mid = int64(0)
)
convey.Convey("folderHit", t, func(ctx convey.C) {
p1 := folderHit(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavrelationHit(t *testing.T) {
var (
mid = int64(0)
)
convey.Convey("relationHit", t, func(ctx convey.C) {
p1 := relationHit(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavusersHit(t *testing.T) {
var (
oid = int64(0)
)
convey.Convey("usersHit", t, func(ctx convey.C) {
p1 := usersHit(oid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavcountHit(t *testing.T) {
var (
oid = int64(0)
)
convey.Convey("countHit", t, func(ctx convey.C) {
p1 := countHit(oid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavpingMySQL(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("pingMySQL", t, func(ctx convey.C) {
err := d.pingMySQL(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavFolder(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
fid = int64(1)
)
convey.Convey("Folder", t, func(ctx convey.C) {
f, err := d.Folder(c, tp, mid, fid)
ctx.Convey("Then err should be nil.f should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(f, convey.ShouldNotBeNil)
})
})
}
func TestFavFolderByName(t *testing.T) {
var (
c = context.TODO()
tp = int8(0)
mid = int64(0)
name = ""
)
convey.Convey("FolderByName", t, func(ctx convey.C) {
f, err := d.FolderByName(c, tp, mid, name)
ctx.Convey("Then err should be nil.f should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(f, convey.ShouldNotBeNil)
})
})
}
func TestFavDefaultFolder(t *testing.T) {
var (
c = context.TODO()
tp = int8(0)
mid = int64(0)
)
convey.Convey("DefaultFolder", t, func(ctx convey.C) {
f, err := d.DefaultFolder(c, tp, mid)
ctx.Convey("Then err should be nil.f should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(f, convey.ShouldNotBeNil)
})
})
}
func TestFavAddFolder(t *testing.T) {
var (
c = context.TODO()
f = &model.Folder{}
)
convey.Convey("AddFolder", t, func(ctx convey.C) {
fid, err := d.AddFolder(c, f)
ctx.Convey("Then err should be nil.fid should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(fid, convey.ShouldNotBeNil)
})
})
}
func TestFavUpdateFolder(t *testing.T) {
var (
c = context.TODO()
f = &model.Folder{}
)
convey.Convey("UpdateFolder", t, func(ctx convey.C) {
fid, err := d.UpdateFolder(c, f)
ctx.Convey("Then err should be nil.fid should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(fid, convey.ShouldNotBeNil)
})
})
}
func TestFavUpFolderName(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
fid = int64(0)
name = ""
)
convey.Convey("UpFolderName", t, func(ctx convey.C) {
rows, err := d.UpFolderName(c, typ, mid, fid, name)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavUpFolderAttr(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
fid = int64(0)
attr = int32(0)
)
convey.Convey("UpFolderAttr", t, func(ctx convey.C) {
rows, err := d.UpFolderAttr(c, typ, mid, fid, attr)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavFolderRelations(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
fid = int64(0)
start = int(0)
end = int(0)
)
convey.Convey("FolderRelations", t, func(ctx convey.C) {
fr, err := d.FolderRelations(c, typ, mid, fid, start, end)
ctx.Convey("Then err should be nil.fr should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(fr, convey.ShouldNotBeNil)
})
})
}
func TestFavFolders(t *testing.T) {
var (
c = context.TODO()
fvmids = []*model.ArgFVmid{}
)
convey.Convey("Folders", t, func(ctx convey.C) {
fs, err := d.Folders(c, fvmids)
ctx.Convey("Then err should be nil.fs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(fs, convey.ShouldNotBeNil)
})
})
}
func TestFavRelationFidsByOid(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
oid = int64(1)
)
convey.Convey("RelationFidsByOid", t, func(ctx convey.C) {
_, err := d.RelationFidsByOid(c, tp, mid, oid)
ctx.Convey("Then err should be nil.fids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavRelationFidsByOids(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(8888894)
oids = []int64{1, 2, 3}
)
convey.Convey("RelationFidsByOids", t, func(ctx convey.C) {
fidsMap, err := d.RelationFidsByOids(c, tp, mid, oids)
ctx.Convey("Then err should be nil.fidsMap should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(fidsMap, convey.ShouldNotBeNil)
})
})
}
func TestFavCntRelations(t *testing.T) {
var (
c = context.TODO()
mid = int64(0)
fid = int64(0)
)
convey.Convey("CntRelations", t, func(ctx convey.C) {
count, err := d.CntRelations(c, mid, fid, 2)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
}
func TestFavFolderCnt(t *testing.T) {
var (
c = context.TODO()
tp = int8(0)
mid = int64(0)
)
convey.Convey("FolderCnt", t, func(ctx convey.C) {
count, err := d.FolderCnt(c, tp, mid)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
}
func TestFavAddFav(t *testing.T) {
var (
c = context.TODO()
fr = &model.Favorite{}
)
convey.Convey("AddFav", t, func(ctx convey.C) {
rows, err := d.AddFav(c, fr)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavDelFav(t *testing.T) {
var (
c = context.TODO()
tp = int8(0)
mid = int64(0)
fid = int64(0)
oid = int64(0)
)
convey.Convey("DelFav", t, func(ctx convey.C) {
rows, err := d.DelFav(c, tp, mid, fid, oid)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavAddRelation(t *testing.T) {
var (
c = context.TODO()
fr = &model.Favorite{}
)
convey.Convey("AddRelation", t, func(ctx convey.C) {
rows, err := d.AddRelation(c, fr)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavRelation(t *testing.T) {
var (
c = context.TODO()
tp = int8(0)
mid = int64(0)
fid = int64(0)
oid = int64(0)
)
convey.Convey("Relation", t, func(ctx convey.C) {
_, err := d.Relation(c, tp, mid, fid, oid)
ctx.Convey("Then err should be nil.m should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavDelRelation(t *testing.T) {
var (
c = context.TODO()
tp = int8(0)
mid = int64(0)
fid = int64(0)
oid = int64(0)
)
convey.Convey("DelRelation", t, func(ctx convey.C) {
rows, err := d.DelRelation(c, tp, mid, fid, oid)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavMultiDelRelations(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
fid = int64(0)
oids = []int64{1, 2, 3}
)
convey.Convey("MultiDelRelations", t, func(ctx convey.C) {
rows, err := d.MultiDelRelations(c, tp, mid, fid, oids)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavTxMultiDelRelations(t *testing.T) {
var (
tx, _ = d.BeginTran(context.TODO())
tp = int8(1)
mid = int64(88888894)
fid = int64(0)
oids = []int64{1, 2, 3}
)
convey.Convey("TxMultiDelRelations", t, func(ctx convey.C) {
rows, err := d.TxMultiDelRelations(tx, tp, mid, fid, oids)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavMultiAddRelations(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
fid = int64(0)
oids = []int64{1}
)
convey.Convey("MultiAddRelations", t, func(ctx convey.C) {
rows, err := d.MultiAddRelations(c, tp, mid, fid, oids)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavDelFolder(t *testing.T) {
var (
c = context.TODO()
tp = int8(0)
mid = int64(0)
fid = int64(0)
)
convey.Convey("DelFolder", t, func(ctx convey.C) {
rows, err := d.DelFolder(c, tp, mid, fid)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavUserFolders(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
)
convey.Convey("UserFolders", t, func(ctx convey.C) {
fs, err := d.UserFolders(c, typ, mid)
ctx.Convey("Then err should be nil.fs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(fs, convey.ShouldNotBeNil)
})
})
}
func TestFavFolderSort(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
)
convey.Convey("FolderSort", t, func(ctx convey.C) {
fst, err := d.FolderSort(c, typ, mid)
ctx.Convey("Then err should be nil.fst should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(fst, convey.ShouldNotBeNil)
})
})
}
func TestFavUpFolderSort(t *testing.T) {
var (
c = context.TODO()
fst = &model.FolderSort{}
)
convey.Convey("UpFolderSort", t, func(ctx convey.C) {
rows, err := d.UpFolderSort(c, fst)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavRecentOids(t *testing.T) {
var (
c = context.TODO()
mid = int64(88888894)
fid = int64(1)
)
convey.Convey("RecentOids", t, func(ctx convey.C) {
oids, err := d.RecentOids(c, mid, fid, 1)
ctx.Convey("Then err should be nil.oids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(oids, convey.ShouldNotBeNil)
})
})
}
func TestFavTxCopyRelations(t *testing.T) {
var (
tx, _ = d.BeginTran(context.TODO())
typ = int8(1)
oldmid = int64(88888894)
mid = int64(88888894)
oldfid = int64(0)
newfid = int64(0)
oids = []int64{1}
)
convey.Convey("TxCopyRelations", t, func(ctx convey.C) {
rows, err := d.TxCopyRelations(tx, typ, oldmid, mid, oldfid, newfid, oids)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavCopyRelations(t *testing.T) {
var (
c = context.TODO()
typ = int8(1)
oldmid = int64(88888894)
mid = int64(88888894)
oldfid = int64(0)
newfid = int64(0)
oids = []int64{1}
)
convey.Convey("CopyRelations", t, func(ctx convey.C) {
rows, err := d.CopyRelations(c, typ, oldmid, mid, oldfid, newfid, oids)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
}
func TestFavCntUsers(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
oid = int64(0)
)
convey.Convey("CntUsers", t, func(ctx convey.C) {
count, err := d.CntUsers(c, typ, oid)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
}
func TestFavUsers(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
oid = int64(0)
start = int(0)
end = int(0)
)
convey.Convey("Users", t, func(ctx convey.C) {
us, err := d.Users(c, typ, oid, start, end)
ctx.Convey("Then err should be nil.us should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(us, convey.ShouldNotBeNil)
})
})
}
func TestFavOidCount(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
oid = int64(0)
)
convey.Convey("OidCount", t, func(ctx convey.C) {
count, err := d.OidCount(c, typ, oid)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
}
func TestFavOidsCount(t *testing.T) {
var (
c = context.TODO()
typ = int8(1)
oids = []int64{1, 2, 3}
)
convey.Convey("OidsCount", t, func(ctx convey.C) {
counts, err := d.OidsCount(c, typ, oids)
ctx.Convey("Then err should be nil.counts should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(counts, convey.ShouldNotBeNil)
})
})
}
func TestFavBatchOids(t *testing.T) {
var (
c = context.TODO()
typ = int8(1)
mid = int64(88888894)
limit = int(10)
)
convey.Convey("BatchOids", t, func(ctx convey.C) {
oids, err := d.BatchOids(c, typ, mid, limit)
ctx.Convey("Then err should be nil.oids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(oids, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,649 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"go-common/app/service/main/favorite/model"
"go-common/library/cache/redis"
"go-common/library/log"
xtime "go-common/library/time"
)
const (
_folderKey = "fi_%d_%d" // sortedset fi_type_mid value:fid,score:ctime
_relationKey = "r_%d_%d" // sortedset r_mid_fid(mtime, oid)
_allRelationKey = "ar_%d_%d" // sortedset ar_mid_fid(mtime, oid)
_relationOidsKey = "ro_%d_%d" // set ro_type_mid value:oids
_cleanedKey = "rc_%d_%d" // hash key:rc_type_mid field:fid value:timestamp
// key fb_mid/100000 offset => mid%100000
// bit value 1 mean unfaved; bit value 0 mean faved
_favedBit = "fb_%d_%d"
_bucket = 100000
)
// isFavedKey return user's fav flag key
func favedBitKey(tp int8, mid int64) string {
return fmt.Sprintf(_favedBit, tp, mid/_bucket)
}
// folderKey return a user folder key.
func folderKey(tp int8, mid int64) string {
return fmt.Sprintf(_folderKey, tp, mid)
}
// relationKey return folder relation key.
func relationKey(mid, fid int64) string {
return fmt.Sprintf(_relationKey, mid, fid)
}
// relationKey return folder relation key.
func allRelationKey(mid, fid int64) string {
return fmt.Sprintf(_allRelationKey, mid, fid)
}
// relationOidsKey return a user oids key.
func relationOidsKey(tp int8, mid int64) string {
return fmt.Sprintf(_relationOidsKey, tp, mid)
}
// isCleanedKey return user whether cleaned key.
func isCleanedKey(tp int8, mid int64) string {
return fmt.Sprintf(_cleanedKey, tp, mid)
}
// pingRedis check redis connection
func (d *Dao) pingRedis(c context.Context) (err error) {
conn := d.redis.Get(c)
_, err = conn.Do("SET", "PING", "PONG")
conn.Close()
return
}
// ExpireRelations expire folder relations cache.
func (d *Dao) ExpireRelations(c context.Context, mid, fid int64) (ok bool, err error) {
key := relationKey(mid, fid)
conn := d.redis.Get(c)
if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisExpire)); err != nil {
log.Error("conn.Do(EXPIRE %s) error(%v)", key, err)
}
conn.Close()
return
}
// ExpireAllRelations expire folder relations cache.
func (d *Dao) ExpireAllRelations(c context.Context, mid, fid int64) (ok bool, err error) {
key := allRelationKey(mid, fid)
conn := d.redis.Get(c)
if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisExpire)); err != nil {
log.Error("conn.Do(EXPIRE %s) error(%v)", key, err)
}
conn.Close()
return
}
// ExpireFolder expire folder cache.
func (d *Dao) ExpireFolder(c context.Context, tp int8, mid int64) (ok bool, err error) {
key := folderKey(tp, mid)
conn := d.redis.Get(c)
if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisExpire)); err != nil {
log.Error("conn.Do(EXPIRE %s) error(%v)", key, err)
}
conn.Close()
return
}
// RemFidsRedis del user's fids in redis
func (d *Dao) RemFidsRedis(c context.Context, typ int8, mid int64, fs ...*model.Folder) (err error) {
var (
key = folderKey(typ, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
args := []interface{}{key}
for _, f := range fs {
args = append(args, f.ID)
}
if err = conn.Send("ZREM", args...); err != nil {
log.Error("conn.Send(ZREM %s,%v) error(%v)", key, args, err)
return
}
if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
log.Error("conn.Send(EXPIRE) error(%v)", err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush() error(%v)", err)
return
}
for i := 0; i < len(fs)+1; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
// AddFidsRedis set user's fids to redis
func (d *Dao) AddFidsRedis(c context.Context, typ int8, mid int64, fs ...*model.Folder) (err error) {
var (
key = folderKey(typ, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
for _, f := range fs {
if err = conn.Send("ZADD", key, f.CTime, f.ID); err != nil {
log.Error("conn.Send(ZADD %s,%s,%d) error(%v)", key, f.CTime, f.ID, err)
return
}
}
if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
log.Error("conn.Send(EXPIRE) error(%v)", err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush() error(%v)", err)
return
}
for i := 0; i < len(fs)+1; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
// FidsRedis return the user's all fids from redis.
func (d *Dao) FidsRedis(c context.Context, tp int8, mid int64) (fids []int64, err error) {
var (
key = folderKey(tp, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
if fids, err = redis.Int64s(conn.Do("ZRANGE", key, 0, -1)); err != nil {
log.Error("conn.Do(ZRANGE, %s) error(%v)", key, err)
return
}
return
}
// DelFidsRedis delete user's all fids from redis.
func (d *Dao) DelFidsRedis(c context.Context, typ int8, mid int64) (err error) {
var (
key = folderKey(typ, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
if _, err = conn.Do("DEL", key); err != nil {
log.Error("conn.Do(DEL %s) error(%v)", key, err)
}
return
}
// AddFoldersCache add the user all folders to redis.
func (d *Dao) AddFoldersCache(c context.Context, tp int8, mid int64, folders []*model.Folder) (err error) {
var (
folder *model.Folder
value []byte
key = folderKey(tp, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
for _, folder = range folders {
if value, err = json.Marshal(folder); err != nil {
return
}
if err = conn.Send("HSET", key, folder.ID, value); err != nil {
log.Error("conn.Send(HSET %s,%d,%s) error(%v)", key, folder.ID, value)
return
}
}
if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
log.Error("conn.Send(EXPIRE) error(%v)", err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
for i := 0; i < len(folders)+1; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive error(%v)", err)
return
}
}
return
}
// FolderRelationsCache return the folder all relations from redis.
func (d *Dao) FolderRelationsCache(c context.Context, typ int8, mid, fid int64, start, end int) (res []*model.Favorite, err error) {
conn := d.redis.Get(c)
key := relationKey(mid, fid)
defer conn.Close()
values, err := redis.Values(conn.Do("ZREVRANGE", key, start, end, "WITHSCORES"))
if err != nil {
log.Error("conn.Do(ZREVRANGE,%s,%d,%d) error(%v)", key, start, end, err)
return
}
if len(values) == 0 {
return
}
var oid, t int64
for len(values) > 0 {
if values, err = redis.Scan(values, &oid, &t); err != nil {
log.Error("redis.Scan() error(%v)", err)
return
}
res = append(res, &model.Favorite{Oid: oid, Mid: mid, Fid: fid, Type: typ, MTime: xtime.Time(t)})
}
return
}
// CntRelationsCache return the folder all relation count from redis.
func (d *Dao) CntRelationsCache(c context.Context, mid, fid int64) (cnt int, err error) {
var conn = d.redis.Get(c)
key := relationKey(mid, fid)
defer conn.Close()
if cnt, err = redis.Int(conn.Do("ZCARD", key)); err != nil {
if err == redis.ErrNil {
err = nil
}
log.Error("conn.Do(ZCARD %s) error(%v)", key, err)
return
}
return
}
// CntAllRelationsCache return the folder all relation count from redis.
func (d *Dao) CntAllRelationsCache(c context.Context, mid, fid int64) (cnt int, err error) {
var conn = d.redis.Get(c)
key := allRelationKey(mid, fid)
defer conn.Close()
if cnt, err = redis.Int(conn.Do("ZCARD", key)); err != nil {
if err == redis.ErrNil {
err = nil
}
log.Error("conn.Do(ZCARD %s) error(%v)", key, err)
return
}
return
}
// FolderRelationsCache return the folder all relations from redis.
func (d *Dao) FolderAllRelationsCache(c context.Context, typ int8, mid, fid int64, start, end int) (res []*model.Favorite, err error) {
conn := d.redis.Get(c)
key := allRelationKey(mid, fid)
defer conn.Close()
values, err := redis.Values(conn.Do("ZREVRANGE", key, start, end, "WITHSCORES"))
if err != nil {
log.Error("conn.Do(ZREVRANGE,%s,%d,%d) error(%v)", key, start, end, err)
return
}
if len(values) == 0 {
return
}
var oid, score int64
for len(values) > 0 {
if values, err = redis.Scan(values, &oid, &score); err != nil {
log.Error("redis.Scan() error(%v)", err)
return
}
res = append(res, &model.Favorite{Oid: oid / 100, Mid: mid, Fid: fid, Type: int8(oid % 100), MTime: xtime.Time(score % 1e10)})
}
return
}
// MultiExpireRelations expire folders's relations cache.
func (d *Dao) MultiExpireAllRelations(c context.Context, mid int64, fids []int64) (map[int64]bool, error) {
okMap := make(map[int64]bool, len(fids))
conn := d.redis.Get(c)
defer conn.Close()
for _, fid := range fids {
key := allRelationKey(mid, fid)
if err := conn.Send("EXPIRE", key, d.redisExpire); err != nil {
log.Error("redis.send(EXPIRE %s) error(%v)", key, err)
return nil, err
}
}
if err := conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return nil, err
}
for _, fid := range fids {
ok, err := redis.Bool(conn.Receive())
if err != nil {
log.Error("conn.Receive(%d) error(%v)", fid, err)
return nil, err
}
okMap[fid] = ok
}
return okMap, nil
}
// MultiExpireRelations expire folders's relations cache.
func (d *Dao) MultiExpireRelations(c context.Context, mid int64, fids []int64) (map[int64]bool, error) {
okMap := make(map[int64]bool, len(fids))
conn := d.redis.Get(c)
defer conn.Close()
for _, fid := range fids {
key := relationKey(mid, fid)
if err := conn.Send("EXPIRE", key, d.redisExpire); err != nil {
log.Error("redis.send(EXPIRE %s) error(%v)", key, err)
return nil, err
}
}
if err := conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return nil, err
}
for _, fid := range fids {
ok, err := redis.Bool(conn.Receive())
if err != nil {
log.Error("conn.Receive(%d) error(%v)", fid, err)
return nil, err
}
okMap[fid] = ok
}
return okMap, nil
}
// ExpireRelationOids set expire for faved oids.
func (d *Dao) ExpireRelationOids(c context.Context, tp int8, mid int64) (ok bool, err error) {
key := relationOidsKey(tp, mid)
var conn = d.redis.Get(c)
defer conn.Close()
if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisExpire)); err != nil {
log.Error("conn.Do(EXPIRE, %s) error(%v)", key, err)
}
return
}
// AddRelationOidCache add favoured oid.
func (d *Dao) AddRelationOidCache(c context.Context, tp int8, mid, oid int64) (err error) {
var (
key = relationOidsKey(tp, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
if err = conn.Send("SADD", key, oid); err != nil {
log.Error("conn.Send error(%v)", err)
return
}
if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
log.Error("conn.Send error(%v)", err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
for i := 0; i < 2; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
// RemRelationOidCache del faved oid.
func (d *Dao) RemRelationOidCache(c context.Context, tp int8, mid, oid int64) (err error) {
var (
key = relationOidsKey(tp, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
if err = conn.Send("SREM", key, oid); err != nil {
log.Error("conn.Send error(%v)", err)
return
}
if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
log.Error("conn.Send error(%v)", err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
for i := 0; i < 2; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
// IsFavedCache return true or false to judge object whether faved by user.
func (d *Dao) IsFavedCache(c context.Context, tp int8, mid int64, oid int64) (isFaved bool, err error) {
var (
key = relationOidsKey(tp, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
if isFaved, err = redis.Bool(conn.Do("SISMEMBER", key, oid)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("HGET %v %v error(%v)", key, oid, err)
}
}
return
}
// IsFavedsCache return true or false to judge object whether faved by user.
func (d *Dao) IsFavedsCache(c context.Context, tp int8, mid int64, oids []int64) (favoreds map[int64]bool, err error) {
var (
key = relationOidsKey(tp, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
for _, oid := range oids {
if err = conn.Send("SISMEMBER", key, oid); err != nil {
log.Error("conn.Send(SISMEMBER %s,%d) error(%v)", key, oid, err)
return
}
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush() error(%v)", err)
return
}
favoreds = make(map[int64]bool, len(oids))
for _, oid := range oids {
faved, err := redis.Bool(conn.Receive())
if err != nil {
log.Error("conn.Receive() error(%v)", err)
continue
}
favoreds[oid] = faved
}
return
}
// SetFavedBit set unfaved user bit to 0
func (d *Dao) SetFavedBit(c context.Context, tp int8, mid int64) (err error) {
key := favedBitKey(tp, mid)
offset := mid % _bucket
conn := d.redis.Get(c)
defer conn.Close()
if _, err = conn.Do("SETBIT", key, offset, 0); err != nil {
log.Error("conn.DO(SETBIT) key(%s) offset(%d) err(%v)", key, offset, err)
}
return
}
// SetUnFavedBit set unfaved user bit to 1
func (d *Dao) SetUnFavedBit(c context.Context, tp int8, mid int64) (err error) {
key := favedBitKey(tp, mid)
offset := mid % _bucket
conn := d.redis.Get(c)
defer conn.Close()
if _, err = conn.Do("SETBIT", key, offset, 1); err != nil {
log.Error("conn.DO(SETBIT) key(%s) offset(%d) err(%v)", key, offset, err)
}
return
}
// FavedBit check if user had fav video by bit value
func (d *Dao) FavedBit(c context.Context, tp int8, mid int64) (unfaved bool, err error) {
key := favedBitKey(tp, mid)
offset := mid % _bucket
conn := d.redis.Get(c)
defer conn.Close()
if unfaved, err = redis.Bool(conn.Do("GETBIT", key, offset)); err != nil {
log.Error("conn.DO(GETBIT) key(%s) offset(%d) err(%v)", key, offset, err)
}
return
}
// DelRelationsCache delete the folder relation cache.
func (d *Dao) DelAllRelationsCache(c context.Context, mid, fid int64) (err error) {
key := allRelationKey(mid, fid)
conn := d.redis.Get(c)
defer conn.Close()
if _, err = conn.Do("DEL", key); err != nil {
log.Error("conn.Do(DEL %s) error(%v)", key, err)
}
return
}
// DelRelationsCache delete the folder relation cache.
func (d *Dao) DelRelationsCache(c context.Context, mid, fid int64) (err error) {
key := relationKey(mid, fid)
conn := d.redis.Get(c)
defer conn.Close()
if _, err = conn.Do("DEL", key); err != nil {
log.Error("conn.Do(DEL %s) error(%v)", key, err)
}
return
}
// AddRelationCache add a relation to redis.
func (d *Dao) AddRelationCache(c context.Context, m *model.Favorite) (err error) {
key := relationKey(m.Mid, m.Fid)
conn := d.redis.Get(c)
defer conn.Close()
if err = conn.Send("ZADD", key, m.MTime, m.Oid); err != nil {
log.Error("conn.Send(ZADD %s,%d) error(%v)", key, m.Oid, err)
return
}
if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
log.Error("conn.Send(EXPIRE) error(%v)", err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush() error(%v)", err)
return
}
for i := 0; i < 2; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
// RecentOidsCache return three recent oids of all user's folders from redis.
func (d *Dao) RecentOidsCache(c context.Context, typ int8, mid int64, fids []int64) (rctFidsMap map[int64][]int64, missFids []int64, err error) {
conn := d.redis.Get(c)
defer conn.Close()
for _, fid := range fids {
key := relationKey(mid, fid)
if err = conn.Send("ZREVRANGE", key, 0, 2); err != nil {
log.Error("conn.Do(ZREVRANGE, %s,%d) error(%v)", key, fid, err)
return
}
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
rctFidsMap = make(map[int64][]int64, len(fids))
for _, fid := range fids {
oids, err := redis.Int64s(conn.Receive())
if err != nil {
log.Error("redis.Strings()err(%v)", err)
return nil, fids, err
}
if len(oids) == 0 {
missFids = append(missFids, fid)
}
rctFidsMap[fid] = oids
}
return
}
// BatchOidsRedis return the user's 1000 oids from redis.
func (d *Dao) BatchOidsRedis(c context.Context, tp int8, mid int64, limit int) (oids []int64, err error) {
var (
key = relationOidsKey(tp, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
if oids, err = redis.Int64s(conn.Do("SRANDMEMBER", key, limit)); err != nil {
log.Error("conn.Do(SRANDMEMBER, %s) error(%v)", key, err)
return
}
return
}
// IsCleaned check if user had do clean action
func (d *Dao) IsCleaned(c context.Context, typ int8, mid, fid int64) (cleanedTime int64, err error) {
var (
key = isCleanedKey(typ, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
if cleanedTime, err = redis.Int64(conn.Do("HGET", key, fid)); err != nil {
if err == redis.ErrNil {
err = nil
cleanedTime = 0
} else {
log.Error("conn.Do(HGET, %v, %v) error(%v)", key, fid, err)
}
}
return
}
// SetCleanedCache set cleand flag.
func (d *Dao) SetCleanedCache(c context.Context, typ int8, mid, fid, ftime, expire int64) (err error) {
var (
key = isCleanedKey(typ, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
if err = conn.Send("HSET", key, fid, ftime); err != nil {
log.Error("conn.Send error(%v)", err)
}
if err = conn.Send("EXPIRE", key, expire); err != nil {
log.Error("conn.Send error(%v)", err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
for i := 0; i < 2; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive error(%v)", err)
return
}
}
return
}
// DelRelationOidsCache .
func (d *Dao) DelRelationOidsCache(c context.Context, typ int8, mid int64) (err error) {
key := relationOidsKey(typ, mid)
conn := d.redis.Get(c)
defer conn.Close()
if _, err = conn.Do("DEL", key); err != nil {
log.Error("conn.Do(DEL %s) error(%v)", key, err)
}
return
}

View File

@@ -0,0 +1,490 @@
package dao
import (
"context"
"testing"
"go-common/app/service/main/favorite/model"
"github.com/smartystreets/goconvey/convey"
)
func TestMultiExpireRelations(t *testing.T) {
var (
c = context.TODO()
fids = []int64{1, 2}
mid = int64(0)
)
convey.Convey("RemFidsRedis", t, func(ctx convey.C) {
_, err := d.MultiExpireRelations(c, mid, fids)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavRemFidsRedis(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
fs = &model.Folder{}
)
convey.Convey("RemFidsRedis", t, func(ctx convey.C) {
err := d.RemFidsRedis(c, typ, mid, fs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavfavedBitKey(t *testing.T) {
var (
tp = int8(1)
mid = int64(88888894)
)
convey.Convey("favedBitKey", t, func(ctx convey.C) {
p1 := favedBitKey(tp, mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavAddRelationCache(t *testing.T) {
var (
c = context.TODO()
m = &model.Favorite{
Oid: 1,
Fid: 1,
Mid: 88888894,
Type: 1,
}
)
convey.Convey("AddRelationCache", t, func(ctx convey.C) {
err := d.AddRelationCache(c, m)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavfolderKey(t *testing.T) {
var (
tp = int8(1)
mid = int64(88888894)
)
convey.Convey("folderKey", t, func(ctx convey.C) {
p1 := folderKey(tp, mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavrelationKey(t *testing.T) {
var (
mid = int64(88888894)
fid = int64(1)
)
convey.Convey("relationKey", t, func(ctx convey.C) {
p1 := relationKey(mid, fid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavrelationOidsKey(t *testing.T) {
var (
tp = int8(1)
mid = int64(88888894)
)
convey.Convey("relationOidsKey", t, func(ctx convey.C) {
p1 := relationOidsKey(tp, mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestFavpingRedis(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("pingRedis", t, func(ctx convey.C) {
err := d.pingRedis(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavExpireRelations(t *testing.T) {
var (
c = context.TODO()
mid = int64(88888894)
fid = int64(1)
)
convey.Convey("ExpireRelations", t, func(ctx convey.C) {
ok, err := d.ExpireRelations(c, mid, fid)
ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ok, convey.ShouldNotBeNil)
})
})
}
func TestFavExpireFolder(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
)
convey.Convey("ExpireFolder", t, func(ctx convey.C) {
ok, err := d.ExpireFolder(c, tp, mid)
ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ok, convey.ShouldNotBeNil)
})
})
}
func TestFavAddFidsRedis(t *testing.T) {
var (
c = context.TODO()
typ = int8(0)
mid = int64(0)
fs = &model.Folder{}
)
convey.Convey("AddFidsRedis", t, func(ctx convey.C) {
err := d.AddFidsRedis(c, typ, mid, fs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavFidsRedis(t *testing.T) {
var (
c = context.TODO()
tp = int8(0)
mid = int64(0)
)
convey.Convey("FidsRedis", t, func(ctx convey.C) {
_, err := d.FidsRedis(c, tp, mid)
ctx.Convey("Then err should be nil.fids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavDelFidsRedis(t *testing.T) {
var (
c = context.TODO()
typ = int8(1)
mid = int64(88888894)
)
convey.Convey("DelFidsRedis", t, func(ctx convey.C) {
err := d.DelFidsRedis(c, typ, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavAddFoldersCache(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
folders = []*model.Folder{
&model.Folder{
ID: 1,
},
}
)
convey.Convey("AddFoldersCache", t, func(ctx convey.C) {
err := d.AddFoldersCache(c, tp, mid, folders)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavFolderRelationsCache(t *testing.T) {
var (
c = context.TODO()
typ = int8(1)
mid = int64(88888894)
fid = int64(1)
start = int(1)
end = int(2)
)
convey.Convey("FolderRelationsCache", t, func(ctx convey.C) {
_, err := d.FolderRelationsCache(c, typ, mid, fid, start, end)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavAllFolderRelationsCache(t *testing.T) {
var (
c = context.TODO()
typ = int8(1)
mid = int64(88888894)
fid = int64(1)
start = int(1)
end = int(2)
)
convey.Convey("FolderAllRelationsCache", t, func(ctx convey.C) {
_, err := d.FolderAllRelationsCache(c, typ, mid, fid, start, end)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavCntRelationsCache(t *testing.T) {
var (
c = context.TODO()
mid = int64(88888894)
fid = int64(1)
)
convey.Convey("CntRelationsCache", t, func(ctx convey.C) {
cnt, err := d.CntRelationsCache(c, mid, fid)
ctx.Convey("Then err should be nil.cnt should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(cnt, convey.ShouldNotBeNil)
})
})
}
func TestFavCntAllRelationsCache(t *testing.T) {
var (
c = context.TODO()
mid = int64(88888894)
fid = int64(1)
)
convey.Convey("CntAllRelationsCache", t, func(ctx convey.C) {
cnt, err := d.CntAllRelationsCache(c, mid, fid)
ctx.Convey("Then err should be nil.cnt should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(cnt, convey.ShouldNotBeNil)
})
})
}
func TestFavExpireRelationOids(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
)
convey.Convey("ExpireRelationOids", t, func(ctx convey.C) {
ok, err := d.ExpireRelationOids(c, tp, mid)
ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ok, convey.ShouldNotBeNil)
})
})
}
func TestFavAddRelationOidCache(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
oid = int64(1)
)
convey.Convey("AddRelationOidCache", t, func(ctx convey.C) {
err := d.AddRelationOidCache(c, tp, mid, oid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavRemRelationOidCache(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
oid = int64(123)
)
convey.Convey("RemRelationOidCache", t, func(ctx convey.C) {
err := d.RemRelationOidCache(c, tp, mid, oid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavIsFavedCache(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
oid = int64(123)
)
convey.Convey("IsFavedCache", t, func(ctx convey.C) {
isFaved, err := d.IsFavedCache(c, tp, mid, oid)
ctx.Convey("Then err should be nil.isFaved should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(isFaved, convey.ShouldNotBeNil)
})
})
}
func TestFavIsFavedsCache(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
oids = []int64{1, 2, 3}
)
convey.Convey("IsFavedsCache", t, func(ctx convey.C) {
favoreds, err := d.IsFavedsCache(c, tp, mid, oids)
ctx.Convey("Then err should be nil.favoreds should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(favoreds, convey.ShouldNotBeNil)
})
})
}
func TestFavSetFavedBit(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
)
convey.Convey("SetFavedBit", t, func(ctx convey.C) {
err := d.SetFavedBit(c, tp, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavSetUnFavedBit(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
)
convey.Convey("SetUnFavedBit", t, func(ctx convey.C) {
err := d.SetUnFavedBit(c, tp, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavFavedBit(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
)
convey.Convey("FavedBit", t, func(ctx convey.C) {
unfaved, err := d.FavedBit(c, tp, mid)
ctx.Convey("Then err should be nil.unfaved should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(unfaved, convey.ShouldNotBeNil)
})
})
}
func TestFavDelRelationsCache(t *testing.T) {
var (
c = context.TODO()
mid = int64(88888894)
fid = int64(1)
)
convey.Convey("DelRelationsCache", t, func(ctx convey.C) {
err := d.DelRelationsCache(c, mid, fid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavDelAllRelationsCache(t *testing.T) {
var (
c = context.TODO()
mid = int64(88888894)
fid = int64(1)
)
convey.Convey("DelRelationsCache", t, func(ctx convey.C) {
err := d.DelAllRelationsCache(c, mid, fid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavRecentOidsCache(t *testing.T) {
var (
c = context.TODO()
typ = int8(1)
mid = int64(88888894)
fids = []int64{1}
)
convey.Convey("RecentOidsCache", t, func(ctx convey.C) {
rctFidsMap, missFids, err := d.RecentOidsCache(c, typ, mid, fids)
ctx.Convey("Then err should be nil.rctFidsMap,missFids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(missFids, convey.ShouldNotBeNil)
ctx.So(rctFidsMap, convey.ShouldNotBeNil)
})
})
}
func TestFavBatchOidsRedis(t *testing.T) {
var (
c = context.TODO()
tp = int8(1)
mid = int64(88888894)
limit = int(10)
)
convey.Convey("BatchOidsRedis", t, func(ctx convey.C) {
oids, err := d.BatchOidsRedis(c, tp, mid, limit)
ctx.Convey("Then err should be nil.oids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(oids, convey.ShouldNotBeNil)
})
})
}
func TestIsCleaned(t *testing.T) {
var (
c = context.TODO()
typ = int8(2)
mid = int64(88888894)
fid = int64(59)
)
convey.Convey("IsCleaned", t, func(ctx convey.C) {
_, err := d.IsCleaned(c, typ, mid, fid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestSetCleanedCache(t *testing.T) {
var (
c = context.TODO()
typ = int8(2)
mid = int64(88888894)
fid = int64(59)
ftime = int64(88888894)
expire = int64(86400)
)
convey.Convey("SetCleanedCache", t, func(ctx convey.C) {
err := d.SetCleanedCache(c, typ, mid, fid, ftime, expire)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,42 @@
package dao
import (
"context"
"fmt"
"net/url"
"strconv"
schmdl "go-common/app/service/main/riot-search/model"
"go-common/library/log"
"go-common/library/net/metadata"
"go-common/library/xstr"
)
const _search = "http://api.bilibili.co/x/internal/riot-search/arc/ids"
// SearchArcs return archive ids by aids.
func (d *Dao) SearchArcs(c context.Context, keyword string, ids []int64, pn, ps int) (res *schmdl.IDsResp, err error) {
params := url.Values{}
params.Set("ids", xstr.JoinInts(ids))
params.Set("keyword", keyword)
params.Set("pn", strconv.Itoa(pn))
params.Set("ps", strconv.Itoa(ps))
ip := metadata.String(c, metadata.RemoteIP)
var (
resp = &struct {
Code int `json:"code"`
Data *schmdl.IDsResp `json:"data"`
}{}
)
if err = d.httpClient.Post(c, _search, ip, params, &resp); err != nil {
log.Error("s.httpClient.Post(%s) error(%v)", _search+"?"+params.Encode(), err)
return
}
log.Info("searchArcs(%s) error(%v)", _search+"?"+params.Encode(), err)
if resp.Code != 0 {
err = fmt.Errorf("code is:%d", resp.Code)
log.Error("s.httpClient.Post(%s) error(%v)", _search+"?"+params.Encode(), err)
return
}
return resp.Data, nil
}

View File

@@ -0,0 +1,21 @@
package dao
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestSearchArcs(t *testing.T) {
Convey("SearchArcs", t, func() {
var (
keyword = "comic"
ids = []int64{1, 2, 3}
pn = 1
ps = 30
)
err, _ := d.SearchArcs(context.Background(), keyword, ids, pn, ps) // todo:debug uat 502
So(err, ShouldBeNil)
})
}