public async Task CanLoadMultiplePartialState() { var runner = Substitute.For <IGameRunner>(); var fooSystem = new FooExportSystem(runner); runner.CreateEntityAsync().Returns(Task.FromResult(new SharperEntity())); var entity = await runner.CreateEntityAsync(); var fooComponent = new FooExportableComponent(entity); await fooSystem.RegisterComponentAsync(fooComponent); var provider = new LiteDbProvider(new List <ISharperSystem> { fooSystem }, runner); var id = await provider.SaveAsync(new List <BaseSharperComponent> { fooComponent }); Assert.DoesNotThrowAsync(async() => { await provider.LoadPartialStateAsync(id, new List <string>() { fooComponent.Entity.Id.ToString() }, true); }); }
public async Task ThrowsOnMissingSystem() { var runner = Substitute.For <IGameRunner>(); var barSystem = new BarExportSystem(runner); var fooSystem = new FooExportSystem(runner); runner.CreateEntityAsync().Returns(Task.FromResult(new SharperEntity())); var entity = await runner.CreateEntityAsync(); var fooComponent = new FooExportableComponent(entity); var barComponent = new BarExportableComponent(entity); await fooSystem.RegisterComponentAsync(fooComponent); await barSystem.RegisterComponentAsync(barComponent); var provider = new LiteDbProvider(new List <ISharperSystem> { fooSystem }, runner); var id = await provider.SaveAsync(new List <BaseSharperComponent>() { fooComponent, barComponent }); Assert.ThrowsAsync <InvalidSaveStateException>(() => provider.LoadAsync(id)); }
public async Task FlushHitActivity() { Log.Information("Flushing HitActivity buffer"); var metrics = LiteDbProvider.GetCollections <HitActivity>(); var dateFormat = "yyyy-MM-dd HH"; var dateFormatted = DateTime.Now.ToString(dateFormat); Log.Debug("Filter last hour: {DateFormatted}", dateFormatted); var filteredMetrics = metrics.Find(x => x.Timestamp.ToString(dateFormat) == dateFormatted).ToList(); if (filteredMetrics.Count == 0) { Log.Debug("No HitActivity buffed need to flush"); return; } Log.Debug("Flushing {Count} of {CountAll} data..", filteredMetrics.Count, metrics.Count()); foreach (var hitActivity in filteredMetrics) { var data = new Dictionary <string, object>() { { "via_bot", hitActivity.ViaBot }, { "update_type", hitActivity.UpdateType }, { "from_id", hitActivity.FromId }, { "from_first_name", hitActivity.FromFirstName }, { "from_last_name", hitActivity.FromLastName }, { "from_username", hitActivity.FromUsername }, { "from_lang_code", hitActivity.FromLangCode }, { "chat_id", hitActivity.ChatId }, { "chat_username", hitActivity.ChatUsername }, { "chat_type", hitActivity.ChatType }, { "chat_title", hitActivity.ChatTitle }, { "timestamp", hitActivity.Timestamp } }; var insertHit = await _queryService .CreateMySqlFactory() .FromTable("hit_activity") .InsertAsync(data); Log.Information("Insert Hit: {InsertHit}", insertHit); } Log.Debug("Clearing local data.."); filteredMetrics.ForEach(x => { metrics.DeleteMany(y => y.Timestamp == x.Timestamp); }); LiteDbProvider.Rebuild(); Log.Information("Flush HitActivity done"); }
public async Task CanSavePartialState() { var runner = Substitute.For <IGameRunner>(); var fooSystem = new FooExportSystem(runner); runner.CreateEntityAsync().Returns(Task.FromResult(new SharperEntity())); var entity = await runner.CreateEntityAsync(); var fooComponent = new FooExportableComponent(entity); var barComponent = new BarExportableComponent(entity); var barSystem = new BarExportSystem(runner); await barSystem.RegisterComponentAsync(barComponent); await fooSystem.RegisterComponentAsync(fooComponent); var provider = new LiteDbProvider(new List <ISharperSystem> { fooSystem, barSystem }, runner); fooComponent.Foo = "foo"; var id = await provider.SaveAsync(new List <BaseSharperComponent> { fooComponent }); fooComponent.Foo = "bar"; Assert.DoesNotThrowAsync(async() => { await provider.ModifyAsync(id, new List <BaseSharperComponent> { fooComponent, barComponent }); }); Assert.AreEqual(fooSystem.GetComponents().First().Foo, "bar"); }
public async Task CanHandleMultipleComponentsOnSameEntity() { var runner = Substitute.For <IGameRunner>(); var barSystem = new BarExportSystem(runner); var fooSystem = new FooExportSystem(runner); runner.CreateEntityAsync().Returns(Task.FromResult(new SharperEntity())); var entity = await runner.CreateEntityAsync(); var fooComponentA = new FooExportableComponent(entity); var barComponentA = new BarExportableComponent(entity); var fooComponentB = new FooExportableComponent(entity); var barComponentB = new BarExportableComponent(entity); await fooSystem.RegisterComponentAsync(fooComponentA); await barSystem.RegisterComponentAsync(barComponentA); await fooSystem.RegisterComponentAsync(fooComponentB); await barSystem.RegisterComponentAsync(barComponentB); var provider = new LiteDbProvider(new List <ISharperSystem> { fooSystem, barSystem }, runner); var id = await provider.SaveAsync(new List <BaseSharperComponent>() { fooComponentA, barComponentA, fooComponentB, barComponentB }); await provider.LoadAsync(id); Assert.IsTrue(fooSystem.EntityHasComponent(fooComponentA, entity)); Assert.IsTrue(fooSystem.EntityHasComponent(fooComponentB, entity)); Assert.IsTrue(barSystem.EntityHasComponent(barComponentA, entity)); Assert.IsTrue(barSystem.EntityHasComponent(barComponentB, entity)); }
public async Task NoExcessMemoryUsage() { var runner = Substitute.For <IGameRunner>(); var barSystem = new BarExportSystem(runner); var fooSystem = new FooExportSystem(runner); runner.CreateEntityAsync().Returns(Task.FromResult(new SharperEntity())); var components = new List <BaseSharperComponent>(); for (var i = 0; i < 1000; i++) { var entity = await runner.CreateEntityAsync(); var fooComponent = new FooExportableComponent(entity); var barComponent = new BarExportableComponent(entity); await fooSystem.RegisterComponentAsync(fooComponent); await barSystem.RegisterComponentAsync(barComponent); } var provider = new LiteDbProvider(new List <ISharperSystem> { fooSystem, barSystem }, runner); GC.Collect(); var memory = GC.GetTotalMemory(false); for (var i = 0; i < 20; i++) { var id = await provider.SaveAsync(components); await provider.LoadAsync(id); } GC.Collect(); Assert.Less(GC.GetTotalMemory(false), memory + memory / 20); }
public static async Task SyncWordToLocalAsync(this QueryFactory factory) { var sw = Stopwatch.StartNew(); Log.Information("Starting Sync Words filter"); var wordFilters = (await factory.FromQuery(new Query("word_filter")) .GetAsync <WordFilter>()).ToList(); // var cloudQuery = (await new Query("word_filter") // .ExecForMysql() // .GetAsync() // ).ToList(); // var cloudWords = cloudQuery.ToJson().MapObject<List<WordFilter>>(); var collection = LiteDbProvider.GetCollections <WordFilter>(); collection.DeleteAll(); collection.Insert(wordFilters); // var jsonWords = "local-words".OpenJson(); // Log.Debug("Getting Words Collections"); // var wordCollection = jsonWords.GetCollection<WordFilter>(); // Log.Debug("Deleting old Words"); // await wordCollection.DeleteManyAsync(x => x.Word != null) // ; // Log.Debug("Inserting new Words"); // await wordCollection.InsertManyAsync(cloudWords) // ; Log.Information("Sync {0} Words complete in {1}", wordFilters.Count, sw.Elapsed); // jsonWords.Dispose(); // cloudQuery.Clear(); // cloudWords.Clear(); wordFilters.Clear(); sw.Stop(); }
public async Task CanSaveWithoutDuplicates() { var runner = Substitute.For <IGameRunner>(); var barSystem = new BarExportSystem(runner); var fooSystem = new FooExportSystem(runner); runner.CreateEntityAsync().Returns(Task.FromResult(new SharperEntity())); var entity = await runner.CreateEntityAsync(); var fooComponent = new FooExportableComponent(entity); var barComponent = new BarExportableComponent(entity); await fooSystem.RegisterComponentAsync(fooComponent); await barSystem.RegisterComponentAsync(barComponent); var provider = new LiteDbProvider(new List <ISharperSystem> { fooSystem, barSystem }, runner); var id = await provider.SaveAsync(new List <BaseSharperComponent> { fooComponent, barComponent }); await provider.LoadAsync(id); var barComponents = barSystem.GetComponents(); var fooComponents = fooSystem.GetComponents(); Assert.AreSame(fooComponents.First().Entity, barComponents.First().Entity); }
public CreateStorageCommandRequestHandler(LiteDbProvider provider) { _provider = provider; }
public static async Task SyncWordToLocalAsync() { var sw = Stopwatch.StartNew(); Log.Information("Starting Sync Words filter"); var cloudQuery = (await new Query("word_filter") .ExecForMysql() .GetAsync()).ToList(); var cloudWords = cloudQuery.ToJson().MapObject <List <WordFilter> >(); var collection = LiteDbProvider.GetCollections <WordFilter>(); collection.DeleteAll(); collection.Insert(cloudWords); // var jsonWords = "local-words".OpenJson(); // Log.Debug("Getting Words Collections"); // var wordCollection = jsonWords.GetCollection<WordFilter>(); // Log.Debug("Deleting old Words"); // await wordCollection.DeleteManyAsync(x => x.Word != null) // ; // Log.Debug("Inserting new Words"); // await wordCollection.InsertManyAsync(cloudWords) // ; Log.Information("Sync {0} Words complete in {1}", cloudWords.Count, sw.Elapsed); // jsonWords.Dispose(); cloudQuery.Clear(); cloudWords.Clear(); sw.Stop(); // var localQuery = (await new Query("word_filter") // .ExecForSqLite() // .GetAsync() // ).ToList(); // var localWords = localQuery.ToJson().MapObject<List<WordFilter>>(); // // // var diffWords = cloudWords // .Where(c => localWords.All(l => l.Word != c.Word)).ToList(); // Log.Debug($"DiffWords: {diffWords.Count} item(s)"); // // if (diffWords.Count == 0) // { // Log.Debug("Seem not need sync words to Local storage"); // return; // } // // Log.Information("Starting sync Words to Local"); // var clearData = await new Query("word_filter") // .ExecForSqLite(true) // .DeleteAsync() // ; // // Log.Information($"Deleting local Word Filter: {clearData} rows"); // // foreach (var row in cloudWords) // { // var data = new Dictionary<string, object>() // { // {"word", row.Word}, // {"is_global", row.IsGlobal}, // {"deep_filter", row.DeepFilter}, // {"from_id", row.FromId}, // {"chat_id", row.ChatId}, // {"created_at", row.CreatedAt} // }; // // var insert = await new Query("word_filter") // .ExecForSqLite() // .InsertAsync(data) // ; // } // // Log.Information($"Synced {cloudWords.Count} row(s)"); }
public RemoveDuplicatesRequestHandler(LiteDbProvider liteDbProvider) { _liteDbProvider = liteDbProvider; }
public AddFilmToStorageRequestHandler(LiteDbProvider provider) { _provider = provider; }
public ListFilmsWithQueryRequestHandler(LiteDbProvider provider) { _provider = provider; }
public CreateFilmHandler(LiteDbProvider provider) { _provider = provider; }
public ListFilmsQueryHandler(LiteDbProvider provider) { _provider = provider; }
public ListStorageRequestHandler(LiteDbProvider provider) { _provider = provider; }
public ListFilmWithNoMediaRequestHandler(LiteDbProvider provider) { _provider = provider; }
public AddMediaToFilmHandler(LiteDbProvider provider) { _provider = provider; }
public MediatorPipeline(LiteDbProvider provider) { _provider = provider; }