1 Star 0 Fork 0

liuxuezhan / mylib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
DB_ScoreNormal.go 2.77 KB
一键复制 编辑 原始数据 按行查看 历史
liuxuezhan 提交于 2020-10-14 10:09 . 'new mylib'
package DB
import (
"gitee.com/liuxuezhan/mylib/DBAction"
proto "gitee.com/liuxuezhan/mylib/Protocol"
"gitee.com/liuxuezhan/mylib/Utils/wlog"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type NormalScoreDocument struct {
ID string "_id"
ContentID uint32 "contentid"
SerialID uint32 "serialid"
UserID uint64 "userid"
ActivityIdx uint32 "index" // idx of Activity
ScoreData []byte "data"
}
type NormalScoreDBData struct {
IDx uint32
ContentID uint32
SerialID uint32
UserID uint64
ScoreData *proto.ST_ActivityScore_PB
}
type ScoreNormalObject struct {
DBAction.DBObject
tableName string
}
var (
scoreNormalName = "activity_score_normal"
ScoreNormalObj *ScoreNormalObject
)
func (o *ScoreNormalObject) GetTableName() string {
return o.tableName
}
func (o *ScoreNormalObject) Init(dbName, tableName string) {
o.DBname = dbName
o.tableName = tableName
indexes := make([]mgo.Index, 0)
indexes = append(indexes, // 活动IDx
mgo.Index{
Key: []string{"index"},
Background: true,
})
indexes = append(indexes, // 活动IDx
mgo.Index{
Key: []string{"index", "serialid"},
Background: true,
})
o.DBObject.EnsureIndex(globalSession, o.tableName, indexes)
}
func (o *ScoreNormalObject) RemoveActivityScoreById(id uint32) error {
return o.DBObject.RemoveById(globalSession, o.tableName, id)
}
func LoadActivityScores(activityIdx uint32, serial uint32, res *[]*proto.ST_ActivityScore_User) {
docs := []NormalScoreDocument{}
query := bson.M{"index": activityIdx, "serialid": serial}
ScoreNormalObj.DBObject.GetAll(globalSession, scoreNormalName, "", 0, 50000, query, nil, &docs)
for _, v := range docs {
scoreData := &proto.ST_ActivityScore_User{}
if err := proto.Unmarshal([]byte(v.ScoreData), scoreData); err != nil {
wlog.Error("Parse ActivityScore Error, ", err)
continue
}
//res[scoreData.GetUid()] = scoreData
*res = append(*res, scoreData)
}
// 加载完成,干掉表里面属于这个index的serial不对的项
ScoreNormalObj.DeleteDatasNotSerial(activityIdx, serial)
}
func UpsertActivityScoreNormal(_id string, idx uint32, uid uint64, doc interface{}) error {
keys := make([]DBAction.KeyPairs, 0)
keys = append(keys, DBAction.KeyPairs{"index", idx}, DBAction.KeyPairs{"userid", uid})
//err := ScoreNormalObj.DBObject.UpsertData(globalSession, scoreNormalName, doc, keys)
err := ScoreNormalObj.DBObject.UpsertById(globalSession, scoreNormalName, _id, doc)
return err
}
func (o *ScoreNormalObject) DeleteDatasNotSerial(idx, serial uint32) {
query := bson.M{"index": idx, "serialid": bson.M{"$ne": serial}}
o.DBObject.RemoveAll(globalSession, scoreNormalName, query)
}
func initScoreNormalSingleton(dbName, scoreTableName string) {
ScoreNormalObj = &ScoreNormalObject{}
ScoreNormalObj.Init(dbName, scoreTableName)
}
1
https://gitee.com/liuxuezhan/mylib.git
git@gitee.com:liuxuezhan/mylib.git
liuxuezhan
mylib
mylib
v1.1.3

搜索帮助