示例#1
0
        public override int GetHashCode()
        {
            var aSChd = ShardKey <TRecord> .GetValueBytes(this._greatGrandChildId);

            var aResult = new byte[4];

            if (!(aSChd is null))
            {
                if (aSChd.Length > 0)
                {
                    aResult[0] = aSChd[0];
                }
                if (aSChd.Length > 1)
                {
                    aResult[1] = aSChd[1];
                }
                if (aSChd.Length > 2)
                {
                    aResult[2] = aSChd[2];
                }
                if (aSChd.Length > 3)
                {
                    aResult[3] = aSChd[3];
                }
            }

            return(_key.GetHashCode() | BitConverter.ToInt32(aResult, 0));
        }
示例#2
0
        internal byte[] ToArray()
        {
            var aOrigin   = System.Text.Encoding.UTF8.GetBytes(new[] { this._key.Origin });
            var shardData = ShardKey <TRecord> .GetValueBytes(_key.ShardId);

            var recordData = ShardKey <TRecord> .GetValueBytes(_key.RecordId);

            var childData = ShardKey <TRecord> .GetValueBytes(_key.ChildId);

            var grandChildData = ShardKey <TRecord> .GetValueBytes(_key.GrandChildId);

            var greatGrandChildData = ShardKey <TRecord> .GetValueBytes(_greatGrandChildId);

            var aResult = new byte[aOrigin.Length + shardData.Length + recordData.Length + childData.Length + grandChildData.Length + 1];

            aResult[0] = (byte)(aOrigin.Length | (1 << 2)); //origin length on bits 1 & 2, version (1) on bit 3.
            var resultIndex = 1;

            aOrigin.CopyTo(aResult, resultIndex);
            resultIndex += aOrigin.Length;
            shardData.CopyTo(aResult, resultIndex);
            resultIndex += shardData.Length;
            recordData.CopyTo(aResult, resultIndex);
            resultIndex += recordData.Length;
            childData.CopyTo(aResult, resultIndex);
            resultIndex += childData.Length;
            grandChildData.CopyTo(aResult, resultIndex);
            resultIndex += grandChildData.Length;
            greatGrandChildData.CopyTo(aResult, resultIndex);
            resultIndex += greatGrandChildData.Length;
            return(aResult);
        }
示例#3
0
        public static ShardKey <TRecord, TChild> FromExternalString(string value)
        {
            var aValues = StringExtensions.SerializeFromExternalString(value);

            int orgnLen = aValues[0] & 3;
            var orgn    = System.Text.Encoding.UTF8.GetString(aValues, 1, orgnLen)[0];
            var pos     = orgnLen + 1;

            short shardId = ShardKey <TRecord> .ConvertFromBytes(aValues, ref pos, typeof(short));

            TRecord recordId = ShardKey <TRecord> .ConvertFromBytes(aValues, ref pos, typeof(TRecord));

            TChild childId = ShardKey <TRecord> .ConvertFromBytes(aValues, ref pos, typeof(TChild));

            return(new ShardKey <TRecord, TChild>(orgn, shardId, recordId, childId));
        }
示例#4
0
 /// <summary>
 /// ISerializer constructor
 /// </summary>
 /// <param name="info"></param>
 /// <param name="context"></param>
 public ShardKey(SerializationInfo info, StreamingContext context)
 {
     if (info.MemberCount == 4)
     {
         char    origin   = info.GetChar("origin");
         var     shardId  = (short)info.GetValue("shardId", typeof(short));
         TRecord recordId = (TRecord)info.GetValue("recordId", typeof(TRecord));
         _key     = new ShardKey <TRecord>(origin, shardId, recordId);
         _childId = (TChild)info.GetValue("childId", typeof(TChild));
     }
     else
     {
         var tmp = FromExternalString(info.GetString("ShardKey"));
         _key     = tmp.Key;
         _childId = tmp.ChildId;
     }
 }
示例#5
0
 public static void NullShardChildArguments <TRecord, TChild>(this ILogger logger, string propertyName, ShardKey <TRecord, TChild> shardChild) where TRecord : IComparable where TChild : IComparable
 {
     if (shardChild.Key.ShardId.Equals(null) || shardChild.Key.RecordId.Equals(null) || shardChild.ChildId.Equals(null))
     {
         _sqlShardChildNull(logger, propertyName, shardChild.ToString(), null);
     }
 }
