private void Run()
        {
            for (int i = 0; i < round; ++i)
            {
                var primaryKey = new PrimaryKey();
                primaryKey.Add("PK0", new ColumnValue(pk));
                var                 request     = new GetRowRequest(tableName, primaryKey);
                var                 response    = OTSClient.GetRow(request);
                var                 attr        = response.Attribute["Col1"];
                long                oldIntValue = attr.IntegerValue;
                ColumnValue         oldValue    = new ColumnValue(oldIntValue);
                ColumnValue         newValue    = new ColumnValue(oldIntValue + 1);
                RelationalCondition cc          = new RelationalCondition("Col1", RelationalCondition.CompareOperator.EQUAL, oldValue);
                Condition           cond        = new Condition(RowExistenceExpectation.IGNORE);

                cond.ColumnCondition = cc;
                UpdateOfAttribute updateOfAttributeForPut = new UpdateOfAttribute();
                updateOfAttributeForPut.AddAttributeColumnToPut("Col1", newValue);
                UpdateRowRequest updateReq = new UpdateRowRequest(tableName, cond, primaryKey, updateOfAttributeForPut);
                bool             success   = true;
                try
                {
                    OTSClient.UpdateRow(updateReq);
                }
                catch (OTSServerException)
                {
                    success = false;
                }
                if (success)
                {
                    ++count;
                }
            }
        }
