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,22 @@
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/service/main/favorite/api:all-srcs",
"//app/service/main/favorite/cmd:all-srcs",
"//app/service/main/favorite/conf:all-srcs",
"//app/service/main/favorite/dao:all-srcs",
"//app/service/main/favorite/model:all-srcs",
"//app/service/main/favorite/server:all-srcs",
"//app/service/main/favorite/service:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,476 @@
#### favorite
### Version v7.5.1
> 1. close senstive
### Version v7.5.0
> 1. 播单2.0
### Version v7.4.3
> 1. 收藏夹权限问题修复
### Version v7.4.1
> 1. dao 目录迁移
### Version v7.4.0
> 1. grpc client v1迁移到api
### Version v7.3.0
> 1. 播单支持管理员修改
### Version v7.2.0
> 1. medialist fix bug
### Version v7.0.1
> 1. medialist
### Version v7.0.0
> 1. medialist
### Version v6.0.3
> 1. 最近三条收藏 force index
### Version v6.0.1
> 1. 并发读db获取最近三条收藏
### Version v6.0.0
> 1. 收藏夹停止双写
### Version v4.11.7
> 1. fix rank service err
### Version v4.11.5
> 1. fix grpc server err
### Version v4.11.4
> 1. recentfavs bit
### Version v4.11.3
> 1. fix del bug
### Version 4.11.1
> 1. fix rank pagesize
### Version 4.11.0
> 1. riot and rank search
### Version 4.10.7
> 1. add db rows.error
### Version 4.10.6
> 1. change dir to api
### Version 4.9.15
> 1. upgrade to grpc
### Version 4.9.14
> 1. ut dao test
### Version 4.9.13
> 1. meta data ip
### Version 4.9.11
> 1. change indentify to grpc
### Version 4.9.10
> 1. rm video service
### Version 4.9.8
> 1. common config
### Version 4.9.7
> 1. fix two same folder
### Version 4.9.6
> 1. batchOids ylf db
### Version 4.9.5
> 1. fix batchOids unFavedBit
### Version 4.9.4
> 1. fix oidsCount mc
### Version 4.9.3
> 1. migrate relation redis
### Version 4.9.2
> 1. add api oids count
> 2. add api get 1000 favorites
> 3. fix marshal panic
### Version 4.9.1
> 1. fix favoreds null
### Version 4.9.0
> 1. update infoc sdk
##### Version 4.8.13
> 1.add oid count api
##### Version 4.8.12
> 1.fix http isFavoreds
##### Version 4.8.11
> 1.fix index ix_fid_state_mtime
##### Version 4.8.10
> 1.force index uk_fid_oid_type
##### Version 4.8.9
> 1.add read mysql for push service
##### Version 4.8.9
> 1.add read mysql for push service
##### Version 4.8.7
> 1.fix relation recent oids cache
##### Version 4.8.6
> 1.优化relation cache
##### Version 4.8.4
> 1.增加 userFolder rpc
##### Version 4.8.3
> 1.增加register
##### Version 4.8.2
> 1.add users api for push service
> 2.add fav and del fav move to job
##### Version 4.8.1
> 1.add isFavs rpc
##### Version 4.8.0
> 1.migrate main path
##### Version 4.7.4
> 1.使用account-service v7
##### Version 4.7.3
> 1.fix cleanvideo http response
##### Version 4.7.2
> 1.add bm verfiy setMid
##### Version 4.7.1
> 1.fix batch archives3
##### Version 4.7.0
> 1.migrate blademaster
##### Version 4.6.11
> 1.fix fav dao test
##### Version 4.6.10
> 1.delete statsd
##### Version 4.6.9
> 1.fix save cache context
##### Version 4.6.8
> 1.fix is faved
##### Version 4.6.7
> 1.fav bit redis key mid reverse
##### Version 4.6.6
> 1.迁移 accRPC profile2 myinfo 到 userinfo
##### Version 4.6.5
> 1.fix folder cover by removed unnormal archive's cover
##### Version 4.6.4
> 1.get archives by pages from arc rpc
##### Version 4.6.2
> 1.topic fav read platform table
##### Version 4.6.1
> 1.fix topic fav double write
##### Version 4.6.0
> 1.platform api support
##### Version 4.5.7
> 1.fix mysql clsoe
##### Version 4.5.6
> 1.fix err1 for double write topic to platform
##### Version 4.5.5
> 1.remove folder description \n filtering rule
> 2.fix add fav topic verify
> 3.double write topic to platform
##### Version 4.5.2
> 1.change ecode NoLogin to RequestErr
> 2.httpClient topic upgrade
> 3.remove innerRouter and localRouter
##### Version 4.5.1
> 1.fix get default folder
##### Version 4.5.0
> 1.收藏夹信息存储换用mc
##### Version 4.4.3
> 1.add redis expire
##### Version 4.4.2
> 1.fix invalid cache
##### Version 4.4.1
> 1.folder count 接口
##### Version 4.4.0
> 1.support playlist
##### Version 4.3.11
> 1.archiveRPC archive3
##### Version 4.3.8
> 1.http rounter config
##### Version 4.3.7
> 1.支持supervisor
> 2.添加收藏 取消收藏放到service
##### Version 4.3.5
> 1.httpclient 去掉appkey
##### Version 4.3.2
> 1.全站实名制支持
##### Version 4.3.1
> 1.账号封禁下的收藏操作提示信息修正
##### Version 4.3.0
> 1.一键清理失效视频
##### Version 4.2.11
> 1.优化添加收藏的反作弊上报
##### Version 4.2.10
> 1.get three aids sql force index
##### Version 4.2.8
> 1.fix rename 默认收藏夹
> 2.fix move arcs sql deadlock
##### Version 4.2.7
> 1.fix folder sort index out of range
##### Version 4.2.6
> 1.fix sort err
##### Version 4.2.5
> 1.infoc nil fix
> 2.文章favmdl包名修改
##### Version 4.2.1
> 1.迁移RPC Client
> 2.整合model
##### Version 4.2.0
> 1.接入大仓库
##### Version 4.1.2
> 1.fix 收藏平台type范围
##### Version 4.1.1
> 1.上报日志到AI
##### Version 4.1.0
> 1.http gotest
> 2.接入log agent
> 3.敏感词接入
##### Version 4.0.4
> 1.antispam fix
##### Version 4.0.3
> 1.prome withtimer
##### Version 4.0.2
> 1.fix fids outof range
##### Version 4.0.1
> 1.search fix
##### Version 4.0.0
> 1.收藏平台化
##### Version 3.8.8
> 1.prometheus
##### Version 3.8.7
> 1.fix antispam path
##### Version 3.8.6
> 1.接入防刷 antispam
##### Version 3.8.5
> 1.添加收藏和删除收藏DB操作移至job
##### Version 3.8.4
> 1.add supervision
##### Version 3.8.3
> 1.话题收藏兼容移动端https
##### Version 3.8.2
> 1.视频收藏接入新版搜索
> 2.添加搜索敏感词errcode
##### Version 3.8.1
> 1.视频收藏接入新版搜索
##### Version 3.8.0
> 1.代码reset
> 2.修复访客状态下私密收藏夹展示问题
##### Version 3.7.5
> 1.修复访客状态下私密收藏夹展示问题
##### Version 3.7.3
> 1.fix 私密收藏夹展示问题
##### Version 3.7.2
> 1.fix私密收藏夹展示问题
##### Version 3.7.1
> 1.最近收藏结构优化
##### Version 3.7.0
> 1.视频列表和最近收藏接入新搜索
##### Version 3.6.5
> 1.CSRF日志
##### Version 3.6.4
> 1.CSRF日志
##### Version 3.6.3
> 1.添加CSRF日志
##### Version 3.6.2
> 1.使用CSRF配置
##### Version 3.6.1
> 1.新doker镜像
##### Version 3.6.0
> 1.升级go-common
> 2.接入新的配置中心
##### Version 3.5.4
> 1.新增话题收藏状态
> 2.话题信息调用活动平台接口
##### Version 3.5.3
> 1.fix 默认排序中默认收藏夹不在首位
##### Version 3.5.2
> 1.tw升级新版本需要重新打包上线
##### Version 3.5.1
> 1.fix redis setex 断言错误
##### Version 3.5.0
> 1.收藏夹自定义排序
##### Version 3.4.6
> 1.升级go-buisiness
> 2.MonitorPing检测redis和mysql
##### Version 3.4.5
> 1.收藏夹视频批量移动逻辑改成覆盖
> 2.优化 更新收藏夹是否公开状态接口
##### Version 3.4.1
> 1.回滚http接口支持内外网流量
##### Version 3.4.0
> 1.1.更新http接口支持内外网流量
##### Version 3.3.11
> 1.升级go-commom,go-business修复mo状态码统计
##### Version 3.3.10
> 1.修复family
##### Version 3.3.9
> 1.查询DBmap初始化
##### Version 3.3.7
> 1.增肌批量查询收藏视频接口
> 2.升级go-common,golang,go-business
##### Version 3.3.6
> 1.增加返回收藏夹数
##### Version 3.3.5
> 1.重新构建
##### Version 3.3.4
> 1.修复显示私有收藏夹
##### Version 3.3.3
> 1.default 增加是否有收藏bit判断
##### Version 3.3.2
> 1.redis bit添加用户是否有收藏缓存
> 2.接入配置中心
##### Version 3.3.1
> 1.初始化默认收藏夹缓存
##### Version 3.3.0
> 1.更新identity
> 2.增加稿件状态判断
> 3.cover pipeline
##### Version 3.2.2
> 1.更新vendor依赖
##### Version 3.2.1
> 1.更新vendor依赖
##### Version 3.2.0
> 1.更新基础库依赖
> 2.删除冗余error
##### Version 3.1.1
> 1.修复收藏夹参数为空
##### Version 3.1.0
> 1.获取视屏对应的所有收藏夹
> 2.批量加入/删除 视频到多个收藏夹
> 3.收藏夹封面增加缓存
##### Version 3.0.0
> 1.接入trace_v2
> 2.切换go-business
> 3.接入ecode统一管理
> 4.添加govendor支持
> 5.修复html转义
##### Version 2.2.3
> 1.支持添加收藏夹时指定属性
##### Version 2.2.2
> 1.修复在没登录状态查看别人收藏夹视频列表mid为nil问题
##### Version 2.2.1
> 1.修复cache先进行expire
##### Version 2.2.0
> 1.添加内部接口
##### Version 2.1.0
> 1.话题收藏迁移
##### Version 2.0.0
> 1.收藏夹重构

View File

@@ -0,0 +1,16 @@
# Owner
chenzhihui
zhapuyu
caoguoliang
# Author
libingqi
chenzhihui
zhangshengchao
caoguoliang
# Reviewer
chenzhihui
zhapuyu
zhangshengchao
caoguoliang

View File

@@ -0,0 +1,20 @@
# See the OWNERS docs at https://go.k8s.io/owners
approvers:
- caoguoliang
- chenzhihui
- libingqi
- zhangshengchao
- zhapuyu
labels:
- main
- service
- service/main/favorite
options:
no_parent_owners: true
reviewers:
- caoguoliang
- chenzhihui
- libingqi
- zhangshengchao
- zhapuyu

View File

@@ -0,0 +1,13 @@
#### favorite
##### 项目简介
> 1.用户收藏夹、视频收藏、话题收藏、专栏收藏、小视频收藏、相簿收藏
##### 编译环境
> 请只用golang v1.7.x以上版本编译执行。
##### 依赖包
> 1.公共包go-common
##### 特别说明
> 1.model目录可能会被其他项目引用请谨慎请改并通知各方。

View File

