Skip to content

Commit

Permalink
fix: flush new subject to search engine at once
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Oct 14, 2024
1 parent 09be440 commit dd54b7a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
6 changes: 5 additions & 1 deletion canal/on_subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func (e *eventHandler) OnSubjectField(ctx context.Context, key json.RawMessage,

func (e *eventHandler) onSubjectChange(ctx context.Context, subjectID model.SubjectID, op string) error {
switch op {
case opCreate, opUpdate, opSnapshot:
case opCreate:
if err := e.search.OnSubjectAdded(ctx, subjectID); err != nil {
return errgo.Wrap(err, "search.OnSubjectAdded")
}
case opUpdate, opSnapshot:
if err := e.search.OnSubjectUpdate(ctx, subjectID); err != nil {
return errgo.Wrap(err, "search.OnSubjectUpdate")
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/trim21/errgo v0.0.3
github.com/trim21/go-phpserialize v0.1.0-alpha.5
github.com/trim21/htest v0.0.4
github.com/trim21/pkg v0.0.3
github.com/trim21/pkg v0.0.4
go.uber.org/fx v1.23.0
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.28.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ github.com/trim21/go-phpserialize v0.1.0-alpha.5 h1:bMsUpfwAgPggQzDKdafNBvkPWDCM
github.com/trim21/go-phpserialize v0.1.0-alpha.5/go.mod h1:/3zMYuOzpcKOevwP3ZN0WxdVRaB3CzJh5T2i41QPgRQ=
github.com/trim21/htest v0.0.4 h1:dDIzKNdIClgtB158DlO+Xf0sfwNycmx3kfo/FJuY+eE=
github.com/trim21/htest v0.0.4/go.mod h1:W+zaYAGCBqx38eMrMGvXrALnbcXR6OBtZiRiHahgo+E=
github.com/trim21/pkg v0.0.3 h1:uAqfoFmmYiIMOSretKj8/tvrQs3KG57020Ff0cx8UtE=
github.com/trim21/pkg v0.0.3/go.mod h1:JrRIFidkCLeuU5j0vBP5ZN0NOp2JavagHZNr4D3AH6Q=
github.com/trim21/pkg v0.0.4 h1:0nYODKdqNUzmUaPFvqSiR420u2uXQgIYyVyiNfH7olc=
github.com/trim21/pkg v0.0.4/go.mod h1:edl6xdqBOJrhMuIGvcY2lg5L9cqp/hVuwHRM/kdzbMg=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand Down
47 changes: 47 additions & 0 deletions internal/mocks/SearchClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions internal/search/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,26 @@ func (c *client) Close() {
}
}

// OnSubjectAdded is the hook called by canal.
func (c *client) OnSubjectAdded(ctx context.Context, id model.SubjectID) error {
s, err := c.subjectRepo.Get(ctx, id, subject.Filter{})
if err != nil {
if errors.Is(err, gerr.ErrNotFound) {
return nil
}
return errgo.Wrap(err, "subjectRepo.Get")
}

if s.Redirect != 0 || s.Ban != 0 {
return c.OnSubjectDelete(ctx, id)
}

extracted := extractSubject(&s)

_, err = c.subjectIndex.UpdateDocumentsWithContext(ctx, extracted, "id")
return err
}

// OnSubjectUpdate is the hook called by canal.
func (c *client) OnSubjectUpdate(ctx context.Context, id model.SubjectID) error {
s, err := c.subjectRepo.Get(ctx, id, subject.Filter{})
Expand Down
4 changes: 3 additions & 1 deletion internal/search/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ import (

type Client interface {
Handler
OnSubjectUpdate(ctx context.Context, id model.SubjectID) error
Close()

OnSubjectAdded(ctx context.Context, id model.SubjectID) error
OnSubjectUpdate(ctx context.Context, id model.SubjectID) error
OnSubjectDelete(ctx context.Context, id model.SubjectID) error
}

Expand Down
2 changes: 2 additions & 0 deletions internal/search/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var _ Client = NoopClient{}
type NoopClient struct {
}

func (n NoopClient) OnSubjectAdded(ctx context.Context, id model.SubjectID) error { return nil }

func (n NoopClient) Handle(c echo.Context) error {
return c.String(http.StatusOK, "search is not enable")
}
Expand Down

0 comments on commit dd54b7a

Please sign in to comment.