Skip to content

Commit

Permalink
1.增加对继承的支持
Browse files Browse the repository at this point in the history
2.增加继承的单元测试
  • Loading branch information
lin committed Nov 10, 2022
1 parent bb9d236 commit 940ba25
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 284 deletions.
3 changes: 2 additions & 1 deletion MongoIncUpdate.Base/IncUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public static async Task<UpdateResult> IncUpdate<T, TK>(this IMongoCollection<T>
//更新
var setter = Builders<T>.Update.Combine(defs);
// Console.WriteLine($"update data count:{defs.Count}");
return await collection.UpdateOneAsync(filter, setter, UpdateOptions);
var result = await collection.UpdateOneAsync(filter, setter, UpdateOptions);
return result;
}
catch (Exception) //增量更新失败则退化为全量替换(逻辑上没上用,增量异常了全量替换也大概率一场--仅仅是为了保证逻辑完整--)
{
Expand Down
4 changes: 1 addition & 3 deletions MongoIncUpdate.Fody.Example/Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class NestOldItem
public Dictionary<int, OldItem> OItems { get; set; } //1
}

[MongoIncUpdate]
public class DirtyItem
{
public int Int { get; set; } //0
Expand All @@ -35,7 +34,6 @@ public class DirtyItem
public string? Str { get; set; } //3
}

