optimize counts go get them directly

This commit is contained in:
Umputun
2018-03-11 19:01:04 -05:00
parent 0e4a95026e
commit 92cd33f33e
3 changed files with 6 additions and 12 deletions
+1 -1
View File
@@ -550,7 +550,7 @@ func (b *BoltDB) count(tx *bolt.Tx, postURL string, val int) (int, error) {
if countVal == nil {
countVal = itob(0)
}
if val == 0 {
if val == 0 { // get current count, don't update
return btoi(countVal), nil
}
updatedCount := btoi(countVal) + val
+4 -11
View File
@@ -81,18 +81,11 @@ func (s *Service) EditComment(locator Locator, commentID string, text string, ed
}
// Counts returns postID+count list for given comments
func (s *Service) Counts(siteID string, commentIDs []string) ([]PostInfo, error) {
list, err := s.List(siteID, 0, 0)
if err != nil {
return nil, err
}
func (s *Service) Counts(siteID string, postIDs []string) ([]PostInfo, error) {
res := []PostInfo{}
for _, p := range commentIDs {
for _, l := range list {
if p == l.URL {
res = append(res, l)
}
for _, p := range postIDs {
if c, err := s.Count(Locator{SiteID: siteID, URL: p}); err == nil {
res = append(res, PostInfo{URL: p, Count: c})
}
}
return res, nil
+1
View File
@@ -143,5 +143,6 @@ func TestService_Counts(t *testing.T) {
assert.Equal(t, []PostInfo{
PostInfo{URL: "https://radio-t.com", Count: 2},
PostInfo{URL: "https://radio-t.com/2", Count: 1},
PostInfo{URL: "blah", Count: 0},
}, res)
}