Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <summary>
        /// 未來提供 InitializeShard.sql 來建立 SchemaInfo
        /// </summary>
        private static void CreateSchemaInfo(string shardMapName)
        {
            // 建立 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"));

            // 給 shard map manager 一個名子,並註冊
            s_shardMapManager.GetSchemaInfoCollection().Add(shardMapName, schemaInfo);
        }
 private void AssertEqual(SchemaInfo x, SchemaInfo y)
 {
     AssertExtensions.AssertSequenceEquivalent(x.ReferenceTables, y.ReferenceTables);
     AssertExtensions.AssertSequenceEquivalent(x.ShardedTables, y.ShardedTables);
 }
        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
        }
 private string ToXml(SchemaInfo schemaInfo)
 {
     using (StringWriter sw = new StringWriter())
     {
         using (XmlWriter xw = XmlWriter.Create(sw, new XmlWriterSettings() { Indent = true }))
         {
             DataContractSerializer serializer = new DataContractSerializer(typeof(SchemaInfo));
             serializer.WriteObject(xw, schemaInfo);
         }
         return sw.ToString();
     }
 }
        public void SerializeCompatibility()
        {
            SchemaInfo schemaInfo = new SchemaInfo();
            schemaInfo.Add(new ReferenceTableInfo("r1", "r2"));
            schemaInfo.Add(new ShardedTableInfo("s1", "s2", "s3"));

            // Why is this slightly different from the XML in the DeserializeCompatibility test?
            // Because this is the exact formatting that we expect DataContractSerializer will create.
            string expectedSerializedSchemaInfo = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">
  <ReferenceTableSet xmlns="""" i:type=""ArrayOfReferenceTableInfo"">
    <ReferenceTableInfo>
      <SchemaName>r1</SchemaName>
      <TableName>r2</TableName>
    </ReferenceTableInfo>
  </ReferenceTableSet>
  <ShardedTableSet xmlns="""" i:type=""ArrayOfShardedTableInfo"">
    <ShardedTableInfo>
      <SchemaName>s1</SchemaName>
      <TableName>s2</TableName>
      <KeyColumnName>s3</KeyColumnName>
    </ShardedTableInfo>
  </ShardedTableSet>
</Schema>";
            string actualSerializedSchemaInfo = ToXml(schemaInfo);
            Assert.AreEqual(
                expectedSerializedSchemaInfo,
                actualSerializedSchemaInfo);

            // Deserialize it back as a sanity check
            SchemaInfo finalSchemaInfo = FromXml(actualSerializedSchemaInfo);
            AssertEqual(schemaInfo, finalSchemaInfo);
        }
        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);
        }
        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);

            AssertEqual(sdmdNew, sdmdRead);
        }
        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);
        }