数据分区工具
예제 #1
0
 public virtual void TestPartition()
 {
     // 本例的分区策略:希望将数据水平分成3份,前两份各占25%,第三份占50%。(故本例非均匀分区)
     // |<---------------------1024------------------------>|
     // |<----256--->|<----256--->|<----------512---------->|
     // | partition0 | partition1 | partition2 |
     // | 共2份,故count[0]=2 | 共1份,故count[1]=1 |
     int[] count = {2, 1};
     int[] length = {256, 512};
     var pu = new PartitionUtil(count, length);
     // 下面代码演示分别以offerId字段或memberId字段根据上述分区策略拆分的分配结果
     var DefaultStrHeadLen = 8;
     // cobar默认会配置为此值
     long offerId = 12345;
     var memberId = "qiushuo";
     // 若根据offerId分配,partNo1将等于0,即按照上述分区策略,offerId为12345时将会被分配到partition0中
     var partNo1 = pu.Partition(offerId);
     // 若根据memberId分配,partNo2将等于2,即按照上述分区策略,memberId为qiushuo时将会被分到partition2中
     var partNo2 = pu.Partition(memberId, 0, DefaultStrHeadLen);
     Assert.AreEqual(0, partNo1);
     Assert.AreEqual(2, partNo2);
 }
예제 #2
0
        public virtual void TestPartition()
        {
            // 本例的分区策略:希望将数据水平分成3份,前两份各占25%,第三份占50%。(故本例非均匀分区)
            // |<---------------------1024------------------------>|
            // |<----256--->|<----256--->|<----------512---------->|
            // | partition0 | partition1 | partition2 |
            // | 共2份,故count[0]=2 | 共1份,故count[1]=1 |
            int[] count  = { 2, 1 };
            int[] length = { 256, 512 };
            var   pu     = new PartitionUtil(count, length);
            // 下面代码演示分别以offerId字段或memberId字段根据上述分区策略拆分的分配结果
            var DefaultStrHeadLen = 8;
            // cobar默认会配置为此值
            long offerId  = 12345;
            var  memberId = "qiushuo";
            // 若根据offerId分配,partNo1将等于0,即按照上述分区策略,offerId为12345时将会被分配到partition0中
            var partNo1 = pu.Partition(offerId);
            // 若根据memberId分配,partNo2将等于2,即按照上述分区策略,memberId为qiushuo时将会被分到partition2中
            var partNo2 = pu.Partition(memberId, 0, DefaultStrHeadLen);

            Assert.AreEqual(0, partNo1);
            Assert.AreEqual(2, partNo2);
        }
 public void Initialize()
 {
     _partitionUtilX = new PartitionUtil(_countX, _lengthX);
     _partitionUtilY = new PartitionUtil(_countY, _lengthY);
     BuildAll();
     BuildByX();
     BuildByY();
 }
        /// <returns>null if eval invalid type</returns>
        private static int? Calculate(object eval,
                                      PartitionUtil partitionUtil,
                                      int keyType,
                                      int hashSliceStart,
                                      int hashSliceEnd)
        {
            if (eval == ExpressionConstants.Unevaluatable || eval == null)
                return null;

            switch (keyType)
            {
                case PartitionKeyTypeLong:
                {
                    long longVal;
                    if (eval is Number)
                    {
                        longVal = (long)(Number)eval;
                    }
                    else if (eval is string)
                    {
                        longVal = long.Parse((string)eval);
                    }
                    else
                    {
                        throw new ArgumentException("unsupported data type for partition key: " + eval.GetType());
                    }

                    return partitionUtil.Partition(longVal);
                }

                case PartitionKeyTypeString:
                {
                    var key = eval.ToString();
                    var start = hashSliceStart >= 0 ? hashSliceStart : key.Length + hashSliceStart;
                    var end = hashSliceEnd > 0 ? hashSliceEnd : key.Length + hashSliceEnd;
                    var hash = StringUtil.Hash(key, start, end);
                    return partitionUtil.Partition(hash);
                }

                default:
                {
                    throw new ArgumentException("unsupported partition key type: " + keyType);
                }
            }
        }
예제 #5
0
 public override void Init()
 {
     PartitionUtil = new PartitionUtil(Count, Length);
 }