Ejemplo n.º 1
0
        private static Type GetTypeFromManifestName(Stream stream, DeserializerSession session)
        {
            var bytes   = stream.ReadLengthEncodedByteArray(session);
            var byteArr = ByteArrayKey.Create(bytes);

            return(TypeNameLookup.GetOrAdd(byteArr, b =>
            {
                var shortName = StringEx.FromUtf8Bytes(b.Bytes, 0, b.Bytes.Length);
#if NET45
                if (shortName.Contains("System.Private.CoreLib,%core%"))
                {
                    shortName = shortName.Replace("System.Private.CoreLib,%core%", "mscorlib,%core%");
                }
#endif
#if NETSTANDARD
                if (shortName.Contains("mscorlib,%core%"))
                {
                    shortName = shortName.Replace("mscorlib,%core%", "System.Private.CoreLib,%core%");
                }
#endif

                var typename = ToQualifiedAssemblyName(shortName);
                return Type.GetType(typename, true);
            }));
        }
Ejemplo n.º 2
0
        T IMessagePackFormatter <T> .Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }

            var byteArr = new ByteArrayKey(MessagePackBinary.ReadBytes(bytes, offset, out readSize));

            return((T)_byteTypeNameLookup.GetOrAdd(byteArr, b =>
            {
                var typename = TypeEx.ToQualifiedAssemblyName(MessagePackBinary.ReadString(byteArr.Bytes, 0, out _));
                _macroses.ForEach(x =>
                {
                    typename = typename.Replace(x.Value, x.Key);
                });

                var type = Type.GetType(typename, true);

                _typeByteNameLookup.TryAdd(type, b.Bytes); //add to reverse cache

                return type;
            }));
        }
Ejemplo n.º 3
0
 public override bool Equals(object obj)
 {
     if (obj != null && obj is ByteArrayKey)
     {
         ByteArrayKey e = (ByteArrayKey)obj;
         return(this.matches(e.key_, e.offset_, e.length_));
     }
     return(false);
 }
Ejemplo n.º 4
0
        private static Type GetTypeFromManifestName(Stream stream, DeserializerSession session)
        {
            var bytes   = stream.ReadLengthEncodedByteArray(session);
            var byteArr = ByteArrayKey.Create(bytes);

            return(TypeNameLookup.GetOrAdd(byteArr, b =>
            {
                var shortName = StringEx.FromUtf8Bytes(b.Bytes, 0, b.Bytes.Length);
                var typename = ToQualifiedAssemblyName(shortName);
                return Type.GetType(typename, true);
            }));
        }
        /// <summary>
        /// <para>Trying to update node data with optimistic concurrency strategy according to given <paramref name="request"/>.</para>
        /// <para>Check returned <see cref="UpdateDataResult"/> to see if operation was successful.</para>
        /// </summary>
        public static async Task <UpdateDataResult> UpdateDataAsync(this IZooKeeperClient zooKeeperClient, UpdateDataRequest request)
        {
            try
            {
                for (var i = 0; i < request.Attempts; i++)
                {
                    var readResult = await zooKeeperClient.GetDataAsync(new GetDataRequest(request.Path)).ConfigureAwait(false);

                    if (!readResult.IsSuccessful)
                    {
                        return(UpdateDataResult.Unsuccessful(readResult.Status, readResult.Path, readResult.Exception));
                    }

                    var newData = request.Update(readResult.Data);

                    if (ByteArrayKey.Equals(readResult.Data, newData))
                    {
                        return(UpdateDataResult.Successful(readResult.Path));
                    }

                    var setDataRequest = new SetDataRequest(request.Path, newData)
                    {
                        Version = readResult.Stat.Version
                    };

                    var updateResult = await zooKeeperClient.SetDataAsync(setDataRequest).ConfigureAwait(false);

                    if (updateResult.Status == ZooKeeperStatus.VersionsMismatch)
                    {
                        continue;
                    }

                    return(updateResult.IsSuccessful ? UpdateDataResult.Successful(updateResult.Path) : UpdateDataResult.Unsuccessful(updateResult.Status, updateResult.Path, updateResult.Exception));
                }

                return(UpdateDataResult.Unsuccessful(ZooKeeperStatus.VersionsMismatch, request.Path, null));
            }
            catch (Exception e)
            {
                return(UpdateDataResult.Unsuccessful(ZooKeeperStatus.UnknownError, request.Path, e));
            }
        }