示例#6
0
 public ShardKey(char origin, short shardId, TRecord recordId, TChild childRecordId, TGrandChild grandChildRecordId, TGreatGrandChild greatGrandChildRecordId)
 {
     _key = new ShardKey <TRecord, TChild, TGrandChild>(origin, shardId, recordId, childRecordId, grandChildRecordId);
     _greatGrandChildId = greatGrandChildRecordId;
 }
示例#7
0
 public ShardKey(ShardKey <TRecord, TChild, TGrandChild> key, TGreatGrandChild grandChildRecordId)
 {
     _key = key;
     _greatGrandChildId = grandChildRecordId;
 }
示例#8
0
 public bool Equals(ShardKey <TRecord, TChild, TGrandChild, TGreatGrandChild> other)
 {
     return((other.GrandChild == _key) && (other.GreatGrandChildId.CompareTo(_greatGrandChildId) == 0));
 }
示例#9
0
        internal static byte[] GetValueBytes(IComparable value)
        {
            switch (value)
            {
            case int i:
                return(BitConverter.GetBytes(i));

            case byte b:
                return(new byte[1] {
                    b
                });

            case short s:
                return(BitConverter.GetBytes(s));

            case long l:
                return(BitConverter.GetBytes(l));

            case char c:
                return(BitConverter.GetBytes(c));

            case decimal d:
                var aid = Decimal.GetBits(d);
                var i0  = BitConverter.GetBytes(aid[0]);
                var i1  = BitConverter.GetBytes(aid[1]);
                var i2  = BitConverter.GetBytes(aid[2]);
                var i3  = BitConverter.GetBytes(aid[3]);
                return(new byte[16] {
                    i0[0], i0[1], i0[2], i0[3], i1[0], i1[1], i1[2], i1[3], i2[0], i2[1], i2[2], i2[3], i3[0], i3[1], i3[2], i3[3]
                });

            case double o:
                return(BitConverter.GetBytes(o));

            case float f:
                return(BitConverter.GetBytes(f));

            case uint ui:
                return(BitConverter.GetBytes(ui));

            case ulong ul:
                return(BitConverter.GetBytes(ul));

            case ushort us:
                return(BitConverter.GetBytes(us));

            case sbyte sb:
                return(new byte[1] {
                    (byte)((int)sb + 128)
                });

            //case bool bln:
            //    return BitConverter.GetBytes(bln);
            case DateTime dt:
                return(BitConverter.GetBytes(dt.Ticks));

            case string str:
                if (str is null)
                {
                    return(new byte[1] {
                        0
                    });
                }
                else
                {
                    var aStr = System.Text.Encoding.UTF8.GetBytes(str);
                    if (aStr.Length > 128)
                    {
                        throw new Exception("Shard values cannot serialize strings longer than 128 bytes.");
                    }
                    var aResult = new byte[aStr.Length + 1];
                    aResult[0] = Convert.ToByte((aStr.Length << 1) + 1);
                    aStr.CopyTo(aResult, 1);
                    return(aResult);
                }

            case Enum e:
                var type     = Enum.GetUnderlyingType(value.GetType());
                var newValue = Convert.ChangeType(value, type);
                return(ShardKey <TRecord> .GetValueBytes(newValue as IComparable));

            case DateTimeOffset dto:
                var adt = BitConverter.GetBytes(dto.Ticks);
                var tsp = BitConverter.GetBytes(dto.Offset.Ticks);
                return(new byte[] { adt[0], adt[1], adt[2], adt[3], adt[4], adt[5], adt[6], adt[7], tsp[0], tsp[1], tsp[2], tsp[3], tsp[4], tsp[5], tsp[6], tsp[7] });

            case TimeSpan ts:
                return(BitConverter.GetBytes(ts.Ticks));

            case Guid g:
                return(g.ToByteArray());

            //case null:
            //    return new byte[0];
            default:
                if (value is Nullable)
                {
                    if (value == null)
                    {
                        return(BitConverter.GetBytes(true));
                    }
                    var shdType      = Nullable.GetUnderlyingType(value.GetType());
                    var nonNullValue = Convert.ChangeType(value, shdType);
                    var aVal         = GetValueBytes(nonNullValue as IComparable);
                    var valResult    = new byte[1 + aVal.Length];
                    valResult[0] = BitConverter.GetBytes(false)[0];
                    aVal.CopyTo(valResult, 1);
                    return(valResult);
                }
                else
                {
                    throw new Exception("Cannot serialize this type.");
                }
            }
        }
