public static Sector FromName(string settingName, string sectorName) { // TODO: Having this method supports deserializing data that refers generically to // sectors by name. // * Consider decoupling sector *names* from sector data // * Consider Location (the offender) having a default setting if (settingName == null) { settingName = SectorMap.DefaultSetting; } if (settingName != SectorMap.DefaultSetting) { throw new ArgumentException("Only OTU setting is currently supported"); } if (s_OTU == null) { throw new MapNotInitializedException(); } return(s_OTU.FromName(sectorName)); }
public Sector FromName(string name) => map.FromName(name, milieu);
public Sector FromName(string name) { return(map.FromName(name, milieu)); }
public static void PopulateDatabase(ResourceManager resourceManager, StatusCallback callback) { // Lock on this class rather than the cacheResults. Tile requests are not // blocked but we don't index twice. lock (typeof(SearchEngine)) { // NOTE: This (re)initializes a static data structure used for // resolving names into sector locations, so needs to be run // before any other objects (e.g. Worlds) are loaded. SectorMap map = SectorMap.FromName(SectorMap.DefaultSetting, resourceManager); using (var connection = DBUtil.MakeConnection()) { SqlCommand sqlCommand; // // Repopulate the tables - locally first // FUTURE: will need to batch this up rather than keep it in memory! // DataTable dt_sectors = new DataTable(); for (int i = 0; i < 3; ++i) { dt_sectors.Columns.Add(new DataColumn()); } DataTable dt_subsectors = new DataTable(); for (int i = 0; i < 4; ++i) { dt_subsectors.Columns.Add(new DataColumn()); } DataTable dt_worlds = new DataTable(); for (int i = 0; i < 6; ++i) { dt_worlds.Columns.Add(new DataColumn()); } callback("Parsing data..."); foreach (Sector sector in map.Sectors) { if (!sector.Tags.Contains("OTU")) { continue; } foreach (Name name in sector.Names) { DataRow row = dt_sectors.NewRow(); row.ItemArray = new object[] { sector.X, sector.Y, name.Text }; dt_sectors.Rows.Add(row); } foreach (Subsector subsector in sector.Subsectors) { DataRow row = dt_subsectors.NewRow(); row.ItemArray = new object[] { sector.X, sector.Y, subsector.Index, subsector.Name }; dt_subsectors.Rows.Add(row); } #if DEBUG if (!sector.Selected) { continue; } #endif // NOTE: May need to page this at some point WorldCollection worlds = sector.GetWorlds(resourceManager, cacheResults: false); if (worlds == null) { continue; } foreach (World world in worlds) { DataRow row = dt_worlds.NewRow(); row.ItemArray = new object[] { sector.X, sector.Y, world.X, world.Y, world.Name != null && world.Name.Length > 0 ? (object)world.Name : (object)DBNull.Value, world.UWP }; dt_worlds.Rows.Add(row); } } // // Rebuild the tables with fresh schema // string[] rebuild_schema = { "IF EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'sectors') AND type = (N'U')) DROP TABLE sectors", "CREATE TABLE sectors(x int NOT NULL,y int NOT NULL,name nvarchar(50) NULL)", "CREATE NONCLUSTERED INDEX sector_name ON sectors ( name ASC ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)", "IF EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'subsectors') AND type = (N'U')) DROP TABLE subsectors", "CREATE TABLE subsectors (sector_x int NOT NULL, sector_y int NOT NULL, subsector_index char(1) NOT NULL, name nvarchar(50) NULL )", "CREATE NONCLUSTERED INDEX subsector_name ON subsectors ( name ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)", "IF EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'worlds') AND type = (N'U')) DROP TABLE worlds", "CREATE TABLE worlds( sector_x int NOT NULL, sector_y int NOT NULL, hex_x int NOT NULL, hex_y int NOT NULL, name nvarchar(50) NULL, uwp nchar(9) NULL )", "CREATE NONCLUSTERED INDEX world_name ON worlds ( name ASC ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)", "CREATE NONCLUSTERED INDEX world_uwp ON worlds ( uwp ASC ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)", }; callback("Rebuilding schema..."); foreach (string cmd in rebuild_schema) { sqlCommand = new SqlCommand(cmd, connection); sqlCommand.ExecuteNonQuery(); } // // And shovel the data into the database en masse // using (var bulk = new SqlBulkCopy(connection, SqlBulkCopyOptions.TableLock, null)) { callback(String.Format("Writing {0} sectors...", dt_sectors.Rows.Count)); bulk.BatchSize = dt_sectors.Rows.Count; bulk.DestinationTableName = "sectors"; bulk.WriteToServer(dt_sectors); bulk.Close(); } using (var bulk = new SqlBulkCopy(connection, SqlBulkCopyOptions.TableLock, null)) { callback(String.Format("Writing {0} subsectors...", dt_subsectors.Rows.Count)); bulk.BatchSize = dt_subsectors.Rows.Count; bulk.DestinationTableName = "subsectors"; bulk.WriteToServer(dt_subsectors); bulk.Close(); } using (var bulk = new SqlBulkCopy(connection, SqlBulkCopyOptions.TableLock, null)) { callback(String.Format("Writing {0} worlds...", dt_worlds.Rows.Count)); bulk.BatchSize = 4096; bulk.DestinationTableName = "worlds"; bulk.WriteToServer(dt_worlds); bulk.Close(); } } callback("Complete!"); } }