Skip to content

Commit

Permalink
merge dev
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-nitehawk committed Aug 18, 2020
2 parents 16e9794 + a8bd7a6 commit c110b36
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 97 deletions.
4 changes: 0 additions & 4 deletions MongoDB.Entities/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# To learn more about .editorconfig see https://aka.ms/editorconfigdocs
root = true

# CS files
[*.cs]
dotnet_diagnostic.RCS1090.severity = none
dotnet_diagnostic.CS1573.severity = none
dotnet_diagnostic.IDE1006.severity = none
dotnet_diagnostic.CS1591.severity = none

# All files
[*]
indent_style = space

# Xml files
[*.xml]
indent_size = 2
5 changes: 2 additions & 3 deletions MongoDB.Entities/Commands/Find.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,9 @@ public Find<T, TProjection> Project(Func<ProjectionDefinitionBuilder<T>, Project
public Find<T, TProjection> ProjectExcluding(Expression<Func<T, object>> exclusion)
{
var props = (exclusion.Body as NewExpression)?.Arguments
.Select(a => a.ToString().Split('.')[1])
.ToArray();
.Select(a => a.ToString().Split('.')[1]);

if (props.Length == 0)
if (!props.Any())
throw new ArgumentException("Unable to get any properties from the exclusion expression!");

var defs = new List<ProjectionDefinition<T>>();
Expand Down
32 changes: 10 additions & 22 deletions MongoDB.Entities/Core/FileEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
Expand Down Expand Up @@ -214,50 +213,41 @@ public async Task UploadAsync(Stream stream, int chunkSizeKB = 256, Cancellation
}
}

private async Task CleanUpAsync(IClientSessionHandle session)
private Task CleanUpAsync(IClientSessionHandle session)
{
await (session == null
? chunkCollection.DeleteManyAsync(c => c.FileID == parent.ID)
: chunkCollection.DeleteManyAsync(session, c => c.FileID == parent.ID));

parent.FileSize = 0;
parent.ChunkCount = 0;
parent.UploadSuccessful = false;
return session == null
? chunkCollection.DeleteManyAsync(c => c.FileID == parent.ID)
: chunkCollection.DeleteManyAsync(session, c => c.FileID == parent.ID);
}

private async Task FlushToDBAsync(IClientSessionHandle session, bool isLastChunk = false, CancellationToken cancellation = default)
{
if (!isLastChunk)
{
dataChunk.AddRange(
readCount == buffer.Length ?
buffer :
new ArraySegment<byte>(buffer, 0, readCount).ToArray());

dataChunk.AddRange(new ArraySegment<byte>(buffer, 0, readCount));
parent.FileSize += readCount;
}

if (dataChunk.Count >= chunkSize || isLastChunk)
{
doc.ID = ObjectId.GenerateNewId().ToString();
doc.Data = dataChunk.ToArray();
await (session == null
? chunkCollection.InsertOneAsync(doc, null, cancellation)
: chunkCollection.InsertOneAsync(session, doc, null, cancellation));
parent.ChunkCount++;
doc.Data = null;
dataChunk.Clear();
parent.ChunkCount++;
await (session == null
? chunkCollection.InsertOneAsync(doc, null, cancellation)
: chunkCollection.InsertOneAsync(session, doc, null, cancellation));
}
}

private Task UpdateMetaDataAsync(IClientSessionHandle session)
{
var type = parent.GetType();
var attribute = type.GetCustomAttribute<NameAttribute>(false);
var collName = attribute != null ? attribute.Name : type.Name;

var coll = db.GetDatabase().GetCollection<FileEntity>(collName);

var coll = db.GetDatabase().GetCollection<FileEntity>(attribute != null ? attribute.Name : type.Name);
var filter = Builders<FileEntity>.Filter.Eq(e => e.ID, parent.ID);
var update = Builders<FileEntity>.Update
.Set(e => e.FileSize, parent.FileSize)
Expand All @@ -269,6 +259,4 @@ private Task UpdateMetaDataAsync(IClientSessionHandle session)
: coll.UpdateOneAsync(session, filter, update);
}
}


}
11 changes: 5 additions & 6 deletions MongoDB.Entities/Core/Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ public Template(string template) : base(template) { }
/// Executes the tag replacement and returns a pipeline definition.
/// <para>TIP: if all the tags don't match, an exception will be thrown.</para>
/// </summary>
public PipelineDefinition<T, TResult> ToPipeline() => base.ToPipeline<T, TResult>();
public PipelineDefinition<T, TResult> ToPipeline() => ToPipeline<T, TResult>();