@@ -0,0 +1,70 @@
load(
"@io_bazel_rules_go//proto:def.bzl",
"go_proto_library",
)
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
proto_library(
name = "api_proto",
srcs = ["api.proto"],
tags = ["automanaged"],
deps = [
"//app/service/main/favorite/model:model_proto",
"@com_google_protobuf//:empty_proto",
"@gogo_special_proto//github.com/gogo/protobuf/gogoproto",
],
)
go_proto_library(
name = "api_go_proto",
compilers = ["@io_bazel_rules_go//proto:gogofast_grpc"],
importpath = "go-common/app/service/main/favorite/api",
proto = ":api_proto",
tags = ["automanaged"],
deps = [
"//app/service/main/favorite/model:model_go_proto",
"@com_github_gogo_protobuf//gogoproto:go_default_library",
"@io_bazel_rules_go//proto/wkt:empty_go_proto",
],
)
go_library(
name = "go_default_library",
srcs = ["client.go"],
embed = [":api_go_proto"],
importpath = "go-common/app/service/main/favorite/api",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/favorite/model:go_default_library",
"//library/net/rpc/warden:go_default_library",
"@com_github_gogo_protobuf//gogoproto:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@io_bazel_rules_go//proto/wkt:empty_go_proto",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_x_net//context:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/service/main/favorite/api/gorpc:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,426 @@
syntax = "proto3";
package community.service.favorite;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
import "app/service/main/favorite/model/fav.proto";
import "google/protobuf/empty.proto";
option go_package = "api";
message ModelPage {
int32 num = 1;
int32 size = 2;
int32 count = 3;
}
message ModelFavorite {
int64 id = 1;
int64 oid = 2;
int64 mid = 3;
int64 fid = 4;
int32 type = 5;
int32 state = 6;
int64 ctime = 7;
int64 mtime = 8;
}
message ModelPartition {
int32 tid = 1;
string name = 2;
int32 count = 3;
}
message ModelFavorites {
ModelPage page = 1;
repeated ModelFavorite list = 2;
}
message AddFavReply {
}
message AddFavReq {
int32 tp = 2;
int64 mid = 3;
int64 fid = 4;
int64 oid = 5;
int32 otype = 6;
}
message DelFavReply {
}
message DelFavReq {
int32 tp = 2;
int64 mid = 3;
int64 fid = 4;
int64 oid = 5;
int32 otype = 6;
}
message FavoritesReply {
ModelFavorites res = 1;
}
message FavoritesReq {
int32 tp = 2;
int64 mid = 3;
int64 uid = 4;
int64 fid = 5;
int32 pn = 6;
int32 ps = 7;
int32 tid = 8;
int32 tv = 9;
string keyword = 10;
string order = 11;
}
message TlistsReply {
repeated ModelPartition res = 1;
}
message TlistsReq {
int32 tp = 2;
int64 mid = 3;
int64 uid = 4;
int64 fid = 5;
}
message RecentFavsReply {
repeated int64 res = 1;
}
message RecentFavsReq {
int32 tp = 2;
int64 mid = 3;
int32 size = 4;
}
message IsFavoredReply {
bool faved = 1;
}
message IsFavoredReq {
int32 typ = 2;
int64 mid = 3;
int64 oid = 4;
}
message IsFavoredByFidReq {
int32 type = 1;
int64 mid = 2;
int64 oid = 3;
int64 fid = 4;
}
message IsFavoredsReply {
map<int64,bool> faveds = 1;
}
message IsFavoredsReq{
int32 typ = 2;
int64 mid = 3;
repeated int64 oids = 4;
}
message UserFoldersReply {
repeated .model.Folder res = 1;
}
message UserFoldersReq {
int32 typ = 2;
int64 mid = 3;
int64 vmid = 4;
int64 oid = 5;
bool allCount = 6;
int32 otype = 7;
}
message UserFolderReply {
.model.Folder res = 1;
}
message UserFolderReq {
int32 typ = 2;
int64 mid = 3;
int64 vmid = 4;
int64 fid = 5;
bool allCount = 6;
}
message AddFolderReq {
int32 typ = 1;
int64 mid = 2;
string name = 3;
string description = 4;
string cover = 5;
int32 public = 6;
string cookie = 7;
string accessKey = 8;
}
message AddFolderReply {
int64 fid = 1;
}
message UpdateFolderReq {
int32 typ =1;
int64 fid =2;
int64 mid =3;
string name =4;
string description =5;
string cover =6;
int32 public =7;
string cookie =8;
string accessKey =9;
}
message AdminUpdateFolderReq {
int32 typ =1;
int64 fid =2;
int64 mid =3;
string name =4;
string description =5;
string cover =6;
int32 state =7;
int32 attr =8;
}
message DelFolderReq {
int32 typ =1;
int64 mid =2;
int64 fid =3;
}
message MultiAddReq {
int32 typ =1;
int64 mid =2;
repeated int64 oids =3;
int64 fid =4;
}
message MultiDelReq {
int32 typ =1;
int64 mid =2;
repeated int64 oids =3;
int64 fid =4;
}
message InDefaultFolderReq {
int32 typ =1;
int64 mid =2;
int64 oid =3;
}
message InDefaultFolderReply {
bool isIn =1;
}
message CntUserFoldersReq {
int32 typ =1;
int64 mid =2;
int64 vmid =3;
}
message CntUserFoldersReply {
int32 count =1;
}
message User {
int64 id =1;
int64 oid =2;
int64 mid =3;
int32 typ =4;
int32 state =5;
int64 ctime =6;
int64 mtime =7;
}
message FavedUsersReply {
ModelPage page =1;
repeated User user =2;
}
message FavedUsersReq {
int32 Type =1;
int64 Oid =2;
int32 pn =3;
int32 ps =4;
}
message SetFolderSortReq {
int32 typ =1;
int64 mid =3;
repeated int64 fids =4;
}
message FoldersReply {
repeated .model.Folder res = 1;
}
message FoldersReq {
// id = fid*100 + mid%100
repeated FolderID ids = 1;
int32 typ = 2;
int64 mid = 3;
}
message FolderID {
int64 fid = 1;
int64 mid = 2;
}
message MoveFavsReq {
int32 typ = 1;
int64 mid = 2;
int64 oldFid = 3;
int64 newFid = 4;
repeated int64 oids = 5;
}
message CopyFavsReq {
int32 typ = 1;
int64 oldMid = 2;
int64 mid = 3;
int64 oldFid = 4;
int64 newFid = 5;
repeated int64 oids = 6;
}
message CleanStateReq {
int32 typ =1;
int64 mid =2;
int64 fid =3;
}
message CleanStateReply {
int32 cleanState =1;
}
message CleanInvalidFavsReq {
int32 typ =1;
int64 mid =2;
int64 fid =3;
}
message FavAddFoldersReq {
int64 oid = 1;
int32 typ = 2;
repeated int64 fids = 3;
int64 mid = 4;
int32 otype = 5;
}
message FavDelFoldersReq {
int64 oid = 1;
int32 typ = 2;
repeated int64 fids = 3;
int64 mid = 4;
int32 otype = 5;
}
message SortFavsReq{
repeated SortFav sorts = 1;
int64 mid = 2;
int64 fid = 3;
int32 typ = 4;
}
message SortFav {
.model.Resource pre = 1;
.model.Resource insert = 2;
}
message UpFolderNameReq {
int32 typ =1;
int64 fid =2;
int64 mid =3;
string name =4;
string cookie =5;
string accessKey =6;
}
message UpFolderAttrReq {
int32 typ =1;
int64 fid =2;
int64 mid =3;
int32 public =4;
}
message RecentResourcesReq {
int32 tp = 2;
int64 mid = 3;
int32 size = 4;
}
message RecentResourcesReply {
repeated .model.Resource res = 1;
}
service Favorite {
// 更新收藏夹V2版本
rpc AdminUpdateFolder(AdminUpdateFolderReq) returns(.google.protobuf.Empty);
// 删除一个资源从多个收藏夹
rpc FavDelFolders(FavDelFoldersReq) returns(.google.protobuf.Empty);
// 添加一个资源至多个收藏夹
rpc FavAddFolders(FavAddFoldersReq) returns(.google.protobuf.Empty);
// 收藏夹重命名
rpc UpFolderName(UpFolderNameReq) returns(.google.protobuf.Empty);
// 设置收藏夹公开
rpc UpFolderAttr(UpFolderAttrReq) returns(.google.protobuf.Empty);
// 清除不合法的收藏资源
rpc CleanInvalidFavs(CleanInvalidFavsReq) returns(.google.protobuf.Empty);
// 返回某个收藏夹是否已经是clean的
rpc CleanState(CleanStateReq) returns(CleanStateReply);
// 拷贝收藏夹内的资源至另外一个收藏夹
rpc CopyFavs(CopyFavsReq) returns(.google.protobuf.Empty);
// 移动收藏夹内的资源至另外一个收藏夹
rpc MoveFavs(MoveFavsReq) returns(.google.protobuf.Empty);
// 收藏夹排序
rpc SetFolderSort(SetFolderSortReq) returns(.google.protobuf.Empty);
// 哪些用户收藏了该资源
rpc FavedUsers(FavedUsersReq) returns(FavedUsersReply);
// 用户收藏夹数量
rpc CntUserFolders(CntUserFoldersReq) returns(CntUserFoldersReply);
// 该资源是否在默认收藏夹中
rpc InDefault(InDefaultFolderReq) returns(InDefaultFolderReply);
// 批量删除多个资源
rpc MultiDel(MultiDelReq) returns(.google.protobuf.Empty);
// 批量添加多个资源
rpc MultiAdd(MultiAddReq) returns(.google.protobuf.Empty);
// 删除收藏夹
rpc DelFolder(DelFolderReq) returns(.google.protobuf.Empty);
// 更新收藏夹
rpc UpdateFolder(UpdateFolderReq) returns(.google.protobuf.Empty);
// 创建收藏夹
rpc AddFolder(AddFolderReq) returns(AddFolderReply);
// 收藏夹内的资源列表(只显示和收藏夹相同type的资源)
rpc Favorites(FavoritesReq) returns(FavoritesReply);
// 收藏夹内的资源列表(显示所有资源)
rpc FavoritesAll(FavoritesReq) returns(FavoritesReply);
// 分区信息
rpc Tlists(TlistsReq) returns(TlistsReply);
// 该用户最近收藏
rpc RecentFavs(RecentFavsReq) returns(RecentFavsReply);
// 该用户最近收藏的资源
rpc RecentResources(RecentResourcesReq) returns(RecentResourcesReply);
// 用户是否收藏该资源
rpc IsFavored(IsFavoredReq) returns(IsFavoredReply);
// 该资源是否在某个收藏夹内
rpc IsFavoredByFid(IsFavoredByFidReq) returns(IsFavoredReply);
// 多个资源是否被用户收藏
rpc IsFavoreds(IsFavoredsReq) returns(IsFavoredsReply);
// 添加收藏
rpc AddFav(AddFavReq) returns(AddFavReply);
// 删除收藏
rpc DelFav(DelFavReq) returns(DelFavReply);
// 用户的收藏夹列表
rpc UserFolders(UserFoldersReq) returns(UserFoldersReply);
// 用户的一个收藏夹
rpc UserFolder(UserFolderReq) returns(UserFolderReply);
// 通过ID批量返回收藏夹 id = fid*100 + mid%100
rpc Folders(FoldersReq) returns(FoldersReply);
// 改变被搜藏的元素的顺序
rpc SortFavs(SortFavsReq) returns(.google.protobuf.Empty);
}

View File

@@ -0,0 +1,20 @@
package api
import (
"context"
"fmt"
"go-common/library/net/rpc/warden"
"google.golang.org/grpc"
)
// AppID .
const AppID = "community.service.favorite"
// New fav service client
func New(c *warden.ClientConfig, opts ...grpc.DialOption) (FavoriteClient, error) {
client := warden.NewClient(c, opts...)
conn, err := client.Dial(context.Background(), fmt.Sprintf("discovery://default/%s", AppID))
return NewFavoriteClient(conn), err
}

View File

@@ -0,0 +1,32 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["favorite.go"],
importpath = "go-common/app/service/main/favorite/api/gorpc",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/favorite/model:go_default_library",
"//library/net/rpc: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,152 @@
package favorite
import (
"context"
"go-common/app/service/main/favorite/model"
"go-common/library/net/rpc"
)
const (
_allFolders = "RPC.AllFolders"
_folder = "RPC.Folder"
_folders = "RPC.Folders"
_addFolder = "RPC.AddFolder"
_delFolder = "RPC.DelFolder"
_updateFolder = "RPC.UpdateFolder"
_favorites = "RPC.Favorites"
_add = "RPC.Add"
_del = "RPC.Del"
_multiAdd = "RPC.MultiAdd"
_multiDel = "RPC.MultiDel"
_isFav = "RPC.IsFav"
_isFavs = "RPC.IsFavs"
_isInDefault = "RPC.InDefault"
_isFavedByFid = "RPC.IsFavedByFid"
_cntUserFolders = "RPC.CntUserFolders"
_users = "RPC.Users"
_tlists = "RPC.Tlists"
_recents = "RPC.Recents"
// fav v2
_addVideo = "RPC.AddVideo"
)
const (
_appid = "community.service.favorite"
)
var (
_noRes = &struct{}{}
)
type Service struct {
client *rpc.Client2
}
func New2(c *rpc.ClientConfig) (s *Service) {
s = &Service{}
s.client = rpc.NewDiscoveryCli(_appid, c)
return
}
func (s *Service) AllFolders(c context.Context, arg *model.ArgAllFolders) (res []*model.Folder, err error) {
err = s.client.Call(c, _allFolders, arg, &res)
return
}
func (s *Service) Folder(c context.Context, arg *model.ArgFolder) (res *model.Folder, err error) {
err = s.client.Call(c, _folder, arg, &res)
return
}
func (s *Service) Folders(c context.Context, arg *model.ArgFolders) (res []*model.Folder, err error) {
err = s.client.Call(c, _folders, arg, &res)
return
}
func (s *Service) AddFolder(c context.Context, arg *model.ArgAddFolder) (fid int64, err error) {
err = s.client.Call(c, _addFolder, arg, &fid)
return
}
func (s *Service) UpdateFolder(c context.Context, arg *model.ArgUpdateFolder) (err error) {
err = s.client.Call(c, _updateFolder, arg, _noRes)
return
}
func (s *Service) DelFolder(c context.Context, arg *model.ArgDelFolder) (err error) {
err = s.client.Call(c, _delFolder, arg, _noRes)
return
}
func (s *Service) Favorites(c context.Context, arg *model.ArgFavs) (res *model.Favorites, err error) {
err = s.client.Call(c, _favorites, arg, &res)
return
}
func (s *Service) Add(c context.Context, arg *model.ArgAdd) (err error) {
err = s.client.Call(c, _add, arg, _noRes)
return
}
func (s *Service) Del(c context.Context, arg *model.ArgDel) (err error) {
err = s.client.Call(c, _del, arg, _noRes)
return
}
func (s *Service) MultiAdd(c context.Context, arg *model.ArgMultiAdd) (err error) {
err = s.client.Call(c, _multiAdd, arg, _noRes)
return
}
func (s *Service) MultiDel(c context.Context, arg *model.ArgMultiDel) (err error) {
err = s.client.Call(c, _multiDel, arg, _noRes)
return
}
func (s *Service) InDefault(c context.Context, arg *model.ArgInDefaultFolder) (faved bool, err error) {
err = s.client.Call(c, _isInDefault, arg, &faved)
return
}
func (s *Service) IsFav(c context.Context, arg *model.ArgIsFav) (faved bool, err error) {
err = s.client.Call(c, _isFav, arg, &faved)
return
}
func (s *Service) IsFavedByFid(c context.Context, arg *model.ArgIsFavedByFid) (faved bool, err error) {
err = s.client.Call(c, _isFavedByFid, arg, &faved)
return
}
func (s *Service) CntUserFolders(c context.Context, arg *model.ArgCntUserFolders) (count int, err error) {
err = s.client.Call(c, _cntUserFolders, arg, &count)
return
}
func (s *Service) Users(c context.Context, arg *model.ArgUsers) (res *model.UserList, err error) {
err = s.client.Call(c, _users, arg, &res)
return
}
// AddVideo add video fav.
func (s *Service) AddVideo(c context.Context, arg *model.ArgAddVideo) (err error) {
err = s.client.Call(c, _addVideo, arg, _noRes)
return
}
// IsFavs .
func (s *Service) IsFavs(c context.Context, arg *model.ArgIsFavs) (res map[int64]bool, err error) {
err = s.client.Call(c, _isFavs, arg, &res)
return
}
func (s *Service) Tlists(c context.Context, arg *model.ArgTlists) (res []*model.Partition, err error) {
err = s.client.Call(c, _tlists, arg, &res)
return
}
func (s *Service) Recents(c context.Context, arg *model.ArgRecents) (res []int64, err error) {
err = s.client.Call(c, _recents, arg, &res)
return
}

View File

@@ -0,0 +1,46 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_binary",
"go_library",
)
go_binary(
name = "cmd",
embed = [":go_default_library"],
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = ["main.go"],
data = ["favorite-service-test.toml"],
importpath = "go-common/app/service/main/favorite/cmd",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/favorite/conf:go_default_library",
"//app/service/main/favorite/server/gorpc:go_default_library",
"//app/service/main/favorite/server/grpc:go_default_library",
"//app/service/main/favorite/server/http:go_default_library",
"//app/service/main/favorite/service:go_default_library",
"//library/ecode/tip:go_default_library",
"//library/log:go_default_library",
"//library/net/trace: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,187 @@
[log]
stdout = true
[fav]
maxFolders = 50
maxPagesize = 30
maxBatchSize = 1000
maxDataSize = 2000
MaxRecentSize= 10
maxNameLen = 30
maxOperationNum = 30
defaultFolderLimit = 100000
normalFolderLimit = 999
apiHost = "http://api.bilibili.co"
expire = "24h"
cleanCDTime = "1m"
[platform]
maxFolders = 100
maxNameLen = 60
maxDescLen = 250
[bm]
network = "tcp"
addr = "0.0.0.0:7671"
timeout = "1s"
readTimeout = "1s"
writeTimeout = "1s"
[httpClient]
key = "0dc647722719f2ea"
secret = "a3769b553680da10b40994265fb48d20"
dial = "30ms"
timeout = "900ms"
keepAlive = "60s"
timer = 128
[httpClient.breaker]
window ="3s"
sleep ="100ms"
bucket = 10
ratio = 0.1
request = 100
[topic]
topicURL = "http://matsuri.bilibili.co/activity/pages"
[topic.httpClient]
key = "0dc647722719f2ea"
secret = "a3769b553680da10b40994265fb48d20"
dial = "30ms"
timeout = "800ms"
keepAlive = "60s"
timer = 128
[topic.httpClient.breaker]
window ="3s"
sleep ="100ms"
bucket = 10
ratio = 0.1
request = 100
[mysql]
[mysql.fav]
addr = "172.22.34.101:3306"
dsn = "test_3306:UJPZaGKjpb2ylFx3HNhmLuwOYft4MCAi@tcp(172.22.34.101:3306)/bilibili_fav?timeout=2s&readTimeout=5s&writeTimeout=5s&parseTime=true&loc=Local&charset=utf8,utf8mb4"
active = 20
idle = 10
idleTimeout ="4h"
queryTimeout = "1s"
execTimeout = "1s"
tranTimeout = "1s"
[mysql.fav.breaker]
window = "3s"
sleep = "100ms"
bucket = 10
ratio = 0.5
request = 100
[mysql.read]
addr = "172.22.34.101:3306"
dsn = "test_3306:UJPZaGKjpb2ylFx3HNhmLuwOYft4MCAi@tcp(172.22.34.101:3306)/bilibili_fav?timeout=2s&readTimeout=5s&writeTimeout=5s&parseTime=true&loc=Local&charset=utf8,utf8mb4"
active = 20
idle = 10
idleTimeout ="4h"
queryTimeout = "1s"
execTimeout = "1s"
tranTimeout = "1s"
[mysql.read.breaker]
window = "3s"
sleep = "100ms"
bucket = 10
ratio = 0.5
request = 100
[mysql.push]
addr = "172.22.34.101:3306"
dsn = "test_3306:UJPZaGKjpb2ylFx3HNhmLuwOYft4MCAi@tcp(172.22.34.101:3306)/bilibili_fav?timeout=2s&readTimeout=5s&writeTimeout=5s&parseTime=true&loc=Local&charset=utf8,utf8mb4"
active = 20
idle = 10
idleTimeout ="4h"
queryTimeout = "1s"
execTimeout = "1s"
tranTimeout = "1s"
[mysql.push.breaker]
window = "3s"
sleep = "100ms"
bucket = 10
ratio = 0.5
request = 100
[supervisor]
on = true
begin = "2017-10-01T15:04:05+08:00"
end = "2017-10-22T11:10:05+08:00"
[antispam]
on=true
second=5
n=10
hour=1
m=5
[antispam.redis]
name = "favorite/favorite"
proto = "tcp"
addr = "172.16.33.54:6379"
idle = 100
active = 100
dialTimeout = "500ms"
readTimeout = "1s"
writeTimeout = "1s"
idleTimeout = "80s"
expire = "10m"
[memcache]
proto = "tcp"
addr = "172.18.33.61:11231"
active = 10
idle = 2
dialTimeout = "30ms"
readTimeout = "150ms"
writeTimeout = "150ms"
idleTimeout = "80s"
expire = "24h"
[redis]
name = "favorite/favorite"
proto = "tcp"
addr = "172.18.33.61:6808"
active = 60000
idle = 128
dialTimeout = "30ms"
readTimeout = "50ms"
writeTimeout = "50ms"
idleTimeout = "80s"
expire = "32h"
coverExpire = "30m"
[jobDatabus]
key = "170e302355453683"
secret= "3d0e8db7bed0503949e545a469789279"
group= "FavoriteJob-MainCommunity-P"
topic= "FavoriteJob-T"
action="pub"
name = "favorite/job"
proto = "tcp"
addr = "172.18.33.50:6205"
idle = 1
active = 10
dialTimeout = "1s"
readTimeout = "1s"
writeTimeout = "1s"
idleTimeout = "10s"
expire = "1h"
[rpcServer]
proto = "tcp"
addr = "0.0.0.0:7679"
weight = 10
[rpcClient]
[rpcClient.account]
timeout = "150ms"
[rpcClient.archive]
timeout = "150ms"
[rpcClient.filter]
timeout = "1s"
[rpcClient.rank]
timeout = "1s"

View File

@@ -0,0 +1,61 @@
package main
import (
"context"
"flag"
"os"
"os/signal"
"syscall"
"time"
"go-common/app/service/main/favorite/conf"
"go-common/app/service/main/favorite/server/gorpc"
gserver "go-common/app/service/main/favorite/server/grpc"
"go-common/app/service/main/favorite/server/http"
"go-common/app/service/main/favorite/service"
ecode "go-common/library/ecode/tip"
"go-common/library/log"
"go-common/library/net/trace"
)
func main() {
flag.Parse()
if err := conf.Init(); err != nil {
log.Error("conf.Init() error(%v)", err)
panic(err)
}
// init log
log.Init(conf.Conf.Log)
trace.Init(conf.Conf.Tracer)
defer trace.Close()
defer log.Close()
log.Info("favorite start")
ecode.Init(conf.Conf.Ecode)
// service init
svc := service.New(conf.Conf)
rpcSvr := rpc.New(conf.Conf, svc)
grpcSvr := gserver.New(conf.Conf.WardenServer, svc)
if _, err := grpcSvr.Start(); err != nil {
panic(err)
}
http.Init(conf.Conf, svc)
// init signal
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
si := <-ch
switch si {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
log.Info("get a signal %s, stop the consume process", si.String())
rpcSvr.Close()
grpcSvr.Shutdown(context.Background())
time.Sleep(time.Second * 2)
svc.Close()
return
case syscall.SIGHUP:
default:
return
}
}
}

