From c93b41dcfe687a6d1af8bbfec0ccdc877e05a6c2 Mon Sep 17 00:00:00 2001 From: rasmus Date: Sun, 6 Nov 2022 16:52:22 +0200 Subject: [PATCH] mongo: indexes --- cmd/mongo_struct.go | 13 +++++++++++++ cmd/mower.go | 5 +++++ 2 files changed, 18 insertions(+) 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))