// create indexes on the files and chunks collection if needed
create indexes on the files and chunks collection if needed
// create indexes on the files and chunks collection if needed
func (b *Bucket) createIndexes(ctx context.Context) error {
	cloned, err := b.filesColl.Clone(options.Collection().SetReadPreference(readpref.Primary()))
	if err != nil {
		return err
	}
	docRes := cloned.FindOne(ctx, bsonx.Doc{}, options.FindOne().SetProjection(bsonx.Doc{{"_id", bsonx.Int32(1)}}))
	_, err = docRes.DecodeBytes()
	if err != mongo.ErrNoDocuments {
		return err
	}
	filesIv := b.filesColl.Indexes()
	chunksIv := b.chunksColl.Indexes()
	filesModel := mongo.IndexModel{
		Keys: bson.D{
			{"filename", int32(1)},
			{"uploadDate", int32(1)},
		},
	}
	chunksModel := mongo.IndexModel{
		Keys: bson.D{
			{"files_id", int32(1)},
			{"n", int32(1)},
		},
		Options: options.Index().SetUnique(true),
	}
	if err = createIndexIfNotExists(ctx, filesIv, filesModel); err != nil {
		return err
	}
	if err = createIndexIfNotExists(ctx, chunksIv, chunksModel); err != nil {
		return err
	}
	return nil
}
func ( b * Bucket ) createIndexes ( ctx context . Context ) error { cloned , err := b . filesColl . Clone ( options . Collection ( ) . SetReadPreference ( readpref . Primary ( ) ) ) if err != nil { return err } docRes := cloned . FindOne ( ctx , bsonx . Doc { } , options . FindOne ( ) . SetProjection ( bsonx . Doc { { " " , bsonx . Int32 ( 1 ) } } ) ) _ , err = docRes . DecodeBytes ( ) if err != mongo . ErrNoDocuments { return err } filesIv := b . filesColl . Indexes ( ) chunksIv := b . chunksColl . Indexes ( ) filesModel := mongo . IndexModel { Keys : bson . D { { " " , int32 ( 1 ) } , { " " , int32 ( 1 ) } , } , } chunksModel := mongo . IndexModel { Keys : bson . D { { " " , int32 ( 1 ) } , { " " , int32 ( 1 ) } , } , Options : options . Index ( ) . SetUnique ( true ) , } if err = createIndexIfNotExists ( ctx , filesIv , filesModel ) ; err != nil { return err } if err = createIndexIfNotExists ( ctx , chunksIv , chunksModel ) ; err != nil { return err } return nil }
create indexes on the files and chunks collection if needed
create indexes on the files and chunks collection if needed
// must use primary read pref mode to check if files coll empty // nil, or error that occured during the FindOne operation