View File

@@ -0,0 +1,47 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["conf.go"],
importpath = "go-common/app/service/main/favorite/conf",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/cache/memcache:go_default_library",
"//library/cache/redis:go_default_library",
"//library/conf:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode/tip:go_default_library",
"//library/log:go_default_library",
"//library/log/infoc:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/http/blademaster/middleware/antispam:go_default_library",
"//library/net/http/blademaster/middleware/supervisor:go_default_library",
"//library/net/http/blademaster/middleware/verify:go_default_library",
"//library/net/rpc:go_default_library",
"//library/net/rpc/warden:go_default_library",
"//library/net/trace:go_default_library",
"//library/queue/databus:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/BurntSushi/toml: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,170 @@
package conf
import (
"flag"
"go-common/library/cache/memcache"
"go-common/library/cache/redis"
"go-common/library/conf"
"go-common/library/database/sql"
ecode "go-common/library/ecode/tip"
"go-common/library/log"
"go-common/library/log/infoc"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/middleware/antispam"
"go-common/library/net/http/blademaster/middleware/supervisor"
"go-common/library/net/http/blademaster/middleware/verify"
"go-common/library/net/rpc"
"go-common/library/net/rpc/warden"
"go-common/library/net/trace"
"go-common/library/queue/databus"
"go-common/library/time"
"github.com/BurntSushi/toml"
)
var (
confPath string
// Conf Config
Conf *Config
)
// Config is favorte conf
type Config struct {
// base
// log
Log *log.Config
App *bm.App
// favorite config
Fav *Fav
Platform *Platform
// BM blademaster
BM *bm.ServerConfig
// rpc server2
RPCServer *rpc.ServerConfig
// db
MySQL *MySQL
// redis
Redis *Redis
// memcache
Memcache *Memcache
// databus
JobDatabus *databus.Config
// verify
Verify *verify.Config
// rpc client
RPCClient2 *RPC
// tracer
Tracer *trace.Config
// http client
HTTPClient *bm.ClientConfig
// ecode
Ecode *ecode.Config
// TopicClient
Topic *Topic
// Antispam
Antispam *antispam.Config
// Supervisior
Supervisor *supervisor.Config
// collector
Infoc2 *infoc.Config
//grpc warden
WardenServer *warden.ServerConfig
}
// RPC contain all rpc conf
type RPC struct {
Account *warden.ClientConfig
Archive *rpc.ClientConfig
Filter *rpc.ClientConfig
Rank *rpc.ClientConfig
}
// Topic Topic
type Topic struct {
TopicURL string
HTTPClient *bm.ClientConfig
}
// Fav config
type Fav struct {
// the max of the num of favorite folders
MaxFolders int
MaxPagesize int
MaxBatchSize int
MaxDataSize int
MaxParallelSize int
MaxRecentSize int
MaxNameLen int
MaxDescLen int
// the num of operation
MaxOperationNum int
// the num of default favorite
DefaultFolderLimit int
NormalFolderLimit int
// ApiHost api.bilibili.co .
APIHost string
// cache expire
Expire time.Duration
// cdtime cool down time
CleanCDTime time.Duration
// real-name switch
RealNameOn bool
}
// Platform config
type Platform struct {
MaxFolders int
MaxNameLen int
MaxDescLen int
}
// MySQL is mysql conf
type MySQL struct {
// favorite db
Fav *sql.Config
Read *sql.Config
Push *sql.Config
}
// Redis redis conf
type Redis struct {
*redis.Config
Expire time.Duration
CoverExpire time.Duration
}
// Memcache memcache conf
type Memcache struct {
*memcache.Config
Expire time.Duration
}
func init() {
flag.StringVar(&confPath, "conf", "", "config path")
}
// Init init conf
func Init() (err error) {
if confPath == "" {
return configCenter()
}
_, err = toml.DecodeFile(confPath, &Conf)
return
}
func configCenter() (err error) {
var (
ok bool
value string
client *conf.Client
)
if client, err = conf.New(); err != nil {
return
}
if value, ok = client.Toml2(); !ok {
panic(err)
}
_, err = toml.Decode(value, &Conf)
return
}

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)
})
}

View File

