Skip to content
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

Bucket modification via Upsert #152

Merged
merged 2 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/MongoFramework/AssemblyInternals.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("MongoFramework.Tests")]
9 changes: 6 additions & 3 deletions src/MongoFramework/Attributes/BucketSetOptionsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ namespace MongoFramework.Attributes
[AttributeUsage(AttributeTargets.Property)]
public class BucketSetOptionsAttribute : DbSetOptionsAttribute
{
public int BucketSize { get; private set; }
public int BucketSize { get; }
public string EntityTimeProperty { get; }

public BucketSetOptionsAttribute(int bucketSize)
public BucketSetOptionsAttribute(int bucketSize, string entityTimeProperty)
{
BucketSize = bucketSize;
EntityTimeProperty = entityTimeProperty;
}

public override IDbSetOptions GetOptions()
{
return new BucketSetOptions
{
BucketSize = BucketSize
BucketSize = BucketSize,
EntityTimeProperty = EntityTimeProperty
};
}
}
Expand Down
1 change: 1 addition & 0 deletions src/MongoFramework/BucketSetOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ namespace MongoFramework
public class BucketSetOptions : IDbSetOptions
{
public int BucketSize { get; set; }
public string EntityTimeProperty { get; set; }
}
}
6 changes: 4 additions & 2 deletions src/MongoFramework/EntityBucket.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace MongoFramework
{
public class EntityBucket<TGroup, TSubEntity> where TGroup : class
{
public string Id { get; set; }
public TGroup Group { get; set; }
public int Index { get; set; }
public DateTime Min { get; set; }
public DateTime Max { get; set; }
public int ItemCount { get; set; }
public int BucketSize { get; set; }
public List<TSubEntity> Items { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion src/MongoFramework/IMongoDbBucketSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

namespace MongoFramework
{
public interface IMongoDbBucketSet<TGroup, TSubEntity> : IMongoDbSet, IQueryable<EntityBucket<TGroup, TSubEntity>> where TGroup : class
public interface IMongoDbBucketSet<TGroup, TSubEntity> : IMongoDbSet, IQueryable<EntityBucket<TGroup, TSubEntity>>
where TGroup : class
where TSubEntity : class
{
void Add(TGroup group, TSubEntity entity);
void AddRange(TGroup group, IEnumerable<TSubEntity> entities);
void Remove(TGroup group);
IQueryable<TSubEntity> WithGroup(TGroup group);
IQueryable<TGroup> Groups();
}
Expand Down
51 changes: 51 additions & 0 deletions src/MongoFramework/Infrastructure/Commands/AddToBucketCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using MongoDB.Driver;
using MongoFramework.Infrastructure.Mapping;

namespace MongoFramework.Infrastructure.Commands
{
public class AddToBucketCommand<TGroup, TSubEntity> : IWriteCommand<EntityBucket<TGroup, TSubEntity>>
where TGroup : class
where TSubEntity : class
{
private TGroup Group { get; }
private TSubEntity SubEntity { get; }
private IEntityProperty EntityTimeProperty { get; }
private int BucketSize { get; }

public AddToBucketCommand(TGroup group, TSubEntity subEntity, IEntityProperty entityTimeProperty, int bucketSize)
{
Group = group;
SubEntity = subEntity;
BucketSize = bucketSize;
EntityTimeProperty = entityTimeProperty;
}

public IEnumerable<WriteModel<EntityBucket<TGroup, TSubEntity>>> GetModel()
{
var filterBuilder = Builders<EntityBucket<TGroup, TSubEntity>>.Filter;
var filter = filterBuilder.And(
filterBuilder.Eq(b => b.Group, Group),
filterBuilder.Where(b => b.ItemCount < BucketSize)
);

var entityDefinition = EntityMapping.GetOrCreateDefinition(typeof(EntityBucket<TGroup, TSubEntity>));

var itemTimeValue = (DateTime)EntityTimeProperty.GetValue(SubEntity);

var updateDefinition = Builders<EntityBucket<TGroup, TSubEntity>>.Update
.Inc(b => b.ItemCount, 1)
.Push(b => b.Items, SubEntity)
.Min(b => b.Min, itemTimeValue)
.Max(b => b.Max, itemTimeValue)
.SetOnInsert(b => b.BucketSize, BucketSize)
.SetOnInsert(b => b.Id, entityDefinition.KeyGenerator.Generate());

yield return new UpdateOneModel<EntityBucket<TGroup, TSubEntity>>(filter, updateDefinition)
{
IsUpsert = true
};
}
}
}
26 changes: 26 additions & 0 deletions src/MongoFramework/Infrastructure/Commands/RemoveBucketCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;

namespace MongoFramework.Infrastructure.Commands
{
public class RemoveBucketCommand<TGroup, TSubEntity> : IWriteCommand<EntityBucket<TGroup, TSubEntity>>
where TGroup : class
where TSubEntity : class
{
private TGroup Group { get; }

public RemoveBucketCommand(TGroup group)
{
Group = group;
}

public IEnumerable<WriteModel<EntityBucket<TGroup, TSubEntity>>> GetModel()
{
var filterBuilder = Builders<EntityBucket<TGroup, TSubEntity>>.Filter;
var filter = filterBuilder.Eq(b => b.Group, Group);
yield return new DeleteManyModel<EntityBucket<TGroup, TSubEntity>>(filter);
}
}
}
120 changes: 0 additions & 120 deletions src/MongoFramework/Infrastructure/EntityBucketStagingCollection.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class EntityDefinition : IEntityDefinition
{
public Type EntityType { get; set; }
public string CollectionName { get; set; }
public IEntityKeyGenerator KeyGenerator { get; set; }
public IEnumerable<IEntityProperty> Properties { get; set; } = Enumerable.Empty<IEntityProperty>();
public IEnumerable<IEntityRelationship> Relationships { get; set; } = Enumerable.Empty<IEntityRelationship>();
public IEnumerable<IEntityIndex> Indexes { get; set; } = Enumerable.Empty<IEntityIndex>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;
using MongoDB.Bson;
using MongoFramework.Infrastructure.Internal;

namespace MongoFramework.Infrastructure.Mapping
Expand Down
22 changes: 22 additions & 0 deletions src/MongoFramework/Infrastructure/Mapping/EntityKeyGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Bson.Serialization;

namespace MongoFramework.Infrastructure.Mapping
{
internal class EntityKeyGenerator : IEntityKeyGenerator
{
private IIdGenerator IdGenerator { get; }

public EntityKeyGenerator(IIdGenerator idGenerator)
{
IdGenerator = idGenerator;
}

public object Generate()
{
return IdGenerator.GenerateId(null, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface IEntityDefinition
{
Type EntityType { get; set; }
string CollectionName { get; set; }
IEntityKeyGenerator KeyGenerator { get; set; }
IEnumerable<IEntityProperty> Properties { get; set; }
IEnumerable<IEntityRelationship> Relationships { get; set; }
IEnumerable<IEntityIndex> Indexes { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MongoFramework.Infrastructure.Mapping
{
public interface IEntityKeyGenerator
{
object Generate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap)
classMap.MapIdMember(idProperty.PropertyInfo);
entityProperty.IsKey = true;

//If there is no Id generator, set a default based on the member type
if (classMap.IdMemberMap.IdGenerator == null)
//Set an Id Generator based on the member type
var idMemberMap = classMap.IdMemberMap;
var memberType = BsonClassMap.GetMemberInfoType(idMemberMap.MemberInfo);
if (memberType == typeof(string))
{
var idMemberMap = classMap.IdMemberMap;
var memberType = BsonClassMap.GetMemberInfoType(idMemberMap.MemberInfo);
if (memberType == typeof(string))
{
idMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
}
else if (memberType == typeof(Guid))
{
idMemberMap.SetIdGenerator(CombGuidGenerator.Instance);
}
else if (memberType == typeof(ObjectId))
{
idMemberMap.SetIdGenerator(ObjectIdGenerator.Instance);
}
idMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
definition.KeyGenerator = new EntityKeyGenerator(StringObjectIdGenerator.Instance);
}
else if (memberType == typeof(Guid))
{
idMemberMap.SetIdGenerator(CombGuidGenerator.Instance);
definition.KeyGenerator = new EntityKeyGenerator(CombGuidGenerator.Instance);
}
else if (memberType == typeof(ObjectId))
{
idMemberMap.SetIdGenerator(ObjectIdGenerator.Instance);
definition.KeyGenerator = new EntityKeyGenerator(ObjectIdGenerator.Instance);
}
}
}
Expand Down
Loading