mongo: indexes

This commit is contained in:
rasmus 2022-11-06 16:52:22 +02:00
parent 1744b94cef
commit c93b41dcfe
2 changed files with 18 additions and 0 deletions

View File

@ -1,11 +1,24 @@
package logmower package logmower
import ( import (
"context"
"time" "time"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
) )
func initializeIndexes(ctx context.Context, col *mongo.Collection) error {
ind := col.Indexes()
// (does not create duplicates)
_, err := ind.CreateOne(mongoTimeoutCtx(ctx), mongo.IndexModel{
Keys: bson.D{{Key: mongoKeyFileBasename, Value: 1}, {Key: mongoKeyOffset, Value: -1}},
})
return err
}
// when editing, also edit everything in this file! // when editing, also edit everything in this file!
type mLog struct { type mLog struct {
HostInfo HostInfo HostInfo HostInfo

View File

@ -124,6 +124,11 @@ var App = &cli.App{
state.db = dbClient.Database(uriParsed.Path).Collection("logs") state.db = dbClient.Database(uriParsed.Path).Collection("logs")
err = initializeIndexes(ctx.Context, state.db)
if err != nil {
l.Fatal("initializing indexes", zap.Error(err))
}
state.hostInfo, err = getHostInfo(ctx.String("node-name")) state.hostInfo, err = getHostInfo(ctx.String("node-name"))
if err != nil { if err != nil {
l.Fatal("gathering host info", zap.Error(err)) l.Fatal("gathering host info", zap.Error(err))