Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
perf: add indexes of mongo collections (#98)
Browse files Browse the repository at this point in the history
* add indexes of mongo collections

* refactor
  • Loading branch information
rot1024 authored Jan 17, 2022
1 parent ee144e1 commit 691cb78
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 74 deletions.
2 changes: 1 addition & 1 deletion internal/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (w *WebServer) Run() {
if w.appServer.Debug {
debugLog += " with debug mode"
}
log.Infof("server started%s at %s\n", debugLog, w.address)
log.Infof("server started%s at http://%s\n", debugLog, w.address)

go func() {
err := w.appServer.Start(w.address)
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *assetRepo) FindByTeam(ctx context.Context, id id.TeamID, pagination *us
}

func (r *assetRepo) init() {
i := r.client.CreateIndex(context.Background(), nil)
i := r.client.CreateIndex(context.Background(), []string{"team"})
if len(i) > 0 {
log.Infof("mongo: %s: index created: %s", "asset", i)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewDataset(client *mongodoc.Client) repo.Dataset {
}

func (r *datasetRepo) init() {
i := r.client.CreateIndex(context.Background(), nil)
i := r.client.CreateIndex(context.Background(), []string{"scene", "schema"})
if len(i) > 0 {
log.Infof("mongo: %s: index created: %s", "dataset", i)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/dataset_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewDatasetSchema(client *mongodoc.Client) repo.DatasetSchema {
}

func (r *datasetSchemaRepo) init() {
i := r.client.CreateIndex(context.Background(), nil)
i := r.client.CreateIndex(context.Background(), []string{"scene"})
if len(i) > 0 {
log.Infof("mongo: %s: index created: %s", "datasetSchema", i)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewLayer(client *mongodoc.Client) repo.Layer {
}

func (r *layerRepo) init() {
i := r.client.CreateIndex(context.Background(), nil)
i := r.client.CreateIndex(context.Background(), []string{"plugin", "extension", "scene", "group.layers", "tags.id", "tags.tags.id"})
if len(i) > 0 {
log.Infof("mongo: %s: index created: %s", "layer", i)
}
Expand Down
14 changes: 7 additions & 7 deletions internal/infrastructure/mongo/mongodoc/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import (
"github.com/reearth/reearth-backend/pkg/id"
)

type DatasetFieldDocument struct {
Field string
Type string
Value interface{}
Source string
}

type DatasetDocument struct {
ID string
Source string
Expand All @@ -22,6 +15,13 @@ type DatasetDocument struct {
Scene string
}

type DatasetFieldDocument struct {
Field string
Type string
Value interface{}
Source string
}

type DatasetExtendedDocument struct {
DatasetDocument
Graph []*DatasetExtendedDocument
Expand Down
48 changes: 24 additions & 24 deletions internal/infrastructure/mongo/mongodoc/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ import (
"go.mongodb.org/mongo-driver/bson"
)

type LayerDocument struct {
ID string
Name string
Visible bool
Scene string
Plugin *string
Extension *string
Property *string
Infobox *LayerInfoboxDocument
Item *LayerItemDocument
Group *LayerGroupDocument
Tags LayerTagListDocument
}

type LayerItemDocument struct {
LinkedDataset *string
}

type LayerGroupDocument struct {
Layers []string
LinkedDatasetSchema *string
Root bool
}

type LayerInfoboxFieldDocument struct {
ID string
Plugin string
Expand All @@ -28,30 +52,6 @@ type LayerTagDocument struct {

type LayerTagListDocument []LayerTagDocument

type LayerItemDocument struct {
LinkedDataset *string
}

type LayerGroupDocument struct {
Layers []string
LinkedDatasetSchema *string
Root bool
}

type LayerDocument struct {
ID string
Name string
Visible bool
Scene string
Plugin *string
Extension *string
Property *string
Infobox *LayerInfoboxDocument
Item *LayerItemDocument
Group *LayerGroupDocument
Tags LayerTagListDocument
}

type LayerConsumer struct {
Rows []*layer.Layer
GroupRows []*layer.Group
Expand Down
26 changes: 13 additions & 13 deletions internal/infrastructure/mongo/mongodoc/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ import (
"github.com/reearth/reearth-backend/pkg/scene"
)

type SceneDocument struct {
ID string
Project string
Team string
RootLayer string
Widgets []SceneWidgetDocument
AlignSystem *WidgetAlignSystemDocument
Plugins []ScenePluginDocument
UpdateAt time.Time
Property string
Clusters []SceneClusterDocument
}

type SceneWidgetDocument struct {
ID string
Plugin string
Expand All @@ -30,19 +43,6 @@ type SceneClusterDocument struct {
Property string
}

type SceneDocument struct {
ID string
Project string
Team string
RootLayer string
Widgets []SceneWidgetDocument
AlignSystem *WidgetAlignSystemDocument
Plugins []ScenePluginDocument
UpdateAt time.Time
Property string
Clusters []SceneClusterDocument
}

type SceneConsumer struct {
Rows []*scene.Scene
}
Expand Down
16 changes: 8 additions & 8 deletions internal/infrastructure/mongo/mongodoc/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
"github.com/reearth/reearth-backend/pkg/tag"
)

type TagDocument struct {
ID string
Label string
Scene string
Item *TagItemDocument
Group *TagGroupDocument
}

type TagItemDocument struct {
Parent *string
LinkedDatasetFieldID *string
Expand All @@ -20,14 +28,6 @@ type TagGroupDocument struct {
Tags []string
}

type TagDocument struct {
ID string
Label string
Scene string
Item *TagItemDocument
Group *TagGroupDocument
}

type TagConsumer struct {
Rows []*tag.Tag
GroupRows []*tag.Group
Expand Down
22 changes: 11 additions & 11 deletions internal/infrastructure/mongo/mongodoc/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package mongodoc

import (
"github.com/reearth/reearth-backend/pkg/id"
user1 "github.com/reearth/reearth-backend/pkg/user"
"github.com/reearth/reearth-backend/pkg/user"
"go.mongodb.org/mongo-driver/bson"
)

Expand All @@ -18,7 +18,7 @@ type TeamDocument struct {
}

type TeamConsumer struct {
Rows []*user1.Team
Rows []*user.Team
}

func (c *TeamConsumer) Consume(raw bson.Raw) error {
Expand All @@ -38,7 +38,7 @@ func (c *TeamConsumer) Consume(raw bson.Raw) error {
return nil
}

func NewTeam(team *user1.Team) (*TeamDocument, string) {
func NewTeam(team *user.Team) (*TeamDocument, string) {
membersDoc := map[string]TeamMemberDocument{}
for user, r := range team.Members().Members() {
membersDoc[user.String()] = TeamMemberDocument{
Expand All @@ -54,32 +54,32 @@ func NewTeam(team *user1.Team) (*TeamDocument, string) {
}, id
}

func (d *TeamDocument) Model() (*user1.Team, error) {
func (d *TeamDocument) Model() (*user.Team, error) {
tid, err := id.TeamIDFrom(d.ID)
if err != nil {
return nil, err
}

members := map[id.UserID]user1.Role{}
members := map[id.UserID]user.Role{}
if d.Members != nil {
for user, member := range d.Members {
uid, err := id.UserIDFrom(user)
for uid, member := range d.Members {
uid, err := id.UserIDFrom(uid)
if err != nil {
return nil, err
}
members[uid] = user1.Role(member.Role)
members[uid] = user.Role(member.Role)
}
}
return user1.NewTeam().
return user.NewTeam().
ID(tid).
Name(d.Name).
Members(members).
Personal(d.Personal).
Build()
}

func NewTeams(teams []*user1.Team) ([]interface{}, []string) {
res := make([]interface{}, 0, len(teams))
func NewTeams(teams []*user.Team) ([]*TeamDocument, []string) {
res := make([]*TeamDocument, 0, len(teams))
ids := make([]string, 0, len(teams))
for _, d := range teams {
if d == nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewPlugin(client *mongodoc.Client) repo.Plugin {
}

func (r *pluginRepo) init() {
i := r.client.CreateIndex(context.Background(), nil)
i := r.client.CreateIndex(context.Background(), []string{"scene"})
if len(i) > 0 {
log.Infof("mongo: %s: index created: %s", "plugin", i)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewProject(client *mongodoc.Client) repo.Project {
}

func (r *projectRepo) init() {
i := r.client.CreateIndex(context.Background(), []string{"alias"})
i := r.client.CreateIndex(context.Background(), []string{"alias", "team"})
if len(i) > 0 {
log.Infof("mongo: %s: index created: %s", "project", i)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewProperty(client *mongodoc.Client) repo.Property {
}

func (r *propertyRepo) init() {
i := r.client.CreateIndex(context.Background(), nil)
i := r.client.CreateIndex(context.Background(), []string{"scene", "schema"})
if len(i) > 0 {
log.Infof("mongo: %s: index created: %s", "property", i)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewScene(client *mongodoc.Client) repo.Scene {
}

func (r *sceneRepo) init() {
i := r.client.CreateIndex(context.Background(), nil)
i := r.client.CreateIndex(context.Background(), []string{"project"})
if len(i) > 0 {
log.Infof("mongo: %s: index created: %s", "scene", i)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewTag(client *mongodoc.Client) repo.Tag {
}

func (r *tagRepo) init() {
i := r.client.CreateIndex(context.Background(), nil)
i := r.client.CreateIndex(context.Background(), []string{"scene", "group.tags", "item.parent"})
if len(i) > 0 {
log.Infof("mongo: %s: index created: %s", "tag", i)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/infrastructure/mongo/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ func (r *teamRepo) SaveAll(ctx context.Context, teams []*user.Team) error {
return nil
}
docs, ids := mongodoc.NewTeams(teams)
return r.client.SaveAll(ctx, ids, docs)
docs2 := make([]interface{}, 0, len(teams))
for _, d := range docs {
docs2 = append(docs2, d)
}
return r.client.SaveAll(ctx, ids, docs2)
}

func (r *teamRepo) Remove(ctx context.Context, id id.TeamID) error {
Expand Down

0 comments on commit 691cb78

Please sign in to comment.