diff --git a/cif/update.go b/cif/update.go index cc5e560..7889dfd 100644 --- a/cif/update.go +++ b/cif/update.go @@ -51,15 +51,8 @@ func runCifFullDownload(cfg *helpers.Configuration) error { // Set parsed to nil to encourage garbage collection parsed = nil - // Clear out of date schedules - cutoff := time.Now().Add(-time.Hour * 24 * 7) - log.Debug("Attempting to remove outdated services", zap.Time("scheduleEnd before", cutoff)) - count, err := dbAccess.RemoveOutdatedServices(cutoff) - if err != nil { - log.Warn("Out of date services not removed", zap.Error(err)) - } else { - log.Info("Out of date services removed", zap.Int64("removal count", count)) - } + // Create Indexes + dbAccess.CreateTimetableIndexes() postTime := time.Now() updateDuration := postTime.Sub(preTime) @@ -111,11 +104,22 @@ func runCifUpdateDownload(cfg *helpers.Configuration, metadata *dbAccess.CifMeta parsed = nil } + // Write metadata to database ok := dbAccess.PutCifMetadata(metadata, dailyUpdateType) if !ok { log.Warn("CIF Data updated, but metadata write failed.") } + // Clear out of date schedules + cutoff := time.Now().Add(-time.Hour * 24 * 7) + log.Debug("Attempting to remove outdated services", zap.Time("scheduleEnd before", cutoff)) + count, err := dbAccess.RemoveOutdatedServices(cutoff) + if err != nil { + log.Warn("Out of date services not removed", zap.Error(err)) + } else { + log.Info("Out of date services removed", zap.Int64("removal count", count)) + } + endTime := time.Now() duration := endTime.Sub(startTime) log.Info("CIF Update process ended", zap.Duration("duration", duration)) diff --git a/corpus/update.go b/corpus/update.go index e2984c8..ee6d6eb 100644 --- a/corpus/update.go +++ b/corpus/update.go @@ -47,5 +47,9 @@ func RunCorpusUpdate(cfg *helpers.Configuration) error { return err } + if err := dbAccess.CreateCorpusIndexes(); err != nil { + log.Error("Corpus Indexes creation failed, application speed will be reduced", zap.Error(err)) + } + return nil } diff --git a/dbAccess/cif.go b/dbAccess/cif.go index f879ef8..13b3f08 100644 --- a/dbAccess/cif.go +++ b/dbAccess/cif.go @@ -151,3 +151,25 @@ func RemoveOutdatedServices(cutoff time.Time) (count int64, err error) { count = res.DeletedCount return // Automatically returns names values } + +// Creates indexes on the Timetable collection +func CreateTimetableIndexes() error { + coll := MongoClient.Database(DatabaseName).Collection(TimetableCollection) + indexModels := []mongo.IndexModel{ + { + Keys: bson.M{ + "trainUid": 1, + "stpIndicator": 1, + "scheduleStartDate": 1, + }, + Options: options.Index().SetName("delete_query"), + }, + } + + _, err := coll.Indexes().CreateMany(context.Background(), indexModels) + if err != nil { + return err + } + + return nil +} diff --git a/dbAccess/corpus.go b/dbAccess/corpus.go index 8466bdc..b041453 100644 --- a/dbAccess/corpus.go +++ b/dbAccess/corpus.go @@ -4,6 +4,9 @@ import ( "context" "git.fjla.uk/owlboard/go-types/pkg/database" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" ) // Puts an array of Corpus Documents into the database @@ -53,3 +56,27 @@ func convertStationsToInterfaceSlice(stationsData *[]database.StationEntry) *[]i } return &interfaceSlice } + +func CreateCorpusIndexes() error { + coll := MongoClient.Database(DatabaseName).Collection(CorpusCollection) + indexModels := []mongo.IndexModel{ + { + Keys: bson.M{ + "tiploc": 1, + }, + Options: options.Index().SetName("tiploc"), + }, + { + Keys: bson.M{ + "3alpha": 1, + }, + Options: options.Index().SetName("3alpha"), + }, + } + + _, err := coll.Indexes().CreateMany(context.Background(), indexModels) + if err != nil { + return err + } + return nil +}