예제 #1
0
        public void AlterIndexedTable()
        {
            DropTables(this.regex);

            Assert.IsFalse(Ns.TableExists("test-1"));

            const string SchemaA =
                "<Schema><AccessGroup name=\"default\">" +
                "<ColumnFamily><Name>a</Name><Index>true</Index><QualifierIndex>true</QualifierIndex></ColumnFamily>" +
                "</AccessGroup>" + "</Schema>";

            Ns.CreateTable("test-1", SchemaA);
            Assert.IsTrue(Ns.TableExists("test-1"));
            Ns.CreateTable("test-1", SchemaA, CreateDispositions.CreateIfNotExist);

            if (IsHyper || IsThrift)
            {
                Assert.IsTrue(Ns.TableExists("^test-1"));
                Assert.IsTrue(Ns.TableExists("^^test-1"));
            }

            var _schemaA = Ns.GetTableSchema("test-1");

            using (var table = Ns.OpenTable("test-1")) {
                Assert.AreEqual(table.Name, Ns.Name + "/test-1");
                Assert.AreEqual(table.Schema, _schemaA);
            }

            const string SchemaB =
                "<Schema><AccessGroup name=\"default\">" +
                "<ColumnFamily><Name>b</Name></ColumnFamily>" +
                "</AccessGroup></Schema>";

            Ns.AlterTable("test-1", SchemaB);

            if (IsHyper || IsThrift)
            {
                Assert.IsFalse(Ns.TableExists("^test-1"));
                Assert.IsFalse(Ns.TableExists("^^test-1"));
            }

            var _schemaB = Ns.GetTableSchema("test-1");

            Assert.AreNotEqual(_schemaB, _schemaA);
            using (var table = Ns.OpenTable("test-1")) {
                Assert.AreEqual(table.Name, Ns.Name + "/test-1");
                Assert.AreEqual(table.Schema, _schemaB);
            }

            const string SchemaC =
                "<Schema><AccessGroup name=\"default\">" +
                "<ColumnFamily><Name>a</Name></ColumnFamily>" +
                "<ColumnFamily><Name>b</Name><Index>true</Index></ColumnFamily>" +
                "</AccessGroup>" + "</Schema>";

            Ns.AlterTable("test-1", SchemaC);

            if (IsHyper || IsThrift)
            {
                Assert.IsTrue(Ns.TableExists("^test-1"));
                Assert.IsFalse(Ns.TableExists("^^test-1"));
            }

            const string SchemaD =
                "<Schema><AccessGroup name=\"default\">" +
                "<ColumnFamily><Name>a</Name><Index>true</Index><QualifierIndex>true</QualifierIndex></ColumnFamily>" +
                "<ColumnFamily><Name>b</Name><Index>true</Index></ColumnFamily>" +
                "</AccessGroup>" + "</Schema>";

            Ns.AlterTable("test-1", SchemaD);

            if (IsHyper || IsThrift)
            {
                Assert.IsTrue(Ns.TableExists("^test-1"));
                Assert.IsTrue(Ns.TableExists("^^test-1"));
            }
        }
예제 #2
0
        public void AlterTable()
        {
            DropTables(this.regex);

            Assert.IsFalse(Ns.TableExists("test-1"));

            const string SchemaA =
                "<Schema><AccessGroup name=\"default\">" +
                "<ColumnFamily><Name>a</Name></ColumnFamily>" +
                "</AccessGroup></Schema>";

            const string SchemaB =
                "<Schema><AccessGroup name=\"default\">" +
                "<ColumnFamily><Name>b</Name></ColumnFamily>" +
                "</AccessGroup></Schema>";

            Ns.CreateTable("test-1", SchemaA);
            Assert.IsTrue(Ns.TableExists("test-1"));
            Ns.CreateTable("test-1", SchemaA, CreateDispositions.CreateIfNotExist);
            var _schemaA = Ns.GetTableSchema("test-1");

            using (var table = Ns.OpenTable("test-1")) {
                Assert.AreEqual(table.Name, Ns.Name + "/test-1");
                Assert.AreEqual(table.Schema, _schemaA);

                using (var mutator = table.CreateMutator())
                {
                    mutator.Set(new Key("R1", "a"), new byte[] { 1, 2, 3 });
                }
            }

            Ns.AlterTable("test-1", SchemaB);
            var _schemaB = Ns.GetTableSchema("test-1");

            Assert.AreNotEqual(_schemaB, _schemaA);
            using (var table = Ns.OpenTable("test-1")) {
                Assert.AreEqual(table.Name, Ns.Name + "/test-1");
                Assert.AreEqual(table.Schema, _schemaB);

                using (var mutator = table.CreateMutator())
                {
                    try
                    {
                        mutator.Set(new Key("R1", "a"), new byte[] { 1, 2, 3 });
                        Assert.Fail();
                    }
                    catch (BadKeyException)
                    {
                    }
                    catch
                    {
                        Assert.Fail();
                    }

                    mutator.Set(new Key("R2", "b"), new byte[] { 1, 2, 3 });
                }

                Ns.AlterTable("test-1", SchemaA);

                using (var mutator = table.CreateMutator())
                {
                    mutator.Set(new Key("R1", "a"), new byte[] { 1, 2, 3 });

                    try
                    {
                        mutator.Set(new Key("R2", "b"), new byte[] { 1, 2, 3 });
                    }
                    catch (BadKeyException)
                    {
                    }
                    catch
                    {
                        Assert.Fail();
                    }
                }

                var existingSchema = TableSchema.Parse(Ns.GetTableSchema("test-1", true));
                Assert.IsTrue(existingSchema.GenerationSpecified);
                Assert.IsTrue(existingSchema.AccessGroups.All(ag => ag.GenerationSpecified));
                Assert.IsTrue(existingSchema.AccessGroups.SelectMany(ag => ag.ColumnFamilies).All(cf => cf.GenerationSpecified));

                var newSchema = TableSchema.Parse(Ns.GetTableSchema("test-1"));
                newSchema.AccessGroups.First().ColumnFamilies.Insert(0, new ColumnFamily {
                    Name = "new"
                });
                Ns.AlterTable("test-1", newSchema.ToString());
                var alteredSchema = TableSchema.Parse(Ns.GetTableSchema("test-1", true));
                Assert.IsTrue(alteredSchema.GenerationSpecified);
                Assert.IsTrue(existingSchema.Generation < alteredSchema.Generation);
                Assert.IsTrue(existingSchema.AccessGroups.All(ag => ag.GenerationSpecified));
                Assert.IsTrue(existingSchema.AccessGroups.SelectMany(ag => ag.ColumnFamilies).All(cf => cf.GenerationSpecified));

                var d = alteredSchema.AccessGroups.SelectMany(ag => ag.ColumnFamilies).ToDictionary(cf => cf.Id);
                foreach (var cf in existingSchema.AccessGroups.SelectMany(ag => ag.ColumnFamilies))
                {
                    Assert.IsTrue(d.ContainsKey(cf.Id));
                    Assert.AreEqual(cf.Name, d[cf.Id].Name);
                }
            }

            try
            {
                Ns.AlterTable("test-11", SchemaB);
                Assert.Fail();
            }
            catch (TableNotFoundException)
            {
            }
            catch
            {
                Assert.Fail();
            }

            try
            {
                Ns.CreateNamespace("test-22");
                Ns.AlterTable("test-22", SchemaB);
                Assert.Fail();
            }
            catch (TableNotFoundException)
            {
            }
            catch
            {
                Assert.Fail();
            }
        }