/
user_video_collaborative_mysql_dao.go
210 lines (186 loc) · 5.67 KB
/
user_video_collaborative_mysql_dao.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package module
import (
"database/sql"
"fmt"
"math/rand"
"strconv"
"strings"
"github.com/alibaba/pairec/v2/context"
"github.com/alibaba/pairec/v2/log"
"github.com/alibaba/pairec/v2/persist/mysqldb"
"github.com/alibaba/pairec/v2/recconf"
)
type UserVideoCollaborativeMysqlDao struct {
db *sql.DB
userTable string
itemTable string
itemType string
recallName string
normalization bool
}
var (
sql_user_filter = "SELECT item_ids FROM %s WHERE user_id='%s'"
sql_item_filter = "SELECT item_id, similar_item_ids FROM %s WHERE item_id in (%s)"
)
func NewUserVideoCollaborativeMysqlDao(config recconf.RecallConfig) *UserVideoCollaborativeMysqlDao {
dao := &UserVideoCollaborativeMysqlDao{}
mysql, err := mysqldb.GetMysql(config.UserCollaborativeDaoConf.MysqlName)
if err != nil {
log.Error(fmt.Sprintf("%v", err))
return nil
}
dao.db = mysql.DB
dao.userTable = config.UserCollaborativeDaoConf.User2ItemTable
dao.itemTable = config.UserCollaborativeDaoConf.Item2ItemTable
dao.itemType = config.ItemType
dao.recallName = config.Name
if config.UserCollaborativeDaoConf.Normalization == "on" || config.UserCollaborativeDaoConf.Normalization == "" {
dao.normalization = true
}
return dao
}
func (d *UserVideoCollaborativeMysqlDao) ListItemsByUser(user *User, context *context.RecommendContext) (ret []*Item) {
uid := string(user.Id)
sql := fmt.Sprintf(sql_user_filter, d.userTable, uid)
rows, err := d.db.Query(sql)
if err != nil {
log.Error(fmt.Sprintf("requestId=%s\tmodule=UserVideoCollaborativeMysqlDao\tuid=%s\terror=%v", context.RecommendId, uid, err))
return
}
itemIds := make([]string, 0)
preferScoreMap := make(map[string]float64)
for rows.Next() {
var ids string
if err := rows.Scan(&ids); err == nil {
idList := strings.Split(ids, ",")
for _, id := range idList {
strs := strings.Split(id, ":")
if strs[0] == "" {
continue
}
itemIds = append(itemIds, strs[0])
preferScoreMap[strs[0]] = 1
if len(strs) > 1 {
if score, err := strconv.ParseFloat(strs[1], 64); err == nil {
preferScoreMap[strs[0]] = score
} else {
log.Error(fmt.Sprintf("requestId=%s\tmodule=UserVideoCollaborativeMysqlDao\tevent=ParsePreferScore\tuid=%s\terr=%v", context.RecommendId, uid, err))
}
}
}
}
}
rows.Close()
if len(itemIds) == 0 {
log.Info(fmt.Sprintf("module=UserVideoCollaborativeMysqlDao\tuid=%s\terr=item ids empty", uid))
return
}
if len(itemIds) > 100 {
rand.Shuffle(len(itemIds)/2, func(i, j int) {
itemIds[i], itemIds[j] = itemIds[j], itemIds[i]
})
itemIds = itemIds[:100]
}
cpuCount := 4
maps := make(map[int][]string)
for i, id := range itemIds {
maps[i%cpuCount] = append(maps[i%cpuCount], "'"+id+"'")
}
itemIdCh := make(chan []string, cpuCount)
for _, ids := range maps {
itemIdCh <- ids
}
itemCh := make(chan []*Item, cpuCount)
for i := 0; i < cpuCount; i++ {
go func() {
result := make([]*Item, 0)
LOOP:
for {
select {
case ids := <-itemIdCh:
sql := fmt.Sprintf(sql_item_filter, d.itemTable, strings.Join(ids, ","))
rows, err := d.db.Query(sql)
if err != nil {
log.Error(fmt.Sprintf("requestId=%s\tmodule=UserVideoCollaborativeMysqlDao\tsql=%s\terror=%v", context.RecommendId, sql, err))
goto LOOP
}
for rows.Next() {
var triggerId, ids string
if err := rows.Scan(&triggerId, &ids); err != nil {
log.Error(fmt.Sprintf("requestId=%s\tmodule=UserVideoCollaborativeMysqlDao\terror=%v", context.RecommendId, err))
goto LOOP
}
preferScore := preferScoreMap[triggerId]
list := strings.Split(ids, ",")
for _, str := range list {
strs := strings.Split(str, ":")
if len(strs) == 2 && len(strs[0]) > 0 && strs[0] != "null" {
item := NewItem(strs[0])
item.RetrieveId = d.recallName
item.ItemType = d.itemType
if tmpScore, err := strconv.ParseFloat(strings.TrimSpace(strs[1]), 64); err == nil {
item.Score = tmpScore * preferScore
} else {
item.Score = preferScore
}
result = append(result, item)
}
}
}
rows.Close()
default:
goto DONE
}
}
DONE:
itemCh <- result
}()
}
ret = mergeUserCollaborativeItemsResult(itemCh, cpuCount, d.normalization)
close(itemCh)
close(itemIdCh)
return
}
func (d *UserVideoCollaborativeMysqlDao) GetTriggers(user *User, context *context.RecommendContext) (itemTriggers map[string]float64) {
itemTriggers = make(map[string]float64)
triggerInfos := d.GetTriggerInfos(user, context)
for _, trigger := range triggerInfos {
itemTriggers[trigger.ItemId] = trigger.Weight
}
return
}
func (d *UserVideoCollaborativeMysqlDao) GetTriggerInfos(user *User, context *context.RecommendContext) (triggerInfos []*TriggerInfo) {
uid := string(user.Id)
sql := fmt.Sprintf(sql_user_filter, d.userTable, uid)
rows, err := d.db.Query(sql)
if err != nil {
log.Error(fmt.Sprintf("requestId=%s\tmodule=UserVideoCollaborativeMysqlDao\tuid=%s\terror=%v", context.RecommendId, uid, err))
return
}
defer rows.Close()
for rows.Next() {
var ids string
if err := rows.Scan(&ids); err == nil {
idList := strings.Split(ids, ",")
for _, id := range idList {
strs := strings.Split(id, ":")
if strs[0] == "" {
continue
}
trigger := &TriggerInfo{
ItemId: strs[0],
Weight: 1,
}
if len(strs) > 1 {
if score, err := strconv.ParseFloat(strs[1], 64); err == nil {
trigger.Weight = score
} else {
log.Error(fmt.Sprintf("requestId=%s\tmodule=UserVideoCollaborativeMysqlDao\tevent=ParsePreferScore\tuid=%s\terr=%v", context.RecommendId, uid, err))
}
}
triggerInfos = append(triggerInfos, trigger)
}
}
}
return
}