Skip to content

Commit

Permalink
Implement multi count for objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Simmerl committed Aug 21, 2017
1 parent 43459d7 commit d056320
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 49 deletions.
15 changes: 6 additions & 9 deletions core/comment.go
Expand Up @@ -8,9 +8,6 @@ import (
)

const (
// TypeComment identifies a comment object.
TypeComment = "tg_comment"

attachmentContent = "content"
)

Expand Down Expand Up @@ -76,7 +73,7 @@ func CommentCreate(
OwnerID: origin.UserID,
Owned: true,
Private: input.Private,
Type: TypeComment,
Type: object.TypeComment,
Visibility: post.Visibility,
}

Expand Down Expand Up @@ -120,7 +117,7 @@ func CommentDelete(
origin,
},
Types: []string{
TypeComment,
object.TypeComment,
},
Owned: &defaultOwned,
})
Expand Down Expand Up @@ -188,7 +185,7 @@ func CommentList(
postID,
},
Types: []string{
TypeComment,
object.TypeComment,
},
Owned: &defaultOwned,
})
Expand Down Expand Up @@ -237,7 +234,7 @@ func CommentRetrieve(
origin,
},
Types: []string{
TypeComment,
object.TypeComment,
},
Owned: &defaultOwned,
})
Expand Down Expand Up @@ -286,7 +283,7 @@ func CommentUpdate(
},
Owned: &defaultOwned,
Types: []string{
TypeComment,
object.TypeComment,
},
})
if err != nil {
Expand Down Expand Up @@ -316,7 +313,7 @@ func CommentUpdate(

// IsComment indicates if Object is a comment.
func IsComment(o *object.Object) bool {
if o.Type != TypeComment {
if o.Type != object.TypeComment {
return false
}

Expand Down
18 changes: 9 additions & 9 deletions core/comment_test.go
Expand Up @@ -37,7 +37,7 @@ func TestCommentCreate(t *testing.T) {
ID: &created.ID,
Owned: &defaultOwned,
Types: []string{
TypeComment,
object.TypeComment,
},
})
if err != nil {
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestCommentDelete(t *testing.T) {
ID: &created.ID,
Owned: &defaultOwned,
Types: []string{
TypeComment,
object.TypeComment,
},
})
if err != nil {
Expand Down Expand Up @@ -245,7 +245,7 @@ func TestCommentUpdate(t *testing.T) {
ID: &created.ID,
Owned: &defaultOwned,
Types: []string{
TypeComment,
object.TypeComment,
},
})
if err != nil {
Expand Down Expand Up @@ -303,7 +303,7 @@ func testComment(ownerID uint64, post *object.Object) *object.Object {
ObjectID: post.ID,
OwnerID: ownerID,
Owned: true,
Type: TypeComment,
Type: object.TypeComment,
Visibility: post.Visibility,
}
}
Expand All @@ -319,7 +319,7 @@ func testCommentSet(ownerID uint64, post *object.Object) []*object.Object {
ObjectID: post.ID,
OwnerID: ownerID,
Owned: true,
Type: TypeComment,
Type: object.TypeComment,
Visibility: post.Visibility,
},
{
Expand All @@ -331,7 +331,7 @@ func testCommentSet(ownerID uint64, post *object.Object) []*object.Object {
ObjectID: post.ID,
OwnerID: ownerID + 1,
Owned: true,
Type: TypeComment,
Type: object.TypeComment,
Visibility: post.Visibility,
},
{
Expand All @@ -343,7 +343,7 @@ func testCommentSet(ownerID uint64, post *object.Object) []*object.Object {
ObjectID: post.ID,
OwnerID: ownerID - 1,
Owned: true,
Type: TypeComment,
Type: object.TypeComment,
Visibility: post.Visibility,
},
{
Expand All @@ -355,7 +355,7 @@ func testCommentSet(ownerID uint64, post *object.Object) []*object.Object {
ObjectID: post.ID,
OwnerID: ownerID,
Owned: true,
Type: TypeComment,
Type: object.TypeComment,
Visibility: post.Visibility,
},
{
Expand All @@ -367,7 +367,7 @@ func testCommentSet(ownerID uint64, post *object.Object) []*object.Object {
ObjectID: post.ID,
OwnerID: ownerID,
Owned: true,
Type: TypeComment,
Type: object.TypeComment,
Visibility: post.Visibility,
},
}
Expand Down
4 changes: 2 additions & 2 deletions core/feed.go
Expand Up @@ -1053,7 +1053,7 @@ func sourceComment(
ObjectIDs: postIDs,
Owned: &defaultOwned,
Types: []string{
TypeComment,
object.TypeComment,
},
})
if err != nil {
Expand All @@ -1075,7 +1075,7 @@ func sourceComment(
ID: id,
ObjectID: comment.ObjectID,
Owned: true,
Type: TypeComment,
Type: object.TypeComment,
UserID: comment.OwnerID,
Visibility: event.VisibilityPrivate,
CreatedAt: comment.CreatedAt,
Expand Down
4 changes: 2 additions & 2 deletions core/pipieline_test.go
Expand Up @@ -539,7 +539,7 @@ func TestPipelineObjectCondObjectOwner(t *testing.T) {
Criteria: &rule.CriteriaObject{
New: &object.QueryOptions{
Owned: &defaultOwned,
Types: []string{TypeComment},
Types: []string{object.TypeComment},
},
Old: nil,
},
Expand Down Expand Up @@ -623,7 +623,7 @@ func TestPipelineObjectCondOwner(t *testing.T) {
Criteria: &rule.CriteriaObject{
New: &object.QueryOptions{
Owned: &defaultOwned,
Types: []string{TypeComment},
Types: []string{object.TypeComment},
},
Old: nil,
},
Expand Down
25 changes: 9 additions & 16 deletions core/post.go
Expand Up @@ -35,7 +35,7 @@ type Post struct {

// PostCounts bundles all connected entity counts.
type PostCounts struct {
Comments int
Comments uint64
Likes int
ReactionCounts reaction.Counts
}
Expand Down Expand Up @@ -555,7 +555,12 @@ func enrichCounts(
currentApp *app.App,
ps PostList,
) error {
countsMap, err := reactions.CountMulti(currentApp.Namespace(), reaction.QueryOptions{
commentsMap, err := objects.CountMulti(currentApp.Namespace(), ps.objectIDs()...)
if err != nil {
return err
}

reactionsMap, err := reactions.CountMulti(currentApp.Namespace(), reaction.QueryOptions{
Deleted: &defaultDeleted,
ObjectIDs: ps.objectIDs(),
})
Expand All @@ -564,21 +569,9 @@ func enrichCounts(
}

for _, p := range ps {
comments, err := objects.Count(currentApp.Namespace(), object.QueryOptions{
ObjectIDs: []uint64{
p.ID,
},
Types: []string{
TypeComment,
},
})
if err != nil {
return err
}

p.Counts = PostCounts{
Comments: comments,
ReactionCounts: countsMap[p.ObjectID],
Comments: commentsMap[p.ObjectID].Comments,
ReactionCounts: reactionsMap[p.ObjectID],
}
}

Expand Down
2 changes: 1 addition & 1 deletion handler/http/post.go
Expand Up @@ -445,7 +445,7 @@ func (p *payloadPosts) MarshalJSON() ([]byte, error) {
}

type postCounts struct {
Comments int `json:"comments"`
Comments uint64 `json:"comments"`
Likes int `json:"likes"`
Reactions reactionCounts `json:"reactions"`
}
Expand Down
4 changes: 4 additions & 0 deletions service/object/cache.go
Expand Up @@ -50,6 +50,10 @@ func (s *cacheService) Count(ns string, opts QueryOptions) (int, error) {
return count, err
}

func (s *cacheService) CountMulti(ns string, objectIDs ...uint64) (m CountsMap, err error) {
return s.next.CountMulti(ns, objectIDs...)
}

func (s *cacheService) Put(ns string, input *Object) (output *Object, err error) {
key := cacheCountKey(QueryOptions{
Types: []string{
Expand Down
56 changes: 56 additions & 0 deletions service/object/helper_test.go
@@ -1,6 +1,8 @@
package object

import (
"math/rand"
"reflect"
"testing"
"time"
)
Expand Down Expand Up @@ -279,6 +281,60 @@ func testServiceCount(t *testing.T, p prepareFunc) {
}
}

func testServiceCountMulti(t *testing.T, p prepareFunc) {
var (
namespace = "service_count_multi"
service = p(namespace, t)
objectIDs = []uint64{
uint64(rand.Int63()),
uint64(rand.Int63()),
uint64(rand.Int63()),
}
ownerID = uint64(rand.Int63())
want = CountsMap{}
)

for _, oid := range objectIDs {
article := *testArticle

article.ObjectID = oid
article.OwnerID = ownerID

_, err := service.Put(namespace, &article)
if err != nil {
t.Fatal(err)
}

it := rand.Intn(12)

for i := 0; i < it; i++ {
_, err = service.Put(namespace, &Object{
ObjectID: oid,
Owned: true,
OwnerID: uint64(rand.Int63()),
Type: TypeComment,
Visibility: VisibilityPublic,
})
if err != nil {
t.Fatal(err)
}
}

want[oid] = Counts{
Comments: uint64(it),
}
}

have, err := service.CountMulti(namespace, objectIDs...)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(have, want) {
t.Errorf("have %v, want %v", have, want)
}
}

func testServiceQuery(t *testing.T, p prepareFunc) {
var (
namespace = "service_query"
Expand Down
8 changes: 8 additions & 0 deletions service/object/instrumentation.go
Expand Up @@ -48,6 +48,14 @@ func (s *instrumentService) Count(ns string, opts QueryOptions) (count int, err
return s.next.Count(ns, opts)
}

func (s *instrumentService) CountMulti(ns string, objectIDs ...uint64) (m CountsMap, err error) {
defer func(begin time.Time) {
s.track("CountMulti", ns, begin, err)
}(time.Now())

return s.next.CountMulti(ns, objectIDs...)
}

func (s *instrumentService) Put(ns string, object *Object) (o *Object, err error) {
defer func(begin time.Time) {
s.track("put", ns, begin, err)
Expand Down
21 changes: 20 additions & 1 deletion service/object/logging.go
Expand Up @@ -30,7 +30,7 @@ func (s *logService) Count(ns string, opts QueryOptions) (count int, err error)
ps := []interface{}{
"count", count,
"duration_ns", time.Since(begin).Nanoseconds(),
"method", "Query",
"method", "Count",
"namespace", ns,
"opts", opts,
}
Expand All @@ -45,6 +45,25 @@ func (s *logService) Count(ns string, opts QueryOptions) (count int, err error)
return s.next.Count(ns, opts)
}

func (s *logService) CountMulti(ns string, objectIDs ...uint64) (m CountsMap, err error) {
defer func(begin time.Time) {
ps := []interface{}{
"duration_ns", time.Since(begin).Nanoseconds(),
"ids_count", len(objectIDs),
"method", "CountMulti",
"namespace", ns,
}

if err != nil {
ps = append(ps, "err", err)
}

_ = s.logger.Log(ps...)
}(time.Now())

return s.next.CountMulti(ns, objectIDs...)
}

func (s *logService) Put(ns string, input *Object) (output *Object, err error) {
defer func(begin time.Time) {
ps := []interface{}{
Expand Down
5 changes: 5 additions & 0 deletions service/object/mem.go
@@ -1,6 +1,7 @@
package object

import (
"fmt"
"math"
"sort"
"time"
Expand Down Expand Up @@ -32,6 +33,10 @@ func (s *memService) Count(ns string, opts QueryOptions) (int, error) {
return len(filterList(listFromMap(bucket), opts)), nil
}

func (s *memService) CountMulti(ns string, objectIDs ...uint64) (m CountsMap, err error) {
return nil, fmt.Errorf("memService.CountMulti not implemented")
}

func (s *memService) Put(ns string, object *Object) (*Object, error) {
if err := object.Validate(); err != nil {
return nil, err
Expand Down

0 comments on commit d056320

Please sign in to comment.