Ejemplo n.º 6
0
        public void Should_works_correctly()
        {
            var random = new Random();

            var expected = new Dictionary <string, Guid>();
            var actual   = new Dictionary <ByteArrayKey, Guid>();

            for (var times = 1; times < 100; times++)
            {
                var bytes = new byte[100];
                random.NextBytes(bytes);

                for (var offset = 0; offset < bytes.Length; offset++)
                {
                    for (var length = 1; offset + length <= bytes.Length; length++)
                    {
                        var key   = new ByteArrayKey(bytes, offset, length);
                        var part  = string.Join(" ", bytes.Skip(offset).Take(length));
                        var value = Guid.NewGuid();

                        if (expected.ContainsKey(part))
                        {
                            actual.TryGetValue(key, out var oldValue).Should().BeTrue();
                            oldValue.Should().Be(expected[part]);

                            expected[part] = value;
                            actual[key]    = value;
                        }
                        else
                        {
                            expected.Add(part, value);
                            actual.Add(key, value);
                        }
                    }
                }
            }

            actual.Count.Should().Be(expected.Count);
        }
Ejemplo n.º 7
0
        private static Type GetTypeFromManifestName(Stream stream, DeserializerSession session)
        {
            var bytes   = stream.ReadLengthEncodedByteArray(session);
            var byteArr = ByteArrayKey.Create(bytes);

            return(TypeNameLookup.GetOrAdd(byteArr, b =>
            {
                var shortName = StringEx.FromUtf8Bytes(b.Bytes, 0, b.Bytes.Length);
                var typename = ToQualifiedAssemblyName(shortName);
                string assemblyName = typename.Split(',')[1].Trim();
                if (TypeDiscoveryFunc != null)
                {
                    string simpleTypeName = typename.Split(',')[0].Trim();
                    Type t = TypeDiscoveryFunc(assemblyName, simpleTypeName);
                    if (t != null)
                    {
                        return t;
                    }
                }

                return Type.GetType(typename, true);
            }));
        }
Ejemplo n.º 8
0
        private static Type GetTypeFromManifestName(Stream stream, DeserializerSession session)
        {
            var bytes   = stream.ReadLengthEncodedByteArray(session);
            var byteArr = ByteArrayKey.Create(bytes);

            return(TypeNameLookup.GetOrAdd(byteArr, b =>
            {
                var shortName = StringEx.FromUtf8Bytes(b.Bytes, 0, b.Bytes.Length);
                var overrides = session.Serializer.Options.CrossFrameworkPackageNameOverrides;

                var oldName = shortName;
                foreach (var adapter in overrides)
                {
                    shortName = adapter(shortName);
                    if (!ReferenceEquals(oldName, shortName))
                    {
                        break;
                    }
                }

                return LoadTypeByName(shortName);
            }));
        }
Ejemplo n.º 9
0
        public int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                return(MessagePackBinary.WriteNil(ref bytes, offset));
            }

            var stringAsBytes = _typeByteNameLookup.GetOrAdd(value, type =>
            {
                var shortName = type.GetShortAssemblyQualifiedName();
                _macroses.ForEach(x =>
                {
                    shortName = shortName.Replace(x.Key, x.Value);
                });

                var byteArr = new ByteArrayKey(MessagePackBinary.GetEncodedStringBytes(shortName));

                _byteTypeNameLookup.TryAdd(byteArr, type); //add to reverse cache

                return(byteArr.Bytes);
            });

            return(MessagePackBinary.WriteBytes(ref bytes, offset, stringAsBytes));
        }