示例#1
0
        /// <summary>
        /// <see cref="BobNodeClient"/> constructor
        /// </summary>
        /// <param name="innerClient">Low-level BobNodeClient</param>
        /// <param name="keySerializer">Serializer for <typeparamref name="TKey"/>  (null for default serializer)</param>
        /// <param name="keySerializationPoolSize">Size of the Key serialization pool (null - shared pool, 0 or less - pool is disabled, 1 or greater - custom pool with specified size)</param>
        protected internal BobNodeClient(BobNodeClient innerClient, BobKeySerializer <TKey> keySerializer, int?keySerializationPoolSize)
        {
            if (innerClient == null)
            {
                throw new ArgumentNullException(nameof(innerClient));
            }

            _innerClient = innerClient;

            if (keySerializer != null || BobDefaultKeySerializers.TryGetKeySerializer <TKey>(out keySerializer))
            {
                _keySerializer = keySerializer;
            }
            else
            {
                throw new ArgumentException($"KeySerializer is null and no default key serializer found for key type '{typeof(TKey).Name}'", nameof(keySerializer));
            }

            keySerializationPoolSize = keySerializationPoolSize ?? innerClient.ConnectionParameters.KeySerializationPoolSize;
            if (keySerializationPoolSize == null)
            {
                _keySerializationPool = SharedKeySerializationArrayPools.GetOrCreateSharedPool(_keySerializer);
            }
            else if (keySerializationPoolSize.Value > 0)
            {
                _keySerializationPool = new ByteArrayPool(_keySerializer.SerializedSize, keySerializationPoolSize.Value);
            }
            else
            {
                _keySerializationPool = null;
            }
        }
示例#2
0
        public void GetOrCreateTest(Type keyType)
        {
            void CoreLogic <TKey>()
            {
                BobKeySerializer <TKey> keySerializer = null;

                Assert.True(BobDefaultKeySerializers.TryGetKeySerializer <TKey>(out keySerializer));

                ByteArrayPool pool = SharedKeySerializationArrayPools.GetOrCreateSharedPool(keySerializer);

                Assert.NotNull(pool);
                Assert.True(pool.MaxElementCount > 0);
                Assert.Equal(keySerializer.SerializedSize, pool.ByteArrayLength);

                var pool2 = SharedKeySerializationArrayPools.GetOrCreateSharedPool(keySerializer);

                Assert.True(object.ReferenceEquals(pool, pool2));

                Assert.True(SharedKeySerializationArrayPools.TryGetSharedPool(keySerializer, out pool2));
                Assert.True(object.ReferenceEquals(pool, pool2));
            }

            var referenceDelegate = new Action(CoreLogic <int>);
            var typedDelegate     = (Action)Delegate.CreateDelegate(typeof(Action), referenceDelegate.Method.GetGenericMethodDefinition().MakeGenericMethod(keyType));

            typedDelegate();
        }