/// <summary>
/// Executes the tag replacement and returns array filter definitions.
/// <para>TIP: if all the tags don't match, an exception will be thrown.</para>
/// </summary>
public IEnumerable<ArrayFilterDefinition> ToArrayFilters() => base.ToArrayFilters<T>();
public IEnumerable<ArrayFilterDefinition> ToArrayFilters() => ToArrayFilters<T>();
}

/// <summary>
Expand Down Expand Up @@ -277,12 +277,11 @@ public Template Tag(string tagName, string replacementValue)
/// Executes the tag replacement and returns the pipeline stages as an array of BsonDocuments.
/// <para>TIP: if all the tags don't match, an exception will be thrown.</para>
/// </summary>
public BsonDocument[] ToStages()
public IEnumerable<BsonDocument> ToStages()
{
return BsonSerializer
.Deserialize<BsonArray>(ToString())
.Select(v => v.AsBsonDocument)
.ToArray();
.Select(v => v.AsBsonDocument);
}

/// <summary>
Expand All @@ -293,7 +292,7 @@ public BsonDocument[] ToStages()
/// <typeparam name="TOutput">The output type</typeparam>
public PipelineDefinition<TInput, TOutput> ToPipeline<TInput, TOutput>()
{
return ToStages();
return ToStages().ToArray();
}

/// <summary>
Expand Down
13 changes: 6 additions & 7 deletions MongoDB.Entities/DB.Save.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public static Task<UpdateResult> SavePreservingAsync<T>(T entity, Expression<Fun
!(p.IsDefined(typeof(BsonIgnoreIfDefaultAttribute), false) && p.GetValue(entity) == default) &&
!(p.IsDefined(typeof(BsonIgnoreIfNullAttribute), false) && p.GetValue(entity) == null));

string[] propsToPreserve = new string[0];
IEnumerable<string> propsToPreserve = default;

if (preservation == null)
{
Expand All @@ -204,24 +204,23 @@ public static Task<UpdateResult> SavePreservingAsync<T>(T entity, Expression<Fun

if (dontProps.Any())
{
propsToPreserve = propsToUpdate.Where(p => !dontProps.Contains(p.Name)).Select(p => p.Name).ToArray();
propsToPreserve = propsToUpdate.Where(p => !dontProps.Contains(p.Name)).Select(p => p.Name);
}

if (presProps.Any())
{
propsToPreserve = propsToUpdate.Where(p => presProps.Contains(p.Name)).Select(p => p.Name).ToArray();
propsToPreserve = propsToUpdate.Where(p => presProps.Contains(p.Name)).Select(p => p.Name);
}

if (propsToPreserve.Length == 0)
if (!propsToPreserve.Any())
throw new ArgumentException("No properties are being preserved. Please use .Save() method instead!");
}
else
{
propsToPreserve = (preservation.Body as NewExpression)?.Arguments
.Select(a => a.ToString().Split('.')[1])
.ToArray();
.Select(a => a.ToString().Split('.')[1]);

if (propsToPreserve.Length == 0)
if (!propsToPreserve.Any())
throw new ArgumentException("Unable to get any properties from the preservation expression!");
}

Expand Down
4 changes: 2 additions & 2 deletions MongoDB.Entities/DB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static DB()
new IgnoreExtraElementsConvention(true),
new IgnoreManyPropertiesConvention()
},
type => true);
_ => true);
}

//todo: remove obsoletes at version 15
Expand All @@ -39,7 +39,7 @@ static DB()
[Obsolete("Please use .DatabaseName() method...")]
public string Database() => DatabaseName();

internal string dbName = null;
internal string dbName;

