ZocMonLib.SetupSystemTables.ValidateAndCreateDataTables C# (CSharp) Method

ValidateAndCreateDataTables() public method

public ValidateAndCreateDataTables ( IDbConnection conn ) : void
conn IDbConnection
return void
        public void ValidateAndCreateDataTables(IDbConnection conn)
        {
            try
            {
                // This is in case multiple threads try to create new configurations at the same time.
                // The issue isn't concurrent modification of the dictionary, but duplicate attempts to
                // insert the same data in the DB.
                lock (_cache.MonitorConfigs)
                {
                    //get the known table names
                    var tablesKnown = new List<string>();
                    var tablesKnownComparison = new List<string>();
                    var tablesConfigResolution = new Dictionary<string, Tuple<string, long>>();

                    foreach (var monitorConfig in _cache.MonitorConfigs.Values)
                    {
                        // validate/create the tables for reduced data
                        foreach (var reduceLevel in monitorConfig.ReduceLevels)
                        {
                            var tableName = Support.MakeReducedName(monitorConfig.Name, reduceLevel.Resolution);

                            tablesKnown.Add(tableName);
                            if (monitorConfig.ComparisonCalculator != null)
                                tablesKnownComparison.Add(Support.MakeComparisonName(monitorConfig.Name, reduceLevel.Resolution));
                            tablesConfigResolution.Add(tableName, new Tuple<string, long>(monitorConfig.Name, reduceLevel.Resolution));
                        }
                    }

                    //get list of existing tables
                    var existingTables = _storageCommands.SelectListAllExistingTables(conn);

                    //only generate sql for the ones that we know about that don't already exist
                    var needCreatingTable = tablesKnown.Except(existingTables).ToList();
                    var needCreatingTableComparison = tablesKnownComparison.Except(existingTables).ToList();

                    _storageCommands.BuildTables(needCreatingTable, needCreatingTableComparison, tablesConfigResolution, conn);
                }
            }
            catch (Exception e)
            {
                _logger.Fatal("Exception swallowed: ", e);
                if (_settings.Debug)
                    throw;
            }
        }

Usage Example

            public void ShouldBeAbleToUse()
            {
                var connection = new Mock<IDbConnection>();
                var connectionInstance = connection.Object;

                var settings = BuildSettings();

                var monitorConfigs = new ConcurrentDictionary<string, MonitorConfig>();
                monitorConfigs.TryAdd("Test1", new MonitorConfig { Name = "Test1", ComparisonCalculator = new ProcessingInstructionAverage(), ReduceLevels = new List<ReduceLevel> { new ReduceLevel { Resolution = 1000 }, new ReduceLevel { Resolution = 60000 } } });
                monitorConfigs.TryAdd("Test2", new MonitorConfig { Name = "Test2", ComparisonCalculator = new ProcessingInstructionAverage(), ReduceLevels = new List<ReduceLevel> { new ReduceLevel { Resolution = 1000 }, new ReduceLevel { Resolution = 60000 } } });
                monitorConfigs.TryAdd("Test3", new MonitorConfig { Name = "Test3", ComparisonCalculator = new ProcessingInstructionAverage(), ReduceLevels = new List<ReduceLevel> { new ReduceLevel { Resolution = 1000 }, new ReduceLevel { Resolution = 60000 } } });
                monitorConfigs.TryAdd("Test4", new MonitorConfig { Name = "Test4", ComparisonCalculator = new ProcessingInstructionAverage(), ReduceLevels = new List<ReduceLevel> { new ReduceLevel { Resolution = 1000 }, new ReduceLevel { Resolution = 60000 } } });

                var cache = new Mock<IDataCache>();
                cache.SetupGet(x => x.MonitorConfigs).Returns(monitorConfigs).Verifiable();

                IEnumerable<string> needCreatingTable = null;
                IEnumerable<string> needCreatingTableComparison = null;
                Dictionary<string, Tuple<string, long>> tablesConfigResolution = null;

                var existingTables = new List<string> { "Test1SecondlyData", "Test1MinutelyData", "Test2SecondlyData", "Test2MinutelyData", "Test1SecondlyComparison", "Test1MinutelyComparison", "Test2SecondlyComparison", "Test2MinutelyComparison" };
                var storageCommands = new Mock<IStorageCommandsSetup>();
                storageCommands.Setup(x => x.SelectListAllExistingTables(connectionInstance)).Returns(existingTables);
                storageCommands.Setup(x => x.BuildTables(It.IsAny<IEnumerable<string>>(), It.IsAny<IEnumerable<string>>(), It.IsAny<Dictionary<string, Tuple<string, long>>>(), connectionInstance))
                    .Callback((IEnumerable<string> nCT, IEnumerable<string> nCTC, Dictionary<string, Tuple<string, long>> tCR, IDbConnection c) => { needCreatingTable = nCT; needCreatingTableComparison = nCTC; tablesConfigResolution = tCR; }).Verifiable();

                var setupSystemTables = new SetupSystemTables(cache.Object, storageCommands.Object, settings.Object);
                setupSystemTables.ValidateAndCreateDataTables(connectionInstance);

                Assert.Equal(4, needCreatingTable.Count());
                Assert.True(needCreatingTable.Contains("Test3SecondlyData"));
                Assert.True(needCreatingTable.Contains("Test3MinutelyData"));
                Assert.True(needCreatingTable.Contains("Test4SecondlyData"));
                Assert.Equal(4, needCreatingTableComparison.Count());
                Assert.True(needCreatingTableComparison.Contains("Test3SecondlyComparison"));
                Assert.True(needCreatingTableComparison.Contains("Test3MinutelyComparison"));
                Assert.True(needCreatingTableComparison.Contains("Test4SecondlyComparison"));
                Assert.Equal(8, tablesConfigResolution.Count());
                Assert.True(tablesConfigResolution.ContainsKey("Test3SecondlyData"));
                Assert.True(tablesConfigResolution.ContainsKey("Test3MinutelyData"));
                Assert.True(tablesConfigResolution.ContainsKey("Test4SecondlyData"));

                cache.VerifyAll();
                storageCommands.VerifyAll();
            }