Beispiel #1
0
        /// <summary>
        /// 通过多个参数构造一个新的<see cref="GetRangeRequest"/>
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="direction">前向还是后向</param>
        /// <param name="inclusiveStartPrimaryKey">区间开始位置,包含</param>
        /// <param name="exclusiveEndPrimaryKey">区间结束位置,不包含</param>
        /// <param name="columnsToGet">返回的列名称的列表</param>
        /// <param name="limit">最大返回数</param>
        public GetRangeRequest(string tableName,
                               GetRangeDirection direction,
                               PrimaryKey inclusiveStartPrimaryKey,
                               PrimaryKey exclusiveEndPrimaryKey,
                               HashSet <string> columnsToGet = null,
                               int?limit = null,
                               ColumnCondition condition = null)
        {
            QueryCriteria = new RangeRowQueryCriteria(tableName);

            QueryCriteria.Direction = direction;

            QueryCriteria.Limit = limit;

            QueryCriteria.InclusiveStartPrimaryKey = inclusiveStartPrimaryKey;

            QueryCriteria.ExclusiveEndPrimaryKey = exclusiveEndPrimaryKey;

            if (columnsToGet != null)
            {
                QueryCriteria.SetColumnsToGet(columnsToGet);
            }

            if (condition != null)
            {
                QueryCriteria.Filter = condition;
            }
        }
Beispiel #2
0
        private string GetSearchParamValue(ColumnCondition columnCondition)
        {
            var result = columnCondition.Argument.Execute(_program);

            if (result != null)
            {
                return(result.ToString());
            }
            return(null);
        }
Beispiel #3
0
 /// <summary>
 /// 通过多个参数构造一个新的<see cref="GetRangeRequest"/>
 /// </summary>
 /// <param name="tableName">表名称</param>
 /// <param name="direction">前向还是后向</param>
 /// <param name="inclusiveStartPrimaryKey">区间开始位置,包含</param>
 /// <param name="exclusiveEndPrimaryKey">区间结束位置,不包含</param>
 /// <param name="consumedCapacityUnitCounter">用户传入的CapacityUnit消耗计数器。</param>
 /// <param name="columnsToGet">返回的列名称的列表</param>
 /// <param name="limit">最大返回数</param>
 public GetIteratorRequest(string tableName,
                           GetRangeDirection direction,
                           PrimaryKey inclusiveStartPrimaryKey,
                           PrimaryKey exclusiveEndPrimaryKey,
                           CapacityUnit consumedCapacityUnitCounter,
                           HashSet <string> columnsToGet = null,
                           int?limit = null,
                           ColumnCondition condition = null)
     : base(tableName, direction, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey,
            columnsToGet, limit, condition)
 {
     ConsumedCapacityUnitCounter = consumedCapacityUnitCounter;
 }
        public void CheckSingleRow(string tableName, PrimaryKey primaryKey,
                                   AttributeColumns attribute,
                                   CapacityUnit expectCapacityUnitConsumed = null,
                                   HashSet <string> columnsToGet           = null,
                                   bool isEmpty = false,
                                   ColumnCondition condition = null)
        {
            var request = new GetRowRequest(tableName, primaryKey, columnsToGet, condition);

            var response = OTSClient.GetRow(request);

            PrimaryKey       primaryKeyToExpect;
            AttributeColumns attributeToExpect;

            if (isEmpty)
            {
                primaryKeyToExpect = new PrimaryKey();
                attributeToExpect  = new AttributeColumns();
            }
            else if (columnsToGet == null || columnsToGet.Count == 0)
            {
                primaryKeyToExpect = primaryKey;
                attributeToExpect  = attribute;
            }
            else
            {
                primaryKeyToExpect = new PrimaryKey();
                attributeToExpect  = new AttributeColumns();
                foreach (var columnName in columnsToGet)
                {
                    if (primaryKey.ContainsKey(columnName))
                    {
                        primaryKeyToExpect.Add(columnName, primaryKey[columnName]);
                    }

                    if (attribute.ContainsKey(columnName))
                    {
                        attributeToExpect.Add(columnName, attribute[columnName]);
                    }
                }
            }

            AssertColumns(primaryKeyToExpect, response.PrimaryKey);
            AssertColumns(attributeToExpect, response.Attribute);

            if (expectCapacityUnitConsumed != null)
            {
                AssertCapacityUnit(expectCapacityUnitConsumed, response.ConsumedCapacityUnit);
            }
        }
        /// <summary>
        /// 构造一个新的<see cref="GetRowRequest" />实例。
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="primaryKey">主键</param>
        /// <param name="columnsToGet">获取的列名称列表,如果为空,则获取所有列</param>
        /// <param name="condition">过滤条件</param>
        public GetRowRequest(string tableName,
                             PrimaryKey primaryKey,
                             HashSet <string> columnsToGet = null,
                             ColumnCondition condition     = null)
        {
            QueryCriteria = new SingleRowQueryCriteria(tableName);
            QueryCriteria.RowPrimaryKey = primaryKey;

            if (columnsToGet != null)
            {
                QueryCriteria.SetColumnsToGet(columnsToGet);
            }

            if (condition != null)
            {
                QueryCriteria.Filter = condition;
            }
        }
 private PB.ColumnCondition MakeColumnCondition(ColumnCondition cc)
 {
     PB.ColumnCondition.Builder builder = PB.ColumnCondition.CreateBuilder();
     builder.SetType(MakeColumnConditionType(cc.GetType()));
     if (cc.GetType() == ColumnConditionType.COMPOSITE_CONDITION)
     {
         builder.SetCondition(BuildCompositeCondition((CompositeCondition)cc));
     }
     else if (cc.GetType() == ColumnConditionType.RELATIONAL_CONDITION)
     {
         builder.SetCondition(BuildRelationalCondition((RelationalCondition)cc));
     }
     else
     {
         throw new OTSClientException(String.Format("Invalid column condition type: {0}", cc.GetType()));
     }
     return(builder.Build());
 }
        public IEnumerable <RowDataFromGetRange> GetRangeIterator(
            string tableName,
            GetRangeDirection direction,
            PrimaryKey inclusiveStartPrimaryKey,
            PrimaryKey exclusiveEndPrimaryKey,
            CapacityUnit consumedCapacityUnitCounter,
            HashSet <string> columnsToGet = null,
            int?count = null,
            ColumnCondition condition = null)
        {
            int?leftCount = count;

            if (leftCount != null && leftCount < 0)
            {
                throw new OTSClientException("the value of count must be larger than 0");
            }

            PrimaryKey nextStartPrimaryKey = inclusiveStartPrimaryKey;

            while (nextStartPrimaryKey != null)
            {
                var request = new GetRangeRequest(
                    tableName, direction, nextStartPrimaryKey, exclusiveEndPrimaryKey,
                    columnsToGet, leftCount, condition);

                var response = GetRange(request);
                consumedCapacityUnitCounter.Read += response.ConsumedCapacityUnit.Read;
                nextStartPrimaryKey = response.NextPrimaryKey;

                foreach (var rowData in response.RowDataList)
                {
                    yield return(rowData);
                }

                if (leftCount != null)
                {
                    leftCount -= response.RowDataList.Count;
                    if (leftCount <= 0)
                    {
                        break;
                    }
                }
            }
        }