private static readonly Dictionary<string, IMongoDatabase> dbs = new Dictionary<string, IMongoDatabase>();
private static readonly Dictionary<string, DB> instances = new Dictionary<string, DB>();
Expand Down
14 changes: 6 additions & 8 deletions MongoDB.Entities/MongoDB.Entities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
<Description>A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management.</Description>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/dj-nitehawk/MongoDB.Entities</PackageProjectUrl>
<Version>14</Version>
<AssemblyVersion>14.0.0.0</AssemblyVersion>
<FileVersion>14.0.0.0</FileVersion>
<Version>14.1</Version>
<AssemblyVersion>14.1.0.0</AssemblyVersion>
<FileVersion>14.1.0.0</FileVersion>
<Copyright>Đĵ ΝιΓΞΗΛψΚ</Copyright>
<PackageReleaseNotes>- [breaking] remove MongoDB.Entities.Core namespace
- [breaking] make ModifiedOn property opt-in using IModifiedOn interface
- [breaking] remove support for geo haystack indexes as deprecated in official driver
- new interface ICreatedOn for optionally adding auto-managed creation date to entities
- misc. changes</PackageReleaseNotes>
<PackageReleaseNotes>- file data streamer optimizations
- transaction support for all Many&lt;T&gt; methods
- misc. performance optimizations</PackageReleaseNotes>
<PackageId>MongoDB.Entities</PackageId>
<Product>MongoDB.Entities</Product>
<RepositoryUrl></RepositoryUrl>
Expand Down
54 changes: 29 additions & 25 deletions MongoDB.Entities/Relationships/Many.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public abstract class ManyBase
{
//shared state for all Many<T> instances
internal static ConcurrentBag<string> indexedCollections = new ConcurrentBag<string>();

internal static string PropType = typeof(Many<Entity>).Name;
}

Expand All @@ -37,28 +36,28 @@ public class Many<TChild> : ManyBase, IEnumerable<TChild> where TChild : IEntity
private IEntity parent;

/// <inheritdoc/>
public IEnumerator<TChild> GetEnumerator()
{
return ChildrenQueryable().GetEnumerator();
}
public IEnumerator<TChild> GetEnumerator() => ChildrenQueryable().GetEnumerator();

/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return ChildrenQueryable().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => ChildrenQueryable().GetEnumerator();

/// <summary>
/// Gets the IMongoCollection of JoinRecords for this relationship.
/// <para>TIP: Try never to use this unless really neccessary.</para>
/// </summary>
public IMongoCollection<JoinRecord> JoinCollection { get; private set; } = null;
public IMongoCollection<JoinRecord> JoinCollection { get; private set; }

/// <summary>
/// An IQueryable of JoinRecords for this relationship
/// </summary>
/// <param name="session">An optional session if using within a transaction</param>
/// <param name="options">An optional AggregateOptions object</param>
public IMongoQueryable<JoinRecord> JoinQueryable(AggregateOptions options = null) => JoinCollection.AsQueryable(options);
public IMongoQueryable<JoinRecord> JoinQueryable(IClientSessionHandle session = null, AggregateOptions options = null)
{
return session == null
? JoinCollection.AsQueryable(options)
: JoinCollection.AsQueryable(session, options);
}