@@ -0,0 +1,66 @@
load(
"@io_bazel_rules_go//proto:def.bzl",
"go_proto_library",
)
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
proto_library(
name = "model_proto",
srcs = ["fav.proto"],
tags = ["automanaged"],
deps = ["@gogo_special_proto//github.com/gogo/protobuf/gogoproto"],
)
go_proto_library(
name = "model_go_proto",
compilers = ["@io_bazel_rules_go//proto:gogofast_proto"],
importpath = "go-common/app/service/main/favorite/model",
proto = ":model_proto",
tags = ["automanaged"],
deps = [
"//library/time:go_default_library",
"@com_github_gogo_protobuf//gogoproto:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"archive.go",
"fav.go",
"job.go",
"rpc.go",
"topic.go",
"video.go",
],
embed = [":model_go_proto"],
importpath = "go-common/app/service/main/favorite/model",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/archive/api:go_default_library",
"//library/time:go_default_library",
"@com_github_gogo_protobuf//gogoproto:go_default_library",
"@com_github_gogo_protobuf//proto: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,66 @@
package model
import (
"go-common/app/service/main/archive/api"
xtime "go-common/library/time"
)
type Archive struct {
Id int64 `json:"id"`
Mid int64 `json:"mid"`
Fid int64 `json:"fid"`
Aid int64 `json:"aid"`
CTime xtime.Time `json:"-"`
MTime xtime.Time `json:"-"`
}
type SearchArchive struct {
Code int `json:"code,omitempty"`
Seid string `json:"seid"`
Page int `json:"page"`
PageSize int `json:"pagesize"`
NumPages int `json:"numPages,omitempty"`
PageCount int `json:"pagecount"`
NumResults int `json:"numResults,omitempty"`
Total int `json:"total"`
SuggestKeyword string `json:"suggest_keyword"`
Mid int64 `json:"mid"`
Fid int64 `json:"fid"`
Tid int `json:"tid"`
Order string `json:"order"`
Keyword string `json:"keyword"`
TList []struct {
Tid int `json:"tid"`
Name string `json:"name"`
Count int `json:"count"`
} `json:"tlist,omitempty"`
Result []*SearchArchiveResult `json:"result,omitempty"`
Archives []*FavArchive `json:"archives"`
}
type SearchArchiveResult struct {
ID int64 `json:"id"`
Title string `json:"title"`
Play string `json:"play"`
FavTime int64 `json:"fav_time"`
}
type FavArchive struct {
*api.Arc
FavAt int64 `json:"fav_at"`
PlayNum string `json:"play_num"`
HighlightTitle string `json:"highlight_title"`
}
type AppInfo struct {
Platform string
Build string
MobiApp string
Device string
}
type Partition struct {
Tid int `json:"tid"`
Name string `json:"name"`
Count int `json:"count"`
}

View File

@@ -0,0 +1,360 @@
package model
import (
"errors"
"fmt"
"sort"
"strings"
"go-common/library/time"
)
const (
// default name of folder
InitFolderName = "默认收藏夹"
// state
StateNormal = int8(0)
StateIsDel = int8(1)
// attr bit bit from left
AttrBitPublic = uint(0)
AttrBitDefault = uint(1)
AttrBitAudit = uint(2)
AttrBitAdminDelete = uint(3)
AttrBitName = uint(4)
AttrBitDesc = uint(5)
AttrBitCover = uint(6)
AttrBitSensitive = uint(7)
AttrIsPublic = int32(0) // 公开
AttrIsDefault = int32(0) // 默认
// foler attr
AttrBitPrivate = int32(1)
AttrBitNoDefault = int32(1) << AttrBitDefault
AttrBitNeedAudit = int32(1) << AttrBitAudit
AttrBitHitSensitive = int32(1) << AttrBitSensitive
AttrDefaultPublic = 0 // binary 0 / int 0
AttrDefaultNoPublic = AttrBitPrivate // binary 01 / int 1
AttrNormalPublic = AttrBitNoDefault // binary 10 / int 2
AttrNormalNoPublic = AttrBitNoDefault | AttrBitPrivate // binary 11 / int 3
// limit
DefaultFolderLimit = 50000
NormalFolderLimit = 999
// cache
CacheNotFound = -1
// max type
TypeMax = 20
// sort field
SortPubtime = "pubtime"
SortMtime = "mtime"
SortView = "view"
)
func (r *Resource) ResourceID() int64 {
return r.Oid*100 + int64(r.Typ)
}
func IsMediaList(typ int32) bool {
return typ == int32(TypeVideo) || typ == int32(TypeMusicNew)
}
type Favorite struct {
ID int64 `json:"id"`
Oid int64 `json:"oid"`
Mid int64 `json:"mid"`
Fid int64 `json:"fid"`
Type int8 `json:"type"`
State int8 `json:"state"`
CTime time.Time `json:"ctime"`
MTime time.Time `json:"mtime"`
Sequence uint64 `json:"sequence"`
}
func (f *Favorite) ResourceID() int64 {
return int64(f.Oid)*100 + int64(f.Type)
}
type Favorites struct {
Page struct {
Num int `json:"num"`
Size int `json:"size"`
Count int `json:"count"`
} `json:"page"`
List []*Favorite `json:"list"`
}
type User struct {
ID int64 `json:"id"`
Oid int64 `json:"oid"`
Mid int64 `json:"mid"`
Type int8 `json:"type"`
State int8 `json:"state"`
CTime time.Time `json:"ctime"`
MTime time.Time `json:"mtime"`
}
type UserList struct {
Page struct {
Num int `json:"num"`
Size int `json:"size"`
Total int `json:"total"`
} `json:"page"`
List []*User `json:"list"`
}
// AttrVal get attr val by bit.
func (f *Folder) AttrVal(bit uint) int32 {
return (f.Attr >> bit) & int32(1)
}
// AttrSet set attr value by bit.
func (f *Folder) AttrSet(v int32, bit uint) {
f.Attr = f.Attr&(^(1 << bit)) | (v << bit)
}
// IsDefault return true if folder is default.
func (f *Folder) IsDefault() bool {
return f.Attr&AttrBitNoDefault == int32(0)
}
// IsPublic return true if folder is public.
func (f *Folder) IsPublic() bool {
return f.AttrVal(AttrBitPublic) == AttrIsPublic
}
// Access return true if the user has the access permission to the folder.
func (f *Folder) Access(mid int64) bool {
return f.IsPublic() || f.Mid == mid
}
// IsLimited return true if folder count is eq or gt conf limit.
func (f *Folder) IsLimited(cnt int, defaultLimit int, normalLimit int) bool {
switch f.IsDefault() {
case true:
return f.Count+cnt > defaultLimit
case false:
return f.Count+cnt > normalLimit
}
return true
}
func (f *Folder) MediaID() int64 {
return f.ID*100 + f.Mid%100
}
func CheckArg(tp int8, oid int64) error {
if tp <= 0 || oid <= 0 {
return errors.New("negative number and zero not allowed")
}
return CheckType(tp)
}
func CheckType(typ int8) error {
if typ < Article || typ > TypeMax {
return errors.New("type code out of range")
}
return nil
}
// CompleteURL adds host on path.
func CompleteURL(path string) (url string) {
if path == "" {
// url = "http://static.hdslb.com/images/transparent.gif"
return
}
url = path
if strings.Index(path, "//") == 0 || strings.Index(path, "http://") == 0 || strings.Index(path, "https://") == 0 {
return
}
url = "https://i0.hdslb.com" + url
return
}
// CleanURL cuts host.
func CleanURL(url string) (path string) {
path = url
if strings.Index(url, "//") == 0 {
path = url[14:]
} else if strings.Index(url, "http://") == 0 {
path = url[19:]
} else if strings.Index(url, "https://") == 0 {
path = url[20:]
}
return
}
// Folders .
type Folders []*Folder
func (f Folders) Len() int { return len(f) }
func (f Folders) Less(i, j int) bool {
if f[i].IsDefault() {
return true
}
if f[j].IsDefault() {
return false
}
if f[i].ID > f[j].ID {
return true
}
return false
}
func (f Folders) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
// FolderSort folder index.
type FolderSort struct {
ID int64 `json:"id"`
Type int8 `json:"type"`
Mid int64 `json:"mid"`
Sort []int64 `json:"sort"`
Map map[int64]struct{} `json:"-"`
CTime time.Time `json:"ctime"`
MTime time.Time `json:"mtime"`
}
// Index return index for fids.
func (f *FolderSort) Index() []byte {
var (
i int
v int64
fs = f.Sort
n = len(fs) * 8
b = make([]byte, n)
)
for i = 0; i < n; i += 8 {
v = fs[i/8]
b[i] = byte(v >> 56)
b[i+1] = byte(v >> 48)
b[i+2] = byte(v >> 40)
b[i+3] = byte(v >> 32)
b[i+4] = byte(v >> 24)
b[i+5] = byte(v >> 16)
b[i+6] = byte(v >> 8)
b[i+7] = byte(v)
}
return b
}
// SetIndex set sort fids.
func (f *FolderSort) SetIndex(b []byte) (err error) {
var (
i int
id int64
n = len(b)
ids = make([]int64, n/8)
)
if len(b)%8 != 0 {
err = fmt.Errorf("invalid sort index:%v", b)
return
}
f.Map = make(map[int64]struct{}, n)
for i = 0; i < n; i += 8 {
id = int64(b[i+7]) |
int64(b[i+6])<<8 |
int64(b[i+5])<<16 |
int64(b[i+4])<<24 |
int64(b[i+3])<<32 |
int64(b[i+2])<<40 |
int64(b[i+1])<<48 |
int64(b[i])<<56
ids[i/8] = id
f.Map[id] = struct{}{}
}
f.Sort = ids
return
}
// ToBytes return []byte for ids.
func ToBytes(ids []int64) []byte {
var (
i int
v int64
n = len(ids) * 8
b = make([]byte, n)
)
for i = 0; i < n; i += 8 {
v = ids[i/8]
b[i] = byte(v >> 56)
b[i+1] = byte(v >> 48)
b[i+2] = byte(v >> 40)
b[i+3] = byte(v >> 32)
b[i+4] = byte(v >> 24)
b[i+5] = byte(v >> 16)
b[i+6] = byte(v >> 8)
b[i+7] = byte(v)
}
return b
}
// ToInt64s bytes to int64s.
func ToInt64s(b []byte) (ids []int64, err error) {
var (
i int
id int64
n = len(b)
)
ids = make([]int64, n/8)
if len(b)%8 != 0 {
err = fmt.Errorf("invalid bytes:%v", b)
return
}
for i = 0; i < n; i += 8 {
id = int64(b[i+7]) |
int64(b[i+6])<<8 |
int64(b[i+5])<<16 |
int64(b[i+4])<<24 |
int64(b[i+3])<<32 |
int64(b[i+2])<<40 |
int64(b[i+1])<<48 |
int64(b[i])<<56
ids[i/8] = id
}
return
}
// SortFolders sort the favorites by index.
func (f *FolderSort) SortFolders(fs map[int64]*Folder, isSelf bool) (res []*Folder, update bool) {
var (
ok bool
id int64
sorted []int64
fav *Folder
idx = f.Sort
)
res = make([]*Folder, 0, len(fs))
if len(f.Sort) == 0 {
for _, fav = range fs {
if !isSelf && !fav.IsPublic() {
continue
}
res = append(res, fav)
}
sort.Sort(Folders(res))
return
}
if len(idx) != len(fs) {
sorted = append(sorted, idx[0])
for id = range fs {
if _, ok = f.Map[id]; !ok {
sorted = append(sorted, id)
}
}
for _, id := range idx[1:] {
if _, ok = fs[id]; ok {
sorted = append(sorted, id)
}
}
update = true
f.Sort = sorted
}
for _, id = range f.Sort {
if fav, ok = fs[id]; ok {
if !isSelf && !fav.IsPublic() {
continue
}
res = append(res, fav)
}
}
return
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,45 @@
syntax = "proto3";
/*
* v0.1.0
* 收藏夹信息
*/
package model;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.goproto_enum_prefix_all) = false;
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.unmarshaler_all) = true;
option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.stringer_all) = true;
message Folder {
int64 ID = 1 [(gogoproto.jsontag) = "id"];
int64 Mid = 2 [(gogoproto.jsontag) = "mid"];
int32 Count = 3 [(gogoproto.jsontag) = "count",(gogoproto.casttype) = "int"];
string Name = 4 [(gogoproto.jsontag) = "name"];
string Cover = 5 [(gogoproto.jsontag) = "cover"];
string Description = 6 [(gogoproto.jsontag) = "description"];
int32 Type = 7 [(gogoproto.jsontag) = "type",(gogoproto.casttype) = "int8"];
int32 Attr = 8 [(gogoproto.jsontag) = "attr"];
int32 State = 9 [(gogoproto.jsontag) = "state",(gogoproto.casttype) = "int8"];
int32 Favored = 10 [(gogoproto.jsontag) = "favored",(gogoproto.casttype) = "int8"];
int64 CTime = 11 [(gogoproto.jsontag) = "ctime", (gogoproto.casttype) = "go-common/library/time.Time"];
int64 MTime = 12 [(gogoproto.jsontag) = "mtime", (gogoproto.casttype) = "go-common/library/time.Time"];
repeated int64 RecentOids = 13 [(gogoproto.jsontag) = "recent_oids"];
int32 FavedCount = 14 [(gogoproto.jsontag) = "faved_count"];
int32 PlayCount = 15 [(gogoproto.jsontag) = "play_count"];
int32 ShareCount = 16 [(gogoproto.jsontag) = "share_count"];
int32 LikeCount = 17 [(gogoproto.jsontag) = "like_count"];
int32 ReplyCount = 18 [(gogoproto.jsontag) = "reply_count"];
repeated Resource RecentRes = 19 [(gogoproto.jsontag) = "recent_res"];
}
message Resource {
int64 Oid = 1[(gogoproto.jsontag) = "oid"];
int32 Typ = 2[(gogoproto.jsontag) = "typ"];
}

View File

@@ -0,0 +1,56 @@
package model
const (
// type
FieldFav = "folder"
FieldArc = "video"
FieldResource = "resource"
// action
ActionAdd = "add"
ActionDel = "del"
ActionMove = "move"
ActionCopy = "copy"
ActionMdel = "mdel"
ActionIndef = "indef"
ActionIncol = "incol"
ActionClean = "clean"
ActionInitRelationFids = "initRelationFids"
ActionInitFolderRelations = "initFolderRelations"
ActionInitAllFolderRelations = "initAllFolderRelations"
ActionMultiAdd = "multiAdd"
ActionMultiDel = "multiDel"
ActionFolderAdd = "folderAdd"
ActionFolderDel = "folderDel"
ActionSortFavs = "sortFavs"
)
type Message struct {
Field string `json:"field,omitempty"`
Action string `json:"action,omitempty"`
Oid int64 `json:"oid,omitempty"`
Otype int8 `json:"otype,omitempty"`
Type int8 `json:"type,omitempty"`
Mid int64 `json:"mid,omitempty"`
OldMid int64 `json:"old_mid,omitempty"`
Fid int64 `json:"fid,omitempty"`
FidState int8 `json:"fid_state,omitempty"`
FolderAttr int32 `json:"folder_attr,omitempty"`
OldFolderAttr int32 `json:"old_folder_attr,omitempty"`
NewFolderAttr int32 `json:"new_folder_attr,omitempty"`
Aid int64 `json:"aid,omitempty"`
OldFid int64 `json:"old_fid,omitempty"`
OldFidState int8 `json:"old_fid_state,omitempty"`
NewFid int64 `json:"new_fid,omitempty"`
NewFidState int8 `json:"new_fid_state,omitempty"`
Affected int64 `json:"affected,omitempty"`
Aids []int64 `json:"aids,omitempty"`
Oids []int64 `json:"oids,omitempty"`
Mids []int64 `json:"mids,omitempty"`
FTime int64 `json:"ftime,omitempty"`
SortFavs []SortFav `json:"sort_favs,omitempty"`
}
type SortFav struct {
Pre *Resource `json:"preID,omitempty"`
Insert *Resource `json:"id,omitempty"`
}

View File

@@ -0,0 +1,214 @@
package model
const (
Article = int8(1)
TypeVideo = int8(2)
TypeMusic = int8(3)
TypeTopic = int8(4)
TypePlayVideo = int8(5)
TypePlayList = int8(6)
TypeBangumi = int8(7)
TypeMoe = int8(8)
TypeComic = int8(9)
TypeEsports = int8(10)
TypeMediaList = int8(11)
TypeMusicNew = int8(12)
)
type ArgAllFolders struct {
Type int8
Mid int64
Vmid int64
Oid int64
RealIP string
}
type ArgFolder struct {
Type int8
Fid int64
Mid int64
Vmid int64
RealIP string
}
type ArgFVmid struct {
Fid int64
Vmid int64
}
func (f *ArgFVmid) MediaID() int64 {
return f.Fid*100 + f.Vmid%100
}
type ArgFolders struct {
Type int8
Mid int64
FVmids []*ArgFVmid
RealIP string
}
type ArgAddFolder struct {
Type int8
Mid int64
Name string
Description string
Cover string
Public int8
Cookie string
AccessKey string
RealIP string
}
type ArgUpdateFolder struct {
Type int8
Fid int64
Mid int64
Name string
Description string
Cover string
Public int8
Cookie string
AccessKey string
RealIP string
}
type ArgDelFolder struct {
Type int8
Mid int64
Fid int64
RealIP string
}
type ArgFavs struct {
Type int8
Mid int64
Vmid int64
Fid int64
Tv int
Tid int
Pn int
Ps int
Keyword string
Order string
RealIP string
}
type ArgAdd struct {
Type int8
Mid int64
Oid int64
Fid int64
RealIP string
}
type ArgDel struct {
Type int8
Mid int64
Oid int64
Fid int64
RealIP string
}
type ArgAdds struct {
Type int8
Mid int64
Oid int64
Fids []int64
RealIP string
}
type ArgDels struct {
Type int8
Mid int64
Oid int64
Fids []int64
RealIP string
}
type ArgMultiAdd struct {
Type int8
Mid int64
Oids []int64
Fid int64
RealIP string
}
type ArgMultiDel struct {
Type int8
Mid int64
Oids []int64
Fid int64
RealIP string
}
type ArgIsFav struct {
Type int8
Mid int64
Oid int64
RealIP string
}
type ArgIsFavs struct {
Type int8
Mid int64
Oids []int64
RealIP string
}
type ArgInDefaultFolder struct {
Type int8
Mid int64
Oid int64
RealIP string
}
type ArgIsFavedByFid struct {
Type int8
Mid int64
Oid int64
Fid int64
RealIP string
}
type ArgCntUserFolders struct {
Type int8
Mid int64
Vmid int64
RealIP string
}
type ArgAddVideo struct {
Mid int64
Fids []int64
Aid int64
Cookie string
AccessKey string
RealIP string
}
type ArgFavoredVideos struct {
Mid int64
Aids []int64
RealIP string
}
type ArgUsers struct {
Type int8
Oid int64
Pn int
Ps int
RealIP string
}
type ArgTlists struct {
Type int8
Mid int64
Vmid int64
Fid int64
RealIP string
}
type ArgRecents struct {
Type int8
Mid int64
Size int
RealIP string
}

