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,63 @@
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",
"memcache_test.go",
"mysql_test.go",
"redis_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/favorite/conf:go_default_library",
"//app/service/main/favorite/model:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"memcache.go",
"mysql.go",
"redis.go",
],
importpath = "go-common/app/job/main/favorite/dao/fav",
tags = ["automanaged"],
deps = [
"//app/job/main/favorite/conf:go_default_library",
"//app/job/main/favorite/model:go_default_library",
"//app/service/main/favorite/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/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,70 @@
package fav
import (
"context"
"time"
"go-common/app/job/main/favorite/conf"
"go-common/library/cache/memcache"
"go-common/library/cache/redis"
"go-common/library/database/sql"
"go-common/library/log"
)
// Dao favorite dao.
type Dao struct {
db *sql.DB
redis *redis.Pool
mc *memcache.Pool
redisExpire int
mcExpire int32
}
// New new a dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
db: sql.NewMySQL(c.DB.Fav),
// 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),
}
return
}
// Close close all connection.
func (d *Dao) Close() (err error) {
if d.db != nil {
d.db.Close()
}
if d.redis != nil {
d.redis.Close()
}
if d.mc != nil {
d.mc.Close()
}
return
}
// Ping ping all resource.
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.pingRedis(c); err != nil {
log.Error("d.pingRedis error(%v)", err)
return
}
if err = d.pingMC(c); err != nil {
log.Error("d.pingMC error(%v)", err)
return
}
if err = d.pingMySQL(c); err != nil {
log.Error("d.pingMySQL error(%v)", err)
}
return
}
// BeginTran crate a *sql.Tx for database transaction.
func (d *Dao) BeginTran(c context.Context) (*sql.Tx, error) {
return d.db.Begin(c)
}

View File

@@ -0,0 +1,34 @@
package fav
import (
"flag"
"go-common/app/job/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-job")
flag.Set("conf_token", "29adace99d5c5be327ae4a6b70c6582d")
flag.Set("tree_id", "2295")
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-job-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,216 @@
package fav
import (
"context"
"fmt"
favmdl "go-common/app/service/main/favorite/model"
"go-common/library/cache/memcache"
"go-common/library/log"
"strconv"
)
const (
_folder = "f_%d_%d" // key:f_{mid%100}_{fid},value:{*Fodler}.pb
_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
)
// folderMcKey
func folderMcKey(mid, fid int64) string {
return fmt.Sprintf(_folder, mid%100, fid)
}
// relationFidsKey
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)
}
// SetFoldersMc add folders mc cache.
func (d *Dao) SetFoldersMc(c context.Context, vs ...*favmdl.Folder) (err error) {
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
}
// FolderMc return one folder from mc.
func (d *Dao) FolderMc(c context.Context, typ int8, mid, fid int64) (f *favmdl.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(favmdl.Folder)
if err = conn.Scan(item, f); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
f = nil
}
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 := favmdl.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 = favmdl.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, oid int64) (err error) {
var (
key = relationFidsKey(typ, mid, oid)
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
}
// SetOidCountMc return oid count from 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
}
// DelBatchOidsMc delete oids mc cache.
func (d *Dao) DelBatchOidsMc(c context.Context, typ int8, mid int64) (err error) {
key := batchOidsKey(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
}
// 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
}

View File

