Skip to content

Commit

Permalink
Create mongod indexes for each query schema field (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
pijng authored Nov 5, 2024
1 parent 21df0c5 commit b0f2cd7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
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

0 comments on commit b0f2cd7

Please sign in to comment.