-
Notifications
You must be signed in to change notification settings - Fork 12
/
user_group_hot_recall_hologres_dao.go
133 lines (121 loc) · 3.13 KB
/
user_group_hot_recall_hologres_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
package module
import (
"database/sql"
"fmt"
"math/rand"
"strings"
"sync"
"github.com/huandu/go-sqlbuilder"
"github.com/alibaba/pairec/context"
"github.com/alibaba/pairec/log"
"github.com/alibaba/pairec/persist/holo"
"github.com/alibaba/pairec/recconf"
"github.com/alibaba/pairec/utils"
)
type UserGroupHotRecallHologresDao struct {
db *sql.DB
itemType string
recallName string
table string
recallCount int
mu sync.RWMutex
userStmt *sql.Stmt
trigger *Trigger
}
func NewUserGroupHotRecallHologresDao(config recconf.RecallConfig) *UserGroupHotRecallHologresDao {
hologres, err := holo.GetPostgres(config.DaoConf.HologresName)
if err != nil {
log.Error(fmt.Sprintf("error=%v", err))
return nil
}
dao := &UserGroupHotRecallHologresDao{
recallCount: config.RecallCount,
db: hologres.DB,
table: config.DaoConf.HologresTableName,
itemType: config.ItemType,
recallName: config.Name,
trigger: NewTrigger(config.Triggers),
}
return dao
}
func (d *UserGroupHotRecallHologresDao) ListItemsByUser(user *User, context *context.RecommendContext) (ret []*Item) {
uid := string(user.Id)
builder := sqlbuilder.PostgreSQL.NewSelectBuilder()
builder.Select("item_ids")
builder.From(d.table)
builder.Where(builder.Equal("trigger_id", d.trigger.GetValue(user.MakeUserFeatures2())))
sqlquery, args := builder.Build()
if d.userStmt == nil {
d.mu.Lock()
if d.userStmt == nil {
stmt, err := d.db.Prepare(sqlquery)
if err != nil {
log.Error(fmt.Sprintf("requestId=%s\tmodule=UserGroupHotRecallHologresDao\terror=hologres error(%v)", context.RecommendId, err))
d.mu.Unlock()
return
}
d.userStmt = stmt
d.mu.Unlock()
} else {
d.mu.Unlock()
}
}
rows, err := d.userStmt.Query(args...)
if err != nil {
log.Error(fmt.Sprintf("requestId=%s\tmodule=UserGroupHotRecallHologresDao\tuid=%s\terror=%v", context.RecommendId, uid, err))
return
}
itemIds := make([]string, 0, d.recallCount)
for rows.Next() {
var ids string
if err := rows.Scan(&ids); err == nil {
idList := strings.Split(ids, ",")
for _, id := range idList {
if len(id) > 0 {
itemIds = append(itemIds, id)
}
}
}
}
rows.Close()
if len(itemIds) == 0 {
return
}
if len(itemIds) > d.recallCount {
rand.Shuffle(len(itemIds)/2, func(i, j int) {
itemIds[i], itemIds[j] = itemIds[j], itemIds[i]
})
itemIds = itemIds[:d.recallCount]
}
for _, id := range itemIds {
strs := strings.Split(id, ":")
if len(strs) == 1 {
// itemid
item := NewItem(strs[0])
item.ItemType = d.itemType
item.RetrieveId = d.recallName
ret = append(ret, item)
} else if len(strs) == 2 {
// itemid:RetrieveId
item := NewItem(strs[0])
item.ItemType = d.itemType
if strs[1] != "" {
item.RetrieveId = strs[1]
} else {
item.RetrieveId = d.recallName
}
ret = append(ret, item)
} else if len(strs) == 3 {
item := NewItem(strs[0])
item.ItemType = d.itemType
if strs[1] != "" {
item.RetrieveId = strs[1]
} else {
item.RetrieveId = d.recallName
}
item.Score = utils.ToFloat(strs[2], float64(0))
ret = append(ret, item)
}
}
return
}