Esempio n. 1
0
        public void TestForNormal()
        {
            var result      = PutRows();
            var primaryKeys = result.Item1;
            var columns     = result.Item2;

            var batchGetRequest = new BatchGetRowRequest();

            batchGetRequest.Add(TestTableName, primaryKeys);

            var response = OTSClient.BatchGetRow(batchGetRequest);
            var tables   = response.RowDataGroupByTable;

            Assert.AreEqual(1, tables.Count);

            var rows = tables[TestTableName];

            Assert.AreEqual(batchCount, rows.Count);

            for (int i = 0; i < batchCount; i++)
            {
                AssertPrimaryKey(primaryKeys[i], rows[i].Row.GetPrimaryKey());
                AssertAttribute(columns[i], (rows[i].Row as Row).GetColumns());
            }

            DeleteTable(TestTableName);
        }
Esempio n. 2
0
        public void TestForError()
        {
            var schema = new PrimaryKeySchema
            {
                { "pk1", ColumnValueType.String }
            };

            CreateTestTable(TestTableName, schema, new CapacityUnit(0, 0));


            var request = new BatchGetRowRequest();
            List <PrimaryKey> primaryKeys = new List <PrimaryKey>();
            var pk1 = new PrimaryKey
            {
                { "pk1", new ColumnValue("1") },
                { "pk2", new ColumnValue("1") }
            };

            var pk2 = new PrimaryKey
            {
                { "pk1", new ColumnValue("2") },
                { "pk2", new ColumnValue("2") }
            };

            var pk3 = new PrimaryKey
            {
                { "pk1", new ColumnValue("3") },
                { "pk2", new ColumnValue("3") }
            };

            primaryKeys.Add(pk1);
            primaryKeys.Add(pk2);
            primaryKeys.Add(pk3);

            request.Add(TestTableName, primaryKeys);

            var response = OTSClient.BatchGetRow(request);

            var tables = response.RowDataGroupByTable;

            Assert.AreEqual(1, tables.Count);

            var rows = tables[TestTableName];

            Assert.AreEqual(3, rows.Count);

            Assert.AreEqual("OTSInvalidPK", rows[0].ErrorCode);
            Assert.AreEqual("OTSInvalidPK", rows[1].ErrorCode);
            Assert.AreEqual("OTSInvalidPK", rows[2].ErrorCode);

            Assert.AreEqual("Validate PK size fail. Input: 2, Meta: 1.", rows[0].ErrorMessage);
            Assert.AreEqual("Validate PK size fail. Input: 2, Meta: 1.", rows[1].ErrorMessage);
            Assert.AreEqual("Validate PK size fail. Input: 2, Meta: 1.", rows[2].ErrorMessage);

            DeleteTable(TestTableName);
        }
        public static void BatchGetRowWithFilter()
        {
            Console.WriteLine("Start batch get row with filter ...");
            PrepareTable();
            PrepareData();
            OTSClient otsClient = Config.GetClient();

            // 批量一次读10行
            BatchGetRowRequest request     = new BatchGetRowRequest();
            List <PrimaryKey>  primaryKeys = new List <PrimaryKey>();

            for (int i = 0; i < 10; i++)
            {
                PrimaryKey primaryKey = new PrimaryKey
                {
                    { "pk0", new ColumnValue(i) },
                    { "pk1", new ColumnValue("abc") }
                };
                primaryKeys.Add(primaryKey);
            }

            var condition = new RelationalCondition("col2",
                                                    CompareOperator.EQUAL,
                                                    new ColumnValue(false));

            request.Add(TableName, primaryKeys, null, condition);

            BatchGetRowResponse response = otsClient.BatchGetRow(request);
            var tableRows = response.RowDataGroupByTable;
            var rows      = tableRows[TableName];

            foreach (var row in rows)
            {
                // 注意:batch操作可能部分成功部分失败,需要为每行检查状态
                if (row.IsOK)
                {
                    Console.WriteLine("-----------------");
                    foreach (KeyValuePair <string, ColumnValue> entry in row.PrimaryKey)
                    {
                        Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
                    }
                    foreach (KeyValuePair <string, ColumnValue> entry in row.Attribute)
                    {
                        Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
                    }
                    Console.WriteLine("-----------------");
                }
                else
                {
                    Console.WriteLine("Read row with filter failed: " + row.ErrorMessage);
                }
            }

            Console.WriteLine("RowsCount with filter");
        }
        public static void BatchGetRow()
        {
            Console.WriteLine("Start batch get row...");
            PrepareTable();
            PrepareData();
            OTSClient otsClient = Config.GetClient();

            // 批量一次读10行
            BatchGetRowRequest request     = new BatchGetRowRequest();
            List <PrimaryKey>  primaryKeys = new List <PrimaryKey>();

            for (int i = 0; i < 10; i++)
            {
                PrimaryKey primaryKey = new PrimaryKey();
                primaryKey.Add("pk0", new ColumnValue(i));
                primaryKey.Add("pk1", new ColumnValue("abc"));
                primaryKeys.Add(primaryKey);
            }

            request.Add(TableName, primaryKeys);

            BatchGetRowResponse response = otsClient.BatchGetRow(request);
            var tableRows = response.RowDataGroupByTable;
            var rows      = tableRows[TableName];

            foreach (var row in rows)
            {
                // 注意:batch操作可能部分成功部分失败,需要为每行检查状态
                if (row.IsOK)
                {
                    Console.WriteLine("-----------------");
                    foreach (KeyValuePair <string, ColumnValue> entry in row.PrimaryKey)
                    {
                        Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
                    }
                    foreach (KeyValuePair <string, ColumnValue> entry in row.Attribute)
                    {
                        Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
                    }
                    Console.WriteLine("-----------------");
                }
                else
                {
                    Console.WriteLine("Read row failed: " + row.ErrorMessage);
                }
            }

            Console.WriteLine("RowsCount: " + rows.Count);
        }
        public void TestForError()
        {
            var schema = new PrimaryKeySchema();

            schema.Add("pk1", ColumnValueType.String);

            CreateTestTable(TestTableName, schema, new CapacityUnit(0, 0));

            {
                var request = new BatchGetRowRequest();
                List <PrimaryKey> primaryKeys = new List <PrimaryKey>();
                var pk1 = new PrimaryKey();
                pk1.Add("pk1", new ColumnValue("1"));
                pk1.Add("pk2", new ColumnValue("1"));

                var pk2 = new PrimaryKey();
                pk2.Add("pk1", new ColumnValue("2"));
                pk2.Add("pk2", new ColumnValue("2"));

                var pk3 = new PrimaryKey();
                pk3.Add("pk1", new ColumnValue("3"));
                pk3.Add("pk2", new ColumnValue("3"));

                primaryKeys.Add(pk1);
                primaryKeys.Add(pk2);
                primaryKeys.Add(pk3);

                request.Add(TestTableName, primaryKeys);

                var response = OTSClient.BatchGetRow(request);

                var tables = response.RowDataGroupByTable;
                Assert.AreEqual(1, tables.Count);

                var rows = tables[TestTableName];

                Assert.AreEqual(3, rows.Count);

                Assert.AreEqual("OTSInvalidPK", rows[0].ErrorCode);
                Assert.AreEqual("OTSInvalidPK", rows[1].ErrorCode);
                Assert.AreEqual("OTSInvalidPK", rows[2].ErrorCode);

                Assert.AreEqual("Primary key schema mismatch.", rows[0].ErrorMessage);
                Assert.AreEqual("Primary key schema mismatch.", rows[1].ErrorMessage);
                Assert.AreEqual("Primary key schema mismatch.", rows[2].ErrorMessage);
            }
        }
        public void TestAPIWithParameter(string apiName)
        {
            var tableName                  = TestContext.tableName;
            var pkSchema                   = TestContext.pkSchema;
            var reservedThroughput         = TestContext.reservedThroughput;
            var primaryKey                 = TestContext.primaryKey;
            var attribute                  = TestContext.attribute;
            var condition                  = TestContext.condition;
            var startPrimaryKey            = TestContext.startPrimaryKey;
            var endPrimaryKey              = TestContext.endPrimaryKey;
            var putRowConsumed             = TestContext.putRowConsumed;
            var getRowConsumed             = TestContext.getRowConsumed;
            var updateRowConsumed          = TestContext.updateRowConsumed;
            var deleteRowConsumed          = TestContext.deleteRowConsumed;
            var getRangeConsumed           = TestContext.getRangeConsumed;
            var updateOfAttributeForPut    = TestContext.updateOfAttributeForPut;
            var updateOfAttributeForDelete = TestContext.updateOfAttributeForDelete;
            var columnsToGet               = TestContext.columnsToGet;
            var limit     = TestContext.limit;
            var direction = TestContext.direction;

            var tableMeta = new TableMeta(tableName, pkSchema);

            switch (apiName)
            {
            case "CreateTable":
                var request0 = new CreateTableRequest(tableMeta, reservedThroughput);
                OTSClient.CreateTable(request0);
                return;

            case "ListTable":
                var request1  = new ListTableRequest();
                var response1 = OTSClient.ListTable(request1);
                Assert.AreEqual(new List <string>()
                {
                    tableName
                }, response1.TableNames);
                return;

            case "UpdateTable":
                var request2  = new UpdateTableRequest(tableName, reservedThroughput);
                var response2 = OTSClient.UpdateTable(request2);

                if (reservedThroughput.Read.HasValue && reservedThroughput.Write.HasValue)
                {
                    AssertCapacityUnit(
                        reservedThroughput,
                        response2.ReservedThroughputDetails.CapacityUnit);
                }
                Assert.IsTrue(response2.ReservedThroughputDetails.LastDecreaseTime >= 0);
                Assert.IsTrue(response2.ReservedThroughputDetails.LastIncreaseTime >= 0);
                Assert.IsTrue(response2.ReservedThroughputDetails.NumberOfDecreasesToday >= 0);
                return;

            case "DeleteTable":
                var request3 = new DeleteTableRequest(tableName);
                OTSClient.DeleteTable(request3);

                var request31  = new ListTableRequest();
                var response31 = OTSClient.ListTable(request31);
                Assert.AreEqual(new List <string>()
                {
                }, response31.TableNames);
                return;

            case "DescribeTable":
                var request4  = new DescribeTableRequest(tableName);
                var response4 = OTSClient.DescribeTable(request4);
                Assert.AreEqual(tableName, response4.TableMeta.TableName);
                AssertPrimaryKeySchema(pkSchema, response4.TableMeta.PrimaryKeySchema);
                AssertCapacityUnit(reservedThroughput, response4.ReservedThroughputDetails.CapacityUnit);
                Assert.IsTrue(response4.ReservedThroughputDetails.LastDecreaseTime >= 0);
                Assert.IsTrue(response4.ReservedThroughputDetails.LastIncreaseTime >= 0);
                Assert.IsTrue(response4.ReservedThroughputDetails.NumberOfDecreasesToday >= 0);
                return;

            case "PutRow":
                var request5  = new PutRowRequest(tableName, condition, primaryKey, attribute);
                var response5 = OTSClient.PutRow(request5);
                AssertCapacityUnit(putRowConsumed, response5.ConsumedCapacityUnit);
                return;

            case "GetRow":
                var request6  = new GetRowRequest(tableName, primaryKey, columnsToGet);
                var response6 = OTSClient.GetRow(request6);
                AssertPrimaryKey(primaryKey, response6.PrimaryKey, columnsToGet);
                AssertAttribute(attribute, response6.Attribute, columnsToGet);
                AssertCapacityUnit(getRowConsumed, response6.ConsumedCapacityUnit);
                return;

            case "DeleteRow":
                var request7  = new DeleteRowRequest(tableName, condition, primaryKey);
                var response7 = OTSClient.DeleteRow(request7);
                AssertCapacityUnit(deleteRowConsumed, response7.ConsumedCapacityUnit);

                var request71  = new GetRowRequest(tableName, primaryKey);
                var response71 = OTSClient.GetRow(request71);
                AssertPrimaryKey(new PrimaryKey(), response71.PrimaryKey);
                AssertAttribute(new AttributeColumns(), response71.Attribute);
                return;

            case "UpdateRow_Put":
                var request8  = new UpdateRowRequest(tableName, condition, primaryKey, updateOfAttributeForPut);
                var response8 = OTSClient.UpdateRow(request8);
                AssertCapacityUnit(updateRowConsumed, response8.ConsumedCapacityUnit);

                var request81  = new GetRowRequest(tableName, primaryKey);
                var response81 = OTSClient.GetRow(request81);
                AssertPrimaryKey(primaryKey, response81.PrimaryKey);
                AssertAttribute(attribute, response81.Attribute);
                AssertCapacityUnit(getRowConsumed, response81.ConsumedCapacityUnit);

                return;

            case "UpdateRow_Delete":
                var request9  = new UpdateRowRequest(tableName, condition, primaryKey, updateOfAttributeForDelete);
                var response9 = OTSClient.UpdateRow(request9);
                AssertCapacityUnit(deleteRowConsumed, response9.ConsumedCapacityUnit);

                var request91  = new GetRowRequest(tableName, primaryKey);
                var response91 = OTSClient.GetRow(request91);
                // Don't assert primary key
                AssertAttribute(new AttributeColumns(), response91.Attribute);
                return;

            case "BatchGetRow":
                var request11 = new BatchGetRowRequest();
                request11.Add(tableName, new List <PrimaryKey>()
                {
                    primaryKey
                }, columnsToGet);
                var response11 = OTSClient.BatchGetRow(request11);
                Assert.AreEqual(1, response11.RowDataGroupByTable.Count);
                Assert.IsTrue(response11.RowDataGroupByTable.ContainsKey(tableName));
                Assert.AreEqual(1, response11.RowDataGroupByTable[tableName].Count);

                if (!response11.RowDataGroupByTable[tableName][0].IsOK)
                {
                    throw new OTSServerException(apiName, HttpStatusCode.OK,
                                                 response11.RowDataGroupByTable[tableName][0].ErrorCode,
                                                 response11.RowDataGroupByTable[tableName][0].ErrorMessage);
                }
                AssertPrimaryKey(primaryKey, response11.RowDataGroupByTable[tableName][0].PrimaryKey);
                AssertAttribute(attribute, response11.RowDataGroupByTable[tableName][0].Attribute);
                AssertCapacityUnit(getRowConsumed, response11.RowDataGroupByTable[tableName][0].Consumed);
                return;

            case "BatchWriteRow_Put":
                var request12  = new BatchWriteRowRequest();
                var rowChanges = new RowChanges();
                rowChanges.AddPut(condition, primaryKey, attribute);
                request12.Add(tableName, rowChanges);
                var response12 = OTSClient.BatchWriteRow(request12);
                Assert.AreEqual(1, response12.TableRespones.Count);
                Assert.IsTrue(response12.TableRespones.ContainsKey(tableName));
                Assert.AreEqual(1, response12.TableRespones[tableName].PutResponses.Count);
                Assert.AreEqual(0, response12.TableRespones[tableName].UpdateResponses.Count);
                Assert.AreEqual(0, response12.TableRespones[tableName].DeleteResponses.Count);
                if (response12.TableRespones[tableName].PutResponses[0].IsOK)
                {
                    AssertCapacityUnit(putRowConsumed,
                                       response12.TableRespones[tableName].PutResponses[0].Consumed);
                }
                else
                {
                    throw new OTSServerException("/BatchWriteRow", HttpStatusCode.OK,
                                                 response12.TableRespones[tableName].PutResponses[0].ErrorCode,
                                                 response12.TableRespones[tableName].PutResponses[0].ErrorMessage);
                }


                var request121  = new GetRowRequest(tableName, primaryKey);
                var response121 = OTSClient.GetRow(request121);
                AssertPrimaryKey(primaryKey, response121.PrimaryKey);
                AssertAttribute(attribute, response121.Attribute);
                AssertCapacityUnit(getRowConsumed, response121.ConsumedCapacityUnit);
                return;

            case "BatchWriteRow_Update":
                var request13   = new BatchWriteRowRequest();
                var rowChanges2 = new RowChanges();
                rowChanges2.AddUpdate(condition, primaryKey, updateOfAttributeForPut);
                request13.Add(tableName, rowChanges2);
                var response13 = OTSClient.BatchWriteRow(request13);
                Assert.AreEqual(1, response13.TableRespones.Count);
                Assert.IsTrue(response13.TableRespones.ContainsKey(tableName));
                Assert.AreEqual(0, response13.TableRespones[tableName].PutResponses.Count);
                Assert.AreEqual(1, response13.TableRespones[tableName].UpdateResponses.Count);
                Assert.AreEqual(0, response13.TableRespones[tableName].DeleteResponses.Count);
                if (response13.TableRespones[tableName].UpdateResponses[0].IsOK)
                {
                    AssertCapacityUnit(updateRowConsumed,
                                       response13.TableRespones[tableName].UpdateResponses[0].Consumed);
                }
                else
                {
                    throw new OTSServerException("/BatchWriteRow", HttpStatusCode.OK,
                                                 response13.TableRespones[tableName].UpdateResponses[0].ErrorCode,
                                                 response13.TableRespones[tableName].UpdateResponses[0].ErrorMessage);
                }

                var request131  = new GetRowRequest(tableName, primaryKey);
                var response131 = OTSClient.GetRow(request131);
                AssertPrimaryKey(primaryKey, response131.PrimaryKey);
                AssertAttribute(attribute, response131.Attribute);
                AssertCapacityUnit(getRowConsumed, response131.ConsumedCapacityUnit);
                return;

            case "BatchWriteRow_Delete":
                var request14   = new BatchWriteRowRequest();
                var rowChanges3 = new RowChanges();
                rowChanges3.AddDelete(condition, primaryKey);
                request14.Add(tableName, rowChanges3);
                var response14 = OTSClient.BatchWriteRow(request14);
                Assert.AreEqual(1, response14.TableRespones.Count);
                Assert.IsTrue(response14.TableRespones.ContainsKey(tableName));
                Assert.AreEqual(0, response14.TableRespones[tableName].PutResponses.Count);
                Assert.AreEqual(0, response14.TableRespones[tableName].UpdateResponses.Count);
                Assert.AreEqual(1, response14.TableRespones[tableName].DeleteResponses.Count);

                if (response14.TableRespones[tableName].DeleteResponses[0].IsOK)
                {
                    AssertCapacityUnit(deleteRowConsumed,
                                       response14.TableRespones[tableName].DeleteResponses[0].Consumed);
                }
                else
                {
                    throw new OTSServerException("/BatchWriteRow", HttpStatusCode.OK,
                                                 response14.TableRespones[tableName].DeleteResponses[0].ErrorCode,
                                                 response14.TableRespones[tableName].DeleteResponses[0].ErrorMessage);
                }
                var request141  = new GetRowRequest(tableName, primaryKey);
                var response141 = OTSClient.GetRow(request141);
                AssertPrimaryKey(new PrimaryKey(), response141.PrimaryKey);
                AssertAttribute(new AttributeColumns(), response141.Attribute);
                return;

            case "GetRange":
                var request15 = new GetRangeRequest(tableName, direction,
                                                    startPrimaryKey, endPrimaryKey,
                                                    columnsToGet, limit);
                var response15 = OTSClient.GetRange(request15);
                Assert.AreEqual(1, response15.RowDataList.Count);
                Assert.AreEqual(null, response15.NextPrimaryKey);
                AssertCapacityUnit(getRangeConsumed, response15.ConsumedCapacityUnit);
                AssertPrimaryKey(primaryKey, response15.RowDataList[0].PrimaryKey, columnsToGet);
                AssertAttribute(attribute, response15.RowDataList[0].Attribute, columnsToGet);
                return;

            default:
                throw new Exception(String.Format("invalid api name: {0}", apiName));
            }
        }
        public void TestForNormal()
        {
            var schema = new PrimaryKeySchema();

            schema.Add("pk1", ColumnValueType.String);

            CreateTestTable(TestTableName, schema, new CapacityUnit(0, 0));

            var attr1 = new AttributeColumns();

            attr1.Add("attr", new ColumnValue("attr_value1"));
            var attr2 = new AttributeColumns();

            attr2.Add("attr", new ColumnValue("attr_value2"));
            var attr3 = new AttributeColumns();

            attr3.Add("attr", new ColumnValue("attr_value3"));

            {
                List <PrimaryKey> primaryKeys = new List <PrimaryKey>();
                var pk1 = new PrimaryKey();
                pk1.Add("pk1", new ColumnValue("1"));

                var pk2 = new PrimaryKey();
                pk2.Add("pk1", new ColumnValue("2"));

                var pk3 = new PrimaryKey();
                pk3.Add("pk1", new ColumnValue("3"));

                primaryKeys.Add(pk1);
                primaryKeys.Add(pk2);
                primaryKeys.Add(pk3);

                {
                    var request = new BatchWriteRowRequest();
                    var change  = new RowChanges();
                    change.AddPut(new Condition(RowExistenceExpectation.IGNORE), pk1, attr1);
                    change.AddPut(new Condition(RowExistenceExpectation.IGNORE), pk2, attr2);
                    change.AddPut(new Condition(RowExistenceExpectation.IGNORE), pk3, attr3);

                    request.Add(TestTableName, change);

                    var response = OTSClient.BatchWriteRow(request);
                }

                {
                    var request = new BatchGetRowRequest();
                    request.Add(TestTableName, primaryKeys);

                    var response = OTSClient.BatchGetRow(request);
                    var tables   = response.RowDataGroupByTable;
                    Assert.AreEqual(1, tables.Count);

                    var rows = tables[TestTableName];

                    Assert.AreEqual(3, rows.Count);

                    AssertPrimaryKey(pk1, rows[0].PrimaryKey);
                    AssertPrimaryKey(pk2, rows[1].PrimaryKey);
                    AssertPrimaryKey(pk3, rows[2].PrimaryKey);

                    AssertAttribute(attr1, rows[0].Attribute);
                    AssertAttribute(attr2, rows[1].Attribute);
                    AssertAttribute(attr3, rows[2].Attribute);
                }
            }
        }