示例#10
0
 public bool Equals(ShardKey <TRecord> other)
 {
     return((other.Origin == _origin) && (other.ShardId.CompareTo(_shardId) == 0) && (other.RecordId.CompareTo(_recordId) == 0));
 }
示例#11
0
 internal static dynamic ConvertFromBytes(byte[] data, ref int position, Type valueType)
 {
     if (valueType == typeof(int))
     {
         position += 4;
         return(BitConverter.ToInt32(data, position - 4));
     }
     if (valueType == typeof(long))
     {
         position += 8;
         return(BitConverter.ToInt64(data, position - 8));
     }
     if (valueType == typeof(byte))
     {
         position += 1;
         return(data[position - 1]);
     }
     if (valueType == typeof(short))
     {
         position += 2;
         return(BitConverter.ToInt16(data, position - 2));
     }
     if (valueType == typeof(char))
     {
         position += 2;
         return(BitConverter.ToChar(data, position - 2));
     }
     if (valueType == typeof(decimal))
     {
         var i0 = BitConverter.ToInt32(data, position);
         var i1 = BitConverter.ToInt32(data, position + 4);
         var i2 = BitConverter.ToInt32(data, position + 8);
         var i3 = BitConverter.ToInt32(data, position + 12);
         position += 16;
         return((dynamic) new Decimal(new int[] { i0, i1, i2, i3 }));
     }
     if (valueType == typeof(double))
     {
         position += 8;
         return(BitConverter.ToDouble(data, position - 8));
     }
     if (valueType == typeof(float))
     {
         position += 4;
         return(BitConverter.ToSingle(data, position - 4));
     }
     if (valueType == typeof(uint))
     {
         position += 4;
         return(BitConverter.ToUInt32(data, position - 4));
     }
     if (valueType == typeof(ulong))
     {
         position += 8;
         return(BitConverter.ToUInt64(data, position - 8));
     }
     if (valueType == typeof(ushort))
     {
         position += 2;
         return(BitConverter.ToUInt16(data, position - 2));
     }
     if (valueType == typeof(sbyte))
     {
         position += 1;
         return((sbyte)((int)data[position - 1] - 128));
     }
     if (valueType == typeof(bool))
     {
         position += 1;
         return(BitConverter.ToBoolean(data, position - 1));
     }
     if (valueType == typeof(DateTime))
     {
         position += 8;
         return(new DateTime(BitConverter.ToInt64(data, position - 8)));
     }
     if (valueType == typeof(string))
     {
         if (data[position] == 0)
         {
             position += 1;
             return((string)null);
         }
         var len = (int)data[position] >> 1;
         position += len + 1;
         return(System.Text.Encoding.UTF8.GetString(data, position - len, len));
     }
     if (valueType == typeof(Enum))
     {
         var baseType = Enum.GetUnderlyingType(valueType);
         return(ShardKey <TRecord> .ConvertFromBytes(data, ref position, baseType));
     }
     if (valueType == typeof(Nullable))
     {
         var isNull = BitConverter.ToBoolean(data, position);
         position += 1;
         if (isNull)
         {
             return(null);
         }
         var baseType = Nullable.GetUnderlyingType(valueType);
         return(ConvertFromBytes(data, ref position, baseType));
     }
     if (valueType == typeof(Guid))
     {
         var result = new Guid(new byte[] { data[position], data[position + 1], data[position + 2], data[position + 3], data[position + 4], data[position + 5], data[position + 6], data[position + 7], data[position + 8], data[position + 9], data[position + 10], data[position + 11], data[position + 12], data[position + 13], data[position + 14], data[position + 15] });
         position += 16;
         return(result);
     }
     else
     {
         throw new Exception($"Could not recognized the type {valueType.ToString()}.");
     }
 }
示例#12
0
 public ShardKey(char origin, short shardId, TRecord recordId, TChild childRecordId)
 {
     _key     = new ShardKey <TRecord>(origin, shardId, recordId);
     _childId = childRecordId;
 }
示例#13
0
 public bool Equals(ShardKey <TRecord, TChild> other)
 {
     return((other.Key == this.Key) && (other.ChildId.CompareTo(this.ChildId) == 0));
 }
示例#14
0
 public ShardKey(ShardKey <TRecord> key, TChild childRecordId)
 {
     _key     = key;
     _childId = childRecordId;
 }