View File

@@ -0,0 +1,56 @@
package model
import (
"go-common/library/time"
)
const (
TopicCacheMiss = -1
// http mode
HttpMode4Http = 1 // eg "http://a.bilibili.com"
HttpMode4Https = 2 // eg "https://a.bilibili.com"
HttpMode4Both = 3 // eg "//a.bilibili.com"
)
type TopicFav struct {
ID int64 `json:"id"`
Mid int64 `json:"mid"`
TpID int64 `json:"tpid"`
Ctime time.Time `json:"ctime"`
Mtime time.Time `json:"mtime"`
}
type Topic struct {
ID int64 `json:"id"`
TpID int64 `json:"tp_id"`
MID int64 `json:"mid"`
FavAt time.Time `json:"fav_at"`
State int64 `json:"state"`
Stime string `json:"stime"`
Etime string `json:"etime"`
Ctime string `json:"ctime"`
Mtime string `json:"mtime"`
Name string `json:"name"`
Author string `json:"author"`
PCUrl string `json:"pc_url"`
H5Url string `json:"h5_url"`
PCCover string `json:"pc_cover"`
H5Cover string `json:"h5_cover"`
Rank int64 `json:"rank"`
PageName string `json:"page_name"`
Plat int64 `json:"plat"`
Desc string `json:"desc"`
Click int64 `json:"click"`
TPType int64 `json:"type"`
Mold int64 `json:"mold"`
Series int64 `json:"series"`
Dept int64 `json:"dept"`
ReplyID int64 `json:"reply_id"`
}
type TopicList struct {
PageNum int `json:"page"`
PageSize int `json:"pagesize"`
Total int64 `json:"total"`
List []*Topic `json:"list"`
}

View File

@@ -0,0 +1,208 @@
package model
import (
"fmt"
"sort"
xtime "go-common/library/time"
)
const (
bit1 = int8(1)
bit2 = int8(1) << 1
StateDefaultPublic = int8(0) // binary 00 / int 0
StateDefaultNoPublic = int8(0) | bit1 // binary 01 / int 1
StateNormalPublic = bit2 | int8(0) // binary 10 / int 2
StateNormalNoPublic = bit2 | bit1 // binary 11 / int 3
// DefaultFolderName default name of favorite folder
DefaultFolderName = "默认收藏夹"
// AllFidFlag all folder id flag
AllFidFlag = -1
// CDFlag cool down flag
CDFlag = -1
// search error code
SearchErrWordIllegal = -110 // 非法搜索词错误
// clean state
StateAllowToClean = 0
StateCleaning = 1
StateCleanCD = 2
)
type VideoFolder struct {
MediaId int64 `json:"media_id"`
Fid int64 `json:"fid"`
Mid int64 `json:"mid"`
Name string `json:"name"`
MaxCount int `json:"max_count"`
CurCount int `json:"cur_count"`
AttenCount int `json:"atten_count"`
Favoured int8 `json:"favoured"`
State int8 `json:"state"`
CTime xtime.Time `json:"ctime"`
MTime xtime.Time `json:"mtime"`
Cover []*Cover `json:"cover,omitempty"`
}
// IsPublic return true if folder is public.
func (f *VideoFolder) IsPublic() bool {
return f.State&bit1 == int8(0)
}
// IsDefault return true if folder is default.
func (f *VideoFolder) IsDefault() bool {
return f.State&bit2 == int8(0)
}
// StatePub return folder's public state.
func (f *VideoFolder) StatePub() int8 {
return f.State & bit1
}
// StateDef return folder's default state.
func (f *VideoFolder) StateDef() int8 {
return f.State & bit2
}
// IsDefault return true if state is default state.
func IsDefault(state int8) bool {
return (state&(int8(1)<<1) == int8(0))
}
// CheckPublic check user update public value in [0, 1].
func CheckPublic(state int8) bool {
return state == int8(0) || state == bit1
}
type VideoFolders []*VideoFolder
func (f VideoFolders) Len() int { return len(f) }
func (f VideoFolders) Less(i, j int) bool {
if f[i].IsDefault() {
return true
}
if f[j].IsDefault() {
return false
}
if f[i].Fid > f[j].Fid {
return true
}
return false
}
func (f VideoFolders) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
// Cover image
type Cover struct {
Aid int64 `json:"aid"`
Pic string `json:"pic"`
Type int32 `json:"type"`
}
// VideoFolderSort folder index.
type VideoFolderSort struct {
ID int64 `json:"id"`
Mid int64 `json:"mid"`
Sort []int64 `json:"sort"`
Map map[int64]struct{} `json:"map"`
CTime xtime.Time `json:"ctime"`
MTime xtime.Time `json:"mtime"`
}
// Index return index for fids.
func (f *VideoFolderSort) Index() []byte {
var (
i int
v int64
fs = f.Sort
n = len(fs) * 8
b = make([]byte, n)
)
for i = 0; i < n; i += 8 {
v = fs[i/8]
b[i] = byte(v >> 56)
b[i+1] = byte(v >> 48)
b[i+2] = byte(v >> 40)
b[i+3] = byte(v >> 32)
b[i+4] = byte(v >> 24)
b[i+5] = byte(v >> 16)
b[i+6] = byte(v >> 8)
b[i+7] = byte(v)
}
return b
}
// SetIndex set sort fids.
func (f *VideoFolderSort) SetIndex(b []byte) (err error) {
var (
i int
id int64
n = len(b)
ids = make([]int64, n)
)
if len(b)%8 != 0 {
err = fmt.Errorf("invalid sort index:%v", b)
return
}
f.Map = make(map[int64]struct{}, n)
for i = 0; i < n; i += 8 {
id = int64(b[i+7]) |
int64(b[i+6])<<8 |
int64(b[i+5])<<16 |
int64(b[i+4])<<24 |
int64(b[i+3])<<32 |
int64(b[i+2])<<40 |
int64(b[i+1])<<48 |
int64(b[i])<<56
ids[i/8] = id
f.Map[id] = struct{}{}
}
f.Sort = ids
return
}
// SortFavs sort the favorites by index.
func (f *VideoFolderSort) SortFavs(fs map[int64]*VideoFolder, isSelf bool) (res []*VideoFolder, update bool) {
var (
ok bool
id int64
sorted []int64
fav *VideoFolder
idx = f.Sort
)
res = make([]*VideoFolder, 0, len(fs))
if len(f.Sort) == 0 {
for _, fav = range fs {
if !isSelf && !fav.IsPublic() {
continue
}
res = append(res, fav)
}
sort.Sort(VideoFolders(res))
return
}
if len(idx) != len(fs) {
sorted = append(sorted, idx[0])
for id = range fs {
if _, ok = f.Map[id]; !ok {
sorted = append(sorted, id)
}
}
for _, id := range idx[1:] {
if _, ok = fs[id]; ok {
sorted = append(sorted, id)
}
}
update = true
f.Sort = sorted
}
for _, id = range f.Sort {
if fav, ok = fs[id]; ok {
if !isSelf && !fav.IsPublic() {
continue
}
res = append(res, fav)
}
}
return
}

View File

@@ -0,0 +1,18 @@
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/service/main/favorite/server/gorpc:all-srcs",
"//app/service/main/favorite/server/grpc:all-srcs",
"//app/service/main/favorite/server/http:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,49 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["server_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/favorite/api/gorpc: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 = ["server.go"],
importpath = "go-common/app/service/main/favorite/server/gorpc",
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/favorite/service:go_default_library",
"//library/net/rpc:go_default_library",
"//library/net/rpc/context: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,173 @@
package rpc
import (
"go-common/app/service/main/favorite/conf"
"go-common/app/service/main/favorite/model"
"go-common/app/service/main/favorite/service"
"go-common/library/net/rpc"
"go-common/library/net/rpc/context"
)
// RPC favorite rpc.
type RPC struct {
c *conf.Config
s *service.Service
}
// New init rpc.
func New(c *conf.Config, s *service.Service) (svr *rpc.Server) {
r := &RPC{
c: c,
s: s,
}
svr = rpc.NewServer(c.RPCServer)
if err := svr.Register(r); err != nil {
panic(err)
}
return
}
// Ping check connection success.
func (r *RPC) Ping(c context.Context, arg *struct{}, res *struct{}) (err error) {
return
}
// Folder return folder by mid.
func (r *RPC) Folder(c context.Context, a *model.ArgFolder, res *model.Folder) (err error) {
var fl *model.Folder
if fl, err = r.s.Folder(c, a.Type, a.Mid, a.Vmid, a.Fid); err == nil {
*res = *fl
}
return
}
// Folders return folder by mid.
func (r *RPC) Folders(c context.Context, a *model.ArgFolders, res *[]*model.Folder) (err error) {
*res, err = r.s.Folders(c, a.Type, a.Mid, a.FVmids)
return
}
// AllFolders return users folders.
func (r *RPC) AllFolders(c context.Context, a *model.ArgAllFolders, res *[]*model.Folder) (err error) {
*res, err = r.s.UserFolders(c, a.Type, a.Mid, a.Vmid, a.Oid, a.Type)
return
}
// AddFolder add a Folder.
func (r *RPC) AddFolder(c context.Context, a *model.ArgAddFolder, res *int64) (err error) {
var fid int64
if fid, err = r.s.AddFolder(c, a.Type, a.Mid, a.Name, a.Description, a.Cover, int32(a.Public), a.Cookie, a.AccessKey); err == nil {
*res = fid
}
return
}
// UpdateFolder update a Folder.
func (r *RPC) UpdateFolder(c context.Context, a *model.ArgUpdateFolder, res *struct{}) (err error) {
err = r.s.UpdateFolder(c, a.Type, a.Fid, a.Mid, a.Name, a.Description, a.Cover, int32(a.Public), nil, nil)
return
}
// DelFolder del a folder.
func (r *RPC) DelFolder(c context.Context, a *model.ArgDelFolder, res *struct{}) (err error) {
err = r.s.DelFolder(c, a.Type, a.Mid, a.Fid)
return
}
// Favorites return favorites by mid.
func (r *RPC) Favorites(c context.Context, a *model.ArgFavs, res *model.Favorites) (err error) {
var fs *model.Favorites
if fs, err = r.s.Favorites(c, a.Type, a.Mid, a.Vmid, a.Fid, a.Tid, a.Tv, a.Pn, a.Ps, a.Keyword, a.Order); err == nil {
*res = *fs
}
return
}
// Add add a favorite relation.
func (r *RPC) Add(c context.Context, a *model.ArgAdd, res *struct{}) (err error) {
err = r.s.AddFav(c, a.Type, a.Mid, a.Fid, a.Oid, a.Type)
return
}
// Del del a favorite relation.
func (r *RPC) Del(c context.Context, a *model.ArgDel, res *struct{}) (err error) {
err = r.s.DelFav(c, a.Type, a.Mid, a.Fid, a.Oid, a.Type)
return
}
// Adds add a resource to folders.
func (r *RPC) Adds(c context.Context, a *model.ArgAdds, res *struct{}) (err error) {
for _, fid := range a.Fids {
err = r.s.AddFav(c, a.Type, a.Mid, fid, a.Oid, a.Type)
}
return
}
// Dels del a resource in fodlers.
func (r *RPC) Dels(c context.Context, a *model.ArgDels, res *struct{}) (err error) {
for _, fid := range a.Fids {
err = r.s.DelFav(c, a.Type, a.Mid, fid, a.Oid, a.Type)
}
return
}
// MultiAdd multi add favorite relations.
func (r *RPC) MultiAdd(c context.Context, a *model.ArgMultiAdd, res *struct{}) (err error) {
err = r.s.MultiAddFavs(c, a.Type, a.Mid, a.Fid, a.Oids)
return
}
// MultiDel multi del favorite relations.
func (r *RPC) MultiDel(c context.Context, a *model.ArgMultiDel, res *struct{}) (err error) {
err = r.s.MultiDelFavs(c, a.Type, a.Mid, a.Fid, a.Oids)
return
}
// IsFav check favorited relation.
func (r *RPC) IsFav(c context.Context, a *model.ArgIsFav, faved *bool) (err error) {
*faved, err = r.s.IsFavored(c, a.Type, a.Mid, a.Oid)
return
}
// IsFavs return favored relation map.
func (r *RPC) IsFavs(c context.Context, a *model.ArgIsFavs, res *map[int64]bool) (err error) {
*res, err = r.s.IsFavoreds(c, a.Type, a.Mid, a.Oids)
return
}
// InDefault return favored in default folder.
func (r *RPC) InDefault(c context.Context, a *model.ArgInDefaultFolder, in *bool) (err error) {
*in, err = r.s.InDefaultFolder(c, a.Type, a.Mid, a.Oid)
return
}
// IsFavedByFid check the oid and fid relation.
func (r *RPC) IsFavedByFid(c context.Context, a *model.ArgIsFavedByFid, faved *bool) (err error) {
*faved, err = r.s.IsFavedByFid(c, a.Type, a.Mid, a.Oid, a.Fid)
return
}
// CntUserFolders count user's folders.
func (r *RPC) CntUserFolders(c context.Context, a *model.ArgCntUserFolders, count *int) (err error) {
*count, err = r.s.CntUserFolders(c, a.Type, a.Mid, a.Vmid)
return
}
// Users return favored users by mid.
func (r *RPC) Users(c context.Context, a *model.ArgUsers, res *model.UserList) (err error) {
var us *model.UserList
if us, err = r.s.UserList(c, a.Type, a.Oid, a.Pn, a.Ps); err == nil {
*res = *us
}
return
}
// under v2 ===
// AddVideo add a favorite video.
func (r *RPC) AddVideo(c context.Context, a *model.ArgAddVideo, res *struct{}) (err error) {
for _, fid := range a.Fids {
err = r.s.AddFav(c, model.TypeVideo, a.Mid, fid, a.Aid, model.TypeVideo)
}
return
}

