11using System ;
2+ using System . Collections . Generic ;
23using System . Threading ;
34using System . Threading . Tasks ;
45using MongoDB . Bson . Serialization ;
@@ -27,6 +28,8 @@ static MongoNonceRepository()
2728 {
2829 cm . MapIdMember ( c => c . Nonce ) ;
2930 cm . MapField ( c => c . ExpirationDate ) ;
31+ cm . MapField ( c => c . IsDeleted ) ;
32+ cm . MapField ( c => c . ExtraProperties ) ;
3033 cm . SetIgnoreExtraElements ( true ) ;
3134 } ) ;
3235 }
@@ -38,24 +41,51 @@ public MongoNonceRepository(IMongoDatabase database, MongoNonceOptions options)
3841
3942 _collection . Indexes . CreateOne ( new CreateIndexModel < Token > (
4043 Builders < Token > . IndexKeys . Ascending ( nameof ( IdentifiableToken . Identifier ) ) ) ) ;
44+
45+ _collection . Indexes . CreateOne ( new CreateIndexModel < Token > (
46+ Builders < Token > . IndexKeys . Combine (
47+ Builders < Token > . IndexKeys . Ascending ( nameof ( Token . Nonce ) ) ,
48+ Builders < Token > . IndexKeys . Ascending ( nameof ( Token . IsDeleted ) ) ) ) ) ;
49+
50+ _collection . Indexes . CreateOne ( new CreateIndexModel < Token > (
51+ Builders < Token > . IndexKeys . Ascending ( nameof ( Token . ExpirationDate ) ) ,
52+ new CreateIndexOptions
53+ {
54+ ExpireAfter = TimeSpan . FromDays ( options . RecordExpireAfterDays )
55+ } ) ) ;
4156 }
4257
4358 public async ValueTask InsertOneAsync (
4459 Token token , CancellationToken cancellationToken )
4560 {
61+ if ( token . ExtraProperties != null )
62+ {
63+ foreach ( KeyValuePair < string , string > searchAttribute in token . ExtraProperties )
64+ {
65+ _collection . Indexes . CreateOne ( new CreateIndexModel < Token > (
66+ Builders < Token > . IndexKeys . Ascending ( $ "{ nameof ( token . ExtraProperties ) } .{ searchAttribute . Key } ") ) ) ;
67+ }
68+ }
69+
4670 await _collection . InsertOneAsync ( token , cancellationToken : cancellationToken ) ;
4771 }
4872
4973 public async ValueTask < Token ? > TakeOneAsync (
5074 string token ,
5175 CancellationToken cancellationToken )
5276 {
53- FilterDefinition < Token > findFilter = Builders < Token > . Filter . Eq ( n => n . Nonce , token ) ;
77+ FilterDefinition < Token > findFilter =
78+ Builders < Token > . Filter . Eq ( n => n . Nonce , token ) &
79+ ( Builders < Token > . Filter . Not ( Builders < Token > . Filter . Exists ( n => n . IsDeleted ) ) |
80+ Builders < Token > . Filter . Eq ( n => n . IsDeleted , false ) ) ;
81+
82+ UpdateDefinition < Token > updateDefinition =
83+ Builders < Token > . Update . Set ( x => x . IsDeleted , true ) ;
5484
5585 if ( _options . NonceUsage == NonceUsage . OneTime )
5686 {
5787 return await _collection
58- . FindOneAndDeleteAsync ( findFilter , cancellationToken : cancellationToken ) ;
88+ . FindOneAndUpdateAsync ( findFilter , updateDefinition , cancellationToken : cancellationToken ) ;
5989 }
6090
6191 return await _collection
0 commit comments