-
Notifications
You must be signed in to change notification settings - Fork 17
/
man_article_summary.go
157 lines (127 loc) · 3.95 KB
/
man_article_summary.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
package schema
import (
"github.com/Ptt-official-app/go-openbbsmiddleware/db"
"github.com/Ptt-official-app/go-openbbsmiddleware/mand"
"github.com/Ptt-official-app/go-openbbsmiddleware/types"
"github.com/Ptt-official-app/go-pttbbs/bbs"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
)
type ManArticleSummary struct {
BBoardID bbs.BBoardID `bson:"bid"` //
ArticleID types.ManArticleID `bson:"aid"` //
LevelIdx types.ManArticleID `bson:"level_idx"` //
CreateTime types.NanoTS `bson:"create_time_nano_ts"` //
MTime types.NanoTS `bson:"mtime_nano_ts"`
Title string `bson:"title"` //
IsDir bool `bson:"is_dir"`
UpdateNanoTS types.NanoTS `bson:"update_nano_ts"` // used by article-summary
Idx int `bson:"pttidx"`
}
var (
EMPTY_MAN_ARTICLE_SUMMARY = &ManArticleSummary{}
manArticleSummaryFields = getFields(EMPTY_MAN_ARTICLE, EMPTY_MAN_ARTICLE_SUMMARY)
)
func NewManArticleSummaryFromPB(entry *mand.Entry, boardID bbs.BBoardID, levelIdx types.ManArticleID, updateNanoTS types.NanoTS, idx int) (articleSummary *ManArticleSummary) {
articleID := types.ManArticleID(entry.Path)
createTime := articleID.ToCreateTime()
return &ManArticleSummary{
BBoardID: boardID,
LevelIdx: levelIdx,
ArticleID: articleID,
CreateTime: createTime,
MTime: createTime,
Title: entry.Title,
IsDir: entry.IsDir,
UpdateNanoTS: updateNanoTS,
Idx: idx,
}
}
func UpdateManArticleSummaries(articleSummaries []*ManArticleSummary, updateNanoTS types.NanoTS) (err error) {
if len(articleSummaries) == 0 {
return nil
}
// create items which do not exists yet.
theList := make([]*db.UpdatePair, len(articleSummaries))
for idx, each := range articleSummaries {
query := &ManArticleQuery{
BBoardID: each.BBoardID,
LevelIdx: each.LevelIdx,
Idx: each.Idx,
}
theList[idx] = &db.UpdatePair{
Filter: query,
Update: each,
}
}
r, err := ManArticle_c.BulkCreateOnly(theList)
if err != nil {
return err
}
logrus.Infof("UpdateManArticleSummaries: after BulkCreateaOnly: count: %v, theList: %v", r.UpsertedCount, theList)
if r.UpsertedCount == int64(len(articleSummaries)) { // all are created
return nil
}
upsertedIDs := r.UpsertedIDs
updateArticleSummaries := make([]*db.UpdatePair, 0, len(theList))
for idx, each := range theList {
_, ok := upsertedIDs[int64(idx)]
if ok {
continue
}
origFilter := each.Filter.(*ManArticleQuery)
filter := bson.M{
"$or": bson.A{
bson.M{
MAN_ARTICLE_BBOARD_ID_b: origFilter.BBoardID,
MAN_ARTICLE_LEVEL_IDX_b: origFilter.LevelIdx,
MAN_ARTICLE_IDX_b: origFilter.Idx,
MAN_ARTICLE_UPDATE_NANO_TS_b: bson.M{
"$exists": false,
},
MAN_ARTICLE_IS_DELETED_b: bson.M{"$exists": false},
},
bson.M{
MAN_ARTICLE_BBOARD_ID_b: origFilter.BBoardID,
MAN_ARTICLE_LEVEL_IDX_b: origFilter.LevelIdx,
MAN_ARTICLE_IDX_b: origFilter.Idx,
MAN_ARTICLE_UPDATE_NANO_TS_b: bson.M{
"$lt": updateNanoTS,
},
MAN_ARTICLE_IS_DELETED_b: bson.M{"$exists": false},
},
},
}
each.Filter = filter
updateArticleSummaries = append(updateArticleSummaries, each)
}
// update items with comparing update-nano-ts
_, err = ManArticle_c.BulkUpdateOneOnly(updateArticleSummaries)
return err
}
func RemoveManArticleSummaries(boardID bbs.BBoardID, levelIdx types.ManArticleID, idx int) (err error) {
query := bson.M{
MAN_ARTICLE_BBOARD_ID_b: boardID,
MAN_ARTICLE_LEVEL_IDX_b: levelIdx,
MAN_ARTICLE_IDX_b: bson.M{
"$gte": idx,
},
}
return ManArticle_c.Remove(query)
}
func GetManArticleSummaries(boardID bbs.BBoardID, LevelIdx types.ManArticleID) (result []*ManArticleSummary, err error) {
query := bson.M{
MAN_ARTICLE_BBOARD_ID_b: boardID,
MAN_ARTICLE_LEVEL_IDX_b: LevelIdx,
}
// sort opts
sortOpts := bson.D{
{Key: MAN_ARTICLE_IDX_b, Value: 1},
}
// find
err = ManArticle_c.Find(query, int64(0), &result, manArticleSummaryFields, sortOpts)
if err != nil {
return nil, err
}
return result, nil
}