private Response.OTSResponse DecodeGetRange(byte[] body, out IMessage _message)
        {
            var builder = PB.GetRangeResponse.CreateBuilder();

            builder.MergeFrom(body);
            var message  = builder.Build();
            var response = new Response.GetRangeResponse
            {
                ConsumedCapacityUnit = ParseCapacityUnit(message.Consumed.CapacityUnit)
            };

            if (!message.HasNextStartPrimaryKey)
            {
                response.NextPrimaryKey = null;
            }
            else
            {
                var inputStream = new PB.PlainBufferCodedInputStream(message.NextStartPrimaryKey.CreateCodedInput());
                var rows        = inputStream.ReadRowsWithHeader();
                if (rows.Count != 1)
                {
                    throw new IOException("Expect only one row return. Row count: " + rows.Count);
                }

                PB.PlainBufferRow row = rows[0];
                if (row.HasDeleteMarker() || row.HasCells())
                {
                    throw new IOException("The next primary key should only have primary key: " + row);
                }

                response.NextPrimaryKey = PB.PlainBufferConversion.ToPrimaryKey(row.GetPrimaryKey());
            }


            if (message.HasRows && !message.Rows.IsEmpty)
            {
                List <DataModel.Row> rows = new List <DataModel.Row>();
                var inputStream           = new PB.PlainBufferCodedInputStream(message.Rows.CreateCodedInput());

                List <PB.PlainBufferRow> pbRows = inputStream.ReadRowsWithHeader();
                foreach (var pbRow in pbRows)
                {
                    rows.Add((DataModel.Row)PB.PlainBufferConversion.ToRow(pbRow));
                }

                response.RowDataList = rows;
            }

            if (message.HasNextToken)
            {
                response.NextToken = message.NextToken.ToByteArray();
            }

            _message = message;
            return(response);
        }
        private DataModel.Row ParseRow(ByteString row)
        {
            PB.PlainBufferCodedInputStream inputStream = new PB.PlainBufferCodedInputStream(row.CreateCodedInput());
            List <PB.PlainBufferRow>       rows        = inputStream.ReadRowsWithHeader();

            if (rows.Count != 1)
            {
                throw new IOException("Expect only returns one row. Row count: " + rows.Count);
            }

            return(PB.PlainBufferConversion.ToRow(rows[0]) as DataModel.Row);
        }
        private IList <Response.BatchGetRowResponseItem> ParseTableInBatchGetRowResponse(PB.TableInBatchGetRowResponse table)
        {
            var ret   = new List <Response.BatchGetRowResponseItem>();
            int index = 0;

            foreach (var row in table.RowsList)
            {
                DataModel.IRow result = null;
                if (!row.IsOk)
                {
                    ret.Add(new Response.BatchGetRowResponseItem(row.Error.Code, row.Error.Message));
                    continue;
                }

                if (row.HasRow && !row.Row.IsEmpty)
                {
                    var inputStream = new PB.PlainBufferCodedInputStream(row.Row.CreateCodedInput());
                    List <PB.PlainBufferRow> rows = inputStream.ReadRowsWithHeader();
                    if (rows.Count != 1)
                    {
                        throw new IOException("Expect only returns one row. Row count: " + rows.Count);
                    }

                    result = PB.PlainBufferConversion.ToRow(rows[0]);
                }

                Response.BatchGetRowResponseItem item = null;

                var capacityUnit = ParseCapacityUnit(row.Consumed.CapacityUnit);

                if (row.HasNextToken)
                {
                    item = new Response.BatchGetRowResponseItem(table.TableName, result, capacityUnit, index, row.NextToken.ToByteArray());
                }
                else
                {
                    item = new Response.BatchGetRowResponseItem(table.TableName, result, capacityUnit, index);
                }

                index++;

                ret.Add(item);
            }

            return(ret);
        }
        private IList <Response.BatchWriteRowResponseItem> ParseBatchWriteRowResponseItems(string tableName, IList <PB.RowInBatchWriteRowResponse> responseItems)
        {
            var ret   = new List <Response.BatchWriteRowResponseItem>();
            int index = 0;

            foreach (var responseItem in responseItems)
            {
                DataModel.IRow row = null;
                if (responseItem.IsOk)
                {
                    if (responseItem.HasRow && !responseItem.Row.IsEmpty)
                    {
                        try
                        {
                            var inputStream = new PB.PlainBufferCodedInputStream(responseItem.Row.CreateCodedInput());
                            List <PB.PlainBufferRow> rows = inputStream.ReadRowsWithHeader();
                            if (rows.Count != 1)
                            {
                                throw new IOException("Expect only returns one row. Row count: " + rows.Count);
                            }

                            row = PB.PlainBufferConversion.ToRow(rows[0]);
                        }
                        catch (Exception e)
                        {
                            throw new OTSException("Failed to parse row data." + e.Message);
                        }
                    }

                    ret.Add(new Response.BatchWriteRowResponseItem(
                                ParseCapacityUnit(responseItem.Consumed.CapacityUnit), tableName, index++, row));
                }
                else
                {
                    ret.Add(new Response.BatchWriteRowResponseItem(
                                responseItem.Error.Code, responseItem.Error.Message, tableName, index++));
                }
            }

            return(ret);
        }