Beispiel #8
0
        /// <summary>
        /// 添加一个表的多行读请求。
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="primaryKeys">多行的主键</param>
        /// <param name="columnsToGet">要读取的列</param>
        /// <param name="condition">过滤条件</param>
        public void Add(string tableName,
                        List <PrimaryKey> primaryKeys,
                        HashSet <string> columnsToGet = null,
                        ColumnCondition condition     = null)
        {
            var rowQueryCriteria = new MultiRowQueryCriteria(tableName);

            rowQueryCriteria.SetRowKeys(primaryKeys);

            if (columnsToGet != null)
            {
                rowQueryCriteria.SetColumnsToGet(columnsToGet);
            }

            if (condition != null)
            {
                rowQueryCriteria.Filter = condition;
            }

            rowQueryCriteriaDict[tableName] = rowQueryCriteria;
        }
        private bool DeleteRow(String tableName, Int64 pk, ColumnCondition cond)
        {
            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue(pk));
            Condition c = new Condition(RowExistenceExpectation.IGNORE);

            c.ColumnCondition = cond;
            var  request = new DeleteRowRequest(tableName, c, primaryKey);
            bool success = true;

            try
            {
                OTSClient.DeleteRow(request);
            }
            catch (OTSServerException e)
            {
                Console.WriteLine("DeleteRow fail:{0}", e.ErrorMessage);
                success = false;
            }
            return(success);
        }
        private void UpdateRow(OTSClient client, String tableName, ColumnCondition cond)
        {
            var primaryKey = new PrimaryKey();

            primaryKey.Add(COLUMN_GID_NAME, new ColumnValue(1));
            primaryKey.Add(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);

            condition.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);
            }
        }
        public bool UpdateRow(String tableName, Int64 pk, String colName, ColumnValue colValue, ColumnCondition cond)
        {
            bool success    = true;
            var  primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue(pk));
            Condition rowCond = new Condition(RowExistenceExpectation.IGNORE);

            rowCond.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);
        }
        private bool PutRow(string tableName, Int64 pk, string colName, ColumnValue colValue, ColumnCondition cc)
        {
            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue(pk));

            var attribute = new AttributeColumns();

            attribute.Add(colName, colValue);
            Condition cond = new Condition(RowExistenceExpectation.IGNORE);

            cond.ColumnCondition = cc;
            var request = new PutRowRequest(tableName, cond, primaryKey, attribute);

            bool success = true;

            try
            {
                OTSClient.PutRow(request);
            }
            catch (OTSServerException e)
            {
                Console.WriteLine("PutRow fail: {0}", e.ErrorMessage);
                success = false;
            }
            return(success);
        }
Beispiel #13
0
 public CompositeCondition AddCondition(ColumnCondition condition)
 {
     subConditions.Add(condition);
     return(this);
 }