-
Notifications
You must be signed in to change notification settings - Fork 612
Add new features to MongoDbStorage provider #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
NickCraver
merged 18 commits into
MiniProfiler:main
from
IanKemp:IanKemp/add-mongodb-expiration
Aug 2, 2023
Merged
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
601e4ea
add expiration option to MongoDbStorage
iankemp-CapitalOnTap 4378998
fix
iankemp-CapitalOnTap 00f8165
bump mongodb driver version
iankemp-CapitalOnTap a83465d
fix issues
iankemp-CapitalOnTap beaf706
cleanup
iankemp-CapitalOnTap 95783d6
preserve backwards compatibility
iankemp-CapitalOnTap cb2b2d1
change implementation to be fully backwards (binary) compatible and c…
iankemp-CapitalOnTap c5b651d
more backwards compat
iankemp-CapitalOnTap aa9871e
improve docs
iankemp-CapitalOnTap fa0fe4b
Add explicit handling around AutomaticallyRecreateIndexes
NickCraver 07667f0
Merge branch 'main' into pr/613
NickCraver 8d8d67c
RavenDB: Use separate databases like other suites
NickCraver c7fdd88
Change detection to error code
NickCraver 00b9b58
Fix NRTs and GUID serialization
NickCraver 6dfbbd1
Take 12
NickCraver 91598df
Use options memoized
NickCraver ad81673
Don't run MongoDB on AppVeyor
NickCraver 90d3c32
Update release notes
NickCraver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/MiniProfiler.Providers.MongoDB/IMongoIndexManagerExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using MongoDB.Driver; | ||
| using MongoDB.Driver.Core.Operations; | ||
|
|
||
| namespace StackExchange.Profiling | ||
| { | ||
| /// <summary> | ||
| /// Extension methods for <see cref="IMongoIndexManager{TDocument}"/>. | ||
| /// </summary> | ||
| public static class IMongoIndexManagerExtensions | ||
| { | ||
| /// <summary> | ||
| /// Creates the index specified by <paramref name="model"/>. If an index with the same name already exists, it is first dropped. | ||
| /// </summary> | ||
| /// <typeparam name="TDocument">Type of the document to be indexed.</typeparam> | ||
| /// <param name="indexManager">Manager to create the index in.</param> | ||
| /// <param name="model">Model defining the index.</param> | ||
| /// <param name="options">Additional index creation options, if required.</param> | ||
| /// <returns>Name of the index that was created.</returns> | ||
| /// <remarks>The standard <see cref="IMongoIndexManager{TDocument}.CreateOne(CreateIndexModel{TDocument}, CreateOneIndexOptions, CancellationToken)"/> | ||
| /// method will throw an exception if attempting to create an index with different options to one that already exists. | ||
| /// By dropping that index first, this method ensures an exception will never be thrown, even if different options are used.</remarks> | ||
| public static string CreateOneForce<TDocument>(this IMongoIndexManager<TDocument> indexManager, CreateIndexModel<TDocument> model, CreateOneIndexOptions options = null) | ||
| { | ||
| var indexNames = indexManager | ||
| .List().ToList() | ||
| .SelectMany(index => index.Elements) | ||
| .Where(element => element.Name == "name") | ||
| .Select(name => name.Value.ToString()); | ||
| var indexName = IndexNameHelper.GetIndexName(model.Keys.Render(indexManager.DocumentSerializer, indexManager.Settings.SerializerRegistry)); | ||
|
|
||
| if (indexNames.Contains(indexName)) | ||
| { | ||
| indexManager.DropOne(indexName); | ||
| } | ||
|
|
||
| return indexManager.CreateOne(model, options); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/MiniProfiler.Providers.MongoDB/MongoDbStorageOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| using System; | ||
|
|
||
| namespace StackExchange.Profiling | ||
| { | ||
| /// <summary> | ||
| /// Options for configuring <see cref="MongoDbStorage"/>. | ||
| /// </summary> | ||
| public class MongoDbStorageOptions | ||
| { | ||
| /// <summary> | ||
| /// The connection string to use for connecting to MongoDB. | ||
| /// Defaults to <c>mongodb://localhost</c>. | ||
| /// </summary> | ||
| public string ConnectionString { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Name of the collection in which to store <see cref="MiniProfiler"/> sessions in. | ||
| /// Defaults to <c>profilers</c>. | ||
| /// </summary> | ||
| public string CollectionName { get; set; } = "profilers"; | ||
|
|
||
| /// <summary> | ||
| /// If set to <see langword="true"/>, C# <c>decimal</c> fields will be serialized as <c>NumberDecimal</c>s in MongoDB. | ||
| /// If set to <see langword="false"/>, will serialize these fields as strings (backwards-compatible with older versions of this provider). | ||
| /// Defaults to <see langword="true"/>. | ||
| /// </summary> | ||
| public bool SerializeDecimalFieldsAsNumberDecimal { get; set; } = true; | ||
|
|
||
| /// <summary> | ||
| /// Specifies whether relevant indexes will automatically created when this provider is instantiated. | ||
| /// Defaults to <see langword="true" />. | ||
| /// </summary> | ||
| public bool AutomaticallyCreateIndexes { get; set; } = true; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets how long to cache each <see cref="MiniProfiler"/> for, in absolute terms. | ||
| /// Defaults to one hour. | ||
| /// </summary> | ||
| /// <remarks><list type="bullet"> | ||
| /// <item>You need to either set <see cref="AutomaticallyCreateIndexes"/> to true or call | ||
| /// <see cref="MongoDbStorage.WithIndexCreation(TimeSpan)"/> for this value to have any effect.</item> | ||
| /// <item>Setting this option will drop any (<see cref="MiniProfiler.Started"/>, ascending) index previously | ||
| /// defined, including those with custom options.</item> | ||
| /// </list></remarks> | ||
| public TimeSpan CacheDuration { get; set; } = TimeSpan.FromHours(1); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not expect a method like this (regardless of name) to do a drop. Instead, probably best to have an option for "drop existing" that's very explicitly opt-in. The current overall path (callers to this aren't clear) could unexpectedly lose data for users.