Ejemplo n.º 2
0
        public void TestDuplicateDeleteInUpdateRow()
        {
            var pk = new PrimaryKey();

            pk.Add("PK0", new ColumnValue("123"));
            pk.Add("PK1", new ColumnValue(123));


            var updateOfAttribute = new UpdateOfAttribute();

            updateOfAttribute.AddAttributeColumnToDelete("Col0");
            updateOfAttribute.AddAttributeColumnToDelete("Col0");

            var request = new UpdateRowRequest(
                TestTableName, new Condition(RowExistenceExpectation.IGNORE), pk, updateOfAttribute);

            try {
                var response = OTSClient.UpdateRow(request);
                Assert.Fail();
            } catch (OTSServerException e) {
                AssertOTSServerException(e, new OTSServerException(
                                             "/UpdateRow",
                                             HttpStatusCode.BadRequest,
                                             "OTSParameterInvalid",
                                             "Duplicated attribute column name: 'Col0' while updating row."
                                             ));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 更新行的时候,指定某一列为原子自增列,并对这一列进行原子自增
        /// </summary>
        public static void Increment(int incrementValue)
        {
            Console.WriteLine("Start set increment column...");
            OTSClient otsClient = Config.GetClient();

            // 定义行的主键,必须与创建表时的TableMeta中定义的一致
            PrimaryKey primaryKey = new PrimaryKey
            {
                { Pk1, new ColumnValue(0) },
                { Pk2, new ColumnValue("abc") }
            };
            RowUpdateChange rowUpdateChange = new RowUpdateChange(TableName, primaryKey);

            rowUpdateChange.ReturnType        = ReturnType.RT_AFTER_MODIFY;
            rowUpdateChange.ReturnColumnNames = new List <string>()
            {
                IncrementCol
            };
            //设置一个原子自增列,这一列从0开始自增,每次增增加1。
            rowUpdateChange.Increment(new Column(IncrementCol, new ColumnValue(incrementValue)));

            UpdateRowRequest updateRowRequest = new UpdateRowRequest(rowUpdateChange);

            var response = otsClient.UpdateRow(updateRowRequest);

            Console.WriteLine("set Increment column succeed,Increment result:" + response.Row.GetColumns()[0].Value);
        }
        public void Test1000PutAnd1000DeleteInUpdateRow()
        {
            var pk = new PrimaryKey
            {
                { "PK0", new ColumnValue("123") },
                { "PK1", new ColumnValue(123) }
            };


            var updateOfAttribute = new UpdateOfAttribute();

            for (int i = 0; i < 1000; i++)
            {
                updateOfAttribute.AddAttributeColumnToPut("Put" + i, new ColumnValue("blah"));
                updateOfAttribute.AddAttributeColumnToDelete("Delete" + i);
            }

            var request = new UpdateRowRequest(
                TestTableName, new Condition(RowExistenceExpectation.IGNORE), pk, updateOfAttribute);

            try {
                var response = OTSClient.UpdateRow(request);
            } catch (OTSServerException e) {
                AssertOTSServerException(e, new OTSServerException(
                                             "/UpdateRow",
                                             HttpStatusCode.BadRequest,
                                             "OTSParameterInvalid",
                                             "The number of attribute columns exceeds the limit, limit count: 1024, column count: 2000."
                                             ));
            }
        }
        public void TestEmptyUpdateRow()
        {
            var primaryKey = new PrimaryKey
            {
                { "PK0", new ColumnValue(0) }
            };
            var updateOfAttribute = new UpdateOfAttribute();

            var request = new UpdateRowRequest(TestTableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, updateOfAttribute);

            try
            {
                OTSClient.UpdateRow(request);
                Assert.Fail();
            }
            catch (OTSServerException e)
            {
                AssertOTSServerException(e, new OTSServerException(
                                             "/UpdateRow",
                                             HttpStatusCode.BadRequest,
                                             "OTSParameterInvalid",
                                             "Attribute column is missing."
                                             ));
            }
        }
Ejemplo n.º 6
0
        public void UpdateRowTest()
        {
            CreateTable();

            PutRow();

            var otsClient  = OTSClient;
            var primaryKey = new PrimaryKey
            {
                { "PK0", new ColumnValue("ABC") },
                { "PK1", new ColumnValue(123) }
            };

            var updateOfAttribute = new UpdateOfAttribute();

            updateOfAttribute.AddAttributeColumnToPut("NewColumn", new ColumnValue(123));
            updateOfAttribute.AddAttributeColumnToDelete("Col0");

            var updateRowRequest = new UpdateRowRequest(
                TestTableName,
                new Condition(RowExistenceExpectation.IGNORE),
                primaryKey,
                updateOfAttribute);
            var updateRowResponse = otsClient.UpdateRow(updateRowRequest);

            Console.WriteLine("UpdateRow CU Consumed: Read {0} Write {1}",
                              updateRowResponse.ConsumedCapacityUnit.Read,
                              updateRowResponse.ConsumedCapacityUnit.Write);
            DeleteTable();
        }
        public async stt::Task UpdateRowAsync()
        {
            moq::Mock <TablesService.TablesServiceClient> mockGrpcClient = new moq::Mock <TablesService.TablesServiceClient>(moq::MockBehavior.Strict);
            UpdateRowRequest request = new UpdateRowRequest
            {
                Row        = new Row(),
                UpdateMask = new wkt::FieldMask(),
            };
            Row expectedResponse = new Row
            {
                RowName = RowName.FromTableRow("[TABLE]", "[ROW]"),
                Values  =
                {
                    {
                        "key8a0b6e3c",
                        new wkt::Value()
                    },
                },
            };

            mockGrpcClient.Setup(x => x.UpdateRowAsync(request, moq::It.IsAny <grpccore::CallOptions>())).Returns(new grpccore::AsyncUnaryCall <Row>(stt::Task.FromResult(expectedResponse), null, null, null, null));
            TablesServiceClient client = new TablesServiceClientImpl(mockGrpcClient.Object, null);
            Row responseCallSettings   = await client.UpdateRowAsync(request.Row, request.UpdateMask, gaxgrpc::CallSettings.FromCancellationToken(st::CancellationToken.None));

            xunit::Assert.Same(expectedResponse, responseCallSettings);
            Row responseCancellationToken = await client.UpdateRowAsync(request.Row, request.UpdateMask, st::CancellationToken.None);

            xunit::Assert.Same(expectedResponse, responseCancellationToken);
            mockGrpcClient.VerifyAll();
        }
        public void UpdateRow()
        {
            moq::Mock <TablesService.TablesServiceClient> mockGrpcClient = new moq::Mock <TablesService.TablesServiceClient>(moq::MockBehavior.Strict);
            UpdateRowRequest request = new UpdateRowRequest
            {
                Row        = new Row(),
                UpdateMask = new wkt::FieldMask(),
            };
            Row expectedResponse = new Row
            {
                RowName = RowName.FromTableRow("[TABLE]", "[ROW]"),
                Values  =
                {
                    {
                        "key8a0b6e3c",
                        new wkt::Value()
                    },
                },
            };

            mockGrpcClient.Setup(x => x.UpdateRow(request, moq::It.IsAny <grpccore::CallOptions>())).Returns(expectedResponse);
            TablesServiceClient client = new TablesServiceClientImpl(mockGrpcClient.Object, null);
            Row response = client.UpdateRow(request.Row, request.UpdateMask);

            xunit::Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
Ejemplo n.º 9
0
        public bool UpdateRow(String tableName, Int64 pk, String colName, ColumnValue colValue, IColumnCondition cond)
        {
            bool success    = true;
            var  primaryKey = new PrimaryKey
            {
                { "PK0", new ColumnValue(pk) }
            };
            Condition rowCond = new Condition(RowExistenceExpectation.IGNORE)
            {
                ColumnCondition = cond
            };
            UpdateOfAttribute updateOfAttributeForPut = new UpdateOfAttribute();

            updateOfAttributeForPut.AddAttributeColumnToPut(colName, colValue);
            var request = new UpdateRowRequest(tableName, rowCond, primaryKey, updateOfAttributeForPut);

            try
            {
                OTSClient.UpdateRow(request);
            }
            catch (OTSServerException e)
            {
                Console.WriteLine("UpdateRow fail: {0}", e.ErrorMessage);
                success = false;
            }
            return(success);
        }
Ejemplo n.º 10
0
        public void GetTimeRangeRowTest()
        {
            CreateTable();
            PutRow();
            var otsClient  = OTSClient;
            var primaryKey = new PrimaryKey
            {
                { "PK0", new ColumnValue("ABC") },
                { "PK1", new ColumnValue(123) }
            };

            // update row
            var updateOfAttribute = new UpdateOfAttribute();

            updateOfAttribute.AddAttributeColumnToPut("Col0", new ColumnValue("Beijing"));

            var updateRowRequest = new UpdateRowRequest(
                TestTableName,
                new Condition(RowExistenceExpectation.IGNORE),
                primaryKey,
                updateOfAttribute);
            var updateRowResponse = otsClient.UpdateRow(updateRowRequest);

            var getRowRequest = new GetRowRequest(
                TestTableName,
                primaryKey
                );

            getRowRequest.QueryCriteria.TimeRange = new TimeRange
            {
                StartTime = 0,
                EndTime   = Int64.MaxValue
            };

            var getRowResponse = otsClient.GetRow(getRowRequest);

            Console.WriteLine("GetRow CU Consumed: Read {0} Write {1}",
                              getRowResponse.ConsumedCapacityUnit.Read,
                              getRowResponse.ConsumedCapacityUnit.Write);

            var pk0 = getRowResponse.PrimaryKey["PK0"];

            Console.WriteLine("PrimaryKey PK0 Value {0}", pk0.StringValue);
            var pk1 = getRowResponse.PrimaryKey["PK1"];

            Console.WriteLine("PrimaryKey PK1 Value {0}", pk1.IntegerValue);

            var row = getRowResponse.Row;

            foreach (Column column in row.GetColumn("Col0"))
            {
                Console.WriteLine(column);
            }

            Assert.AreEqual(2, row.GetColumn("Col0").Count);

            DeleteTable();
        }
        public void Test4PutAnd4DeleteInUpdateRow()
        {
            CreateTestTableWith2PK();

            var pk = new PrimaryKey
            {
                { "PK0", new ColumnValue("123") },
                { "PK1", new ColumnValue(123) }
            };

            var attribute = new AttributeColumns
            {
                { "Col0", new ColumnValue("0") },
                { "Col1", new ColumnValue("1") },
                { "Col2", new ColumnValue("2") },
                { "Col3", new ColumnValue("3") },
                { "Col4", new ColumnValue("4") }
            };

            PutSingleRow(TestTableName, pk, attribute);

            var updateOfAttribute = new UpdateOfAttribute();

            updateOfAttribute.AddAttributeColumnToPut("Col3", new ColumnValue("5"));
            updateOfAttribute.AddAttributeColumnToPut("Col4", new ColumnValue("6"));
            updateOfAttribute.AddAttributeColumnToPut("Col5", new ColumnValue("7"));
            updateOfAttribute.AddAttributeColumnToPut("Col6", new ColumnValue("8"));

            updateOfAttribute.AddAttributeColumnToDelete("Col1");
            updateOfAttribute.AddAttributeColumnToDelete("Col2");
            updateOfAttribute.AddAttributeColumnToDelete("Col7");
            updateOfAttribute.AddAttributeColumnToDelete("Col8");

            var request = new UpdateRowRequest(
                TestTableName, new Condition(RowExistenceExpectation.IGNORE), pk, updateOfAttribute);
            var response = OTSClient.UpdateRow(request);

            AssertCapacityUnit(new CapacityUnit(0, 1), response.ConsumedCapacityUnit);

            var expectAttribute = new AttributeColumns
            {
                { "Col0", new ColumnValue("0") },
                { "Col3", new ColumnValue("5") },
                { "Col4", new ColumnValue("6") },
                { "Col5", new ColumnValue("7") },
                { "Col6", new ColumnValue("8") }
            };

            CheckSingleRow(TestTableName, pk, expectAttribute);
        }
 /// <summary>Snippet for UpdateRow</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void UpdateRowRequestObject()
 {
     // Create client
     TablesServiceClient tablesServiceClient = TablesServiceClient.Create();
     // Initialize request argument(s)
     UpdateRowRequest request = new UpdateRowRequest
     {
         Row        = new Row(),
         UpdateMask = new FieldMask(),
         View       = View.Unspecified,
     };
     // Make the request
     Row response = tablesServiceClient.UpdateRow(request);
 }
Ejemplo n.º 13
0
        /// <summary>Snippet for UpdateRowAsync</summary>
        public async Task UpdateRowRequestObjectAsync()
        {
            // Snippet: UpdateRowAsync(UpdateRowRequest, CallSettings)
            // Additional: UpdateRowAsync(UpdateRowRequest, CancellationToken)
            // Create client
            TablesServiceClient tablesServiceClient = await TablesServiceClient.CreateAsync();

            // Initialize request argument(s)
            UpdateRowRequest request = new UpdateRowRequest
            {
                Row        = new Row(),
                UpdateMask = new FieldMask(),
                View       = View.Unspecified,
            };
            // Make the request
            Row response = await tablesServiceClient.UpdateRowAsync(request);

            // End snippet
        }
        private void UpdateRow(OTSClient client, String tableName, IColumnCondition cond)
        {
            var primaryKey = new PrimaryKey
            {
                { COLUMN_GID_NAME, new ColumnValue(1) },
                { COLUMN_UID_NAME, new ColumnValue(101) }
            };

            UpdateOfAttribute updateOfAttributeForPut = new UpdateOfAttribute();

            updateOfAttributeForPut.AddAttributeColumnToPut(COLUMN_NAME_NAME, new ColumnValue("张三"));
            updateOfAttributeForPut.AddAttributeColumnToPut(COLUMN_ADDRESS_NAME, new ColumnValue("中国B地"));
            updateOfAttributeForPut.AddAttributeColumnToDelete(COLUMN_MOBILE_NAME);
            updateOfAttributeForPut.AddAttributeColumnToDelete(COLUMN_AGE_NAME);

            Condition condition = new Condition(RowExistenceExpectation.IGNORE)
            {
                ColumnCondition = cond
            };

            var request = new UpdateRowRequest(tableName, condition, primaryKey, updateOfAttributeForPut);

            try
            {
                client.UpdateRow(request);
                Console.WriteLine("UpdateRow success");
            }
            catch (OTSServerException e)
            {
                //服务端异常
                Console.WriteLine("操作失败:{0}", e.ErrorMessage);
                Console.WriteLine("请求ID:{0}", e.RequestID);
            }
            catch (OTSClientException e)
            {
                //可能是网络不好或者返回结果有问题
                Console.WriteLine("请求失败:{0}", e.ErrorMessage);
            }
        }
Ejemplo n.º 15
0
        public static void UpdateRow()
        {
            Console.WriteLine("Start update row...");
            PrepareTable();
            OTSClient otsClient = Config.GetClient();

            // 定义行的主键,必须与创建表时的TableMeta中定义的一致
            PrimaryKey primaryKey = new PrimaryKey();

            primaryKey.Add("pk0", new ColumnValue(0));
            primaryKey.Add("pk1", new ColumnValue("abc"));

            // 定义要写入改行的属性列
            UpdateOfAttribute attribute = new UpdateOfAttribute();

            attribute.AddAttributeColumnToPut("col0", new ColumnValue(0));
            attribute.AddAttributeColumnToPut("col1", new ColumnValue("b")); // 将原先的值'a'改为'b'
            attribute.AddAttributeColumnToPut("col2", new ColumnValue(true));
            UpdateRowRequest request = new UpdateRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

            otsClient.UpdateRow(request);
            Console.WriteLine("Update row succeed.");
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Write a single row into the table.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public UpdateRowResponse UpdateRow(UpdateRowRequest request)
        {
            var uri = new Uri($"{GetEndPoint(NoSQLServices.Tables, this.Region)}/{request.TableNameOrId}/rows");

            var httpRequestHeaderParam = new HttpRequestHeaderParam()
            {
                IfMatch      = request.IfMatch,
                OpcRequestId = request.OpcRequestId
            };

            using (var webResponse = this.RestClient.Put(uri, request.UpdateRowDetails, httpRequestHeaderParam))
                using (var stream = webResponse.GetResponseStream())
                    using (var reader = new StreamReader(stream))
                    {
                        var response = reader.ReadToEnd();

                        return(new UpdateRowResponse()
                        {
                            UpdateRowResult = this.JsonSerializer.Deserialize <UpdateRowResult>(response),
                            OpcRequestId = webResponse.Headers.Get("opc-request-id"),
                            Etag = webResponse.Headers.Get("etag")
                        });
                    }
        }
Ejemplo n.º 17
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            UpdateRowRequest request;

            try
            {
                request = new UpdateRowRequest
                {
                    TableNameOrId    = TableNameOrId,
                    UpdateRowDetails = UpdateRowDetails,
                    IfMatch          = IfMatch,
                    OpcRequestId     = OpcRequestId
                };

                response = client.UpdateRow(request).GetAwaiter().GetResult();
                WriteOutput(response, response.UpdateRowResult);
                FinishProcessing(response);
            }
            catch (Exception ex)
            {
                TerminatingErrorDuringExecution(ex);
            }
        }
        public static void ConditionUpdateRow()
        {
            Console.WriteLine("Start update row...");

            PrepareTable();
            var otsClient = Config.GetClient();

            // 定义行的主键,必须与创建表时的TableMeta中定义的一致
            var primaryKey = new PrimaryKey();

            primaryKey.Add("pk0", new ColumnValue(1));
            primaryKey.Add("pk1", new ColumnValue("abc"));

            // 定义要写入改行的属性列
            var attribute = new AttributeColumns();

            attribute.Add("col0", new ColumnValue(1));
            attribute.Add("col1", new ColumnValue("a"));
            attribute.Add("col2", new ColumnValue(true));

            var request = new PutRowRequest(tableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

            // 新创建一行数据
            try
            {
                otsClient.PutRow(request);

                Console.WriteLine("Put row succeeded.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Put row failed. error:{0}", ex.Message);
            }


            // 当col0不等于5,且col1等于'a'时,允许修改,否则不允许修改
            try
            {
                // 构造condition
                var cond1 = new RelationalCondition("col0",
                                                    CompareOperator.NOT_EQUAL,
                                                    new ColumnValue(5));
                var cond2 = new RelationalCondition("col1", CompareOperator.EQUAL,
                                                    new ColumnValue("a"));
                var columenCondition = new CompositeCondition(LogicOperator.AND);
                columenCondition.AddCondition(cond1);
                columenCondition.AddCondition(cond2);

                var condition = new Condition(RowExistenceExpectation.IGNORE);
                condition.ColumnCondition = columenCondition;

                // 构造更新请求
                var updateOfAttribute = new UpdateOfAttribute();
                updateOfAttribute.AddAttributeColumnToPut("col2", new ColumnValue(false));
                var updateRowRequest = new UpdateRowRequest(tableName, condition, primaryKey, updateOfAttribute);

                // 更新数据
                otsClient.UpdateRow(updateRowRequest);

                // 更新成功
                Console.WriteLine("Update row succeeded.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Update row failed. error:{0}", ex.Message);
            }
        }
        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));
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        public void UpdateRow(Object obj)
        {
            var type = obj.GetType();
            var name = tableNames.ContainsKey(type) ? tableNames[type] : SetTable(type);

            var keys    = new PrimaryKey();
            var columns = new UpdateOfAttribute();

            foreach (PropertyInfo info in rft.GetPropertyList(type))
            {
                var methodInfo = info.GetGetMethod();
                if (methodInfo == null || methodInfo.IsStatic || !methodInfo.IsPublic)
                {
                    continue;
                }
                if (info.GetCustomAttribute(typeof(System.ComponentModel.DataAnnotations.KeyAttribute)) == null)
                {
                    #region 属性列处理
                    var propertyValue = info.GetValue(obj, null);
                    if (propertyValue == null)
                    {
                        continue;
                    }
                    else if (info.PropertyType == typeof(int))
                    {
                        columns.AddAttributeColumnToPut(info.Name, new ColumnValue((int)propertyValue));
                    }
                    else if (info.PropertyType == typeof(long))
                    {
                        columns.AddAttributeColumnToPut(info.Name, new ColumnValue((long)propertyValue));
                    }
                    else if (info.PropertyType == typeof(string))
                    {
                        columns.AddAttributeColumnToPut(info.Name, new ColumnValue((string)propertyValue));
                    }
                    else if (info.PropertyType == typeof(bool))
                    {
                        columns.AddAttributeColumnToPut(info.Name, new ColumnValue((bool)propertyValue));
                    }
                    else if (info.PropertyType == typeof(double))
                    {
                        columns.AddAttributeColumnToPut(info.Name, new ColumnValue((double)propertyValue));
                    }
                    else if (info.PropertyType == typeof(ulong))
                    {
                        columns.AddAttributeColumnToPut(info.Name, new ColumnValue((ulong)propertyValue));
                    }
                    else if (info.PropertyType == typeof(byte[]))
                    {
                        columns.AddAttributeColumnToPut(info.Name, new ColumnValue((byte[])propertyValue));
                    }
                    #endregion
                }
                else
                {
                    #region 主键字段处理
                    var propertyValue = info.GetValue(obj, null);
                    if (propertyValue == null)
                    {
                        throw new OTSException("PrimaryKey can not null");
                    }
                    else if (info.PropertyType == typeof(int))
                    {
                        keys.Add(info.Name, new ColumnValue((int)propertyValue));
                    }
                    else if (info.PropertyType == typeof(string))
                    {
                        keys.Add(info.Name, new ColumnValue((string)propertyValue));
                    }
                    else if (info.PropertyType == typeof(byte[]))
                    {
                        keys.Add(info.Name, new ColumnValue((byte[])propertyValue));
                    }
                    #endregion
                }
            }

            var req = new UpdateRowRequest(name, new Condition(RowExistenceExpectation.EXPECT_EXIST), keys, columns);
            OtsClient.UpdateRow(req);
        }
Ejemplo n.º 21
0
 /// <summary>
 /// UpdateRow的异步形式。
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public Task <UpdateRowResponse> UpdateRowAsync(UpdateRowRequest request)
 {
     return(CallAsync <UpdateRowRequest, UpdateRowResponse>("/UpdateRow", request));
 }
Ejemplo n.º 22
0
 /// <summary>
 /// 更新指定行的数据,如果该行不存在,则新增一行;若该行存在,则根据请求的内容在这一行中新增、修改或者删除指定列的值。
 /// </summary>
 /// <param name="request">请求实例</param>
 /// <returns>响应实例</returns>
 /// <example>
 /// 更新一行,新增一列"NewColumn",并删除一列"IntAttr0"。
 /// <code>
 /// var primaryKey = new PrimaryKey();
 /// primaryKey.Add("PK0", new ColumnValue("ABC"));
 /// primaryKey.Add("PK1", new ColumnValue(123));
 /// var updateOfAttribute = new UpdateOfAttribute();
 /// updateOfAttribute.AddAttributeColumnToPut("NewColumn", new ColumnValue(123));
 /// updateOfAttribute.AddAttributeColumnToDelete("IntAttr0");
 ///
 /// var updateRowRequest = new UpdateRowRequest(
 ///                            "SampleTableName",
 ///                            new Condition(RowExistenceExpectation.EXPECT_EXIST),
 ///                            primaryKey,
 ///                            updateOfAttribute);
 /// var updateRowResponse = otsClient.UpdateRow(updateRowRequest);
 ///
 /// System.Console.WriteLine("UpdateRow CU Consumed: Read {0} Write {0}",
 ///     updateRowResponse.ConsumedCapacityUnit.Read,
 ///     updateRowResponse.ConsumedCapacityUnit.Write);
 /// </code>
 /// </example>
 public UpdateRowResponse UpdateRow(UpdateRowRequest request)
 {
     return(GetResponseFromAsyncTask(UpdateRowAsync(request)));
 }