@@ -0,0 +1,234 @@
package fav
import (
"context"
favmdl "go-common/app/service/main/favorite/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestFavfolderMcKey(t *testing.T) {
convey.Convey("folderMcKey", t, func(ctx convey.C) {
var (
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavrelationFidsKey(t *testing.T) {
convey.Convey("relationFidsKey", t, func(ctx convey.C) {
var (
typ = int8(0)
mid = int64(0)
oid = int64(0)
)
ctx.Convey("When everything goes positive", 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) {
convey.Convey("oidCountKey", t, func(ctx convey.C) {
var (
typ = int8(0)
oid = int64(0)
)
ctx.Convey("When everything goes positive", 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) {
convey.Convey("batchOidsKey", t, func(ctx convey.C) {
var (
typ = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavrecentOidsKey(t *testing.T) {
convey.Convey("recentOidsKey", t, func(ctx convey.C) {
var (
typ = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := recentOidsKey(typ, mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestFavPingMC(t *testing.T) {
convey.Convey("PingMC", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", 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) {
convey.Convey("SetFoldersMc", t, func(ctx convey.C) {
var (
c = context.Background()
vs = &favmdl.Folder{}
)
ctx.Convey("When everything goes positive", 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 TestFavFolderMc(t *testing.T) {
convey.Convey("FolderMc", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavSetRelaitonFidsMc(t *testing.T) {
convey.Convey("SetRelaitonFidsMc", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
mid = int64(0)
oid = int64(0)
fids = []int64{}
)
ctx.Convey("When everything goes positive", 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) {
convey.Convey("RelaitonFidsMc", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
mid = int64(0)
oid = int64(0)
)
ctx.Convey("When everything goes positive", 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) {
convey.Convey("DelRelationFidsMc", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
mid = int64(0)
oid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelRelationFidsMc(c, typ, mid, oid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestFavSetOidCountMc(t *testing.T) {
convey.Convey("SetOidCountMc", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
oid = int64(0)
count = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavDelBatchOidsMc(t *testing.T) {
convey.Convey("DelBatchOidsMc", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelBatchOidsMc(c, typ, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestFavDelRecentOidsMc(t *testing.T) {
convey.Convey("DelRecentOidsMc", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelRecentOidsMc(c, typ, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,403 @@
package fav
import (
"context"
"database/sql"
"fmt"
favmdl "go-common/app/service/main/favorite/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
xtime "go-common/library/time"
"go-common/library/xstr"
)
const (
_countSharding int64 = 50 // count by oid
_folderSharding int64 = 100 // folder by mid
_relationSharding int64 = 500 // relations in folder by mid
_usersSharding int64 = 500 // users by oid
// folder
_folderSQL = "SELECT id,type,mid,name,cover,description,count,attr,state,ctime,mtime FROM fav_folder_%s WHERE id=? AND type=? AND mid=? AND state=0"
_upFolderCntSQL = "UPDATE fav_folder_%s SET count=?,mtime=? WHERE id=?"
// relation
_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 mtime>=? AND state=0 AND type=? ORDER BY mtime ASC LIMIT ?"
_allRelationsSQL = "SELECT id,type,oid,mid,fid,state,ctime,mtime,sequence FROM fav_relation_%s FORCE INDEX(ix_fid_state_mtime) WHERE fid=? AND mtime>=? AND state=0 ORDER BY mtime ASC LIMIT ?"
_relationOidsSQL = "SELECT oid FROM fav_relation_%s FORCE INDEX(ix_fid_state_type_mtime) WHERE fid=? AND state=0 AND type=? ORDER BY mtime DESC LIMIT ?,?"
_relationFidsByOidSQL = "SELECT fid FROM fav_relation_%s WHERE type=? AND mid=? AND oid=? and state=0"
_relationFidsSQL = "SELECT oid,fid FROM fav_relation_%s WHERE type=? AND mid=? AND state=0"
_cntRelationSQL = "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,sequence) VALUES(?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE state=?,mtime=?,sequence=?"
_delRelationSQL = "UPDATE fav_relation_%s SET state=1,mtime=? WHERE type=? AND fid=? AND oid=?"
_delRelationsByOidsSQL = "UPDATE fav_relation_%s SET state=1,mtime=? WHERE type=? AND fid=? AND oid in (%s)"
_selRelationsByOidsSql = "SELECT id,type,oid,mid,fid,state,ctime,mtime,sequence FROM fav_relation_%s WHERE type=? AND fid=? AND oid in (%s)"
_updateAllRelationSeq = "UPDATE fav_relation_%s SET mtime=mtime,sequence=(case id %s end) WHERE id in (%s)"
_updateRelationSeq = "UPDATE fav_relation_%s SET sequence=?,mtime=? WHERE fid=? and oid=? and type=?"
_selectMaxSequence = "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 1"
_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"
// users
_addUserSQL = "INSERT INTO fav_users_%s (type,oid,mid,state,ctime,mtime) VALUES(?,?,?,?,?,?) ON DUPLICATE KEY UPDATE state=?,mtime=?"
_delUserSQL = "UPDATE fav_users_%s SET state=1,mtime=? WHERE type=? AND oid=? AND mid=?"
// stat
_statCntSQL = "SELECT count from fav_count_%s WHERE type=? AND oid=?"
_upStatCntSQL = "INSERT INTO fav_count_%s (type,oid,count,ctime,mtime) VALUES(?,?,?,?,?) ON DUPLICATE KEY UPDATE count=count+?,mtime=?"
)
func cntHit(mid int64) string {
return fmt.Sprintf("%02d", mid%_countSharding)
}
func folderHit(mid int64) string {
return fmt.Sprintf("%02d", mid%_folderSharding)
}
func relationHit(mid int64) string {
return fmt.Sprintf("%03d", mid%_relationSharding)
}
func usersHit(oid int64) string {
return fmt.Sprintf("%03d", oid%_usersSharding)
}
// pingMySQL check mysql connection.
func (d *Dao) pingMySQL(c context.Context) error {
return d.db.Ping(c)
}
func (d *Dao) TxUpdateFavSequence(tx *xsql.Tx, mid int64, fid, oid int64, typ int8, sequence uint64, mtime xtime.Time) (rows int64, err error) {
res, err := tx.Exec(fmt.Sprintf(_updateRelationSeq, relationHit(mid)), sequence, mtime, fid, oid, typ)
if err != nil {
log.Error("d.db.Exec(%s,%d,%d,%d,%d,%d) error(%v)", _updateRelationSeq, sequence, mtime, fid, oid, typ, err)
return
}
return res.RowsAffected()
}
func (d *Dao) BatchUpdateSeq(c context.Context, mid int64, favs []*favmdl.Favorite) (int64, error) {
var caseStr string
var ids []int64
for _, fav := range favs {
ids = append(ids, fav.ID)
caseStr += fmt.Sprintf("when %d then %d ", fav.ID, fav.Sequence)
}
if len(ids) <= 0 {
return 0, nil
}
res, err := d.db.Exec(c, fmt.Sprintf(_updateAllRelationSeq, relationHit(mid), caseStr, xstr.JoinInts(ids)))
if err != nil {
return 0, err
}
return res.RowsAffected()
}
// RecentRes return user's three newest fav from a folder.
func (d *Dao) RecentRes(c context.Context, mid, fid int64) (res []*favmdl.Resource, err error) {
rows, err := d.db.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, &favmdl.Resource{Oid: oid, Typ: typ})
}
err = rows.Err()
return
}
// Folder get a Folder by fid from mysql.
func (d *Dao) Folder(c context.Context, tp int8, mid, fid int64) (f *favmdl.Folder, err error) {
f = &favmdl.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
}
// UpFolderCnt update folder count to mysql.
func (d *Dao) UpFolderCnt(c context.Context, mid, fid int64, cnt int, now xtime.Time) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_upFolderCntSQL, folderHit(mid)), cnt, now, fid)
if err != nil {
log.Error("db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// MaxRelation get a max sequence relation from mysql.
func (d *Dao) MaxRelation(c context.Context, mid, fid int64) (m *favmdl.Favorite, err error) {
m = &favmdl.Favorite{}
row := d.db.QueryRow(c, fmt.Sprintf(_selectMaxSequence, relationHit(mid)), fid)
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
}
// Relation get a relation from mysql.
func (d *Dao) Relation(c context.Context, tp int8, mid, fid, oid int64) (m *favmdl.Favorite, err error) {
m = &favmdl.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
}
// AllRelations get favorite relations from mysql.
func (d *Dao) AllRelations(c context.Context, mid, fid int64, mtime xtime.Time, limit int) (fr []*favmdl.Favorite, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_allRelationsSQL, relationHit(mid)), fid, mtime, limit)
if err != nil {
log.Error("d.db.Query(%s,%d,%d,%d,%d) error(%v)", fmt.Sprintf(_allRelationsSQL, relationHit(mid)), mid, fid, mtime, limit, err)
return
}
defer rows.Close()
fr = make([]*favmdl.Favorite, 0)
for rows.Next() {
var r = &favmdl.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)", mid, fid)
continue
}
fr = append(fr, r)
}
err = rows.Err()
return
}
func (d *Dao) RelationsByOids(c context.Context, typ int8, mid, fid int64, oids []int64) (fr []*favmdl.Favorite, err error) {
if len(oids) <= 0 {
return
}
rows, err := d.db.Query(c, fmt.Sprintf(_selRelationsByOidsSql, relationHit(mid), xstr.JoinInts(oids)), typ, fid)
if err != nil {
log.Error("d.db.Query(%s,%d,%d,%v) error(%v)", fmt.Sprintf(_selRelationsByOidsSql, relationHit(mid), xstr.JoinInts(oids)), typ, fid, err)
return
}
defer rows.Close()
fr = make([]*favmdl.Favorite, 0)
for rows.Next() {
var r = &favmdl.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
}
// Relations get favorite relations from mysql.
func (d *Dao) Relations(c context.Context, typ int8, mid, fid int64, mtime xtime.Time, limit int) (fr []*favmdl.Favorite, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_relationsSQL, relationHit(mid)), fid, mtime, typ, limit)
if err != nil {
log.Error("d.db.Query(%s,%d,%d,%d,%d,%d) error(%v)", fmt.Sprintf(_relationsSQL, relationHit(mid)), typ, mid, fid, mtime, limit, err)
return
}
defer rows.Close()
fr = make([]*favmdl.Favorite, 0)
for rows.Next() {
var r = &favmdl.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)", typ, mid, fid)
continue
}
fr = append(fr, r)
}
err = rows.Err()
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
idx = relationHit(mid)
)
rows, err := d.db.Query(c, fmt.Sprintf(_relationFidsByOidSQL, idx), tp, mid, oid)
if err != nil {
log.Error("d.db.QueryRow(%d,%d,%d) error(%v)", mid, 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
}
// RelationCnt get favoried folders count in relation from mysql.
func (d *Dao) RelationCnt(c context.Context, mid, fid int64) (cnt int, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_cntRelationSQL, relationHit(mid)), fid)
if err = row.Scan(&cnt); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// AddRelation add a favorite relation to mysql.
func (d *Dao) AddRelation(c context.Context, fr *favmdl.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.Sequence, fr.State, fr.MTime, fr.Sequence)
if err != nil {
log.Error("db.Exec error(%v)", err)
return
}
return res.LastInsertId()
}
// DelRelation delete a favorite relation to mysql.
func (d *Dao) DelRelation(c context.Context, tp int8, mid, fid, oid int64, now xtime.Time) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_delRelationSQL, relationHit(mid)), now, 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()
}
// UpStatCnt update stat count to mysql.
func (d *Dao) UpStatCnt(c context.Context, tp int8, oid int64, incr int, now xtime.Time) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_upStatCntSQL, cntHit(oid)), tp, oid, incr, now, now, incr, now)
if err != nil {
log.Error("db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// StatCnt return stat count from mysql.
func (d *Dao) StatCnt(c context.Context, tp int8, oid int64) (cnt int, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_statCntSQL, cntHit(oid)), tp, oid)
if err = row.Scan(&cnt); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// RelationFids get the map [oid -> "fid,fid"] for user.
func (d *Dao) RelationFids(c context.Context, tp int8, mid int64) (rfids map[int64][]int64, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_relationFidsSQL, relationHit(mid)), tp, mid)
if err != nil {
log.Error("db.Query(%d) error(%v)", mid, err)
return
}
defer rows.Close()
rfids = make(map[int64][]int64, 128)
for rows.Next() {
var oid, fid int64
if err = rows.Scan(&oid, &fid); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
rfids[oid] = append(rfids[oid], fid)
}
err = rows.Err()
return
}
// OidsByFid get oids by fid
func (d *Dao) OidsByFid(c context.Context, typ int8, mid, fid int64, offset, limit int) (oids []int64, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_relationOidsSQL, relationHit(mid)), fid, typ, offset, limit)
if err != nil {
log.Error("d.db.Query(%d,%d,%d,%d,%d) error(%v)", typ, mid, fid, offset, limit, err)
return
}
defer rows.Close()
oid := int64(0)
oids = make([]int64, 0, limit)
for rows.Next() {
if err = rows.Scan(&oid); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
oids = append(oids, oid)
}
err = rows.Err()
return
}
// DelRelationsByOids update del state by fid and oids.
func (d *Dao) DelRelationsByOids(c context.Context, typ int8, mid, fid int64, oids []int64, now xtime.Time) (rows int64, err error) {
if len(oids) == 0 {
return
}
oidsStr := xstr.JoinInts(oids)
res, err := d.db.Exec(c, fmt.Sprintf(_delRelationsByOidsSQL, relationHit(mid), oidsStr), now, typ, fid)
if err != nil {
log.Error("d.db.Exec(%d,%d,%d,%s) error(%v)", typ, mid, fid, oidsStr, now, err)
return
}
return res.RowsAffected()
}
// AddUser add a favorite user to mysql.
func (d *Dao) AddUser(c context.Context, u *favmdl.User) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_addUserSQL, usersHit(u.Oid)), u.Type, u.Oid, u.Mid, u.State, u.CTime, u.MTime, u.State, u.MTime)
if err != nil {
log.Error("db.Exec error(%v)", err)
return
}
return res.LastInsertId()
}
// DelUser delete a favorite user.
func (d *Dao) DelUser(c context.Context, u *favmdl.User) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_delUserSQL, usersHit(u.Oid)), u.MTime, u.Type, u.Oid, u.Mid)
if err != nil {
log.Error("d.db.Exec(%d,%d,%d,%d) error(%v)", u.MTime, u.Type, u.Oid, u.Mid, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,395 @@
package fav
import (
"context"
favmdl "go-common/app/service/main/favorite/model"
xtime "go-common/library/time"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestFavcntHit(t *testing.T) {
convey.Convey("cntHit", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := cntHit(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestFavfolderHit(t *testing.T) {
convey.Convey("folderHit", t, func(ctx convey.C) {
var (
mid = int64(1)
)
ctx.Convey("When everything goes positive", 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) {
convey.Convey("relationHit", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything goes positive", 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) {
convey.Convey("usersHit", t, func(ctx convey.C) {
var (
oid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavPingMySQL(t *testing.T) {
convey.Convey("PingMySQL", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", 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 TestRecentFolder(t *testing.T) {
convey.Convey("Folder Recents", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.RecentRes(c, mid, fid)
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestUpdateFavSequence(t *testing.T) {
convey.Convey("Test Update Sequence", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
t, err := d.BeginTran(c)
ctx.So(err, convey.ShouldBeNil)
_, err = d.TxUpdateFavSequence(t, mid, fid, 1, 2, 123, xtime.Time(0))
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavFolder(t *testing.T) {
convey.Convey("Folder", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, 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)
})
})
})
}
func TestFavUpFolderCnt(t *testing.T) {
convey.Convey("UpFolderCnt", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
fid = int64(0)
cnt = int(0)
now xtime.Time
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.UpFolderCnt(c, mid, fid, cnt, now)
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) {
convey.Convey("Relation", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
mid = int64(0)
fid = int64(0)
oid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavRelations(t *testing.T) {
convey.Convey("Relations", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
mid = int64(0)
fid = int64(0)
mtime xtime.Time
limit = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
fr, err := d.Relations(c, typ, mid, fid, mtime, limit)
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 TestFavRelationFidsByOid(t *testing.T) {
convey.Convey("RelationFidsByOid", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
mid = int64(0)
oid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavRelationCnt(t *testing.T) {
convey.Convey("RelationCnt", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
cnt, err := d.RelationCnt(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 TestFavAddRelation(t *testing.T) {
convey.Convey("AddRelation", t, func(ctx convey.C) {
var (
c = context.Background()
fr = &favmdl.Favorite{}
)
ctx.Convey("When everything goes positive", 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 TestFavDelRelation(t *testing.T) {
convey.Convey("DelRelation", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
mid = int64(0)
fid = int64(0)
oid = int64(0)
now xtime.Time
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.DelRelation(c, tp, mid, fid, oid, now)
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 TestFavUpStatCnt(t *testing.T) {
convey.Convey("UpStatCnt", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
oid = int64(0)
incr = int(0)
now xtime.Time
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.UpStatCnt(c, tp, oid, incr, now)
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 TestFavStatCnt(t *testing.T) {
convey.Convey("StatCnt", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
oid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
cnt, err := d.StatCnt(c, tp, oid)
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 TestFavRelationFids(t *testing.T) {
convey.Convey("RelationFids", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rfids, err := d.RelationFids(c, tp, mid)
ctx.Convey("Then err should be nil.rfids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rfids, convey.ShouldNotBeNil)
})
})
})
}
func TestFavOidsByFid(t *testing.T) {
convey.Convey("OidsByFid", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
mid = int64(0)
fid = int64(0)
offset = int(0)
limit = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
oids, err := d.OidsByFid(c, typ, mid, fid, offset, 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 TestFavDelRelationsByOids(t *testing.T) {
convey.Convey("DelRelationsByOids", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
mid = int64(0)
fid = int64(0)
oids = []int64{}
now xtime.Time
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.DelRelationsByOids(c, typ, mid, fid, oids, now)
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 TestFavAddUser(t *testing.T) {
convey.Convey("AddUser", t, func(ctx convey.C) {
var (
c = context.Background()
u = &favmdl.User{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.AddUser(c, u)
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 TestFavDelUser(t *testing.T) {
convey.Convey("DelUser", t, func(ctx convey.C) {
var (
c = context.Background()
u = &favmdl.User{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.DelUser(c, u)
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 TestRelationsByOids(t *testing.T) {
convey.Convey("RelationsByOids", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
favs, err := d.RelationsByOids(c, 2, 1501, 168, []int64{10108138})
ctx.So(err, convey.ShouldBeNil)
ctx.So(favs, convey.ShouldNotBeNil)
_, err = d.BatchUpdateSeq(c, 76, favs)
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,558 @@
package fav
import (
"context"
"encoding/json"
"fmt"
"go-common/app/job/main/favorite/model"
favmdl "go-common/app/service/main/favorite/model"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
// _fid list fid => ([]model.Cover)
_covers = "fcs_"
_folderKey = "fi_%d_%d" // sortedset f_type_mid value:fid,score:ctime
_oldRelationKey = "r_%d_%d_%d"
_allRelationKey = "ar_%d_%d"
_relationKey = "r_%d_%d" // sortedset r_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
)
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)
}
// allRelationKey return folder relation key.
func allRelationKey(mid, fid int64) string {
return fmt.Sprintf(_allRelationKey, mid, fid)
}
// oldRelationKey return folder relation key.
func oldRelationKey(typ int8, mid, fid int64) string {
return fmt.Sprintf(_oldRelationKey, typ, mid, fid)
}
// relationOidsKey return a user oids key.
func relationOidsKey(tp int8, mid int64) string {
return fmt.Sprintf(_relationOidsKey, tp, mid)
}
// cleanKey return user whether cleaned key.
func cleanedKey(tp int8, mid int64) string {
return fmt.Sprintf(_cleanedKey, tp, mid)
}
// redisKey make key for redis by prefix and mid
func coversKey(mid, fid int64) string {
return fmt.Sprintf("%s%d_%d", _covers, mid, fid)
}
// PingRedis ping connection success.
func (d *Dao) pingRedis(c context.Context) (err error) {
conn := d.redis.Get(c)
_, err = conn.Do("SET", "PING", "PONG")
conn.Close()
return
}
// DelNewCoverCache delete cover picture cache.
func (d *Dao) DelNewCoverCache(c context.Context, mid, fid int64) (err error) {
var (
key = coversKey(mid, fid)
conn = d.redis.Get(c)
)
defer conn.Close()
if _, err = conn.Do("DEL", key); err != nil {
log.Error("DEL %v failed error(%v)", key, 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
}
// 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
}
// 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
}
// 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
}
// FolderCache return a favorite folder from redis.
func (d *Dao) FolderCache(c context.Context, tp int8, mid, fid int64) (folder *favmdl.Folder, err error) {
var (
value []byte
key = folderKey(tp, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
if value, err = redis.Bytes(conn.Do("HGET", key, fid)); err != nil {
if err == redis.ErrNil {
err = nil
folder = nil
} else {
log.Error("conn.Do(HGET, %v, %v) error(%v)", key, fid, err)
}
return
}
folder = &favmdl.Folder{}
if err = json.Unmarshal(value, folder); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", value, err)
}
return
}
// DefaultFolderCache return default favorite folder from redis.
func (d *Dao) DefaultFolderCache(c context.Context, tp int8, mid int64) (folder *favmdl.Folder, err error) {
var res map[int64]*favmdl.Folder
if res, err = d.foldersCache(c, tp, mid); err != nil {
return
}
if res == nil {
return
}
for _, folder = range res {
if folder.IsDefault() {
return
}
}
folder = nil
return
}
// foldersCache return the user all folders from redis.
func (d *Dao) foldersCache(c context.Context, tp int8, mid int64) (res map[int64]*favmdl.Folder, err error) {
var (
values map[string]string
key = folderKey(tp, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
if values, err = redis.StringMap(conn.Do("HGETALL", key)); err != nil {
if err == redis.ErrNil {
return nil, nil
}
log.Error("conn.Do(HGETALL %s) error(%v)", key, err)
return
}
res = make(map[int64]*favmdl.Folder, len(res))
for _, data := range values {
folder := &favmdl.Folder{}
if err = json.Unmarshal([]byte(data), folder); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", data, err)
return
}
res[folder.ID] = folder
}
return
}
// RelationCntCache return the folder all relation count from redis.
func (d *Dao) RelationCntCache(c context.Context, mid, fid int64) (cnt int, err error) {
key := relationKey(mid, fid)
conn := d.redis.Get(c)
defer conn.Close()
if cnt, err = redis.Int(conn.Do("ZCARD", key)); err != nil {
if err == redis.ErrNil {
return model.CacheNotFound, nil
}
log.Error("conn.Do(ZCARD %s) error(%v)", key, err)
}
return
}
// MaxScore get the max score from sorted set
func (d *Dao) MaxScore(c context.Context, m *favmdl.Favorite) (score int64, err error) {
key := allRelationKey(m.Mid, m.Fid)
conn := d.redis.Get(c)
defer conn.Close()
values, err := redis.Values(conn.Do("ZREVRANGE", key, 0, 0, "WITHSCORES"))
if err != nil {
log.Error("conn.Do(ZREVRANGE, %s) error(%v)", key, err)
return
}
if len(values) != 2 {
err = fmt.Errorf("redis zrange items(%v) length not 2", values)
return
}
var id int64
redis.Scan(values, &id, &score)
return
}
// AddAllRelationCache add a relation to redis.
func (d *Dao) AddAllRelationCache(c context.Context, m *favmdl.Favorite) (err error) {
key := allRelationKey(m.Mid, m.Fid)
score, err := d.MaxScore(c, m)
if err != nil {
return
}
if score <= 0 {
log.Error("dao.AddAllRelationCache invalid score(%d)!%+v", d, *m)
return
}
seq := int64(score/1e10) + 1
conn := d.redis.Get(c)
defer conn.Close()
if err = conn.Send("ZADD", key, seq*1e10+int64(m.MTime), m.Oid*100+int64(m.Type)); 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
}
// AddRelationCache add a relation to redis.
func (d *Dao) AddRelationCache(c context.Context, m *favmdl.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
}
// AddAllRelationsCache add a relation to redis.
func (d *Dao) AddAllRelationsCache(c context.Context, mid, fid int64, fs []*favmdl.Favorite) (err error) {
key := allRelationKey(mid, fid)
conn := d.redis.Get(c)
defer conn.Close()
for _, fav := range fs {
if err = conn.Send("ZADD", key, fav.Sequence*1e10+uint64(fav.MTime), fav.Oid*100+int64(fav.Type)); err != nil {
log.Error("conn.Send(ZADD %s,%d) error(%v)", key, fav.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 < len(fs)+1; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
// AddRelationsCache add a relation to redis.
func (d *Dao) AddRelationsCache(c context.Context, tp int8, mid, fid int64, fs []*favmdl.Favorite) (err error) {
key := relationKey(mid, fid)
conn := d.redis.Get(c)
defer conn.Close()
for _, fav := range fs {
if err = conn.Send("ZADD", key, fav.MTime, fav.Oid); err != nil {
log.Error("conn.Send(ZADD %s,%d) error(%v)", key, fav.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 < len(fs)+1; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
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
}
// DelAllRelationsCache 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
}
// DelRelationCache delete one relation cache.
func (d *Dao) DelRelationCache(c context.Context, mid, fid, oid int64) (err error) {
key := relationKey(mid, fid)
conn := d.redis.Get(c)
defer conn.Close()
if err = conn.Send("ZREM", 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
}
// DelAllRelationCache delete one relation cache.
func (d *Dao) DelAllRelationCache(c context.Context, mid, fid, oid int64, typ int8) (err error) {
key := allRelationKey(mid, fid)
conn := d.redis.Get(c)
defer conn.Close()
if err = conn.Send("ZREM", key, oid*100+int64(typ)); 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
}
// DelOldRelationsCache delete the folder relation cache. TODO:del at 2018.06.08
func (d *Dao) DelOldRelationsCache(c context.Context, typ int8, mid, fid int64) (err error) {
key := oldRelationKey(typ, 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
}
// 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 favoured 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.Do("SREM", key, oid); err != nil {
log.Error("conn.Do(%s,%d) error(%v)", key, oid, err)
}
return
}
// SetRelationOidsCache set favoured oids .
func (d *Dao) SetRelationOidsCache(c context.Context, tp int8, mid int64, oids []int64) (err error) {
var (
key = relationOidsKey(tp, mid)
conn = d.redis.Get(c)
)
defer conn.Close()
for _, oid := range oids {
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 < len(oids)+1; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
// SetCleanedCache .
func (d *Dao) SetCleanedCache(c context.Context, typ int8, mid, fid, ftime, expire int64) (err error) {
var (
key = cleanedKey(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)
return
}
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,462 @@
package fav
import (
"context"
favmdl "go-common/app/service/main/favorite/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestPing(t *testing.T) {
convey.Convey("ping test", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.Ping(c)
ctx.So(err, convey.ShouldBeNil)
err = d.pingMC(c)
ctx.So(err, convey.ShouldBeNil)
err = d.pingMySQL(c)
ctx.So(err, convey.ShouldBeNil)
err = d.pingRedis(c)
ctx.So(err, convey.ShouldBeNil)
t, err := d.BeginTran(c)
ctx.So(err, convey.ShouldBeNil)
ctx.So(t, convey.ShouldNotBeNil)
t.Rollback()
})
})
}
func TestDelNewCovers(t *testing.T) {
convey.Convey("del covers", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelNewCoverCache(c, mid, fid)
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestExpireAll(t *testing.T) {
convey.Convey("ExpireAll", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.ExpireAllRelations(c, mid, fid)
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDelAll(t *testing.T) {
convey.Convey("DelAll", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelAllRelationCache(c, mid, fid, 1, 2)
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestFavfavedBitKey(t *testing.T) {
convey.Convey("favedBitKey", t, func(ctx convey.C) {
var (
tp = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavfolderKey(t *testing.T) {
convey.Convey("folderKey", t, func(ctx convey.C) {
var (
tp = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", 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) {
convey.Convey("relationKey", t, func(ctx convey.C) {
var (
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavoldRelationKey(t *testing.T) {
convey.Convey("oldRelationKey", t, func(ctx convey.C) {
var (
typ = int8(0)
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := oldRelationKey(typ, mid, fid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestFavrelationOidsKey(t *testing.T) {
convey.Convey("relationOidsKey", t, func(ctx convey.C) {
var (
tp = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavcleanedKey(t *testing.T) {
convey.Convey("cleanedKey", t, func(ctx convey.C) {
var (
tp = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := cleanedKey(tp, mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestFavSetUnFavedBit(t *testing.T) {
convey.Convey("SetUnFavedBit", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavSetFavedBit(t *testing.T) {
convey.Convey("SetFavedBit", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavExpireRelations(t *testing.T) {
convey.Convey("ExpireRelations", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavFolderCache(t *testing.T) {
convey.Convey("FolderCache", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(1)
mid = int64(1)
fid = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.FolderCache(c, tp, mid, fid)
ctx.Convey("Then err should be nil.folder should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestFavDefaultFolderCache(t *testing.T) {
convey.Convey("DefaultFolderCache", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(1)
mid = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.DefaultFolderCache(c, tp, mid)
ctx.Convey("Then err should be nil.folder should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestFavfoldersCache(t *testing.T) {
convey.Convey("foldersCache", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(1)
mid = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.foldersCache(c, tp, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestFavRelationCntCache(t *testing.T) {
convey.Convey("RelationCntCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
cnt, err := d.RelationCntCache(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 TestFavAddRelationCache(t *testing.T) {
convey.Convey("AddRelationCache", t, func(ctx convey.C) {
var (
c = context.Background()
m = &favmdl.Favorite{}
)
ctx.Convey("When everything goes positive", 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 TestFavAddRelationsCache(t *testing.T) {
convey.Convey("AddRelationsCache", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
mid = int64(0)
fid = int64(0)
fs = []*favmdl.Favorite{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.AddRelationsCache(c, tp, mid, fid, fs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestFavDelRelationsCache(t *testing.T) {
convey.Convey("DelRelationsCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavDelRelationCache(t *testing.T) {
convey.Convey("DelRelationCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
fid = int64(0)
oid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelRelationCache(c, mid, fid, oid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestFavDelOldRelationsCache(t *testing.T) {
convey.Convey("DelOldRelationsCache", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
mid = int64(0)
fid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelOldRelationsCache(c, typ, mid, fid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestFavExpireRelationOids(t *testing.T) {
convey.Convey("ExpireRelationOids", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", 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) {
convey.Convey("AddRelationOidCache", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
mid = int64(0)
oid = int64(0)
)
ctx.Convey("When everything goes positive", 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) {
convey.Convey("RemRelationOidCache", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
mid = int64(0)
oid = int64(0)
)
ctx.Convey("When everything goes positive", 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 TestFavSetRelationOidsCache(t *testing.T) {
convey.Convey("SetRelationOidsCache", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int8(0)
mid = int64(0)
oids = []int64{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetRelationOidsCache(c, tp, mid, oids)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestFavSetCleanedCache(t *testing.T) {
convey.Convey("SetCleanedCache", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
mid = int64(0)
fid = int64(0)
ftime = int64(0)
expire = int64(0)
)
ctx.Convey("When everything goes positive", 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)
})
})
})
}
func TestFavDelRelationOidsCache(t *testing.T) {
convey.Convey("DelRelationOidsCache", t, func(ctx convey.C) {
var (
c = context.Background()
typ = int8(0)
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelRelationOidsCache(c, typ, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}