View File

@@ -0,0 +1,172 @@
package rpc
import (
"context"
favmdl "go-common/app/service/main/favorite/model"
favsrv "go-common/app/service/main/favorite/api/gorpc"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
var (
ctx = context.TODO()
)
func WithRPC(f func(client *favsrv.Service)) func() {
return func() {
client := favsrv.New2(nil)
time.Sleep(2 * time.Second)
f(client)
}
}
// func Test_Folders(t *testing.T) {
// Convey("Folders", t, WithRPC(func(client *favsrv.Service) {
// arg := &favmdl.ArgFolders{
// Type: 5,
// Mid: 88888894,
// FVmids: []*favmdl.ArgFVmid{
// {
// Fid: 12331,
// Vmid: 27515232,
// },
// {
// Fid: 12315,
// Vmid: 27515254,
// },
// {
// Fid: 1237,
// Vmid: 27515255,
// },
// },
// }
// res, err := client.Folders(ctx, arg)
// t.Logf("res: %+v, error:%+v", res, err)
// So(err, ShouldBeNil)
// }))
// }
// func Test_AddFolder(t *testing.T) {
// Convey("AddFolder", t, WithRPC(func(client *favsrv.Service) {
// arg := &favmdl.ArgAddFolder{
// Type: 5,
// Mid: 88888894,
// Name: "1234",
// Description: "1234",
// Cover: "1234",
// Cookie: "finger=14bc3c4e; sid=m65qzbpt; LIVE_BUVID=AUTO5915076321203625; fts=1507632123; UM_distinctid=15f05e08d7d1e5-0319506e9267a5-31657c00-1fa400-15f05e08d7e2b3; pgv_pvi=294734848; buvid3=C0E6B232-BC5C-4AEC-879C-1CC61C2336841986infoc; rpdid=kmilkmximpdoswqosospw; tma=136533283.50523868.1508466136624.1508466136624.1508466136624.1; tmd=2.136533283.50523868.1508466136624.; pgv_si=s1585227776; DedeUserID=88888894; DedeUserID__ckMd5=53cf4c9cd3a9e1fb; SESSDATA=6c75f0f3%2C1511521084%2Ce6967cf6; bili_jct=63ab513073169112f9f89b97c1713009; _cnt_pm=0; _cnt_notify=0; _dfcaptcha=db4362e021547cfab3dce5b4718ad573",
// }
// fid, err := client.AddFolder(ctx, arg)
// So(err, ShouldBeNil)
// t.Logf("fid: %d", fid)
// }))
// }
// func Test_UpdateFolder(t *testing.T) {
// Convey("UpdateFolder", t, WithRPC(func(client *favsrv.Service) {
// arg := &favmdl.ArgUpdateFolder{
// Type: 5,
// Fid: 9,
// Mid: 88888894,
// Name: "123",
// Description: "",
// // Cover: "123",
// }
// err := client.UpdateFolder(ctx, arg)
// So(err, ShouldBeNil)
// }))
// }
// func Test_Add(t *testing.T) {
// Convey("Add", t, WithRPC(func(client *favsrv.Service) {
// arg := &favmdl.ArgAdd{
// Type: 1,
// Mid: 88888894,
// Oid: 123,
// Fid: 456,
// }
// err := client.Add(ctx, arg)
// So(err, ShouldBeNil)
// }))
// }
// func Test_AddVideo(t *testing.T) {
// Convey("AddVideo", t, WithRPC(func(client *favsrv.Service) {
// arg := &favmdl.ArgAddVideo{
// Mid: 88888894,
// Aid: 123,
// Fids: []int64{123, 456},
// Cookie: "finger=14bc3c4e; sid=m65qzbpt; LIVE_BUVID=AUTO5915076321203625; fts=1507632123; UM_distinctid=15f05e08d7d1e5-0319506e9267a5-31657c00-1fa400-15f05e08d7e2b3; pgv_pvi=294734848; buvid3=C0E6B232-BC5C-4AEC-879C-1CC61C2336841986infoc; rpdid=kmilkmximpdoswqosospw; tma=136533283.50523868.1508466136624.1508466136624.1508466136624.1; tmd=2.136533283.50523868.1508466136624.; pgv_si=s1585227776; DedeUserID=88888894; DedeUserID__ckMd5=53cf4c9cd3a9e1fb; SESSDATA=6c75f0f3%2C1511521084%2Ce6967cf6; bili_jct=63ab513073169112f9f89b97c1713009; _cnt_pm=0; _cnt_notify=0; _dfcaptcha=db4362e021547cfab3dce5b4718ad573",
// }
// err := client.AddVideo(ctx, arg)
// So(err, ShouldBeNil)
// }))
// }
// func Test_Folder(t *testing.T) {
// Convey("Add", t, WithRPC(func(client *favsrv.Service) {
// arg := &favmdl.ArgFolder{
// Type: 5,
// Mid: 0,
// Fid: 0,
// }
// res, err := client.Folder(ctx, arg)
// t.Logf("res: %+v", res)
// So(err, ShouldBeNil)
// }))
// }
// func Test_IsFavedByFid(t *testing.T) {
// Convey("IsFavedByFid", t, WithRPC(func(client *favsrv.Service) {
// arg := &favmdl.ArgIsFavedByFid{
// Type: 6,
// Mid: 88888894,
// Fid: 23,
// Oid: 11112,
// }
// res, err := client.IsFavedByFid(ctx, arg)
// t.Logf("res: %+v", res)
// So(err, ShouldBeNil)
// }))
// }
func Test_CntUserFolders(t *testing.T) {
Convey("CntUserFolders", t, WithRPC(func(client *favsrv.Service) {
arg := &favmdl.ArgCntUserFolders{
Type: 6,
Mid: 88888894,
Vmid: 88888894,
}
res, err := client.CntUserFolders(ctx, arg)
t.Logf("res: %+v", res)
So(err, ShouldBeNil)
}))
}
// func Test_DelFolder(t *testing.T) {
// Convey("DelFolder", t, WithRPC(func(client *favsrv.Service) {
// arg := &favmdl.ArgDelFolder{
// Type: 5,
// Mid: 88888894,
// Fid: 9,
// }
// err := client.DelFolder(ctx, arg)
// So(err, ShouldBeNil)
// }))
// }
func Test_IsFavs(t *testing.T) {
Convey("IsFavs", t, WithRPC(func(client *favsrv.Service) {
arg := &favmdl.ArgIsFavs{
Type: 1,
Mid: 88888894,
Oids: []int64{123, 456},
}
res, err := client.IsFavs(ctx, arg)
t.Logf("res: %+v", res)
So(res, ShouldNotBeNil)
So(err, ShouldBeNil)
}))
}

View File

@@ -0,0 +1,36 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["server.go"],
importpath = "go-common/app/service/main/favorite/server/grpc",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/favorite/api:go_default_library",
"//app/service/main/favorite/model:go_default_library",
"//app/service/main/favorite/service:go_default_library",
"//library/ecode:go_default_library",
"//library/net/rpc/warden:go_default_library",
"@io_bazel_rules_go//proto/wkt:empty_go_proto",
],
)
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,364 @@
// Package server generate by warden_gen
package server
import (
"context"
"go-common/library/ecode"
pb "go-common/app/service/main/favorite/api"
"go-common/app/service/main/favorite/model"
service "go-common/app/service/main/favorite/service"
"go-common/library/net/rpc/warden"
empty "github.com/golang/protobuf/ptypes/empty"
)
// New Favorite warden rpc server
func New(c *warden.ServerConfig, svr *service.Service) *warden.Server {
ws := warden.NewServer(c)
pb.RegisterFavoriteServer(ws.Server(), &server{svr})
return ws
}
type server struct {
svr *service.Service
}
var _ pb.FavoriteServer = &server{}
func (s *server) SortFavs(ctx context.Context, req *pb.SortFavsReq) (*empty.Empty, error) {
var sorts []model.SortFav
for _, data := range req.Sorts {
sorts = append(sorts, model.SortFav{
Pre: data.Pre,
Insert: data.Insert,
})
}
if len(sorts) == 0 {
return nil, ecode.RequestErr
}
s.svr.SortFavs(ctx, int8(req.Typ), req.Fid, req.Mid, sorts)
return &empty.Empty{}, nil
}
func (s *server) AdminUpdateFolder(ctx context.Context, req *pb.AdminUpdateFolderReq) (*empty.Empty, error) {
attr := int32(req.Attr)
state := int8(req.State)
err := s.svr.UpdateFolder(ctx, int8(req.Typ), req.Fid, req.Mid, req.Name, req.Description, req.Cover, 0, &attr, &state)
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func (s *server) UpFolderName(ctx context.Context, req *pb.UpFolderNameReq) (*empty.Empty, error) {
err := s.svr.UpFolderName(ctx, int8(req.Typ), req.Mid, req.Fid, req.Name, req.Cookie, req.AccessKey)
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func (s *server) FavDelFolders(ctx context.Context, req *pb.FavDelFoldersReq) (*empty.Empty, error) {
for _, fid := range req.Fids {
err := s.svr.DelFav(ctx, int8(req.Typ), req.Mid, fid, req.Oid, int8(req.Otype))
if err != nil {
return nil, err
}
}
return &empty.Empty{}, nil
}
func (s *server) FavAddFolders(ctx context.Context, req *pb.FavAddFoldersReq) (*empty.Empty, error) {
for _, fid := range req.Fids {
err := s.svr.AddFav(ctx, int8(req.Typ), req.Mid, fid, req.Oid, int8(req.Otype))
if err != nil {
return nil, err
}
}
return &empty.Empty{}, nil
}
func (s *server) UpFolderAttr(ctx context.Context, req *pb.UpFolderAttrReq) (*empty.Empty, error) {
err := s.svr.UpFolderAttr(ctx, int8(req.Typ), req.Mid, req.Fid, req.Public)
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func (s *server) CleanState(ctx context.Context, req *pb.CleanStateReq) (*pb.CleanStateReply, error) {
state, err := s.svr.CleanState(ctx, int8(req.Typ), req.Mid, req.Fid)
if err != nil {
return nil, err
}
return &pb.CleanStateReply{CleanState: int32(state)}, nil
}
func (s *server) CleanInvalidFavs(ctx context.Context, req *pb.CleanInvalidFavsReq) (*empty.Empty, error) {
err := s.svr.CleanInvalidArcs(ctx, int8(req.Typ), req.Mid, req.Fid)
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func (s *server) CopyFavs(ctx context.Context, req *pb.CopyFavsReq) (*empty.Empty, error) {
err := s.svr.CopyFavs(ctx, int8(req.Typ), req.OldMid, req.Mid, req.OldFid, req.NewFid, req.Oids)
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func (s *server) MoveFavs(ctx context.Context, req *pb.MoveFavsReq) (*empty.Empty, error) {
err := s.svr.MoveFavs(ctx, int8(req.Typ), req.Mid, req.OldFid, req.NewFid, req.Oids)
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func (s *server) SetFolderSort(ctx context.Context, req *pb.SetFolderSortReq) (*empty.Empty, error) {
err := s.svr.SetFolderSort(ctx, int8(req.Typ), req.Mid, req.Fids)
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func (s *server) FavedUsers(ctx context.Context, req *pb.FavedUsersReq) (*pb.FavedUsersReply, error) {
list, err := s.svr.UserList(ctx, int8(req.Type), req.Oid, int(req.Pn), int(req.Ps))
if err != nil {
return nil, err
}
reply := &pb.FavedUsersReply{
Page: &pb.ModelPage{
Num: int32(list.Page.Num),
Size_: int32(list.Page.Size),
Count: int32(list.Page.Total),
},
}
for _, data := range list.List {
reply.User = append(reply.User, &pb.User{
Id: data.ID,
Oid: data.Oid,
Mid: data.Mid,
Typ: int32(data.Type),
State: int32(data.State),
Ctime: int64(data.CTime),
Mtime: int64(data.MTime),
})
}
return reply, nil
}
func (s *server) CntUserFolders(ctx context.Context, req *pb.CntUserFoldersReq) (*pb.CntUserFoldersReply, error) {
count, err := s.svr.CntUserFolders(ctx, int8(req.Typ), req.Mid, req.Vmid)
if err != nil {
return nil, err
}
return &pb.CntUserFoldersReply{Count: int32(count)}, nil
}
func (s *server) InDefault(ctx context.Context, req *pb.InDefaultFolderReq) (*pb.InDefaultFolderReply, error) {
isIn, err := s.svr.InDefaultFolder(ctx, int8(req.Typ), req.Mid, req.Oid)
if err != nil {
return nil, err
}
return &pb.InDefaultFolderReply{IsIn: isIn}, nil
}
func (s *server) MultiDel(ctx context.Context, req *pb.MultiDelReq) (*empty.Empty, error) {
err := s.svr.MultiDelFavs(ctx, int8(req.Typ), req.Mid, req.Fid, req.Oids)
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func (s *server) MultiAdd(ctx context.Context, req *pb.MultiAddReq) (*empty.Empty, error) {
err := s.svr.MultiAddFavs(ctx, int8(req.Typ), req.Mid, req.Fid, req.Oids)
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func (s *server) DelFolder(ctx context.Context, req *pb.DelFolderReq) (*empty.Empty, error) {
err := s.svr.DelFolder(ctx, int8(req.Typ), req.Mid, req.Fid)
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func (s *server) UpdateFolder(ctx context.Context, req *pb.UpdateFolderReq) (*empty.Empty, error) {
err := s.svr.UpdateFolder(ctx, int8(req.Typ), req.Fid, req.Mid, req.Name, req.Description, req.Cover, req.Public, nil, nil)
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
func (s *server) AddFolder(ctx context.Context, req *pb.AddFolderReq) (*pb.AddFolderReply, error) {
fid, err := s.svr.AddFolder(ctx, int8(req.Typ), req.Mid, req.Name, req.Description, req.Cover, req.Public, req.Cookie, req.AccessKey)
if err != nil {
return nil, err
}
return &pb.AddFolderReply{Fid: fid}, nil
}
// IsFavoredByFid return folders by mid.
func (s *server) IsFavoredByFid(ctx context.Context, req *pb.IsFavoredByFidReq) (*pb.IsFavoredReply, error) {
ok, err := s.svr.IsFavedByFid(ctx, int8(req.Type), req.Mid, req.Oid, req.Fid)
return &pb.IsFavoredReply{Faved: ok}, err
}
// UserFolders return folders by mid.
func (s *server) UserFolders(ctx context.Context, req *pb.UserFoldersReq) (*pb.UserFoldersReply, error) {
if req.Otype <= 0 {
req.Otype = req.Typ
}
uf, err := s.svr.UserFolders(ctx, int8(req.Typ), req.Mid, req.Vmid, req.Oid, int8(req.Otype))
return &pb.UserFoldersReply{Res: uf}, err
}
// UserFolder return one folder by mid.
func (s *server) UserFolder(ctx context.Context, req *pb.UserFolderReq) (*pb.UserFolderReply, error) {
f, err := s.svr.UserFolder(ctx, int8(req.Typ), req.Mid, req.Vmid, req.Fid)
return &pb.UserFolderReply{Res: f}, err
}
// Tlists return partitions .
func (s *server) Tlists(ctx context.Context, req *pb.TlistsReq) (*pb.TlistsReply, error) {
ts, err := s.svr.Tlists(ctx, int8(req.Tp), req.Mid, req.Uid, req.Fid)
if err != nil {
return nil, err
}
reply := &pb.TlistsReply{}
for _, v := range ts {
reply.Res = append(reply.Res, &pb.ModelPartition{
Tid: int32(v.Tid),
Name: v.Name,
Count: int32(v.Count),
})
}
return reply, err
}
// RecentFavs return favs by mid.
func (s *server) RecentFavs(ctx context.Context, req *pb.RecentFavsReq) (*pb.RecentFavsReply, error) {
ids, err := s.svr.RecentFavs(ctx, int8(req.Tp), req.Mid, int(req.Size_))
return &pb.RecentFavsReply{Res: ids}, err
}
// RecentFavs return favs by mid.
func (s *server) RecentResources(ctx context.Context, req *pb.RecentResourcesReq) (*pb.RecentResourcesReply, error) {
recents, err := s.svr.RecentResources(ctx, int8(req.Tp), req.Mid, int(req.Size_))
return &pb.RecentResourcesReply{Res: recents}, err
}
// Favorites return favorieds info by fid.
func (s *server) Favorites(ctx context.Context, req *pb.FavoritesReq) (*pb.FavoritesReply, error) {
f, err := s.svr.Favorites(ctx, int8(req.Tp), req.Mid, req.Uid, req.Fid, int(req.Tid), int(req.Tv), int(req.Pn), int(req.Ps), req.Keyword, req.Order)
if err != nil {
return nil, err
}
reply := &pb.FavoritesReply{Res: &pb.ModelFavorites{Page: &pb.ModelPage{
Num: int32(f.Page.Num),
Size_: int32(f.Page.Size),
Count: int32(f.Page.Count),
}}}
for _, data := range f.List {
reply.Res.List = append(reply.Res.List, &pb.ModelFavorite{
Id: data.ID,
Oid: data.Oid,
Mid: data.Mid,
Fid: data.Fid,
Type: int32(data.Type),
State: int32(data.State),
Ctime: int64(data.CTime),
Mtime: int64(data.MTime),
})
}
return reply, err
}
// AddFav add a favorite into folder.
func (s *server) AddFav(ctx context.Context, req *pb.AddFavReq) (*pb.AddFavReply, error) {
err := s.svr.AddFav(ctx, int8(req.Tp), req.Mid, req.Fid, req.Oid, int8(req.Otype))
return &pb.AddFavReply{}, err
}
// DelFav delete a favorite.
func (s *server) DelFav(ctx context.Context, req *pb.DelFavReq) (*pb.DelFavReply, error) {
err := s.svr.DelFav(ctx, int8(req.Tp), req.Mid, req.Fid, req.Oid, int8(req.Otype))
return &pb.DelFavReply{}, err
}
// IsFavored check if oid faved by user
func (s *server) IsFavored(ctx context.Context, req *pb.IsFavoredReq) (*pb.IsFavoredReply, error) {
is, err := s.svr.IsFavored(ctx, int8(req.Typ), req.Mid, req.Oid)
return &pb.IsFavoredReply{Faved: is}, err
}
// IsFavoreds check if oids faved by user
func (s *server) IsFavoreds(ctx context.Context, req *pb.IsFavoredsReq) (*pb.IsFavoredsReply, error) {
res, err := s.svr.IsFavoreds(ctx, int8(req.Typ), req.Mid, req.Oids)
return &pb.IsFavoredsReply{Faveds: res}, err
}
// FavoritesAll return favorieds info by fid.
func (s *server) FavoritesAll(ctx context.Context, req *pb.FavoritesReq) (*pb.FavoritesReply, error) {
if req.Pn <= 0 {
req.Pn = 1
}
if req.Ps <= 0 {
req.Ps = 20
}
var f *model.Favorites
var err error
if int8(req.Tp) != model.TypeVideo {
f, err = s.svr.Favorites(ctx, int8(req.Tp), req.Mid, req.Uid, req.Fid, int(req.Tid), int(req.Tv), int(req.Pn), int(req.Ps), req.Keyword, req.Order)
} else {
f, err = s.svr.FavoritesAll(ctx, int8(req.Tp), req.Mid, req.Uid, req.Fid, int(req.Tid), int(req.Tv), int(req.Pn), int(req.Ps), req.Keyword, req.Order)
}
if err != nil {
return nil, err
}
reply := &pb.FavoritesReply{Res: &pb.ModelFavorites{Page: &pb.ModelPage{
Num: int32(f.Page.Num),
Size_: int32(f.Page.Size),
Count: int32(f.Page.Count),
}}}
for _, data := range f.List {
reply.Res.List = append(reply.Res.List, &pb.ModelFavorite{
Id: data.ID,
Oid: data.Oid,
Mid: data.Mid,
Fid: data.Fid,
Type: int32(data.Type),
State: int32(data.State),
Ctime: int64(data.CTime),
Mtime: int64(data.MTime),
})
}
return reply, err
}
// Folders return folders by ids.
func (s *server) Folders(ctx context.Context, req *pb.FoldersReq) (*pb.FoldersReply, error) {
var afids []*model.ArgFVmid
for _, id := range req.Ids {
afids = append(afids, &model.ArgFVmid{
Fid: id.Fid,
Vmid: id.Mid % 100,
})
}
res, err := s.svr.Folders(ctx, int8(req.Typ), req.Mid, afids)
if err != nil {
return nil, err
}
return &pb.FoldersReply{Res: res}, nil
}

View File

@@ -0,0 +1,44 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"fav.go",
"http.go",
],
importpath = "go-common/app/service/main/favorite/server/http",
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/favorite/service:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/log/anticheat:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/http/blademaster/middleware/antispam:go_default_library",
"//library/net/http/blademaster/middleware/supervisor:go_default_library",
"//library/net/http/blademaster/middleware/verify: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"],
)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,115 @@
package http
import (
"net/http"
"strconv"
"go-common/app/service/main/favorite/conf"
"go-common/app/service/main/favorite/service"
"go-common/library/log"
"go-common/library/log/anticheat"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/middleware/antispam"
"go-common/library/net/http/blademaster/middleware/supervisor"
"go-common/library/net/http/blademaster/middleware/verify"
)
var (
favSvc *service.Service
verifySvc *verify.Verify
antispamM *antispam.Antispam
supervisorM *supervisor.Supervisor
collector *anticheat.AntiCheat
)
// Init init router
func Init(c *conf.Config, svc *service.Service) {
verifySvc = verify.New(c.Verify)
antispamM = antispam.New(c.Antispam)
supervisorM = supervisor.New(c.Supervisor)
favSvc = svc
if c.Infoc2 != nil {
collector = anticheat.New(c.Infoc2)
}
// init outer router
engineOut := bm.DefaultServer(c.BM)
internalRouter(engineOut)
// init serve
if err := engineOut.Start(); err != nil {
log.Error("engineOut.Start() error(%v)", err)
panic(err)
}
}
// internalRouter init internal router api path
func internalRouter(e *bm.Engine) {
// init api
e.Ping(ping)
e.Register(register)
favV3 := e.Group("/x/internal/v3/fav")
{
favV3.GET("", verifySvc.Verify, setMid, Favorites)
favV3.GET("/test", setMid, Favorites)
favV3.GET("/tlists", verifySvc.Verify, setMid, tlists)
favV3.GET("/recents", verifySvc.VerifyUser, recentFavs)
favV3.GET("/batch", verifySvc.Verify, batchFavs)
favV3.POST("/add", verifySvc.VerifyUser, addFav)
favV3.POST("/del", verifySvc.VerifyUser, delFav)
favV3.POST("/madd", verifySvc.VerifyUser, multiAddFavs)
favV3.POST("/mdel", verifySvc.VerifyUser, multiDelFavs)
favV3.POST("/move", verifySvc.VerifyUser, moveFavs)
favV3.POST("/copy", verifySvc.VerifyUser, copyFavs)
favV3.GET("/favored", verifySvc.VerifyUser, isFavored)
favV3.GET("/favoreds", verifySvc.VerifyUser, isFavoreds)
favV3.GET("/users", verifySvc.Verify, userList)
favV3.GET("/count", verifySvc.Verify, oidCount)
favV3.GET("/counts", verifySvc.Verify, oidsCount)
favV3.GET("/default", verifySvc.VerifyUser, inDefaultFolder)
}
folderV3 := e.Group("/x/internal/v3/fav/folder")
{
folderV3.GET("", verifySvc.Verify, setMid, userFolders)
folderV3.GET("/multi", verifySvc.Verify, folders)
folderV3.GET("/info", verifySvc.Verify, folderInfo)
folderV3.GET("/count", verifySvc.Verify, cntUserFolders)
folderV3.POST("/add", verifySvc.VerifyUser, addFolder)
folderV3.POST("/update", verifySvc.VerifyUser, updateFolder)
folderV3.POST("/del", verifySvc.VerifyUser, delFolder)
folderV3.POST("/rename", verifySvc.VerifyUser, renameFolder)
folderV3.POST("/public", verifySvc.VerifyUser, upAttrFolder)
folderV3.POST("/sort", verifySvc.VerifyUser, sortFolders)
folderV3.GET("/cleaned", verifySvc.VerifyUser, isCleaned)
folderV3.POST("/clean", verifySvc.VerifyUser, cleanInvalidFavs)
}
}
func setMid(c *bm.Context) {
var (
err error
mid int64
)
req := c.Request
midStr := req.Form.Get("mid")
if midStr != "" {
mid, err = strconv.ParseInt(midStr, 10, 64)
if err != nil {
c.JSON(nil, err)
c.Abort()
return
}
}
c.Set("mid", mid)
}
// ping check server ok.
func ping(c *bm.Context) {
if err := favSvc.Ping(c); err != nil {
log.Error("favorite http service ping error(%v)", err)
c.AbortWithStatus(http.StatusServiceUnavailable)
}
}
// register check server ok.
func register(c *bm.Context) {
c.JSON(map[string]interface{}{}, nil)
}

View File

@@ -0,0 +1,72 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"fav_test.go",
"rpc_test.go",
"search_test.go",
"service_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/favorite/conf:go_default_library",
"//library/ecode:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"fav.go",
"rpc.go",
"search.go",
"service.go",
],
importpath = "go-common/app/service/main/favorite/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/account/api:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//app/service/main/archive/api/gorpc:go_default_library",
"//app/service/main/archive/model/archive:go_default_library",
"//app/service/main/favorite/conf:go_default_library",
"//app/service/main/favorite/dao:go_default_library",
"//app/service/main/favorite/model:go_default_library",
"//app/service/main/filter/model/rpc:go_default_library",
"//app/service/main/filter/rpc/client:go_default_library",
"//app/service/main/rank/api/gorpc:go_default_library",
"//app/service/main/rank/model:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/stat/prom:go_default_library",
"//library/sync/errgroup:go_default_library",
"//library/sync/pipeline/fanout:go_default_library",
"//library/time: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"],
)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,102 @@
package service
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_UserFolders(t *testing.T) {
Convey("UserFolders", t, func() {
var (
typ int8 = 1
mid int64 = 88888894
vmid int64 = 88888894
oid int64 = 123123
)
res, err := s.UserFolders(context.TODO(), typ, mid, vmid, oid, typ)
t.Logf("res:%+v", res)
So(err, ShouldBeNil)
So(res, ShouldNotBeNil)
})
}
func Test_recentOids(t *testing.T) {
Convey("recentOids", t, func() {
var (
typ int8 = 1
mid int64 = 88888894
fids = []int64{1}
)
res, err := s.recentOids(context.TODO(), typ, mid, fids)
t.Logf("res:%+v", res)
So(err, ShouldBeNil)
So(res, ShouldNotBeNil)
})
}
func Test_CntUserFolders(t *testing.T) {
Convey("CntUserFolders", t, func() {
var (
typ int8 = 1
mid int64 = 88888894
vmid int64 = 88888894
)
res, err := s.CntUserFolders(context.TODO(), typ, mid, vmid)
t.Logf("res:%+v", res)
So(err, ShouldBeNil)
So(res, ShouldNotBeNil)
})
}
func Test_InDefaultFolder(t *testing.T) {
Convey("InDefaultFolder", t, func() {
var (
typ int8 = 1
mid int64 = 88888894
oid int64 = 987
)
res, err := s.InDefaultFolder(context.TODO(), typ, mid, oid)
t.Logf("res:%+v", res)
So(err, ShouldBeNil)
So(res, ShouldBeFalse)
})
}
func Test_OidCount(t *testing.T) {
Convey("OidCount", t, func() {
var (
typ int8 = 1
oid int64 = 987
)
res, err := s.OidCount(context.TODO(), typ, oid)
t.Logf("res:%+v", res)
So(err, ShouldBeNil)
So(res, ShouldNotBeNil)
})
}
func Test_OidsCount(t *testing.T) {
Convey("OidsCount", t, func() {
var (
typ int8 = 1
oids = []int64{1, 2, 3}
)
res, err := s.OidsCount(context.TODO(), typ, oids)
t.Logf("res:%+v", res)
So(err, ShouldBeNil)
So(res, ShouldNotBeNil)
})
}
func Test_BatchFavs(t *testing.T) {
Convey("BatchFavs", t, func() {
var (
typ int8 = 1
mid int64 = 8888894
limit = 1000
)
res, err := s.BatchFavs(context.TODO(), typ, mid, limit)
t.Logf("res:%+v", res)
So(err, ShouldBeNil)
So(res, ShouldNotBeNil)
})
}

View File

@@ -0,0 +1,85 @@
package service
import (
"context"
"fmt"
"go-common/app/service/main/archive/api"
arcmdl "go-common/app/service/main/archive/model/archive"
rankmdl "go-common/app/service/main/rank/model"
"go-common/library/ecode"
"go-common/library/log"
)
// ArcRPC find archive by rpc
func (s *Service) ArcRPC(c context.Context, aid int64) (a *api.Arc, err error) {
argAid := &arcmdl.ArgAid2{
Aid: aid,
}
if a, err = s.arcRPC.Archive3(c, argAid); err != nil {
log.Error("s.arcRPC.Archive3(%v), error(%v)", argAid, err)
}
if !a.IsNormal() {
err = ecode.ArchiveNotExist
}
return
}
// ArcsRPC find archives by rpc
func (s *Service) ArcsRPC(c context.Context, aids []int64) (as map[int64]*api.Arc, err error) {
if len(aids) == 0 {
return
}
argAids := &arcmdl.ArgAids2{
Aids: aids,
}
if as, err = s.arcRPC.Archives3(c, argAids); err != nil {
log.Error("s.arcRPC.Archives3(%v, archives), err(%v)", argAids, err)
}
return
}
// TypeidsRPC find tids by rpc
func (s *Service) TypeidsRPC(c context.Context, oids []int64) (res *rankmdl.GroupResp, err error) {
if len(oids) == 0 {
return
}
arg := &rankmdl.GroupReq{
Business: rankmdl.BusinessArchive,
Oids: oids,
Field: "pid",
}
if res, err = s.rankRPC.Group(c, arg); err != nil {
log.Error("s.rankRPC.Group(%+v), error(%v)", arg, err)
}
return
}
// SortArcsRPC sort oids by rpc
func (s *Service) SortArcsRPC(c context.Context, tid, tv, pn, ps int, field string, oids []int64) (res *rankmdl.SortResp, err error) {
if len(oids) == 0 {
return
}
fmap := make(map[string]string)
if tid != 0 {
fmap["pid"] = fmt.Sprintf("%d", tid)
}
if tv != 0 {
fmap["result"] = "1"
fmap["deleted"] = "0"
fmap["valid"] = "1"
}
arg := &rankmdl.SortReq{
Business: rankmdl.BusinessArchive,
Oids: oids,
Order: rankmdl.RankOrderByDesc,
Field: field,
Filters: fmap,
Pn: pn,
Ps: ps,
}
if res, err = s.rankRPC.Sort(c, arg); err != nil {
log.Error("s.rankRPC.Sort(%+v), error(%v)", arg, err)
}
return
}

View File

@@ -0,0 +1,21 @@
package service
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_ArcsRPC(t *testing.T) {
Convey("ArcsRPC", t, func() {
var (
aids = []int64{123, 345}
)
res, err := s.ArcsRPC(context.TODO(), aids)
t.Logf("res:%+v", res)
t.Logf("err:%+v", err)
So(res, ShouldNotBeNil)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,200 @@
package service
import (
"context"
"sort"
"strconv"
"sync"
"go-common/app/service/main/favorite/model"
"go-common/library/log"
"go-common/library/sync/errgroup"
)
// Relations return relations info by fid.
func (s *Service) Relations(c context.Context, typ int8, mid, uid, fid int64, tid, tv, pn, ps int, keyword, order string) (res *model.Favorites, err error) {
if uid > 0 {
mid = uid
}
if order == "" {
order = model.SortMtime
}
res = new(model.Favorites)
res.Page.Num = pn
res.Page.Size = ps
un, err := s.favDao.FavedBit(c, typ, mid)
if err != nil || un {
return
}
folders := make(map[int64]*model.Folder)
if fid == int64(model.AllFidFlag) {
if folders, err = s.userFolders(c, typ, mid); err != nil {
log.Error("s.userFolders(%d,%d) error(%v)", typ, mid, err)
return nil, err
}
} else {
fd, err1 := s.folder(c, typ, mid, fid)
if err1 != nil {
log.Error("s.folder(%d,%d,%d) error(%v)", typ, mid, fid, err1)
return nil, err1
}
folders[fd.ID] = fd
}
var oids []int64
favs := make([]*model.Favorite, 0)
favMap := make(map[int64]*model.Favorite)
if len(folders) == 1 {
for _, f := range folders {
unExpired, err1 := s.favDao.ExpireRelations(c, mid, f.ID)
if err1 != nil {
log.Error("s.favDao.ExpireRelations(%d,%d) error(%v)", mid, f.ID, err1)
return nil, err1
}
if favs, err = s.parRelations(c, typ, mid, f.ID, f.Count, unExpired); err != nil {
return nil, err
}
for _, f := range favs {
oids = append(oids, f.Oid)
favMap[f.Oid] = f
}
}
} else {
var (
fids []int64
okMap map[int64]bool
)
for fid := range folders {
fids = append(fids, fid)
}
if okMap, err = s.favDao.MultiExpireRelations(c, mid, fids); err != nil {
log.Error("s.favDao.MultiExpireRelations(%v,%d) error(%v)", fids, err)
return
}
g := new(errgroup.Group)
mux := new(sync.Mutex)
for _, v := range folders {
f := v
unExpired := okMap[f.ID]
g.Go(func() error {
gfavs, err1 := s.parRelations(c, typ, mid, f.ID, f.Count, unExpired)
if err1 != nil {
log.Error("s.parRelations(%d,%d,%d,%d,%t) error(%v)", typ, mid, f.ID, f.Count, unExpired, err1)
}
mux.Lock()
favs = append(favs, gfavs...)
mux.Unlock()
return nil
})
}
if err = g.Wait(); err != nil {
return
}
sort.Slice(favs, func(i, j int) bool {
return favs[i].MTime < favs[j].MTime
})
ts := make([]*model.Favorite, 0)
for _, f := range favs {
if _, ok := favMap[f.Oid]; ok {
continue
}
ts = append(ts, f)
oids = append(oids, f.Oid)
favMap[f.Oid] = f
}
favs = ts
}
favs = make([]*model.Favorite, 0)
res.List = _emptyFavRelations
if keyword != "" { // riot-search
resp, err1 := s.favDao.SearchArcs(c, keyword, oids, pn, ps)
if err1 != nil || resp == nil {
return res, err1
}
res.Page.Count = resp.Page.Total
for _, oid := range resp.IDs {
if v, ok := favMap[int64(oid)]; ok {
favs = append(favs, v)
}
}
} else { // rank-service
rankPn := pn
rankPs := ps
if order == model.SortMtime {
rankPn = 1
rankPs = len(oids)
}
resp, err1 := s.SortArcsRPC(c, tid, tv, rankPn, rankPs, order, oids)
if err1 != nil || resp == nil {
return res, err1
}
res.Page.Count = resp.Page.Total
for _, oid := range resp.Result {
if v, ok := favMap[oid]; ok {
favs = append(favs, v)
}
}
if order == model.SortMtime {
sort.Slice(favs, func(i, j int) bool {
return favs[i].MTime > favs[j].MTime
})
start := (pn - 1) * ps
end := pn * ps
if start > len(favs) {
res.List = make([]*model.Favorite, 0)
return
}
if end > len(favs) {
end = len(favs)
}
res.List = favs[start:end]
return
}
}
res.List = favs
return
}
// Tlists return lists info by fid.
func (s *Service) Tlists(c context.Context, typ int8, mid, uid, fid int64) (res []*model.Partition, err error) {
if uid > 0 {
mid = uid
}
res = make([]*model.Partition, 0)
un, err := s.favDao.FavedBit(c, typ, mid)
if err != nil || un {
return
}
f, err := s.folder(c, typ, mid, fid)
if err != nil {
log.Error("s.folder(%d,%d,%d) error(%v)", typ, mid, fid, err)
return nil, err
}
var favs []*model.Favorite
unExpired, err := s.favDao.ExpireRelations(c, mid, f.ID)
if err != nil {
log.Error("s.favDao.ExpireRelations(%d,%d) error(%v)", mid, f.ID, err)
return
}
if favs, err = s.parRelations(c, typ, mid, f.ID, f.Count, unExpired); err != nil {
return
}
var oids []int64
for _, fav := range favs {
oids = append(oids, fav.Oid)
}
typeids, err := s.TypeidsRPC(c, oids)
if err != nil || typeids == nil {
return
}
for _, v := range typeids.List {
p := new(model.Partition)
p.Count = v.Count
v, _ := strconv.Atoi(v.Key)
p.Tid = v
res = append(res, p)
}
sort.Slice(res, func(i, j int) bool {
return res[i].Tid < res[j].Tid
})
return
}

View File

@@ -0,0 +1,26 @@
package service
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Relations(t *testing.T) {
Convey("Relations", t, func() {
var (
pn = 1
ps = 30
typ int8 = 1
mid int64 = 88888894
uid int64 = 88888894
fid int64 = 59
keyword = "测试一下"
)
res, err := s.Relations(context.Background(), typ, mid, uid, fid, 0, 0, pn, ps, keyword, "")
t.Logf("res:%+v", res)
So(err, ShouldBeNil)
So(res, ShouldNotBeNil)
})
}

View File

@@ -0,0 +1,146 @@
package service
import (
"context"
"time"
accapi "go-common/app/service/main/account/api"
arcrpc "go-common/app/service/main/archive/api/gorpc"
"go-common/app/service/main/favorite/conf"
"go-common/app/service/main/favorite/dao"
fltmdl "go-common/app/service/main/filter/model/rpc"
fltrpc "go-common/app/service/main/filter/rpc/client"
rankrpc "go-common/app/service/main/rank/api/gorpc"
"go-common/library/ecode"
"go-common/library/log"
httpx "go-common/library/net/http/blademaster"
"go-common/library/stat/prom"
"go-common/library/sync/pipeline/fanout"
)
// Service define fav service
type Service struct {
conf *conf.Config
missch chan interface{}
cleanCDTime int64
httpClient *httpx.Client
favDao *dao.Dao
// cache chan
cache *fanout.Fanout
// prom
prom *prom.Prom
// rpc
rankRPC *rankrpc.Service
filterRPC *fltrpc.Service
accClient accapi.AccountClient
arcRPC *arcrpc.Service2
}
// New return fav service
func New(c *conf.Config) (s *Service) {
s = &Service{
conf: c,
missch: make(chan interface{}, 1024),
cleanCDTime: int64(time.Duration(c.Fav.CleanCDTime) / time.Second),
httpClient: httpx.NewClient(c.HTTPClient),
// dao
favDao: dao.New(c),
// cache
cache: fanout.New("cache"),
// prom
prom: prom.New().WithTimer("fav_add_video", []string{"method"}),
// rpc
filterRPC: fltrpc.New(c.RPCClient2.Filter),
rankRPC: rankrpc.New(c.RPCClient2.Rank),
arcRPC: arcrpc.New2(c.RPCClient2.Archive),
}
var err error
if s.accClient, err = accapi.NewClient(c.RPCClient2.Account); err != nil {
panic(err)
}
if s.conf.Fav.MaxParallelSize == 0 {
s.conf.Fav.MaxParallelSize = s.conf.Fav.DefaultFolderLimit
}
return
}
// Ping check service health
func (s *Service) Ping(c context.Context) (err error) {
return s.favDao.Ping(c)
}
// Close close service
func (s *Service) Close() {
s.favDao.Close()
}
// PromError stat and log.
func (s *Service) PromError(name string, format string, args ...interface{}) {
prom.BusinessErrCount.Incr(name)
log.Error(format, args...)
}
// Filter filter folder name
func (s *Service) filter(c context.Context, name string) (err error) {
var (
res *fltmdl.FilterRes
)
arg := &fltmdl.ArgFilter{
Area: "open_medialist",
Message: name,
TypeID: 0,
ID: 0,
}
if res, err = s.filterRPC.Filter(c, arg); err != nil {
log.Error("s.filterRPC.Filter(%s) error(%v)", name, err)
return
}
if res.Level >= 20 {
err = ecode.FavFolderBanned
} else if res.Level > 0 {
err = ecode.FavHitSensitive
}
return
}
func (s *Service) checkUser(c context.Context, mid int64) (err error) {
profileReply, err := s.accClient.Profile3(c, &accapi.MidReq{Mid: mid})
if err != nil {
log.Error("s.accClient.Profile3(%d) error(%v)", mid, err)
return nil
}
profile := profileReply.Profile
if profile.Identification == 0 && profile.TelStatus == 0 {
err = ecode.UserCheckNoPhone
return
}
if profile.Identification == 0 && profile.TelStatus == 2 {
err = ecode.UserCheckInvalidPhone
return
}
if profile.EmailStatus == 0 && profile.TelStatus == 0 {
err = ecode.UserInactive
return
}
if profile.Silence == 1 {
err = ecode.UserDisabled
}
return
}
func (s *Service) checkRealname(c context.Context, mid int64) (err error) {
profileReply, err := s.accClient.Profile3(c, &accapi.MidReq{Mid: mid})
if err != nil {
log.Error("s.accClient.Profile3(%d) error(%v)", mid, err)
return nil
}
profile := profileReply.Profile
if profile.Identification == 0 && profile.TelStatus == 0 {
err = ecode.UserCheckNoPhone
return
}
if profile.Identification == 0 && profile.TelStatus == 2 {
err = ecode.UserCheckInvalidPhone
}
return
}

View File

@@ -0,0 +1,39 @@
package service
import (
"context"
"flag"
"fmt"
"path/filepath"
"testing"
"go-common/app/service/main/favorite/conf"
"go-common/library/ecode"
. "github.com/smartystreets/goconvey/convey"
)
var (
s *Service
)
func init() {
dir, _ := filepath.Abs("../cmd/favorite-service-test.toml")
flag.Set("conf", dir)
err := conf.Init()
if err != nil {
fmt.Printf("conf.Init() error(%v)", err)
}
s = New(conf.Conf)
}
func Test_filter(t *testing.T) {
Convey("filter", t, func() {
var (
name = "枪支迷药"
)
err := s.filter(context.TODO(), name)
t.Logf("err:%v", err)
So(err, ShouldEqual, ecode.FavFolderBanned)
})
}