/// <summary>
/// An IAggregateFluent of JoinRecords for this relationship
Expand All @@ -77,25 +76,27 @@ public IAggregateFluent<JoinRecord> JoinFluent(IClientSessionHandle session = nu
/// </summary>
/// <typeparam name="TParent">The type of the parent IEntity</typeparam>
/// <param name="childID">A child ID</param>
/// <param name="session">An optional session if using within a transaction</param>
/// <param name="options">An optional AggregateOptions object</param>
public IMongoQueryable<TParent> ParentsQueryable<TParent>(string childID, AggregateOptions options = null) where TParent : IEntity
public IMongoQueryable<TParent> ParentsQueryable<TParent>(string childID, IClientSessionHandle session = null, AggregateOptions options = null) where TParent : IEntity
{
return ParentsQueryable<TParent>(new[] { childID }, options);
return ParentsQueryable<TParent>(new[] { childID }, session, options);
}

/// <summary>
/// Get an IQueryable of parents matching multiple child IDs for this relationship.
/// </summary>
/// <typeparam name="TParent">The type of the parent IEntity</typeparam>
/// <param name="childIDs">An IEnumerable of child IDs</param>
/// <param name="session">An optional session if using within a transaction</param>
/// <param name="options">An optional AggregateOptions object</param>
public IMongoQueryable<TParent> ParentsQueryable<TParent>(IEnumerable<string> childIDs, AggregateOptions options = null) where TParent : IEntity
public IMongoQueryable<TParent> ParentsQueryable<TParent>(IEnumerable<string> childIDs, IClientSessionHandle session = null, AggregateOptions options = null) where TParent : IEntity
{
if (typeof(TParent) == typeof(TChild)) throw new InvalidOperationException("Both parent and child types cannot be the same");

if (isInverse)
{
return JoinQueryable(options)
return JoinQueryable(session, options)
.Where(j => childIDs.Contains(j.ParentID))
.Join(
DB.Collection<TParent>(),
Expand All @@ -106,7 +107,7 @@ public IMongoQueryable<TParent> ParentsQueryable<TParent>(IEnumerable<string> ch
}
else
{
return JoinQueryable(options)
return JoinQueryable(session, options)
.Where(j => childIDs.Contains(j.ChildID))
.Join(
DB.Collection<TParent>(),
Expand All @@ -122,16 +123,17 @@ public IMongoQueryable<TParent> ParentsQueryable<TParent>(IEnumerable<string> ch
/// </summary>
/// <typeparam name="TParent">The type of the parent IEntity</typeparam>
/// <param name="children">An IQueryable of children</param>
/// <param name="session">An optional session if using within a transaction</param>
/// <param name="options">An optional AggregateOptions object</param>
public IMongoQueryable<TParent> ParentsQueryable<TParent>(IMongoQueryable<TChild> children, AggregateOptions options = null) where TParent : IEntity
public IMongoQueryable<TParent> ParentsQueryable<TParent>(IMongoQueryable<TChild> children, IClientSessionHandle session = null, AggregateOptions options = null) where TParent : IEntity
{
if (typeof(TParent) == typeof(TChild)) throw new InvalidOperationException("Both parent and child types cannot be the same");

if (isInverse)
{
return children
.Join(
JoinQueryable(options),
JoinQueryable(session, options),
c => c.ID,
j => j.ParentID,
(_, j) => j)
Expand All @@ -146,7 +148,7 @@ public IMongoQueryable<TParent> ParentsQueryable<TParent>(IMongoQueryable<TChild
{
return children
.Join(
JoinQueryable(options),
JoinQueryable(session, options),
c => c.ID,
j => j.ChildID,
(_, j) => j)
Expand Down Expand Up @@ -210,9 +212,10 @@ public IAggregateFluent<TParent> ParentsFluent<TParent>(IAggregateFluent<TChild>
/// <typeparam name="TParent">The type of the parent IEntity</typeparam>
/// <param name="childID">An child ID</param>
/// <param name="session">An optional session if using within a transaction</param>
public IAggregateFluent<TParent> ParentsFluent<TParent>(string childID, IClientSessionHandle session = null) where TParent : IEntity
/// <param name="options">An optional AggregateOptions object</param>
public IAggregateFluent<TParent> ParentsFluent<TParent>(string childID, IClientSessionHandle session = null, AggregateOptions options = null) where TParent : IEntity
{
return ParentsFluent<TParent>(new[] { childID }, session);
return ParentsFluent<TParent>(new[] { childID }, session, options);
}

/// <summary>
Expand Down Expand Up @@ -289,14 +292,15 @@ public Task<long> ChildrenCountAsync(IClientSessionHandle session = null, CountO
/// <summary>
/// An IQueryable of child Entities for the parent.
/// </summary>
/// <param name="session">An optional session if using within a transaction</param>
/// <param name="options">An optional AggregateOptions object</param>
public IMongoQueryable<TChild> ChildrenQueryable(AggregateOptions options = null)
public IMongoQueryable<TChild> ChildrenQueryable(IClientSessionHandle session = null, AggregateOptions options = null)
{
parent.ThrowIfUnsaved();

if (isInverse)
{
return JoinQueryable(options)
return JoinQueryable(session, options)
.Where(j => j.ChildID == parent.ID)
.Join(
DB.Collection<TChild>(),
Expand All @@ -306,7 +310,7 @@ public IMongoQueryable<TChild> ChildrenQueryable(AggregateOptions options = null
}
else
{
return JoinQueryable(options)
return JoinQueryable(session, options)
.Where(j => j.ParentID == parent.ID)
.Join(
DB.Collection<TChild>(),
Expand All @@ -319,7 +323,7 @@ public IMongoQueryable<TChild> ChildrenQueryable(AggregateOptions options = null
/// <summary>
/// An IAggregateFluent of child Entities for the parent.
/// </summary>
/// <param name="session"></param>
/// <param name="session">An optional session if using within a transaction</param>
/// <param name="options">An optional AggregateOptions object</param>
public IAggregateFluent<TChild> ChildrenFluent(IClientSessionHandle session = null, AggregateOptions options = null)
{
Expand Down
Loading

0 comments on commit c110b36

Please sign in to comment.