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 { recordMetadata Content any ContainerTime time.Time StdErr bool // added by toBson() ShipTime time.Time } recordMetadata struct { HostInfo HostInfo File string Offset int64 // byte offset where log entry ends at } ) const ( mongoKeyHostInfo = "host_info" mongoKeyId = "id" mongoKeyHostId = mongoKeyHostInfo + "." + mongoKeyId mongoKeyFileBasename = "file" mongoKeyOffset = "offset" ) // not using marshal, since it is <0.1x performance func (l *mLog) toBson() bson.M { // DO NOT USE QUOTED STRINGS! Move them to const and use variable instead return bson.M{ mongoKeyHostInfo: bson.M{ mongoKeyId: l.HostInfo.id, "name": l.HostInfo.name, "arch": l.HostInfo.arch, }, mongoKeyFileBasename: l.File, mongoKeyOffset: l.Offset, "content": l.Content, "container_time": l.ContainerTime, "stderr": l.StdErr, "ship_time": time.Now(), } }