Skip to content

Commit ab77c9f

Browse files
committed
fold filtering into db layer
1 parent 7321f70 commit ab77c9f

File tree

6 files changed

+26
-59
lines changed

6 files changed

+26
-59
lines changed

server/activity_handler.go

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package server
22

33
import (
44
"github.com/labstack/echo"
5-
log "github.com/sirupsen/logrus"
65
)
76

87
type jsonResponseActivity struct {
@@ -17,7 +16,21 @@ type jsonResponse struct {
1716

1817
func ActivityHandler(db Database) echo.HandlerFunc {
1918
return func(c echo.Context) error {
20-
activity, next, err := fetchActivity(db, LocaleForRequest(c.Request()), c.QueryParam("next"), c.QueryParam("nohelp") == "true")
19+
locale := LocaleForRequest(c.Request())
20+
filter := func(a Activity) bool {
21+
return true
22+
}
23+
if c.QueryParam("nohelp") == "true" {
24+
filter = func(a Activity) bool {
25+
if fp, ok := a.(*ForumPost); ok {
26+
if fp.ForumId == locale.HelpForumId {
27+
return false
28+
}
29+
}
30+
return true
31+
}
32+
}
33+
activity, next, err := db.Activity(locale, c.QueryParam("next"), 50, filter)
2134
if err != nil {
2235
return err
2336
}
@@ -42,51 +55,3 @@ func ActivityHandler(db Database) echo.HandlerFunc {
4255
return c.JSON(200, response)
4356
}
4457
}
45-
46-
const MinPageSize = 50
47-
const DbRequestSize = 50
48-
const MaxDbRequests = 10
49-
50-
func fetchActivity(db Database, locale *Locale, start string, nohelp bool) ([]Activity, string, error) {
51-
activity := []Activity{}
52-
next := start
53-
pred := func(a Activity) bool {
54-
return true
55-
}
56-
if nohelp {
57-
pred = func(a Activity) bool {
58-
if fp, ok := a.(*ForumPost); ok {
59-
if fp.ForumId == locale.HelpForumId {
60-
return false
61-
}
62-
}
63-
return true
64-
}
65-
}
66-
for i := 0; i < MaxDbRequests && len(activity) < MinPageSize; i++ {
67-
as, n, err := db.Activity(locale, next, DbRequestSize)
68-
if err != nil {
69-
return nil, "", err
70-
}
71-
next = n
72-
if len(as) == 0 && n == "" {
73-
log.Debug("end of activity db")
74-
break
75-
}
76-
filtered := 0
77-
for _, a := range as {
78-
if pred(a) {
79-
activity = append(activity, a)
80-
} else {
81-
filtered++
82-
}
83-
}
84-
log.WithFields(log.Fields{
85-
"count": len(as),
86-
"buffered": len(activity),
87-
"filtered": filtered,
88-
"next": next,
89-
}).Debug("processed activity batch")
90-
}
91-
return activity, next, nil
92-
}

server/bolt_database.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (db *BoltDatabase) AddActivity(activity []Activity) error {
4343
})
4444
}
4545

46-
func (db *BoltDatabase) Activity(locale *Locale, start string, count int) ([]Activity, string, error) {
46+
func (db *BoltDatabase) Activity(locale *Locale, start string, count int, filter func(a Activity) bool) ([]Activity, string, error) {
4747
ret := []Activity(nil)
4848
next := ""
4949
if err := db.db.View(func(tx *bolt.Tx) error {
@@ -66,7 +66,7 @@ func (db *BoltDatabase) Activity(locale *Locale, start string, count int) ([]Act
6666
activity, err := unmarshalActivity(k, v)
6767
if err != nil {
6868
return err
69-
} else if activity != nil && locale.ActivityFilter(activity) {
69+
} else if activity != nil && locale.ActivityFilter(activity) && filter(activity) {
7070
ret = append(ret, activity)
7171
next = base64.RawURLEncoding.EncodeToString(k)
7272
}

server/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717

1818
type Database interface {
1919
AddActivity(activity []Activity) error
20-
Activity(locale *Locale, start string, count int) ([]Activity, string, error)
20+
Activity(locale *Locale, start string, count int, filter func(a Activity) bool) ([]Activity, string, error)
2121
Close() error
2222
}
2323

server/database_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,23 @@ func testDatabase_ForumPosts(t *testing.T, db Database) {
3131

3232
db.AddActivity([]Activity{post1, post2})
3333

34-
posts, next, err := db.Activity(locale, "", 1)
34+
all := func(a Activity) bool { return true }
35+
36+
posts, next, err := db.Activity(locale, "", 1, all)
3537
require.NoError(t, err)
3638
require.Equal(t, 1, len(posts))
3739
assert.Equal(t, post1.Id, posts[0].(*ForumPost).Id)
3840
assert.Equal(t, post1.Poster, posts[0].(*ForumPost).Poster)
3941
assert.Equal(t, post1.Time.Unix(), posts[0].(*ForumPost).Time.Unix())
4042

41-
posts, next, err = db.Activity(locale, next, 1)
43+
posts, next, err = db.Activity(locale, next, 1, all)
4244
require.NoError(t, err)
4345
require.Equal(t, 1, len(posts))
4446
assert.Equal(t, post2.Id, posts[0].(*ForumPost).Id)
4547
assert.Equal(t, post2.Poster, posts[0].(*ForumPost).Poster)
4648
assert.Equal(t, post2.Time.Unix(), posts[0].(*ForumPost).Time.Unix())
4749

48-
posts, _, err = db.Activity(locale, next, 1)
50+
posts, _, err = db.Activity(locale, next, 1, all)
4951
require.NoError(t, err)
5052
require.Equal(t, 0, len(posts))
5153
}

server/dynamodb_database.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (db *DynamoDBDatabase) AddActivity(activity []Activity) error {
8282
return nil
8383
}
8484

85-
func (db *DynamoDBDatabase) Activity(locale *Locale, start string, count int) ([]Activity, string, error) {
85+
func (db *DynamoDBDatabase) Activity(locale *Locale, start string, count int, filter func(a Activity) bool) ([]Activity, string, error) {
8686
var activity []Activity
8787

8888
var startKey map[string]dynamodb.AttributeValue
@@ -120,7 +120,7 @@ func (db *DynamoDBDatabase) Activity(locale *Locale, start string, count int) ([
120120
for _, item := range result.Items {
121121
if a, err := unmarshalActivity(item["rk"].B, item["v"].B); err != nil {
122122
return nil, "", err
123-
} else if a != nil {
123+
} else if a != nil && filter(a) {
124124
activity = append(activity, a)
125125
}
126126
}

server/rss_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type rssResponse struct {
4343

4444
func RSSHandler(db Database) echo.HandlerFunc {
4545
return func(c echo.Context) error {
46-
activity, _, err := db.Activity(LocaleForRequest(c.Request()), c.QueryParam("next"), 50)
46+
activity, _, err := db.Activity(LocaleForRequest(c.Request()), c.QueryParam("next"), 50, func(a Activity) bool { return true })
4747
if err != nil {
4848
return err
4949
}

0 commit comments

Comments
 (0)