Skip to content

Commit

Permalink
feat: support searching character & person (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored Nov 26, 2024
1 parent 7f56155 commit 181fb87
Show file tree
Hide file tree
Showing 33 changed files with 1,667 additions and 543 deletions.
5 changes: 4 additions & 1 deletion canal/canal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (

"github.com/bangumi/server/config"
"github.com/bangumi/server/dal"
"github.com/bangumi/server/internal/character"
"github.com/bangumi/server/internal/person"
"github.com/bangumi/server/internal/pkg/cache"
"github.com/bangumi/server/internal/pkg/driver"
"github.com/bangumi/server/internal/pkg/logger"
Expand Down Expand Up @@ -62,7 +64,8 @@ func Main() error {
fx.Provide(
driver.NewMysqlSqlDB,
driver.NewRueidisClient, logger.Copy, cache.NewRedisCache,
subject.NewMysqlRepo, search.New, session.NewMysqlRepo, session.New,
subject.NewMysqlRepo, character.NewMysqlRepo, person.NewMysqlRepo,
search.New, session.NewMysqlRepo, session.New,
driver.NewS3,
tag.NewCachedRepo,
tag.NewMysqlRepo,
Expand Down
4 changes: 4 additions & 0 deletions canal/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func (e *eventHandler) onMessage(key, value []byte) error {
err = e.OnSubjectField(ctx, key, p)
case "chii_subjects":
err = e.OnSubject(ctx, key, p)
case "chii_characters":
err = e.OnCharacter(ctx, key, p)
case "chii_persons":
err = e.OnPerson(ctx, key, p)
case "chii_members":
err = e.OnUserChange(ctx, key, p)
}
Expand Down
45 changes: 45 additions & 0 deletions canal/on_character.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package canal

import (
"context"
"encoding/json"

"github.com/trim21/errgo"
"go.uber.org/zap"

"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/search"
)

type CharacterKey struct {
ID model.CharacterID `json:"crt_id"`
}

func (e *eventHandler) OnCharacter(ctx context.Context, key json.RawMessage, payload Payload) error {
var k CharacterKey
if err := json.Unmarshal(key, &k); err != nil {
return err
}
return e.onCharacterChange(ctx, k.ID, payload.Op)
}

func (e *eventHandler) onCharacterChange(ctx context.Context, characterID model.CharacterID, op string) error {
switch op {
case opCreate:
if err := e.search.EventAdded(ctx, characterID, search.SearchTargetCharacter); err != nil {
return errgo.Wrap(err, "search.OnCharacterAdded")
}
case opUpdate, opSnapshot:
if err := e.search.EventUpdate(ctx, characterID, search.SearchTargetCharacter); err != nil {
return errgo.Wrap(err, "search.OnCharacterUpdate")
}
case opDelete:
if err := e.search.EventDelete(ctx, characterID, search.SearchTargetCharacter); err != nil {
return errgo.Wrap(err, "search.OnCharacterDelete")
}
default:
e.log.Warn("unexpected operator", zap.String("op", op))
}

return nil
}
44 changes: 44 additions & 0 deletions canal/on_person.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package canal

import (
"context"
"encoding/json"

"github.com/trim21/errgo"
"go.uber.org/zap"

"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/search"
)

type PersonKey struct {
ID model.PersonID `json:"prsn_id"`
}

func (e *eventHandler) OnPerson(ctx context.Context, key json.RawMessage, payload Payload) error {
var k PersonKey
if err := json.Unmarshal(key, &k); err != nil {
return err
}
return e.onPersonChange(ctx, k.ID, payload.Op)
}

func (e *eventHandler) onPersonChange(ctx context.Context, personID model.PersonID, op string) error {
switch op {
case opCreate:
if err := e.search.EventAdded(ctx, personID, search.SearchTargetPerson); err != nil {
return errgo.Wrap(err, "search.OnPersonAdded")
}
case opUpdate, opSnapshot:
if err := e.search.EventUpdate(ctx, personID, search.SearchTargetPerson); err != nil {
return errgo.Wrap(err, "search.OnPersonUpdate")
}
case opDelete:
if err := e.search.EventDelete(ctx, personID, search.SearchTargetPerson); err != nil {
return errgo.Wrap(err, "search.OnPersonDelete")
}
default:
e.log.Warn("unexpected operator", zap.String("op", op))
}
return nil
}
23 changes: 12 additions & 11 deletions canal/on_subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ import (
"go.uber.org/zap"

"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/search"
)

type SubjectKey struct {
ID model.SubjectID `json:"subject_id"`
}

type SubjectFieldKey struct {
ID model.SubjectID `json:"field_sid"`
}

func (e *eventHandler) OnSubject(ctx context.Context, key json.RawMessage, payload Payload) error {
var k SubjectKey
if err := json.Unmarshal(key, &k); err != nil {
Expand All @@ -45,15 +54,15 @@ 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:
if err := e.search.OnSubjectAdded(ctx, subjectID); err != nil {
if err := e.search.EventAdded(ctx, subjectID, search.SearchTargetSubject); err != nil {
return errgo.Wrap(err, "search.OnSubjectAdded")
}
case opUpdate, opSnapshot:
if err := e.search.OnSubjectUpdate(ctx, subjectID); err != nil {
if err := e.search.EventUpdate(ctx, subjectID, search.SearchTargetSubject); err != nil {
return errgo.Wrap(err, "search.OnSubjectUpdate")
}
case opDelete:
if err := e.search.OnSubjectDelete(ctx, subjectID); err != nil {
if err := e.search.EventDelete(ctx, subjectID, search.SearchTargetSubject); err != nil {
return errgo.Wrap(err, "search.OnSubjectDelete")
}
default:
Expand All @@ -62,11 +71,3 @@ func (e *eventHandler) onSubjectChange(ctx context.Context, subjectID model.Subj

return nil
}

type SubjectKey struct {
ID model.SubjectID `json:"subject_id"`
}

type SubjectFieldKey struct {
ID model.SubjectID `json:"field_sid"`
}
3 changes: 2 additions & 1 deletion cmd/web/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func start() error {
index.NewMysqlRepo, auth.NewMysqlRepo, episode.NewMysqlRepo, revision.NewMysqlRepo, infra.NewMysqlRepo,
timeline.NewMysqlRepo, pm.NewMysqlRepo, notification.NewMysqlRepo,

dam.New, subject.NewMysqlRepo, subject.NewCachedRepo, person.NewMysqlRepo,
dam.New, subject.NewMysqlRepo, subject.NewCachedRepo,
character.NewMysqlRepo, person.NewMysqlRepo,

tag.NewCachedRepo, tag.NewMysqlRepo,

Expand Down
Loading

0 comments on commit 181fb87

Please sign in to comment.