forked from Mintegral-official/juno
/
mongo_example.go
359 lines (326 loc) · 8.95 KB
/
mongo_example.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"github.com/MintegralTech/juno/builder"
"github.com/MintegralTech/juno/check"
"github.com/MintegralTech/juno/document"
"github.com/MintegralTech/juno/query"
"github.com/MintegralTech/juno/search"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"os"
"os/signal"
"strconv"
"time"
)
var (
uri string
)
type CampaignInfo struct {
CampaignId int64 `bson:"campaignId,omitempty" json:"campaignId,omitempty"`
AdvertiserId *int32 `bson:"advertiserId,omitempty" json:"advertiserId,omitempty"`
Price *float64 `bson:"price,omitempty" json:"price,omitempty"`
Status int32 `bson:"status,omitempty" json:"status,omitempty"`
PackageName string `bson:"packageName,omitempty" json:"packageName,omitempty"`
CampaignType *int32 `bson:"campaignType,omitempty" json:"campaignType,omitempty"`
Platform *int32 `bson:"platform,omitempty" json:"platform,omitempty"`
OsVersionMinV2 *int `bson:"oVersionMinV2,omitempty" json:"osVersionMinV2,omitempty"`
OsVersionMaxV2 *int `bson:"osVersionMaxV2,omitempty" json:"osVersionMaxV2,omitempty"`
StartTime *int `bson:"startTime,omitempty" json:"startTime,omitempty"`
EndTime *int `bson:"endTime,omitempty" json:"endTime,omitempty"`
DeviceTypeV2 []int64 `bson:"deviceTypeV2,omitempty" json:"deviceTypeV2,omitempty"`
Uptime int64 `bson:"updated,omitempty"`
}
type CampaignParser struct {
}
type UserData struct {
UpTime int64
Campaigns []int64
}
func MakeInfo(info *CampaignInfo) *document.DocInfo {
if info == nil {
return nil
}
docInfo := &document.DocInfo{
Fields: []*document.Field{},
}
docInfo.Id = document.DocId(info.CampaignId)
if info.AdvertiserId != nil {
docInfo.Fields = append(docInfo.Fields, &document.Field{
Name: "AdvertiserId",
IndexType: 2,
Value: int64(*info.AdvertiserId),
ValueType: document.IntFieldType,
})
}
if info.Platform != nil {
docInfo.Fields = append(docInfo.Fields, &document.Field{
Name: "Platform",
IndexType: 2,
Value: int64(*info.Platform),
ValueType: document.IntFieldType,
})
}
if info.Price != nil {
docInfo.Fields = append(docInfo.Fields, &document.Field{
Name: "Price",
IndexType: 1,
Value: *info.Price,
ValueType: document.FloatFieldType,
})
}
if info.StartTime != nil {
docInfo.Fields = append(docInfo.Fields, &document.Field{
Name: "StartTime",
IndexType: 2,
Value: int64(*info.StartTime),
ValueType: document.IntFieldType,
})
}
if info.EndTime != nil {
docInfo.Fields = append(docInfo.Fields, &document.Field{
Name: "EndTime",
IndexType: 2,
Value: int64(*info.EndTime),
ValueType: document.IntFieldType,
})
}
if info.CampaignType != nil {
docInfo.Fields = append(docInfo.Fields, &document.Field{
Name: "CampaignType",
IndexType: 1,
Value: int64(*info.CampaignType),
ValueType: document.IntFieldType,
})
}
if info.OsVersionMaxV2 != nil {
docInfo.Fields = append(docInfo.Fields, &document.Field{
Name: "OsVersionMaxV2",
IndexType: 2,
Value: int64(*info.OsVersionMaxV2),
ValueType: document.IntFieldType,
})
}
if info.OsVersionMinV2 != nil {
docInfo.Fields = append(docInfo.Fields, &document.Field{
Name: "OsVersionMinV2",
IndexType: 2,
Value: int64(*info.OsVersionMinV2),
ValueType: document.IntFieldType,
})
}
docInfo.Fields = append(docInfo.Fields, &document.Field{
Name: "PackageName",
IndexType: 1,
Value: info.PackageName,
ValueType: document.StringFieldType,
})
docInfo.Fields = append(docInfo.Fields, &document.Field{
Name: "DeviceTypeV2",
IndexType: 2,
Value: info.DeviceTypeV2,
ValueType: document.SliceFieldType,
})
return docInfo
}
func (c *CampaignParser) Parse(bytes []byte, userData interface{}) *builder.ParserResult {
ud, ok := userData.(*UserData)
if !ok {
return nil
}
campaign := &CampaignInfo{}
if err := bson.Unmarshal(bytes, &campaign); err != nil {
fmt.Println("bson.Unmarshal error:" + err.Error())
}
if ud.UpTime < campaign.Uptime {
ud.UpTime = campaign.Uptime
}
var info = MakeInfo(campaign)
var mode builder.DataMod = builder.DataDel
if campaign.Status == 1 {
mode = builder.DataAddOrUpdate
}
d, _ := json.Marshal(info)
ud.Campaigns = append(ud.Campaigns, campaign.CampaignId)
fmt.Println("docInfo: ", d)
return &builder.ParserResult{
DataMod: mode,
Value: info,
}
}
func main() {
flag.StringVar(&uri, "uri", "", "uri")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// build index
logger := logrus.New()
ud := &UserData{}
b, e := builder.NewMongoIndexBuilder(&builder.MongoIndexManagerOps{
URI: "mongodb://127.0.0.1:27017",
IncInterval: 5,
BaseInterval: 120,
IncParser: &CampaignParser{},
BaseParser: &CampaignParser{},
BaseQuery: bson.M{"advertiserId": 903, "publisherId": 0, "status": 1, "system": 5},
DB: "new_adn",
Collection: "campaign",
ConnectTimeout: 10000,
ReadTimeout: 20000,
UserData: ud,
Logger: logger,
OnBeforeInc: func(userData interface{}) interface{} {
ud, ok := userData.(*UserData)
if !ok {
return nil
}
incQuery := bson.M{"advertiserId": 903, "publisherId": 0, "system": 5, "updated": bson.M{"$gte": ud.UpTime - 5, "$lte": time.Now().Unix()}}
return incQuery
},
OnFinishInc: func(userData interface{}) {
ud, ok := userData.(*UserData)
if !ok {
return
}
fmt.Printf("OnFinishInc[%d], [%d]", time.Now().UnixNano(), ud.UpTime)
},
})
if e != nil {
fmt.Println(e)
return
}
if e := b.Build(ctx, "indexName"); e != nil {
fmt.Println("build error", e.Error())
}
tIndex := b.GetIndex()
// search: advertiserId=457 or platform=android or (price in [20.0, 1.4, 3.6, 5.7, 2.5] And price >= 1.4)
// invert list
invertIdx := tIndex.GetInvertedIndex()
// storage
storageIdx := tIndex.GetStorageIndex()
var p = []float64{2.3, 1.4, 3.65, 2.46, 2.5}
var a0 = []int64{647, 658, 670}
//var dev = []int64{4, 5}
for _, v := range ud.Campaigns {
d, _ := json.Marshal(tIndex.GetValueById(document.DocId(v)))
fmt.Printf("IndexInfo %d: %s\n", v, string(d))
}
for i := 0; i < 0; i++ {
q := query.NewOrQuery([]query.Query{
// ==
query.NewOrQuery([]query.Query{
query.NewTermQuery(invertIdx.Iterator("Platform", "1")),
}, nil),
// ==
query.NewOrQuery([]query.Query{
query.NewTermQuery(invertIdx.Iterator("AdvertiserId", "457")),
}, nil),
query.NewAndQuery([]query.Query{
// in
query.NewAndQuery([]query.Query{
query.NewTermQuery(storageIdx.Iterator("Price")),
}, []check.Checker{
check.NewInChecker(storageIdx.Iterator("Price"), p, nil, false),
}),
// not in
query.NewAndQuery([]query.Query{
query.NewTermQuery(storageIdx.Iterator("AdvertiserId")),
}, []check.Checker{
check.NewNotChecker(storageIdx.Iterator("AdvertiserId"), a0, nil, false),
})}, nil),
}, nil)
q.SetDebug(1)
tquery := time.Now()
r1 := search.NewSearcher()
r1.Search(tIndex, q)
fmt.Println("query: ", time.Since(tquery))
fmt.Println("+****************************+")
fmt.Println("res: ", len(r1.Docs), r1.Time)
fmt.Println(r1.Docs[0])
fmt.Println(invertIdx.GetValueById(document.DocId(1526540701)))
fmt.Println(r1.QueryDebug)
}
c := make(chan os.Signal)
signal.Notify(c)
s := <-c
fmt.Println("退出信号", s)
}
type myOperation struct {
value interface{}
}
func (o *myOperation) Equal(value interface{}) bool {
// your logic
switch o.value.(type) {
case map[string]int:
return o.value.(map[string]int)[value.(string)] == 1
}
return true
}
func (o *myOperation) Less(value interface{}) bool {
// your logic
return true
}
func (o *myOperation) In(value interface{}) bool {
// your logic
switch value.(type) {
// campaign.AdSchedule
case map[string][]int:
v, ok := o.value.(string)
if !ok {
return false
}
if _, ok = value.(map[string][]int)[v]; ok {
return true
}
//condition.AdvertiserBlocklist AdvertiserWhitelist BlockIndustryIds
case map[string]bool:
//if len(value.(map[string]bool)) <= 0 {
// return false
//}
v, ok := o.value.(int64)
if !ok {
return false
}
if _, ok = value.(map[string]bool)[strconv.FormatInt(v, 10)]; ok {
return true
}
// campaign.AuditAdvertiserMap
case map[string]string:
v, ok := o.value.(string)
if !ok {
return false
}
if _, ok := value.(map[string]string)[v]; ok {
return true
}
// campaign.SubCategoryName condition.BSubCategoryName
case []string:
scn, ok := value.([]string)
if !ok {
return false
}
bscn, ok := o.value.([]string)
if !ok {
return false
}
if len(bscn) > len(scn) {
return false
}
for _, v := range bscn {
for i := 0; i < len(scn); i++ {
if v == scn[i] {
continue
} else if i == len(scn) {
return false
}
}
}
return true
}
return false
}
func (o *myOperation) SetValue(value interface{}) {
o.value = value
}