Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create mongod indexes for each query schema field #45

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ info:
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.14.3
version: 1.15.0
externalDocs:
description: Find out more about spec
url: 'https://moonlogs.pages.dev'
Expand Down
17 changes: 2 additions & 15 deletions internal/persistence/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package persistence
import (
"context"
"fmt"
"moonlogs/internal/shared"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
Expand Down Expand Up @@ -37,20 +37,7 @@ func initMongoDB(dataSourceName string) (*mongo.Client, error) {

func createIndexes(client *mongo.Client) error {
collection := client.Database(MONGODB_DATABASE_NAME).Collection("records")

indexNames := []string{"schema_name", "id", "group_hash"}

for _, name := range indexNames {
indexModel := mongo.IndexModel{
Keys: bson.D{{Key: name, Value: 1}},
Options: options.Index().SetUnique(false),
}

_, err := collection.Indexes().CreateOne(context.Background(), indexModel)
if err != nil {
return fmt.Errorf("creating index `%s` on `records` collection: %w", name, err)
}
}

return nil
return shared.CreateIndexes(collection, indexNames)
}
26 changes: 26 additions & 0 deletions internal/shared/mongo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package shared

import (
"context"
"fmt"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

func CreateIndexes(collection *mongo.Collection, indexNames []string) error {
for _, name := range indexNames {
indexModel := mongo.IndexModel{
Keys: bson.D{{Key: name, Value: 1}},
Options: options.Index().SetUnique(false),
}

_, err := collection.Indexes().CreateOne(context.Background(), indexModel)
if err != nil {
return fmt.Errorf("creating index `%s` on `records` collection: %w", name, err)
}
}

return nil
}
13 changes: 13 additions & 0 deletions internal/storage/mongodb_adapter/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"moonlogs/internal/entities"
"moonlogs/internal/shared"
"moonlogs/internal/storage"

"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -49,6 +50,12 @@ func (s *SchemaStorage) CreateSchema(ctx context.Context, schema entities.Schema
return nil, fmt.Errorf("failed querying inserted schema: %w", err)
}

indexNames := make([]string, 0)
for _, field := range schema.Fields {
indexNames = append(indexNames, fmt.Sprintf("query.%s", field.Name))
}
_ = shared.CreateIndexes(s.collection, indexNames)

return &sm, nil
}

Expand All @@ -67,6 +74,12 @@ func (s *SchemaStorage) UpdateSchemaByID(ctx context.Context, id int, schema ent
return nil, fmt.Errorf("failed updating schema: %w", err)
}

indexNames := make([]string, 0)
for _, field := range schema.Fields {
indexNames = append(indexNames, fmt.Sprintf("query.%s", field.Name))
}
_ = shared.CreateIndexes(s.collection, indexNames)

return s.GetById(ctx, id)
}

Expand Down
Loading