Example #1
0
        public void Build(IVocabulary vocabulary)
        {
            var saveQueue = new BlockingCollection <DatabaseChunkPart>();

            PerformAction(() =>
            {
                if (Settings.Current.Building.ChunksCount == 0)
                {
                    Settings.Current.Building.ChunksCount = _chunkController.CreateChunks();
                }

                vocabulary.Fill(false, false);

                Logger.Write(null, LogMessageTypes.Info,
                             $"==================== Conversion to CDM was started ====================");

                var save = Task.Run(() =>
                {
                    while (!saveQueue.IsCompleted)
                    {
                        DatabaseChunkPart data = null;
                        try
                        {
                            data = saveQueue.Take();
                        }
                        catch (InvalidOperationException)
                        {
                        }

                        if (data != null)
                        {
                            var timer = new Stopwatch();
                            timer.Start();
                            data.Save(Settings.Current.Building.Cdm,
                                      Settings.Current.Building.DestinationConnectionString,
                                      Settings.Current.Building.DestinationEngine,
                                      Settings.Current.Building.SourceSchema,
                                      Settings.Current.Building.CdmSchema);
                            Settings.Current.Building.CompletedChunkIds.Add(data.ChunkData.ChunkId);
                            timer.Stop();
                            Logger.Write(data.ChunkData.ChunkId, LogMessageTypes.Info,
                                         $"ChunkId={data.ChunkData.ChunkId} was saved - {timer.ElapsedMilliseconds} ms | {GC.GetTotalMemory(false) / 1024f / 1024f} Mb");
                        }

                        if (CurrentState != BuilderState.Running)
                        {
                            break;
                        }
                    }

                    CurrentState = BuilderState.Stopped;
                });


                Parallel.For(0, Settings.Current.Building.ChunksCount,
                             new ParallelOptions {
                    MaxDegreeOfParallelism = Settings.Current.DegreeOfParallelism
                }, (chunkId, state) =>
                {
                    if (CurrentState != BuilderState.Running)
                    {
                        state.Break();
                    }

                    if (!Settings.Current.Building.CompletedChunkIds.Contains(chunkId))
                    {
                        var chunk = new DatabaseChunkBuilder(chunkId, CreatePersonBuilder);

                        //using (var connection =
                        //    new OdbcConnection(Settings.Current.Building.SourceConnectionString))
                        {
                            //connection.Open();
                            saveQueue.Add(chunk.Process(Settings.Current.Building.SourceEngine,
                                                        Settings.Current.Building.SourceSchema,
                                                        Settings.Current.Building.SourceQueryDefinitions,
                                                        Settings.Current.Building.SourceConnectionString,
                                                        Settings.Current.Building.Vendor));
                        }

                        Settings.Current.Save(false);

                        while (saveQueue.Count > 0)
                        {
                            Thread.Sleep(1000);
                        }
                    }
                });

                saveQueue.CompleteAdding();

                save.Wait();
            });
        }
Example #2
0
        public void CreateLookup(IVocabulary vocabulary)
        {
            PerformAction(() =>
            {
                var timer = new Stopwatch();
                timer.Start();
                vocabulary.Fill(true, false);
                var locationConcepts = new List <Location>();
                var careSiteConcepts = new List <CareSite>();
                var providerConcepts = new List <Provider>();

                Console.WriteLine("Loading locations...");
                var location = Settings.Current.Building.SourceQueryDefinitions.FirstOrDefault(qd => qd.Locations != null);
                if (location != null)
                {
                    FillList <Location>(locationConcepts, location, location.Locations[0]);
                }

                if (locationConcepts.Count == 0)
                {
                    locationConcepts.Add(new Location {
                        Id = Entity.GetId(null)
                    });
                }
                Console.WriteLine("Locations was loaded");

                Console.WriteLine("Loading care sites...");
                var careSite = Settings.Current.Building.SourceQueryDefinitions.FirstOrDefault(qd => qd.CareSites != null);
                if (careSite != null)
                {
                    FillList <CareSite>(careSiteConcepts, careSite, careSite.CareSites[0]);
                }

                if (careSiteConcepts.Count == 0)
                {
                    careSiteConcepts.Add(new CareSite {
                        Id = 0, LocationId = 0, OrganizationId = 0, PlaceOfSvcSourceValue = null
                    });
                }
                Console.WriteLine("Care sites was loaded");

                Console.WriteLine("Loading providers...");
                var provider = Settings.Current.Building.SourceQueryDefinitions.FirstOrDefault(qd => qd.Providers != null);
                if (provider != null)
                {
                    FillList <Provider>(providerConcepts, provider, provider.Providers[0]);
                }
                Console.WriteLine("Providers was loaded");

                Console.WriteLine("Saving lookups...");
                var saver = Settings.Current.Building.DestinationEngine.GetSaver();
                using (saver.Create(Settings.Current.Building.DestinationConnectionString,
                                    Settings.Current.Building.Cdm,
                                    Settings.Current.Building.SourceSchema,
                                    Settings.Current.Building.CdmSchema))
                {
                    saver.SaveEntityLookup(Settings.Current.Building.Cdm, locationConcepts, careSiteConcepts, providerConcepts, null);
                }

                Console.WriteLine("Lookups was saved ");
                timer.Stop();
                Logger.Write(null, LogMessageTypes.Info,
                             $"Care site, Location and Provider tables were saved to CDM database - {timer.ElapsedMilliseconds} ms");

                locationConcepts.Clear();
                careSiteConcepts.Clear();
                providerConcepts.Clear();
                locationConcepts = null;
                careSiteConcepts = null;
                providerConcepts = null;
                GC.Collect();
            });
        }