[MongoIncUpdate]
public class DirtyNestItem
{
[BsonId] public int Id { get; set; }
Expand All @@ -48,7 +46,7 @@ public class DirtyNestItem

[MemoryDiagnoser]
[RankColumn]
public class IncUpdateBenchmark
public partial class IncUpdateBenchmark
{
private const int _stateMapCount = 1000;
private static DirtyNestItem _benchmarkIncUpdateData;
Expand Down
4 changes: 2 additions & 2 deletions MongoIncUpdate.Fody.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace MongoIncUpdate.Fody.Example;

public class Program
{
public static void Main(string[] args)
public static async Task Main(string[] args)
{
Benchmark(args);
// Benchmark(args);
// Test(args);
}

Expand Down
6 changes: 3 additions & 3 deletions MongoIncUpdate.Fody.Test/IncUpdateDirtyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace MongoIncUpdate.Fody.Test;

[MongoIncUpdate]
public class DirtyItem
public sealed class DirtyItem
{
public int Int { get; set; } //0
public float Flo { get; set; } //1
Expand All @@ -18,7 +18,7 @@ public class DirtyItem
}

[MongoIncUpdate]
public class DirtyNestItem
public sealed class DirtyNestItem
{
[BsonId] public int Id { get; set; }

Expand Down Expand Up @@ -56,7 +56,7 @@ private void DirtyTest()
StateMap = new StateMap<int, DirtyItem>
{ { 2, new DirtyItem { Int = 2, Str = "2", Flo = 2.0f, Dou = 2.0 } } }
};
var b = (IDiffUpdateable)a;
var b = (IDiffUpdateable)((object)a);
Assert.NotNull(b);

_output.WriteLine("全量写入结果检查");
Expand Down
188 changes: 188 additions & 0 deletions MongoIncUpdate.Fody.Test/IncUpdateInheritTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using MongoIncUpdate.Base;
using Xunit;

namespace MongoIncUpdate.Fody.Test;

public interface IItemDataBase
{
// 道具id
int ItemId { get; set; }

// 道具数量
int GetCount();

// 设置道具数量
void SetCount(int count)
{
}
}

public interface IIncrItemData : IItemDataBase
{
// 自增id
int IncrId { get; set; }
}

public abstract class BaseIncrSingleItemData : IIncrItemData
{
public int ItemId { get; set; }

public int GetCount()
{
return 1;
}

public int IncrId { get; set; }
}

[MongoIncUpdate]
public sealed class WeaponItemData : BaseIncrSingleItemData
{
[BsonId] public int PlayerId { get; set; }

// 等级
public int Level { get; set; }

// 经验
public int LevelExp { get; set; }

// 突破等级
public int Breach { get; set; }

// 绑定角色id
public int RoleId { get; set; }

// 共鸣等级
public int ResonLevel { get; set; }
}

[MongoIncUpdate]
public sealed class EquipItemData : BaseIncrSingleItemData
{
[BsonId] public int PlayerId { get; set; }

// 等级
public int Level { get; set; }

// 经验
public int LevelExp { get; set; }

// 突破等级
public int Breach { get; set; }

// 绑定角色id
public int RoleId { get; set; }

// 共鸣等级
public int ResonLevel { get; set; }
}

public abstract class NoUsedBaseIncrSingleItemData : IIncrItemData
{
public int ItemId { get; set; }
public int GetCount() => 1;

public int IncrId { get; set; }
}

public class IncUpdateInheritTest
{
private static bool _init;
private static IMongoCollection<WeaponItemData> _cc;
private static IMongoCollection<EquipItemData> _cc2;

public IncUpdateInheritTest()
{
lock (this)
{
if (_init) return;
_init = true;
var connectionString = "mongodb://admin:123456@127.0.0.1:27017/test?authSource=admin";
var mongoClient = new MongoClient(connectionString);
var db = mongoClient.GetDatabase(new MongoUrlBuilder(connectionString).DatabaseName);
_cc = db.GetCollection<WeaponItemData>(nameof(WeaponItemData));
_cc2 = db.GetCollection<EquipItemData>(nameof(EquipItemData));
}
}

[Fact]
public async Task TestItem()
{
{
var a = new WeaponItemData
{
PlayerId = 1, ItemId = 1, IncrId = 2, Level = 3, LevelExp = 4, Breach = 5, RoleId = 6, ResonLevel = 7
};
await _cc.IncUpdate(a);
a.ItemId = 11;
a.IncrId = 12;
a.Level = 13;
a.LevelExp = 14;
a.Breach = 15;
a.RoleId = 16;
a.ResonLevel = 17;
await _cc.IncUpdate(a);
var filer = Builders<WeaponItemData>.Filter.Eq(x => x.PlayerId, a.PlayerId);
var v = (await _cc.FindAsync(filer)).First();
Assert.Equal(v.ItemId, a.ItemId);
Assert.Equal(v.IncrId, a.IncrId);
Assert.Equal(v.Level, a.Level);
Assert.Equal(v.LevelExp, a.LevelExp);
Assert.Equal(v.Breach, a.Breach);
Assert.Equal(v.RoleId, a.RoleId);
Assert.Equal(v.ResonLevel, a.ResonLevel);
}

{
var a = new WeaponItemData
{
PlayerId = 2, ItemId = 1, IncrId = 2, Level = 3, LevelExp = 4, Breach = 5, RoleId = 6, ResonLevel = 7
};
await _cc.IncUpdate(a);
a.ItemId = 111;
a.IncrId = 121;
a.Level = 131;
a.LevelExp = 141;
a.Breach = 151;
a.RoleId = 161;
a.ResonLevel = 171;
await _cc.IncUpdate(a);
var filer = Builders<WeaponItemData>.Filter.Eq(x => x.PlayerId, a.PlayerId);
var v = (await _cc.FindAsync(filer)).First();
Assert.Equal(v.ItemId, a.ItemId);
Assert.Equal(v.IncrId, a.IncrId);
Assert.Equal(v.Level, a.Level);
Assert.Equal(v.LevelExp, a.LevelExp);
Assert.Equal(v.Breach, a.Breach);
Assert.Equal(v.RoleId, a.RoleId);
Assert.Equal(v.ResonLevel, a.ResonLevel);
}

{
var a = new EquipItemData
{
PlayerId = 1, ItemId = 1, IncrId = 2, Level = 3, LevelExp = 4, Breach = 5, RoleId = 6, ResonLevel = 7
};
await _cc2.IncUpdate(a);
a.ItemId = 111;
a.IncrId = 112;
a.Level = 113;
a.LevelExp = 114;
a.Breach = 115;
a.RoleId = 116;
a.ResonLevel = 117;
await _cc2.IncUpdate(a);
var filer = Builders<EquipItemData>.Filter.Eq(x => x.PlayerId, a.PlayerId);
var v = (await _cc2.FindAsync(filer)).First();
Assert.Equal(v.ItemId, a.ItemId);
Assert.Equal(v.IncrId, a.IncrId);
Assert.Equal(v.Level, a.Level);
Assert.Equal(v.LevelExp, a.LevelExp);
Assert.Equal(v.Breach, a.Breach);
Assert.Equal(v.RoleId, a.RoleId);
Assert.Equal(v.ResonLevel, a.ResonLevel);
}
}
}
18 changes: 9 additions & 9 deletions MongoIncUpdate.Fody.Test/IncUpdateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
namespace MongoIncUpdate.Fody.Test;

[MongoIncUpdate]
public class ItemInt
public sealed class ItemInt
{
[BsonId] public int Id { get; set; }
public int I { get; set; }
}

[MongoIncUpdate]
public class ItemNestInt
public sealed class ItemNestInt
{
[BsonId] public int Id { get; set; }
public ItemInt ItemInt { get; set; }
}

[MongoIncUpdate]
public class ItemNestNestInt
public sealed class ItemNestNestInt
{
[BsonId] public int Id { get; set; }
public ItemNestInt ItemNestInt { get; set; }
}

[MongoIncUpdate]
public class ItemIntKeyStateMapInt
public sealed class ItemIntKeyStateMapInt
{
[BsonId] public int Id { get; set; }

Expand All @@ -39,7 +39,7 @@ public class ItemIntKeyStateMapInt
}

[MongoIncUpdate]
public class ItemStringKeyStateMapString
public sealed class ItemStringKeyStateMapString
{
[BsonId] public int Id { get; set; }

Expand All @@ -48,7 +48,7 @@ public class ItemStringKeyStateMapString
}

[MongoIncUpdate]
public class NestStateMapItem
public sealed class NestStateMapItem
{
[BsonId] public int Id { get; set; }

Expand All @@ -57,20 +57,20 @@ public class NestStateMapItem
}

[MongoIncUpdate]
public class NestStateMapStateMapItem
public sealed class NestStateMapStateMapItem
{
[BsonId] public int Id { get; set; }
public StateMap<int, StateMap<int, ItemInt>> StateMapStateMapItem { get; set; } = new();
}

[MongoIncUpdate]
public class ItemNestItemStateMap
public sealed class ItemNestItemStateMap
{
public ItemIntKeyStateMapInt NestItem { get; set; }
}

[MongoIncUpdate]
public class ItemStateMapItemStateMapItem
public sealed class ItemStateMapItemStateMapItem
{
[BsonId] public int Id { get; set; }
public StateMap<int, ItemNestItemStateMap> Item { get; set; } = new();
Expand Down
29 changes: 0 additions & 29 deletions MongoIncUpdate.Fody/CallMapper.cs

This file was deleted.

Loading

0 comments on commit 940ba25

Please sign in to comment.