public void TestSetSchemaInfoWithSpecialChars() { ShardMapManagerFactory.CreateSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerCreateMode.ReplaceExisting); ShardMapManager shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); SchemaInfoCollection siCollection = shardMapManager.GetSchemaInfoCollection(); SchemaInfo si = new SchemaInfo(); ShardedTableInfo sti = new ShardedTableInfo(NewNameWithSpecialChars(), NewNameWithSpecialChars()); si.Add(sti); string mdName = String.Format("TestSI_{0}", Guid.NewGuid()); siCollection.Add(mdName, si); SchemaInfo sdmdRead = siCollection.Get(mdName); AssertEqual(si, sdmdRead); }
/// <summary> /// Creates a new Range Shard Map with the specified name, or gets the Range Shard Map if it already exists. /// </summary> private static RangeShardMap <int> CreateOrGetRangeShardMap(ShardMapManager shardMapManager, string shardMapName) { // Try to get a reference to the Shard Map. RangeShardMap <int> shardMap; bool shardMapExists = shardMapManager.TryGetRangeShardMap(shardMapName, out shardMap); if (!shardMapExists) { shardMap = shardMapManager.CreateRangeShardMap <int>(shardMapName); SchemaInfo schemaInfo = new SchemaInfo(); schemaInfo.Add(new ShardedTableInfo("Votes", "Captain")); shardMapManager.GetSchemaInfoCollection().Add(shardMapName, schemaInfo); for (int i = 0; i < 4; i++) { int low = i * 25; int high = low + 24; CreateShard(shardMap, new Range <int>(low, high)); } } return(shardMap); }
private static void CreateSchemaInfo(ShardMapManager smm, string shardMapName) { SchemaInfo schemaInfo = new SchemaInfo(); schemaInfo.Add(new ShardedTableInfo("Companies", "TenantId")); schemaInfo.Add(new ShardedTableInfo("Professionals", "TenantId")); schemaInfo.Add(new ShardedTableInfo("Worksdone", "TenantId")); smm.GetSchemaInfoCollection().Add(shardMapName, schemaInfo); }
/// <summary> /// Creates schema info for the schema defined in InitializeShard.sql. /// </summary> private static void CreateSchemaInfo(string shardMapName) { // Create schema info SchemaInfo schemaInfo = new SchemaInfo(); schemaInfo.Add(new ReferenceTableInfo("Regions")); schemaInfo.Add(new ReferenceTableInfo("Products")); schemaInfo.Add(new ShardedTableInfo("Customers", "CustomerId")); schemaInfo.Add(new ShardedTableInfo("Orders", "CustomerId")); // Register it with the shard map manager for the given shard map name s_shardMapManager.GetSchemaInfoCollection().Add(shardMapName, schemaInfo); }
public void TestUpdateSchemaInfo() { ShardMapManagerFactory.CreateSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerCreateMode.ReplaceExisting); ShardMapManager shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); SchemaInfoCollection siCollection = shardMapManager.GetSchemaInfoCollection(); SchemaInfo si = new SchemaInfo(); ShardedTableInfo sti = new ShardedTableInfo("dbo", "ShardedTableName1", "ColumnName"); si.Add(sti); ReferenceTableInfo rtmd = new ReferenceTableInfo("ReferenceTableName1"); si.Add(rtmd); string mdName = String.Format("TestSI_{0}", Guid.NewGuid()); // Try updating schema info without adding it first. SchemaInfoException siex = AssertExtensions.AssertThrows <SchemaInfoException>( () => siCollection.Replace(mdName, si)); Assert.AreEqual(SchemaInfoErrorCode.SchemaInfoNameDoesNotExist, siex.ErrorCode); siCollection.Add(mdName, si); SchemaInfo sdmdNew = new SchemaInfo(); sdmdNew.Add(new ShardedTableInfo("dbo", "NewShardedTableName1", "NewColumnName")); sdmdNew.Add(new ReferenceTableInfo("NewReferenceTableName1")); siCollection.Replace(mdName, sdmdNew); SchemaInfo sdmdRead = siCollection.Get(mdName); Assert.IsTrue(AreEqual <ShardedTableInfo>(sdmdNew.ShardedTables, sdmdRead.ShardedTables)); Assert.IsTrue(AreEqual <ReferenceTableInfo>(sdmdNew.ReferenceTables, sdmdRead.ReferenceTables)); }
public void TestAddAndLookupAndDeleteSchemaInfo() { ShardMapManagerFactory.CreateSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerCreateMode.ReplaceExisting); ShardMapManager shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); #region TestAddAndLookup SchemaInfoCollection siCollection = shardMapManager.GetSchemaInfoCollection(); SchemaInfo si = new SchemaInfo(); ShardedTableInfo stmd1 = new ShardedTableInfo("ShardedTableName1", "ColumnName"); ShardedTableInfo stmd2 = new ShardedTableInfo("dbo", "ShardedTableName2", "ColumnName"); si.Add(stmd1); si.Add(stmd2); Assert.AreEqual(2, si.ShardedTables.Count); ReferenceTableInfo rtmd1 = new ReferenceTableInfo("ReferenceTableName1"); ReferenceTableInfo rtmd2 = new ReferenceTableInfo("dbo", "ReferenceTableName2"); si.Add(rtmd1); si.Add(rtmd2); Assert.AreEqual(2, si.ReferenceTables.Count); // Add an existing sharded table again. Make sure it doesn't create duplicate entries. SchemaInfoException siex = AssertExtensions.AssertThrows <SchemaInfoException>( () => si.Add(new ShardedTableInfo("ShardedTableName1", "ColumnName"))); Assert.AreEqual(SchemaInfoErrorCode.TableInfoAlreadyPresent, siex.ErrorCode); // Add an existing sharded table with a different key column name. This should fail too. siex = AssertExtensions.AssertThrows <SchemaInfoException>( () => si.Add(new ShardedTableInfo("ShardedTableName1", "ColumnName_Different"))); Assert.AreEqual(SchemaInfoErrorCode.TableInfoAlreadyPresent, siex.ErrorCode); siex = AssertExtensions.AssertThrows <SchemaInfoException>( () => si.Add(new ShardedTableInfo("dbo", "ShardedTableName2", "ColumnName_Different"))); Assert.AreEqual(SchemaInfoErrorCode.TableInfoAlreadyPresent, siex.ErrorCode); Assert.AreEqual(2, si.ShardedTables.Count); // Add an existing reference tables again. Make sure it doesn't create duplicate entries. siex = AssertExtensions.AssertThrows <SchemaInfoException>( () => si.Add(new ReferenceTableInfo("dbo", "ReferenceTableName2"))); Assert.AreEqual(SchemaInfoErrorCode.TableInfoAlreadyPresent, siex.ErrorCode); Assert.AreEqual(2, si.ReferenceTables.Count); // Now trying adding a reference table as a sharded table and vice versa. Both operations should fail. siex = AssertExtensions.AssertThrows <SchemaInfoException>( () => si.Add(new ShardedTableInfo("ReferenceTableName1", "ColumnName"))); Assert.AreEqual(SchemaInfoErrorCode.TableInfoAlreadyPresent, siex.ErrorCode); Assert.AreEqual(2, si.ShardedTables.Count); siex = AssertExtensions.AssertThrows <SchemaInfoException>( () => si.Add(new ReferenceTableInfo("dbo", "ShardedTableName2"))); Assert.AreEqual(SchemaInfoErrorCode.TableInfoAlreadyPresent, siex.ErrorCode); Assert.AreEqual(2, si.ReferenceTables.Count); // Try removing an existing table info and adding it back. si.Remove(stmd1); si.Add(stmd1); Assert.AreEqual(2, si.ShardedTables.Count); si.Remove(rtmd2); si.Add(rtmd2); Assert.AreEqual(2, si.ReferenceTables.Count); // Test with NULL inputs. ArgumentException arex = AssertExtensions.AssertThrows <ArgumentException>( () => si.Add((ShardedTableInfo)null)); arex = AssertExtensions.AssertThrows <ArgumentException>( () => si.Add((ReferenceTableInfo)null)); string mdName = String.Format("TestSI_{0}", Guid.NewGuid()); siCollection.Add(mdName, si); SchemaInfo sdmdRead = siCollection.Get(mdName); AssertEqual(si, sdmdRead); // Trying to add schema info with the same name again will result in a 'name conflict' exception. siex = AssertExtensions.AssertThrows <SchemaInfoException>( () => siCollection.Add(mdName, si)); Assert.AreEqual(SchemaInfoErrorCode.SchemaInfoNameConflict, siex.ErrorCode); #endregion #region TestLookup // Try looking up schema info with a non-existent name. siex = AssertExtensions.AssertThrows <SchemaInfoException>( () => siCollection.Get(mdName + "Fail")); Assert.AreEqual(SchemaInfoErrorCode.SchemaInfoNameDoesNotExist, siex.ErrorCode); #endregion #region TestDelete // Try removing any of the recently created schema info. siCollection.Remove(mdName); // Lookup should fail on removed data. siex = AssertExtensions.AssertThrows <SchemaInfoException>( () => siCollection.Get(mdName)); Assert.AreEqual(SchemaInfoErrorCode.SchemaInfoNameDoesNotExist, siex.ErrorCode); #endregion }
public void TestGetAll() { ShardMapManagerFactory.CreateSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerCreateMode.ReplaceExisting); ShardMapManager shardMapManager = ShardMapManagerFactory.GetSqlShardMapManager( Globals.ShardMapManagerConnectionString, ShardMapManagerLoadPolicy.Lazy); SchemaInfoCollection siCollection = shardMapManager.GetSchemaInfoCollection(); SchemaInfo[] si = new SchemaInfo[3] { new SchemaInfo(), new SchemaInfo(), new SchemaInfo() }; si[0].Add(new ShardedTableInfo("ShardedTableName1", "ColumnName1")); si[0].Add(new ShardedTableInfo("dbo", "ShardedTableName2", "ColumnName1")); si[0].Add(new ReferenceTableInfo("ReferenceTableName1")); si[0].Add(new ReferenceTableInfo("dbo", "ReferenceTableName2")); si[1].Add(new ShardedTableInfo("ShardedTableName3", "ColumnName2")); si[1].Add(new ShardedTableInfo("dbo", "ShardedTableName4", "ColumnName2")); si[1].Add(new ReferenceTableInfo("ReferenceTableName3")); si[2].Add(new ShardedTableInfo("dbo", "ShardedTableName3", "ColumnName2")); si[2].Add(new ReferenceTableInfo("ReferenceTableName4")); si[2].Add(new ReferenceTableInfo("dbo", "ReferenceTableName5")); string[] siNames = new string[3] { String.Format("TestSI_{0}", Guid.NewGuid()), String.Format("TestSI_{0}", Guid.NewGuid()), String.Format("TestSI_{0}", Guid.NewGuid()) }; siCollection.Add(siNames[0], si[0]); siCollection.Add(siNames[1], si[1]); siCollection.Add(siNames[2], si[2]); int i = 0; bool success = true; foreach (KeyValuePair <string, SchemaInfo> kvp in siCollection) { SchemaInfo sdmdOriginal; try { sdmdOriginal = si[Array.IndexOf(siNames, kvp.Key)]; } catch { success = false; break; } AssertEqual(sdmdOriginal, kvp.Value); i++; } Assert.IsTrue(success); Assert.AreEqual(3, i); }