-
Notifications
You must be signed in to change notification settings - Fork 17
/
content_block_utils.go
137 lines (118 loc) · 3.57 KB
/
content_block_utils.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
package schema
import (
"github.com/Ptt-official-app/go-openbbsmiddleware/db"
"github.com/Ptt-official-app/go-openbbsmiddleware/types"
"github.com/Ptt-official-app/go-pttbbs/bbs"
"go.mongodb.org/mongo-driver/bson"
)
func getContentBlock(boardID bbs.BBoardID, articleID bbs.ArticleID, contentID types.ContentID, idx int, coll *db.Collection) (contentBlock *ContentBlock, err error) {
query := bson.M{
CONTENT_BLOCK_BBOARD_ID_b: boardID,
CONTENT_BLOCK_ARTICLE_ID_b: articleID,
CONTENT_BLOCK_CONTENT_ID_b: contentID,
CONTENT_BLOCK_IDX_b: idx,
}
contentBlock = &ContentBlock{}
err = coll.FindOne(query, &contentBlock, nil)
if err != nil {
return nil, err
}
return contentBlock, nil
}
func getContentBlocks(boardID bbs.BBoardID, articleID bbs.ArticleID, contentID types.ContentID, startIdx int, n int64, coll *db.Collection) (contentBlocks []*ContentBlock, err error) {
query := bson.M{
CONTENT_BLOCK_BBOARD_ID_b: boardID,
CONTENT_BLOCK_ARTICLE_ID_b: articleID,
CONTENT_BLOCK_CONTENT_ID_b: contentID,
CONTENT_BLOCK_IDX_b: bson.M{
"$gte": startIdx,
},
}
sort := bson.D{
{Key: CONTENT_BLOCK_IDX_b, Value: 1},
}
err = coll.Find(query, n, &contentBlocks, nil, sort)
if err != nil {
return nil, err
}
return contentBlocks, nil
}
func updateContentBlocks(contentBlocks []*ContentBlock, updateNanoTS types.NanoTS, coll *db.Collection) (err error) {
if len(contentBlocks) == 0 {
return nil
}
p_contentBlocks := contentBlocks
var first []*ContentBlock
for block := getBlock(len(p_contentBlocks), MAX_CONTENT_BLOCK); len(p_contentBlocks) > 0; block = getBlock(len(p_contentBlocks), MAX_CONTENT_BLOCK) {
first, p_contentBlocks = p_contentBlocks[:block], p_contentBlocks[block:]
err = updateContentBlocksCore(first, updateNanoTS, coll)
if err != nil {
return err
}
}
return nil
}
func updateContentBlocksCore(contentBlocks []*ContentBlock, updateNanoTS types.NanoTS, coll *db.Collection) (err error) {
theList := make([]*db.UpdatePair, len(contentBlocks))
for idx, each := range contentBlocks {
filter := &ContentBlockQuery{
BBoardID: each.BBoardID,
ArticleID: each.ArticleID,
ContentID: each.ContentID,
Idx: each.Idx,
}
theList[idx] = &db.UpdatePair{
Filter: filter,
Update: each,
}
}
r, err := coll.BulkCreateOnly(theList)
if err != nil {
return err
}
if r.UpsertedCount == int64(len(contentBlocks)) { // all are created
return nil
}
upsertedIDs := r.UpsertedIDs
updateContentBlocks := make([]*db.UpdatePair, 0, len(theList))
for idx, each := range theList {
_, ok := upsertedIDs[int64(idx)]
if ok {
continue
}
origFilter := each.Filter.(*ContentBlockQuery)
filter := bson.M{
"$or": bson.A{
bson.M{
CONTENT_BLOCK_BBOARD_ID_b: origFilter.BBoardID,
CONTENT_BLOCK_ARTICLE_ID_b: origFilter.ArticleID,
CONTENT_BLOCK_CONTENT_ID_b: origFilter.ContentID,
CONTENT_BLOCK_IDX_b: origFilter.Idx,
COMMENT_UPDATE_NANO_TS_b: bson.M{
"$exists": false,
},
},
bson.M{
CONTENT_BLOCK_BBOARD_ID_b: origFilter.BBoardID,
CONTENT_BLOCK_ARTICLE_ID_b: origFilter.ArticleID,
CONTENT_BLOCK_CONTENT_ID_b: origFilter.ContentID,
CONTENT_BLOCK_IDX_b: origFilter.Idx,
CONTENT_BLOCK_UPDATE_NANO_TS_b: bson.M{
"$lt": updateNanoTS,
},
},
},
}
each.Filter = filter
origUpdate := each.Update
each.Update = bson.M{
"$set": origUpdate,
"$unset": bson.M{
COMMENT_IS_DELETED_b: true,
},
}
updateContentBlocks = append(updateContentBlocks, each)
}
_, err = coll.BulkUpdateOneOnlyNoSet(updateContentBlocks)
return err
}