diff --git a/cmd/mongo_struct.go b/cmd/mongo_struct.go index afe4fee..7a3eeea 100644 --- a/cmd/mongo_struct.go +++ b/cmd/mongo_struct.go @@ -1,11 +1,24 @@ package logmower import ( + "context" "time" "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! type mLog struct { HostInfo HostInfo diff --git a/cmd/mower.go b/cmd/mower.go index d5d2b87..da05476 100644 --- a/cmd/mower.go +++ b/cmd/mower.go @@ -124,6 +124,11 @@ var App = &cli.App{ 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")) if err != nil { l.Fatal("gathering host info", zap.Error(err))