Beispiel #1
0
        /// <summary>
        /// Read dictionary returned by GET_ALL operation.
        /// </summary>
        /// <param name="reader">Reader.</param>
        /// <returns>Dictionary.</returns>
        private static ICollection <ICacheEntry <TK, TV> > ReadGetAllDictionary(BinaryReader reader)
        {
            if (reader == null)
            {
                return(null);
            }

            IBinaryStream stream = reader.Stream;

            if (stream.ReadBool())
            {
                int size = stream.ReadInt();

                var res = new List <ICacheEntry <TK, TV> >(size);

                for (int i = 0; i < size; i++)
                {
                    TK key = reader.ReadObject <TK>();
                    TV val = reader.ReadObject <TV>();

                    res.Add(new CacheEntry <TK, TV>(key, val));
                }

                return(res);
            }
            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// Read dictionary returned by GET_ALL operation.
        /// </summary>
        /// <param name="reader">Reader.</param>
        /// <returns>Dictionary.</returns>
        private static IDictionary <TK, TV> ReadGetAllDictionary(BinaryReader reader)
        {
            if (reader == null)
            {
                return(null);
            }

            IBinaryStream stream = reader.Stream;

            if (stream.ReadBool())
            {
                int size = stream.ReadInt();

                IDictionary <TK, TV> res = new Dictionary <TK, TV>(size);

                for (int i = 0; i < size; i++)
                {
                    TK key = reader.ReadObject <TK>();
                    TV val = reader.ReadObject <TV>();

                    res[key] = val;
                }

                return(res);
            }
            return(null);
        }
Beispiel #3
0
        /** <inheritdoc /> */
        public void Update(IBinaryStream stream, Marshaller marshaller)
        {
            Debug.Assert(stream != null);
            Debug.Assert(marshaller != null);

            if (_stopped)
            {
                return;
            }

            var mode   = _keepBinary ? BinaryMode.ForceBinary : BinaryMode.Deserialize;
            var reader = marshaller.StartUnmarshal(stream, mode);

            var key    = reader.ReadObject <TK>();
            var hasVal = stream.ReadBool();

            if (hasVal)
            {
                var val  = reader.ReadObject <TV>();
                var part = stream.ReadInt();
                var ver  = new AffinityTopologyVersion(stream.ReadLong(), stream.ReadInt());

                _map[key] = new PlatformCacheEntry <TV>(val, GetBoxedAffinityTopologyVersion(ver), part);
            }
            else
            {
                PlatformCacheEntry <TV> unused;
                _map.TryRemove(key, out unused);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Initializes a new instance of <see cref="BinaryConfigurationClientInternal"/> class.
        /// </summary>
        public BinaryConfigurationClientInternal(IBinaryStream stream)
        {
            Debug.Assert(stream != null);

            _compactFooter  = stream.ReadBool();
            _nameMapperMode = (BinaryNameMapperMode)stream.ReadByte();
        }
Beispiel #5
0
        /// <summary>
        /// Converter for GET_BATCH operation.
        /// </summary>
        /// <param name="stream">Stream.</param>
        /// <returns>Result.</returns>
        protected T[] ConvertGetBatch(IBinaryStream stream)
        {
            var reader = _marsh.StartUnmarshal(stream, _keepBinary);

            var size = reader.ReadInt();

            lock (_syncRoot)
            {
                if (size == 0)
                {
                    _hasNext = false;
                    return(null);
                }

                var res = new T[size];

                for (var i = 0; i < size; i++)
                {
                    res[i] = _readFunc(reader);
                }

                _hasNext = stream.ReadBool();

                return(res);
            }
        }
        public ClientCacheAffinityAwarenessGroup(IBinaryStream stream)
        {
            // Whether this group is eligible for client-side partition awareness.
            var applicable = stream.ReadBool();

            var cachesCount = stream.ReadInt();

            _caches = new List <KeyValuePair <int, Dictionary <int, int> > >(cachesCount);

            for (var i = 0; i < cachesCount; i++)
            {
                var cacheId = stream.ReadInt();
                if (!applicable)
                {
                    _caches.Add(new KeyValuePair <int, Dictionary <int, int> >(cacheId, null));
                    continue;
                }

                var keyCfgCount = stream.ReadInt();
                Dictionary <int, int> keyCfgs = null;
                if (keyCfgCount > 0)
                {
                    keyCfgs = new Dictionary <int, int>(keyCfgCount);
                    for (var j = 0; j < keyCfgCount; j++)
                    {
                        keyCfgs[stream.ReadInt()] = stream.ReadInt();
                    }
                }

                _caches.Add(new KeyValuePair <int, Dictionary <int, int> >(cacheId, keyCfgs));
            }

            if (!applicable)
            {
                return;
            }

            var partMapSize = stream.ReadInt();

            _partitionMap = new List <KeyValuePair <Guid, List <int> > >(partMapSize);

            var reader = BinaryUtils.Marshaller.StartUnmarshal(stream);

            for (var i = 0; i < partMapSize; i++)
            {
                var nodeId = reader.ReadGuid();
                Debug.Assert(nodeId != null);

                var partCount = stream.ReadInt();
                var parts     = new List <int>(partCount);

                for (int j = 0; j < partCount; j++)
                {
                    parts.Add(stream.ReadInt());
                }

                _partitionMap.Add(new KeyValuePair <Guid, List <int> >(nodeId.Value, parts));
            }
        }
Beispiel #7
0
        /// <summary>
        /// Reads cache entry processor and related data from stream, executes it and returns the result.
        /// </summary>
        /// <param name="inOutStream">Stream.</param>
        /// <param name="grid">Grid.</param>
        /// <returns>CacheEntryProcessor result.</returns>
        private CacheEntryProcessorResultHolder ReadAndRunCacheEntryProcessor(IBinaryStream inOutStream,
                                                                              Ignite grid)
        {
            var marsh = grid.Marshaller;

            var key     = marsh.Unmarshal <object>(inOutStream);
            var val     = marsh.Unmarshal <object>(inOutStream);
            var isLocal = inOutStream.ReadBool();

            var holder = isLocal
                ? _handleRegistry.Get <CacheEntryProcessorHolder>(inOutStream.ReadLong(), true)
                : marsh.Unmarshal <CacheEntryProcessorHolder>(inOutStream);

            return(holder.Process(key, val, val != null, grid));
        }
Beispiel #8
0
        /// <summary>
        /// Reads results of InvokeAll operation.
        /// </summary>
        /// <typeparam name="T">The type of the result.</typeparam>
        /// <param name="inStream">Stream.</param>
        /// <returns>Results of InvokeAll operation.</returns>
        private IDictionary <TK, ICacheEntryProcessorResult <T> > ReadInvokeAllResults <T>(IBinaryStream inStream)
        {
            var count = inStream.ReadInt();

            if (count == -1)
            {
                return(null);
            }

            var results = new Dictionary <TK, ICacheEntryProcessorResult <T> >(count);

            for (var i = 0; i < count; i++)
            {
                var key = Unmarshal <TK>(inStream);

                var hasError = inStream.ReadBool();

                results[key] = hasError
                    ? new CacheEntryProcessorResult <T>(ReadException(inStream))
                    : new CacheEntryProcessorResult <T>(Unmarshal <T>(inStream));
            }

            return(results);
        }
Beispiel #9
0
        /// <summary>
        /// Tests the stream.
        /// </summary>
        private static unsafe void TestStream(IBinaryStream stream, bool sameArr, Action flush)
        {
            Action seek = () => Assert.AreEqual(0, stream.Seek(0, SeekOrigin.Begin));

            Action<Action, Func<object>, object> check = (write, read, expectedResult) =>
            {
                seek();
                write();
                flush();
                seek();
                Assert.AreEqual(expectedResult, read());
            };

            // Arrays.
            Assert.AreEqual(sameArr, stream.IsSameArray(stream.GetArray()));
            Assert.IsFalse(stream.IsSameArray(new byte[1]));
            Assert.IsFalse(stream.IsSameArray(stream.GetArrayCopy()));

            // byte*
            byte* bytes = stackalloc byte[10];
            *bytes = 1;
            *(bytes + 1) = 2;

            stream.Write(bytes, 2);
            Assert.AreEqual(2, stream.Position);
            flush();

            seek();
            Assert.AreEqual(sameArr ? 256 : 2, stream.Remaining);
            byte* bytes2 = stackalloc byte[2];
            stream.Read(bytes2, 2);
            Assert.AreEqual(1, *bytes2);
            Assert.AreEqual(2, *(bytes2 + 1));

            // char*
            seek();
            char* chars = stackalloc char[10];
            *chars = 'a';
            *(chars + 1) = 'b';

            Assert.AreEqual(2, stream.WriteString(chars, 2, 2, Encoding.ASCII));
            flush();

            seek();
            stream.Read(bytes2, 2);
            Assert.AreEqual('a', *bytes2);
            Assert.AreEqual('b', *(bytes2 + 1));

            // Others.
            check(() => stream.Write(new byte[] {3, 4, 5}, 1, 2), () => stream.ReadByteArray(2), new byte[] {4, 5});

            check(() => stream.WriteBool(true), () => stream.ReadBool(), true);
            check(() => stream.WriteBoolArray(new[] {true, false}), () => stream.ReadBoolArray(2), 
                new[] {true, false});

            check(() => stream.WriteByte(4), () => stream.ReadByte(), 4);
            check(() => stream.WriteByteArray(new byte[] {4, 5, 6}), () => stream.ReadByteArray(3), 
                new byte[] {4, 5, 6});

            check(() => stream.WriteChar('x'), () => stream.ReadChar(), 'x');
            check(() => stream.WriteCharArray(new[] {'a', 'b'}), () => stream.ReadCharArray(2), new[] {'a', 'b'});

            check(() => stream.WriteDouble(4), () => stream.ReadDouble(), 4d);
            check(() => stream.WriteDoubleArray(new[] {4d}), () => stream.ReadDoubleArray(1), new[] {4d});

            check(() => stream.WriteFloat(4), () => stream.ReadFloat(), 4f);
            check(() => stream.WriteFloatArray(new[] {4f}), () => stream.ReadFloatArray(1), new[] {4f});

            check(() => stream.WriteInt(4), () => stream.ReadInt(), 4);
            check(() => stream.WriteInt(0, 4), () => stream.ReadInt(), 4);
            check(() => stream.WriteIntArray(new[] {4}), () => stream.ReadIntArray(1), new[] {4});

            check(() => stream.WriteLong(4), () => stream.ReadLong(), 4L);
            check(() => stream.WriteLongArray(new[] {4L}), () => stream.ReadLongArray(1), new[] {4L});

            check(() => stream.WriteShort(4), () => stream.ReadShort(), (short)4);
            check(() => stream.WriteShortArray(new short[] {4}), () => stream.ReadShortArray(1), new short[] {4});
        }
        /// <summary>
        /// Tests the stream.
        /// </summary>
        private static unsafe void TestStream(IBinaryStream stream, bool sameArr, Action flush)
        {
            Action seek = () => Assert.AreEqual(0, stream.Seek(0, SeekOrigin.Begin));

            Action <Action, Func <object>, object> check = (write, read, expectedResult) =>
            {
                seek();
                write();
                flush();
                seek();
                Assert.AreEqual(expectedResult, read());
            };

            // Arrays.
            if (stream.CanGetArray)
            {
                Assert.AreEqual(sameArr, stream.IsSameArray(stream.GetArray()));
            }
            Assert.IsFalse(stream.IsSameArray(new byte[1]));
            Assert.IsFalse(stream.IsSameArray(stream.GetArrayCopy()));

            // byte*
            byte *bytes = stackalloc byte[10];

            *bytes = 1;
            *(bytes + 1) = 2;

            stream.Write(bytes, 2);
            Assert.AreEqual(2, stream.Position);

            var proc = new SumStreamProcessor();

            Assert.AreEqual(0, stream.Apply(proc, 0));
            Assert.AreEqual(1, stream.Apply(proc, 1));
            Assert.AreEqual(3, stream.Apply(proc, 2));

            flush();

            seek();
            Assert.AreEqual(sameArr ? 256 : 2, stream.Remaining);
            byte *bytes2 = stackalloc byte[2];

            stream.Read(bytes2, 2);
            Assert.AreEqual(1, *bytes2);
            Assert.AreEqual(2, *(bytes2 + 1));

            // char*
            seek();
            char *chars = stackalloc char[10];

            *chars = 'a';
            *(chars + 1) = 'b';

            Assert.AreEqual(2, stream.WriteString(chars, 2, 2, Encoding.ASCII));
            flush();

            seek();
            stream.Read(bytes2, 2);
            Assert.AreEqual('a', *bytes2);
            Assert.AreEqual('b', *(bytes2 + 1));

            // Others.
            check(() => stream.Write(new byte[] { 3, 4, 5 }, 1, 2), () => stream.ReadByteArray(2), new byte[] { 4, 5 });

            check(() => stream.WriteBool(true), () => stream.ReadBool(), true);
            check(() => stream.WriteBoolArray(new[] { true, false }), () => stream.ReadBoolArray(2),
                  new[] { true, false });

            check(() => stream.WriteByte(4), () => stream.ReadByte(), 4);
            check(() => stream.WriteByteArray(new byte[] { 4, 5, 6 }), () => stream.ReadByteArray(3),
                  new byte[] { 4, 5, 6 });

            check(() => stream.WriteChar('x'), () => stream.ReadChar(), 'x');
            check(() => stream.WriteCharArray(new[] { 'a', 'b' }), () => stream.ReadCharArray(2), new[] { 'a', 'b' });

            check(() => stream.WriteDouble(4), () => stream.ReadDouble(), 4d);
            check(() => stream.WriteDoubleArray(new[] { 4d }), () => stream.ReadDoubleArray(1), new[] { 4d });

            check(() => stream.WriteFloat(4), () => stream.ReadFloat(), 4f);
            check(() => stream.WriteFloatArray(new[] { 4f }), () => stream.ReadFloatArray(1), new[] { 4f });

            check(() => stream.WriteInt(4), () => stream.ReadInt(), 4);
            check(() => stream.WriteInt(0, 4), () => stream.ReadInt(), 4);
            check(() => stream.WriteIntArray(new[] { 4 }), () => stream.ReadIntArray(1), new[] { 4 });

            check(() => stream.WriteLong(4), () => stream.ReadLong(), 4L);
            check(() => stream.WriteLongArray(new[] { 4L }), () => stream.ReadLongArray(1), new[] { 4L });

            check(() => stream.WriteShort(4), () => stream.ReadShort(), (short)4);
            check(() => stream.WriteShortArray(new short[] { 4 }), () => stream.ReadShortArray(1), new short[] { 4 });
        }