Skip to content

Commit

Permalink
Merge pull request #348 from Southclaws/thread-perf
Browse files Browse the repository at this point in the history
improve thread read performance
  • Loading branch information
Southclaws authored Jan 4, 2025
2 parents ccdbf70 + 4437cbc commit 6c2d743
Show file tree
Hide file tree
Showing 24 changed files with 648 additions and 186 deletions.
9 changes: 9 additions & 0 deletions app/resources/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Southclaws/fault/ftag"
"github.com/Southclaws/opt"
"github.com/rs/xid"
"github.com/samber/lo"

"github.com/Southclaws/storyden/app/resources/account/role/held"
"github.com/Southclaws/storyden/app/resources/datagraph"
Expand Down Expand Up @@ -44,6 +45,14 @@ type Account struct {
InvitedBy opt.Optional[Account]
}

type Accounts []*Account

func (a Accounts) Map() Lookup {
return lo.KeyBy(a, func(a *Account) xid.ID { return xid.ID(a.ID) })
}

type Lookup map[xid.ID]*Account

type ExternalLink struct {
Text string
URL url.URL
Expand Down
5 changes: 1 addition & 4 deletions app/resources/account/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ import (
)

func MapAccount(a *ent.Account) (*Account, error) {
rolesEdge, err := a.Edges.AccountRolesOrErr()
if err != nil {
return nil, err
}
rolesEdge := a.Edges.AccountRoles

auths := dt.Map(a.Edges.Authentication, func(a *ent.Authentication) string {
return a.Service
Expand Down
2 changes: 1 addition & 1 deletion app/resources/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Map(in *ent.Event) (*Event, error) {
return nil, err
}

thr, err := thread.FromModel(nil, nil, nil)(threadEdge)
thr, err := thread.Map(threadEdge)
if err != nil {
return nil, err
}
Expand Down
23 changes: 23 additions & 0 deletions app/resources/post/reaction/react.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package reaction
import (
"github.com/forPelevin/gomoji"
"github.com/rs/xid"
"github.com/samber/lo"

"github.com/Southclaws/dt"
"github.com/Southclaws/storyden/app/resources/account"
Expand All @@ -15,8 +16,17 @@ type React struct {
ID ReactID
Emoji string
Author account.Account
target xid.ID
}

type Reacts []*React

func (r Reacts) Map() Lookup {
return lo.GroupBy(r, func(r *React) xid.ID { return r.target })
}

type Lookup map[xid.ID]Reacts

func Map(in *ent.React) (*React, error) {
accountEdge, err := in.Edges.AccountOrErr()
if err != nil {
Expand All @@ -39,6 +49,19 @@ func MapList(in []*ent.React) ([]*React, error) {
return dt.MapErr(in, Map)
}

func Mapper(am account.Lookup) func(in *ent.React) (*React, error) {
return func(in *ent.React) (*React, error) {
acc := am[xid.ID(in.AccountID)]

return &React{
ID: ReactID(in.ID),
Emoji: in.Emoji,
Author: *acc,
target: xid.ID(in.PostID),
}, nil
}
}

func IsValidEmoji(e string) (string, bool) {
if len(e) == 0 {
return "", false
Expand Down
6 changes: 3 additions & 3 deletions app/resources/post/reply/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (d *database) Create(
return nil, fault.Wrap(err, fctx.With(ctx), ftag.With(ftag.Internal))
}

return FromModel(nil)(p)
return Map(p)
}

func (d *database) Get(ctx context.Context, id post.ID) (*Reply, error) {
Expand All @@ -107,7 +107,7 @@ func (d *database) Get(ctx context.Context, id post.ID) (*Reply, error) {
return nil, fault.Wrap(err, fctx.With(ctx), ftag.With(ftag.Internal))
}

return FromModel(nil /* TODO */)(p)
return Map(p)
}

func (d *database) Update(ctx context.Context, id post.ID, opts ...Option) (*Reply, error) {
Expand Down Expand Up @@ -145,7 +145,7 @@ func (d *database) Update(ctx context.Context, id post.ID, opts ...Option) (*Rep
return nil, fault.Wrap(err, fctx.With(ctx), ftag.With(ftag.Internal))
}

return FromModel(nil)(p)
return Map(p)
}

func (d *database) Delete(ctx context.Context, id post.ID) error {
Expand Down
70 changes: 47 additions & 23 deletions app/resources/post/reply/reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/Southclaws/dt"
"github.com/Southclaws/fault"
"github.com/Southclaws/opt"
"github.com/Southclaws/storyden/app/resources/account"
"github.com/Southclaws/storyden/app/resources/collection/collection_item_status"
"github.com/Southclaws/storyden/app/resources/datagraph"
"github.com/Southclaws/storyden/app/resources/link/link_ref"
"github.com/Southclaws/storyden/app/resources/post/reaction"
"github.com/Southclaws/storyden/app/resources/profile"
"github.com/rs/xid"
Expand Down Expand Up @@ -66,17 +66,49 @@ func replyTo(m *ent.Post) opt.Optional[post.ID] {
func (r *Reply) GetCreated() time.Time { return r.CreatedAt }
func (r *Reply) GetUpdated() time.Time { return r.UpdatedAt }

func FromModel(ls post.PostLikesMap) func(m *ent.Post) (*Reply, error) {
return func(m *ent.Post) (*Reply, error) {
authorEdge, err := m.Edges.AuthorOrErr()
if err != nil {
return nil, fault.Wrap(err)
}
func Map(m *ent.Post) (*Reply, error) {
authorEdge, err := m.Edges.AuthorOrErr()
if err != nil {
return nil, fault.Wrap(err)
}

pro, err := profile.ProfileFromModel(authorEdge)
if err != nil {
return nil, fault.Wrap(err)
}
pro, err := profile.ProfileFromModel(authorEdge)
if err != nil {
return nil, fault.Wrap(err)
}

content, err := datagraph.NewRichText(m.Body)
if err != nil {
return nil, fault.Wrap(err)
}

replyTo := replyTo(m)

return &Reply{
Post: post.Post{
ID: post.ID(m.ID),

Content: content,
Author: *pro,
Assets: dt.Map(m.Edges.Assets, asset.Map),
Meta: m.Metadata,

CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
DeletedAt: opt.NewPtr(m.DeletedAt),
},
ReplyTo: replyTo,
}, nil
}

func Mapper(
am account.Lookup,
ls post.PostLikesMap,
rl reaction.Lookup,
) func(m *ent.Post) (*Reply, error) {
return func(m *ent.Post) (*Reply, error) {
authorEdge := am[m.AccountPosts]
pro := profile.ProfileFromAccount(authorEdge)

content, err := datagraph.NewRichText(m.Body)
if err != nil {
Expand All @@ -85,14 +117,7 @@ func FromModel(ls post.PostLikesMap) func(m *ent.Post) (*Reply, error) {

replyTo := replyTo(m)

link := opt.Map(opt.NewPtr(m.Edges.Link), func(in ent.Link) link_ref.LinkRef {
return *link_ref.Map(&in)
})

reacts, err := reaction.MapList(m.Edges.Reacts)
if err != nil {
return nil, err
}
reacts := rl[xid.ID(m.ID)]

reply := &Reply{
Post: post.Post{
Expand All @@ -104,10 +129,9 @@ func FromModel(ls post.PostLikesMap) func(m *ent.Post) (*Reply, error) {
Collections: collection_item_status.Status{
// NOTE: Members cannot yet add replies to collections.
},
Reacts: reacts,
Assets: dt.Map(m.Edges.Assets, asset.Map),
WebLink: link,
Meta: m.Metadata,
Reacts: reacts,
Assets: dt.Map(m.Edges.Assets, asset.Map),
Meta: m.Metadata,

CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
Expand Down
Loading

0 comments on commit 6c2d743

Please sign in to comment.