From 052dc34277b95cd1d275b48a5e326d011e3c94f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?As=C4=B1m=20Arslan?= Date: Thu, 2 May 2019 17:17:27 +0300 Subject: [PATCH] performance improvement on serialization bits, codecs and lazyDictionary introduced --- .../GlobalSerializerSample.cs | 18 +- .../MapEntriesWithPredicateCodec.cs | 16 +- .../MapEntrySetCodec.cs | 16 +- .../MapGetAllCodec.cs | 31 +-- .../MapPutAllCodec.cs | 21 +- .../MapValuesWithPredicateCodec.cs | 2 +- .../BufferBuilder.cs | 4 +- .../ClientMessageBuilder.cs | 2 +- .../SafeBuffer.cs | 73 ++---- .../ClientMapNearCacheProxy.cs | 45 ++-- .../Hazelcast.Client.Proxy/ClientMapProxy.cs | 156 ++++++------ .../ClientMultiMapProxy.cs | 6 +- .../ClientReplicatedMapProxy.cs | 2 +- .../ByteArrayObjectDataInput.cs | 2 +- .../ByteArrayObjectDataOutput.cs | 73 +++--- Hazelcast.Net/Hazelcast.IO/Bits.cs | 183 +++++++-------- .../Hazelcast.NearCache/BaseNearCache.cs | 1 + Hazelcast.Net/Hazelcast.Net.Ext/ByteBuffer.cs | 2 +- .../Hazelcast.Util/AbstractLazyDictionary.cs | 222 ++++++++++++++++++ .../Hazelcast.Util/ReadOnlyLazyDictionary.cs | 57 +++++ .../Hazelcast.Util/ReadOnlyLazyEntrySet.cs | 98 ++++++++ .../Hazelcast.Util/ReadOnlyLazyList.cs | 75 +++--- .../Hazelcast.Util/ReadOnlyLazySet.cs | 2 +- Hazelcast.Net/Properties/AssemblyInfo.cs | 1 + .../AbstractLazyDictionaryTest.cs | 163 +++++++++++++ .../AggregatorAndProjectionTest.cs | 5 +- .../Hazelcast.Client.Test/ClientMapTest.cs | 2 +- .../ClientSslBaseTest.cs | 2 +- .../NonSmartRoutingTest.cs | 5 +- .../ReadOnlyLazyDictionaryTest.cs | 67 ++++++ .../ReadOnlyLazyEntrySetTest.cs | 58 +++++ .../ReadOnlyLazyListTest.cs | 27 ++- .../ReadOnlyLazySetTest.cs | 127 ++++------ start-rc.ps1 | 2 +- start-rc.sh | 2 +- 35 files changed, 1071 insertions(+), 497 deletions(-) create mode 100644 Hazelcast.Net/Hazelcast.Util/AbstractLazyDictionary.cs create mode 100644 Hazelcast.Net/Hazelcast.Util/ReadOnlyLazyDictionary.cs create mode 100644 Hazelcast.Net/Hazelcast.Util/ReadOnlyLazyEntrySet.cs create mode 100644 Hazelcast.Test/Hazelcast.Client.Test/AbstractLazyDictionaryTest.cs create mode 100644 Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazyDictionaryTest.cs create mode 100644 Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazyEntrySetTest.cs diff --git a/Hazelcast.Examples/Org.Website.Samples/GlobalSerializerSample.cs b/Hazelcast.Examples/Org.Website.Samples/GlobalSerializerSample.cs index 31a1d47453..9e72b19a77 100644 --- a/Hazelcast.Examples/Org.Website.Samples/GlobalSerializerSample.cs +++ b/Hazelcast.Examples/Org.Website.Samples/GlobalSerializerSample.cs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; using Hazelcast.Client; using Hazelcast.Config; using Hazelcast.IO; @@ -30,15 +32,19 @@ public void Destroy() { } - public void Write(IObjectDataOutput output, object obj) + public object Read(IObjectDataInput input) { - // out.write(MyFavoriteSerializer.serialize(obj)) + var formatter = new BinaryFormatter(); + var stream = new MemoryStream(input.ReadByteArray()); + return formatter.Deserialize(stream); } - - public object Read(IObjectDataInput input) + + public void Write(IObjectDataOutput output, object obj) { - // return MyFavoriteSerializer.deserialize(input); - return null; + var formatter = new BinaryFormatter(); + var stream = new MemoryStream(); + formatter.Serialize(stream, obj); + output.WriteByteArray(stream.GetBuffer()); } } diff --git a/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapEntriesWithPredicateCodec.cs b/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapEntriesWithPredicateCodec.cs index 093796251c..d3f5c30c87 100644 --- a/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapEntriesWithPredicateCodec.cs +++ b/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapEntriesWithPredicateCodec.cs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System.Collections.Concurrent; using System.Collections.Generic; using Hazelcast.Client.Protocol.Util; using Hazelcast.IO.Serialization; @@ -41,25 +42,16 @@ internal static ClientMessage EncodeRequest(string name, IData predicate) return clientMessage; } - internal class ResponseParameters + internal static void DecodeResponse(IClientMessage clientMessage, ConcurrentQueue> result) { - public IList> response; - } - - internal static ResponseParameters DecodeResponse(IClientMessage clientMessage) - { - var parameters = new ResponseParameters(); var responseSize = clientMessage.GetInt(); - var response = new List>(responseSize); for (var responseIndex = 0; responseIndex < responseSize; responseIndex++) { var responseItemKey = clientMessage.GetData(); var responseItemVal = clientMessage.GetData(); - var responseItem = new KeyValuePair(responseItemKey, responseItemVal); - response.Add(responseItem); + var responseItem = new KeyValuePair(responseItemKey, responseItemVal); + result.Enqueue(responseItem); } - parameters.response = response; - return parameters; } } } \ No newline at end of file diff --git a/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapEntrySetCodec.cs b/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapEntrySetCodec.cs index 1851c3ef2c..e5860b8a63 100644 --- a/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapEntrySetCodec.cs +++ b/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapEntrySetCodec.cs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System.Collections.Concurrent; using System.Collections.Generic; using Hazelcast.Client.Protocol.Util; using Hazelcast.IO.Serialization; @@ -39,25 +40,16 @@ internal static ClientMessage EncodeRequest(string name) return clientMessage; } - internal class ResponseParameters + internal static void DecodeResponse(IClientMessage clientMessage, ConcurrentQueue> result) { - public IList> response; - } - - internal static ResponseParameters DecodeResponse(IClientMessage clientMessage) - { - var parameters = new ResponseParameters(); var responseSize = clientMessage.GetInt(); - var response = new List>(responseSize); for (var responseIndex = 0; responseIndex < responseSize; responseIndex++) { var responseItemKey = clientMessage.GetData(); var responseItemVal = clientMessage.GetData(); - var responseItem = new KeyValuePair(responseItemKey, responseItemVal); - response.Add(responseItem); + var responseItem = new KeyValuePair(responseItemKey, responseItemVal); + result.Enqueue(responseItem); } - parameters.response = response; - return parameters; } } } \ No newline at end of file diff --git a/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapGetAllCodec.cs b/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapGetAllCodec.cs index f6420d1302..b004b62aef 100644 --- a/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapGetAllCodec.cs +++ b/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapGetAllCodec.cs @@ -12,6 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; +using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; using Hazelcast.Client.Protocol.Util; using Hazelcast.IO; @@ -22,19 +25,19 @@ namespace Hazelcast.Client.Protocol.Codec { internal static class MapGetAllCodec { - private static int CalculateRequestDataSize(string name, IList keys) + private static int CalculateRequestDataSize(string name, ArrayList keys) { var dataSize = ClientMessage.HeaderSize; dataSize += ParameterUtil.CalculateDataSize(name); dataSize += Bits.IntSizeInBytes; - foreach (var keysItem in keys) + for (int i = 0; i < keys.Count; i++) { - dataSize += ParameterUtil.CalculateDataSize(keysItem); + dataSize += ParameterUtil.CalculateDataSize((IData)keys[i]); } return dataSize; } - internal static ClientMessage EncodeRequest(string name, IList keys) + internal static ClientMessage EncodeRequest(string name, ArrayList keys) { var requiredDataSize = CalculateRequestDataSize(name, keys); var clientMessage = ClientMessage.CreateForEncode(requiredDataSize); @@ -42,33 +45,25 @@ internal static ClientMessage EncodeRequest(string name, IList keys) clientMessage.SetRetryable(false); clientMessage.Set(name); clientMessage.Set(keys.Count); - foreach (var keysItem in keys) + for (int i = 0; i < keys.Count; i++) { - clientMessage.Set(keysItem); + clientMessage.Set((IData)keys[i]); } clientMessage.UpdateFrameLength(); return clientMessage; } - internal class ResponseParameters + internal static void DecodeResponse(IClientMessage clientMessage, ConcurrentQueue> result) { - public IList> response; - } - - internal static ResponseParameters DecodeResponse(IClientMessage clientMessage) - { - var parameters = new ResponseParameters(); var responseSize = clientMessage.GetInt(); - var response = new List>(responseSize); for (var responseIndex = 0; responseIndex < responseSize; responseIndex++) { var responseItemKey = clientMessage.GetData(); var responseItemVal = clientMessage.GetData(); - var responseItem = new KeyValuePair(responseItemKey, responseItemVal); - response.Add(responseItem); + + var responseItem = new KeyValuePair(responseItemKey, responseItemVal); + result.Enqueue(responseItem); } - parameters.response = response; - return parameters; } } } \ No newline at end of file diff --git a/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapPutAllCodec.cs b/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapPutAllCodec.cs index 56976260a2..4fd04968f9 100644 --- a/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapPutAllCodec.cs +++ b/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapPutAllCodec.cs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; using Hazelcast.Client.Protocol.Util; using Hazelcast.IO; @@ -22,35 +24,30 @@ namespace Hazelcast.Client.Protocol.Codec { internal static class MapPutAllCodec { - private static int CalculateRequestDataSize(string name, IList> entries) + private static int CalculateRequestDataSize(string name, ArrayList entries) { var dataSize = ClientMessage.HeaderSize; dataSize += ParameterUtil.CalculateDataSize(name); dataSize += Bits.IntSizeInBytes; foreach (var entriesItem in entries) { - var entriesItemKey = entriesItem.Key; - var entriesItemVal = entriesItem.Value; - dataSize += ParameterUtil.CalculateDataSize(entriesItemKey); - dataSize += ParameterUtil.CalculateDataSize(entriesItemVal); + dataSize += ParameterUtil.CalculateDataSize((IData)entriesItem); } return dataSize; } - internal static ClientMessage EncodeRequest(string name, IList> entries) + internal static ClientMessage EncodeRequest(string name, ArrayList entries) { var requiredDataSize = CalculateRequestDataSize(name, entries); var clientMessage = ClientMessage.CreateForEncode(requiredDataSize); clientMessage.SetMessageType((int) MapMessageType.MapPutAll); clientMessage.SetRetryable(false); clientMessage.Set(name); - clientMessage.Set(entries.Count); - foreach (var entriesItem in entries) + clientMessage.Set(entries.Count / 2); + for (int i = 0; i < entries.Count; i+=2) { - var entriesItemKey = entriesItem.Key; - var entriesItemVal = entriesItem.Value; - clientMessage.Set(entriesItemKey); - clientMessage.Set(entriesItemVal); + clientMessage.Set((IData)entries[i]); + clientMessage.Set((IData)entries[i+1]); } clientMessage.UpdateFrameLength(); return clientMessage; diff --git a/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapValuesWithPredicateCodec.cs b/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapValuesWithPredicateCodec.cs index 42ac881c6f..b71ce07d0d 100644 --- a/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapValuesWithPredicateCodec.cs +++ b/Hazelcast.Net/Hazelcast.Client.Protocol.Codec/MapValuesWithPredicateCodec.cs @@ -43,7 +43,7 @@ internal static ClientMessage EncodeRequest(string name, IData predicate) internal class ResponseParameters { - public IList response; + public List response; } internal static ResponseParameters DecodeResponse(IClientMessage clientMessage) diff --git a/Hazelcast.Net/Hazelcast.Client.Protocol.Util/BufferBuilder.cs b/Hazelcast.Net/Hazelcast.Client.Protocol.Util/BufferBuilder.cs index a6c8a6fa40..735fcb74f1 100644 --- a/Hazelcast.Net/Hazelcast.Client.Protocol.Util/BufferBuilder.cs +++ b/Hazelcast.Net/Hazelcast.Client.Protocol.Util/BufferBuilder.cs @@ -68,7 +68,7 @@ public virtual BufferBuilder Append(IClientProtocolBuffer srcBuffer, int srcOffs /// /// that encapsulates the internal buffer. /// - public virtual IClientProtocolBuffer Buffer() + public virtual IClientProtocolBuffer ProtocolBuffer() { return _protocolBuffer; } @@ -100,7 +100,7 @@ private void EnsureCapacity(int additionalCapacity) { var newCapacity = QuickMath.NextPowerOfTwo(requiredCapacity); var newBuffer = new byte[newCapacity]; - Array.Copy(_protocolBuffer.ByteArray(), 0, newBuffer, 0, _capacity); + Buffer.BlockCopy(_protocolBuffer.ByteArray(), 0, newBuffer, 0, _capacity); _capacity = newCapacity; _protocolBuffer.Wrap(newBuffer); } diff --git a/Hazelcast.Net/Hazelcast.Client.Protocol.Util/ClientMessageBuilder.cs b/Hazelcast.Net/Hazelcast.Client.Protocol.Util/ClientMessageBuilder.cs index 89789a6edc..c21671e6ac 100644 --- a/Hazelcast.Net/Hazelcast.Client.Protocol.Util/ClientMessageBuilder.cs +++ b/Hazelcast.Net/Hazelcast.Client.Protocol.Util/ClientMessageBuilder.cs @@ -74,7 +74,7 @@ public virtual void OnData(ByteBuffer buffer) if (_message.IsFlagSet(ClientMessage.EndFlag)) { var msgLength = builder.Position(); - var cm = ClientMessage.CreateForDecode(builder.Buffer(), 0); + var cm = ClientMessage.CreateForDecode(builder.ProtocolBuffer(), 0); cm.SetFrameLength(msgLength); //HANDLE-MESSAGE HandleMessage(cm); diff --git a/Hazelcast.Net/Hazelcast.Client.Protocol.Util/SafeBuffer.cs b/Hazelcast.Net/Hazelcast.Client.Protocol.Util/SafeBuffer.cs index 55052badbf..c14f1c3c16 100644 --- a/Hazelcast.Net/Hazelcast.Client.Protocol.Util/SafeBuffer.cs +++ b/Hazelcast.Net/Hazelcast.Client.Protocol.Util/SafeBuffer.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. +// Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -25,7 +25,6 @@ namespace Hazelcast.Client.Protocol.Util /// internal class SafeBuffer : IClientProtocolBuffer { - private const bool ShouldBoundsCheck = true; private byte[] _byteArray; public SafeBuffer(byte[] buffer) @@ -33,44 +32,42 @@ public SafeBuffer(byte[] buffer) Wrap(buffer); } - public virtual void PutLong(int index, long value) + public void PutLong(int index, long value) { Bits.WriteLongL(_byteArray, index, value); } - public virtual void PutInt(int index, int value) + public void PutInt(int index, int value) { Bits.WriteIntL(_byteArray, index, value); } - public virtual void PutShort(int index, short value) + public void PutShort(int index, short value) { Bits.WriteShortL(_byteArray, index, value); } - public virtual void PutByte(int index, byte value) + public void PutByte(int index, byte value) { _byteArray[index] = value; } - public virtual void PutBytes(int index, byte[] src) + public void PutBytes(int index, byte[] src) { PutBytes(index, src, 0, src.Length); } - public virtual void PutBytes(int index, byte[] src, int offset, int length) + public void PutBytes(int index, byte[] src, int offset, int length) { - BoundsCheck(index, length); - BoundsCheck(src, offset, length); - Array.Copy(src, offset, _byteArray, index, length); + Buffer.BlockCopy(src, offset, _byteArray, index, length); } - public virtual int PutStringUtf8(int index, string value) + public int PutStringUtf8(int index, string value) { return PutStringUtf8(index, value, int.MaxValue); } - public virtual int PutStringUtf8(int index, string value, int maxEncodedSize) + public int PutStringUtf8(int index, string value, int maxEncodedSize) { var bytes = Encoding.UTF8.GetBytes(value); if (bytes.Length > maxEncodedSize) @@ -92,78 +89,46 @@ public byte[] ByteArray() return _byteArray; } - public virtual int Capacity() + public int Capacity() { return _byteArray.Length; } - public virtual long GetLong(int index) + public long GetLong(int index) { - BoundsCheck(index, Bits.LongSizeInBytes); return Bits.ReadLongL(_byteArray, index); } - public virtual int GetInt(int index) + public int GetInt(int index) { - BoundsCheck(index, Bits.IntSizeInBytes); return Bits.ReadIntL(_byteArray, index); } - public virtual short GetShort(int index) + public short GetShort(int index) { - BoundsCheck(index, Bits.ShortSizeInBytes); return Bits.ReadShortL(_byteArray, index); } - public virtual byte GetByte(int index) + public byte GetByte(int index) { - BoundsCheck(index, Bits.ByteSizeInBytes); return _byteArray[index]; } - public virtual void GetBytes(int index, byte[] dst) + public void GetBytes(int index, byte[] dst) { GetBytes(index, dst, 0, dst.Length); } - public virtual void GetBytes(int index, byte[] dst, int offset, int length) + public void GetBytes(int index, byte[] dst, int offset, int length) { - BoundsCheck(index, length); - BoundsCheck(dst, offset, length); - Array.Copy(_byteArray, offset + index, dst, offset, length); + Buffer.BlockCopy(_byteArray, offset + index, dst, offset, length); } - public virtual string GetStringUtf8(int offset, int length) + public string GetStringUtf8(int offset, int length) { var stringInBytes = new byte[length]; GetBytes(offset + Bits.IntSizeInBytes, stringInBytes); return Encoding.UTF8.GetString(stringInBytes); } - - /////////////////////////////////////////////////////////////////////////// - public void BoundsCheck(int index, int length) - { - if (ShouldBoundsCheck) - { - if (index < 0 || length < 0 || (index + length) > Capacity()) - { - throw new IndexOutOfRangeException(string.Format("index=%d, length=%d, capacity=%d", index, length, - Capacity())); - } - } - } - - private static void BoundsCheck(byte[] buffer, int index, int length) - { - if (ShouldBoundsCheck) - { - var capacity = buffer.Length; - if (index < 0 || length < 0 || (index + length) > capacity) - { - throw new IndexOutOfRangeException(string.Format("index=%d, length=%d, capacity=%d", index, length, - capacity)); - } - } - } } } \ No newline at end of file diff --git a/Hazelcast.Net/Hazelcast.Client.Proxy/ClientMapNearCacheProxy.cs b/Hazelcast.Net/Hazelcast.Client.Proxy/ClientMapNearCacheProxy.cs index 0affb50c4d..0c91539784 100644 --- a/Hazelcast.Net/Hazelcast.Client.Proxy/ClientMapNearCacheProxy.cs +++ b/Hazelcast.Net/Hazelcast.Client.Proxy/ClientMapNearCacheProxy.cs @@ -13,6 +13,8 @@ // limitations under the License. using System; +using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading.Tasks; using Hazelcast.Core; @@ -256,31 +258,30 @@ protected override object ExecuteOnKeyInternal(IData keyData, IEntryProcessor en } } - protected override void GetAllInternal(ICollection keyDatas, List resultingKeyValuePairs) + protected override void GetAllInternal(ArrayList partitionToKeyData, ConcurrentQueue> resultingKeyValuePairs) { - var list = new List(keyDatas); - foreach (var keyData in keyDatas) + Parallel.For(0, partitionToKeyData.Count, partitionId => { - object value; - if (_nearCache.TryGetValue(keyData, out value)) + var keyList = (ArrayList)partitionToKeyData[partitionId]; + for (int i = keyList.Count-1; i > -1; i--) { - list.Remove(keyData); - resultingKeyValuePairs.Add(keyData); - resultingKeyValuePairs.Add(value); + var keyData = (IData)keyList[i]; + object value; + if (_nearCache.TryGetValue(keyData, out value)) + { + keyList.RemoveAt(i); + resultingKeyValuePairs.Enqueue(new KeyValuePair(keyData, value)); + } } - } - base.GetAllInternal(list, resultingKeyValuePairs); - - for (var i = 0; i < resultingKeyValuePairs.Count;) + }); + base.GetAllInternal(partitionToKeyData, resultingKeyValuePairs); + foreach (var kvp in resultingKeyValuePairs) { - var keyData = (IData) resultingKeyValuePairs[i++]; - var value = resultingKeyValuePairs[i++]; - _nearCache.TryAdd(keyData, value); + _nearCache.TryAdd(kvp.Key, kvp.Value); } } - protected override void PutAllInternal(IDictionary map, - Dictionary> partitions) + protected override void PutAllInternal(IDictionary map, ArrayList partitions) { try { @@ -288,13 +289,15 @@ protected override void PutAllInternal(IDictionary map, } finally { - foreach (var partitionsValue in partitions.Values) + Parallel.For(0, partitions.Count, partitionId => { - foreach (var keyData in partitionsValue.Keys) + var entries = (ArrayList)partitions[partitionId]; + var entryCount = entries.Count / 2; + for (var k = 0; k < entryCount; k += 2) { - _nearCache.Invalidate(keyData); + _nearCache.Invalidate((IData)entries[k]); } - } + }); } } } diff --git a/Hazelcast.Net/Hazelcast.Client.Proxy/ClientMapProxy.cs b/Hazelcast.Net/Hazelcast.Client.Proxy/ClientMapProxy.cs index 22352516b8..d37ec98e08 100644 --- a/Hazelcast.Net/Hazelcast.Client.Proxy/ClientMapProxy.cs +++ b/Hazelcast.Net/Hazelcast.Client.Proxy/ClientMapProxy.cs @@ -13,8 +13,11 @@ // limitations under the License. using System; +using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Hazelcast.Client.Protocol; using Hazelcast.Client.Protocol.Codec; @@ -59,7 +62,7 @@ public ICollection Project(IProjection projection) var request = MapProjectCodec.EncodeRequest(GetName(), ToData(projection)); var response = Invoke(request); var resultParameters = MapProjectCodec.DecodeResponse(response); - return new ReadOnlyLazyList(resultParameters.response, GetContext().GetSerializationService()); + return new ReadOnlyLazyList(resultParameters.response, GetContext().GetSerializationService()); } public ICollection Project(IProjection projection, IPredicate predicate) @@ -69,7 +72,7 @@ public ICollection Project(IProjection projection, IPredicate var request = MapProjectWithPredicateCodec.EncodeRequest(GetName(), ToData(projection), ToData(predicate)); var response = Invoke(request); var resultParameters = MapProjectWithPredicateCodec.DecodeResponse(response); - return new ReadOnlyLazyList(resultParameters.response, GetContext().GetSerializationService()); + return new ReadOnlyLazyList(resultParameters.response, GetContext().GetSerializationService()); } public bool ContainsKey(object key) @@ -118,7 +121,7 @@ public TValue Put(TKey key, TValue value) public TValue Remove(object key) { ValidationUtil.CheckNotNull(key, ValidationUtil.NULL_KEY_IS_NOT_ALLOWED); - var keyData = ToData(key); + var keyData = ToData(key); return RemoveInternal(keyData); } @@ -656,69 +659,56 @@ public IDictionary GetAll(ICollection keys) { if (keys == null || keys.Count == 0) return new Dictionary(); - var keysSize = keys.Count; - var resultingKeyValuePairs = new List(keysSize * 2); - - var keyDatas = new List(); - foreach (var key in keys) + var resultingKeyValuePairs = new ConcurrentQueue>(); + try { - var keyData = ToData(key); - keyDatas.Add(keyData); + var partitionToKeyData = GetPartitionKeyData(keys); + GetAllInternal(partitionToKeyData, resultingKeyValuePairs); } - GetAllInternal(keyDatas, resultingKeyValuePairs); - - var result = new Dictionary(keysSize); - for (var i = 0; i < resultingKeyValuePairs.Count;) + catch (Exception e) { - var key = ToObject((IData) resultingKeyValuePairs[i++]); - var value = ToObject((IData) resultingKeyValuePairs[i++]); - result.Add(key, value); + ExceptionUtil.Rethrow(e); } - return result; + return new ReadOnlyLazyDictionary(resultingKeyValuePairs, GetContext().GetSerializationService()); } - protected virtual void GetAllInternal(ICollection keyDatas, List resultingKeyValuePairs) + protected virtual void GetAllInternal(ArrayList partitionToKeyData, + ConcurrentQueue> resultingKeyValuePairs) { - var partitionToKeyData = GetPartitionKeyData(keyDatas); var invocationService = GetContext().GetInvocationService(); - var futures = new List>(partitionToKeyData.Count); - foreach (var kvp in partitionToKeyData) + var futures = new ConcurrentQueue>(); + Parallel.For(0, partitionToKeyData.Count, partitionId => { - var partitionId = kvp.Key; - var keyList = kvp.Value; + var keyList = (ArrayList) partitionToKeyData[partitionId]; if (keyList.Count > 0) { var request = MapGetAllCodec.EncodeRequest(GetName(), keyList); - futures.Add(invocationService.InvokeOnPartition(request, partitionId)); + futures.Enqueue(invocationService.InvokeOnPartition(request, partitionId)); } - } + }); var messages = ThreadUtil.GetResult(futures); - foreach (var clientMessage in messages) - { - var items = MapGetAllCodec.DecodeResponse(clientMessage).response; - foreach (var entry in items) - { - resultingKeyValuePairs.Add(entry.Key); - resultingKeyValuePairs.Add(entry.Value); - } - } + Parallel.ForEach(messages, + clientMessage => { MapGetAllCodec.DecodeResponse(clientMessage, resultingKeyValuePairs); }); } - private Dictionary> GetPartitionKeyData(ICollection keyDatas) + private ArrayList GetPartitionKeyData(ICollection keys) { var partitionService = GetContext().GetPartitionService(); - // split the keys based on which partition they belong - var partitionToKeyData = new Dictionary>(); - foreach (var keyData in keyDatas) + var partitionCount = partitionService.GetPartitionCount(); + var initialCapacity = 2 * (keys.Count / partitionCount); + var partitionToKeyData = new ArrayList(partitionCount); + for (var i = 0; i < partitionCount; i++) { + partitionToKeyData.Add(ArrayList.Synchronized(new ArrayList(initialCapacity))); + } + Parallel.ForEach(keys, key => + { + ValidationUtil.CheckNotNull(key, ValidationUtil.NULL_KEY_IS_NOT_ALLOWED); + var keyData = ToData(key); var partitionId = partitionService.GetPartitionId(keyData); - IList keyList; - if (!partitionToKeyData.TryGetValue(partitionId, out keyList)) - { - partitionToKeyData[partitionId] = keyList = new List(); - } + var keyList = (ArrayList) partitionToKeyData[partitionId]; keyList.Add(keyData); - } + }); return partitionToKeyData; } @@ -726,21 +716,17 @@ public ICollection Values() { var request = MapValuesCodec.EncodeRequest(GetName()); var list = Invoke(request, m => MapValuesCodec.DecodeResponse(m).response); - return new ReadOnlyLazyList(list, GetContext().GetSerializationService()); + return new ReadOnlyLazyList(list, GetContext().GetSerializationService()); } public ISet> EntrySet() { var request = MapEntrySetCodec.EncodeRequest(GetName()); - var entries = Invoke(request, m => MapEntrySetCodec.DecodeResponse(m).response); - ISet> entrySet = new HashSet>(); - foreach (var entry in entries) - { - var key = ToObject(entry.Key); - var value = ToObject(entry.Value); - entrySet.Add(new KeyValuePair(key, value)); - } - return entrySet; + var response = Invoke(request); + + var result = new ConcurrentQueue>(); + MapEntrySetCodec.DecodeResponse(response, result); + return new ReadOnlyLazyEntrySet(result, GetContext().GetSerializationService()); } public void AddIndex(string attribute, bool ordered) @@ -769,36 +755,39 @@ public bool IsEmpty() public void PutAll(IDictionary m) { var partitionService = GetContext().GetPartitionService(); - var partitions = new Dictionary>(partitionService.GetPartitionCount()); - foreach (var kvp in m) + var partitionCount = partitionService.GetPartitionCount(); + var partitions = new ArrayList(partitionCount); + for (var i = 0; i < partitionCount; i++) + { + partitions.Add(ArrayList.Synchronized(new ArrayList())); + } + Parallel.ForEach(m, kvp => { ValidationUtil.CheckNotNull(kvp.Key, ValidationUtil.NULL_KEY_IS_NOT_ALLOWED); ValidationUtil.CheckNotNull(kvp.Value, ValidationUtil.NULL_VALUE_IS_NOT_ALLOWED); - var keyData = ToData(kvp.Key); + var valueData = ToData(kvp.Value); var partitionId = partitionService.GetPartitionId(keyData); - IDictionary partition; - if (!partitions.TryGetValue(partitionId, out partition)) - { - partition = new Dictionary(); - partitions[partitionId] = partition; - } - - partition[keyData] = ToData(kvp.Value); - } + var partition = (ArrayList) partitions[partitionId]; + partition.Add(keyData); + partition.Add(valueData); + }); PutAllInternal(m, partitions); } - protected virtual void PutAllInternal(IDictionary map, - Dictionary> partitions) + protected virtual void PutAllInternal(IDictionary map, ArrayList partitions) { - var futures = new List>(partitions.Count); - foreach (var kvp in partitions) + var futures = new ConcurrentQueue>(); + Parallel.For(0, partitions.Count, i => { - var request = MapPutAllCodec.EncodeRequest(GetName(), kvp.Value.ToList()); - var future = GetContext().GetInvocationService().InvokeOnPartition(request, kvp.Key); - futures.Add(future); - } + var entries = (ArrayList) partitions[i]; + if (entries.Count > 0) + { + var request = MapPutAllCodec.EncodeRequest(GetName(), entries); + var future = GetContext().GetInvocationService().InvokeOnPartition(request, i); + futures.Enqueue(future); + } + }); ThreadUtil.GetResult(futures); } @@ -825,17 +814,14 @@ public ISet> EntrySet(IPredicate predicate) { return EntrySetWithPagingPredicate((PagingPredicate) predicate); } - var request = MapEntriesWithPredicateCodec.EncodeRequest(GetName(), ToData(predicate)); - var entries = Invoke(request, predicate, m => MapEntriesWithPredicateCodec.DecodeResponse(m).response); - ISet> entrySet = new HashSet>(); - foreach (var dataEntry in entries) + var entries = Invoke(request, predicate, (response) => { - var key = ToObject(dataEntry.Key); - var value = ToObject(dataEntry.Value); - entrySet.Add(new KeyValuePair(key, value)); - } - return entrySet; + var resultQueue = new ConcurrentQueue>(); + MapEntriesWithPredicateCodec.DecodeResponse(response, resultQueue); + return resultQueue; + }); + return new ReadOnlyLazyEntrySet(entries, GetContext().GetSerializationService()); } public ICollection Values(IPredicate predicate) @@ -847,7 +833,7 @@ public ICollection Values(IPredicate predicate) var request = MapValuesWithPredicateCodec.EncodeRequest(GetName(), ToData(predicate)); var result = Invoke(request, predicate, m => MapValuesWithPredicateCodec.DecodeResponse(m).response); - return new ReadOnlyLazyList(result, GetContext().GetSerializationService()); + return new ReadOnlyLazyList(result, GetContext().GetSerializationService()); } private T Invoke(IClientMessage request, IPredicate predicate, Func decodeResponse) diff --git a/Hazelcast.Net/Hazelcast.Client.Proxy/ClientMultiMapProxy.cs b/Hazelcast.Net/Hazelcast.Client.Proxy/ClientMultiMapProxy.cs index 6c26f2664d..5e435d3ab1 100644 --- a/Hazelcast.Net/Hazelcast.Client.Proxy/ClientMultiMapProxy.cs +++ b/Hazelcast.Net/Hazelcast.Client.Proxy/ClientMultiMapProxy.cs @@ -45,7 +45,7 @@ public virtual ICollection Get(TKey key) var keyData = ToData(key); var request = MultiMapGetCodec.EncodeRequest(GetName(), keyData, ThreadUtil.GetThreadId()); var list = Invoke(request, keyData, m => MultiMapGetCodec.DecodeResponse(m).response); - return new ReadOnlyLazyList(list, GetContext().GetSerializationService()); + return new ReadOnlyLazyList(list, GetContext().GetSerializationService()); } public virtual bool Remove(object key, object value) @@ -63,7 +63,7 @@ public virtual ICollection Remove(object key) var request = MultiMapRemoveCodec.EncodeRequest(GetName(), keyData, ThreadUtil.GetThreadId()); var list = Invoke(request, keyData, m => MultiMapRemoveCodec.DecodeResponse(m).response); - return new ReadOnlyLazyList(list, GetContext().GetSerializationService()); + return new ReadOnlyLazyList(list, GetContext().GetSerializationService()); } public virtual ISet KeySet() @@ -77,7 +77,7 @@ public virtual ICollection Values() { var request = MultiMapValuesCodec.EncodeRequest(GetName()); var list = Invoke(request, m => MultiMapValuesCodec.DecodeResponse(m).response); - return new ReadOnlyLazyList(list, GetContext().GetSerializationService()); + return new ReadOnlyLazyList(list, GetContext().GetSerializationService()); } public virtual ISet> EntrySet() diff --git a/Hazelcast.Net/Hazelcast.Client.Proxy/ClientReplicatedMapProxy.cs b/Hazelcast.Net/Hazelcast.Client.Proxy/ClientReplicatedMapProxy.cs index 5b6fc265a8..4db38d163e 100644 --- a/Hazelcast.Net/Hazelcast.Client.Proxy/ClientReplicatedMapProxy.cs +++ b/Hazelcast.Net/Hazelcast.Client.Proxy/ClientReplicatedMapProxy.cs @@ -211,7 +211,7 @@ public ICollection Values() { var request = ReplicatedMapValuesCodec.EncodeRequest(GetName()); var list = InvokeOnPartition(request, _targetPartitionId, m => ReplicatedMapValuesCodec.DecodeResponse(m).response); - return new ReadOnlyLazyList(list, GetContext().GetSerializationService()); + return new ReadOnlyLazyList(list, GetContext().GetSerializationService()); } private void OnEntryEvent(IData keyData, IData valueData, IData oldValueData, IData mergingValue, diff --git a/Hazelcast.Net/Hazelcast.IO.Serialization/ByteArrayObjectDataInput.cs b/Hazelcast.Net/Hazelcast.IO.Serialization/ByteArrayObjectDataInput.cs index 1ce328af9b..486ae22238 100644 --- a/Hazelcast.Net/Hazelcast.IO.Serialization/ByteArrayObjectDataInput.cs +++ b/Hazelcast.Net/Hazelcast.IO.Serialization/ByteArrayObjectDataInput.cs @@ -745,7 +745,7 @@ public int Read(byte[] b, int off, int len) { len = Size - Pos; } - Array.Copy(Data, Pos, b, off, len); + Buffer.BlockCopy(Data, Pos, b, off, len); Pos += len; return len; } diff --git a/Hazelcast.Net/Hazelcast.IO.Serialization/ByteArrayObjectDataOutput.cs b/Hazelcast.Net/Hazelcast.IO.Serialization/ByteArrayObjectDataOutput.cs index 009641dae0..1c77c10c65 100644 --- a/Hazelcast.Net/Hazelcast.IO.Serialization/ByteArrayObjectDataOutput.cs +++ b/Hazelcast.Net/Hazelcast.IO.Serialization/ByteArrayObjectDataOutput.cs @@ -23,22 +23,27 @@ internal class ByteArrayObjectDataOutput : IOutputStream, IBufferObjectDataOutpu private readonly int _initialSize; private readonly bool _isBigEndian; private readonly ISerializationService _service; + private byte[] _buffer; internal ByteArrayObjectDataOutput(int size, ISerializationService service, ByteOrder byteOrder) { _initialSize = size; - Buffer = new byte[size]; + _buffer = new byte[size]; _service = service; _isBigEndian = byteOrder == ByteOrder.BigEndian; } - internal byte[] Buffer { get; set; } + internal byte[] Buffer + { + get { return _buffer; } + set { _buffer = value; } + } internal int Pos { get; set; } public virtual void Write(int position, int b) { - Buffer[position] = unchecked((byte) b); + _buffer[position] = unchecked((byte) b); } /// @@ -80,7 +85,7 @@ public virtual void WriteBytes(string s) EnsureAvailable(len); for (var i = 0; i < len; i++) { - Buffer[Pos++] = unchecked((byte) s[i]); + _buffer[Pos++] = unchecked((byte) s[i]); } } @@ -88,14 +93,14 @@ public virtual void WriteBytes(string s) public virtual void WriteChar(int v) { EnsureAvailable(Bits.CharSizeInBytes); - Bits.WriteChar(Buffer, Pos, (char) v, _isBigEndian); + Bits.WriteChar(_buffer, Pos, (char) v, _isBigEndian); Pos += Bits.CharSizeInBytes; } /// public virtual void WriteChar(int position, int v) { - Bits.WriteChar(Buffer, position, (char) v, _isBigEndian); + Bits.WriteChar(_buffer, position, (char) v, _isBigEndian); } /// @@ -159,78 +164,78 @@ public void WriteFloat(int position, float v, ByteOrder byteOrder) public virtual void WriteInt(int v) { EnsureAvailable(Bits.IntSizeInBytes); - Bits.WriteInt(Buffer, Pos, v, _isBigEndian); + Bits.WriteInt(_buffer, Pos, v, _isBigEndian); Pos += Bits.IntSizeInBytes; } /// public virtual void WriteInt(int position, int v) { - Bits.WriteInt(Buffer, position, v, _isBigEndian); + Bits.WriteInt(_buffer, position, v, _isBigEndian); } public void WriteInt(int v, ByteOrder byteOrder) { EnsureAvailable(Bits.IntSizeInBytes); - Bits.WriteInt(Buffer, Pos, v, byteOrder == ByteOrder.BigEndian); + Bits.WriteInt(_buffer, Pos, v, byteOrder == ByteOrder.BigEndian); Pos += Bits.IntSizeInBytes; } public void WriteInt(int position, int v, ByteOrder byteOrder) { - Bits.WriteInt(Buffer, position, v, byteOrder == ByteOrder.BigEndian); + Bits.WriteInt(_buffer, position, v, byteOrder == ByteOrder.BigEndian); } /// public virtual void WriteLong(long v) { EnsureAvailable(Bits.LongSizeInBytes); - Bits.WriteLong(Buffer, Pos, v, _isBigEndian); + Bits.WriteLong(_buffer, Pos, v, _isBigEndian); Pos += Bits.LongSizeInBytes; } /// public virtual void WriteLong(int position, long v) { - Bits.WriteLong(Buffer, position, v, _isBigEndian); + Bits.WriteLong(_buffer, position, v, _isBigEndian); } public void WriteLong(long v, ByteOrder byteOrder) { EnsureAvailable(Bits.LongSizeInBytes); - Bits.WriteLong(Buffer, Pos, v, byteOrder == ByteOrder.BigEndian); + Bits.WriteLong(_buffer, Pos, v, byteOrder == ByteOrder.BigEndian); Pos += Bits.LongSizeInBytes; } public void WriteLong(int position, long v, ByteOrder byteOrder) { - Bits.WriteLong(Buffer, position, v, byteOrder == ByteOrder.BigEndian); + Bits.WriteLong(_buffer, position, v, byteOrder == ByteOrder.BigEndian); } /// public virtual void WriteShort(int v) { EnsureAvailable(Bits.ShortSizeInBytes); - Bits.WriteShort(Buffer, Pos, (short) v, _isBigEndian); + Bits.WriteShort(_buffer, Pos, (short) v, _isBigEndian); Pos += Bits.ShortSizeInBytes; } /// public virtual void WriteShort(int position, int v) { - Bits.WriteShort(Buffer, position, (short) v, _isBigEndian); + Bits.WriteShort(_buffer, position, (short) v, _isBigEndian); } public void WriteShort(int v, ByteOrder byteOrder) { EnsureAvailable(Bits.ShortSizeInBytes); - Bits.WriteShort(Buffer, Pos, (short) v, byteOrder == ByteOrder.BigEndian); + Bits.WriteShort(_buffer, Pos, (short) v, byteOrder == ByteOrder.BigEndian); Pos += Bits.ShortSizeInBytes; } public void WriteShort(int position, int v, ByteOrder byteOrder) { - Bits.WriteShort(Buffer, position, (short) v, byteOrder == ByteOrder.BigEndian); + Bits.WriteShort(_buffer, position, (short) v, byteOrder == ByteOrder.BigEndian); } /// @@ -243,7 +248,7 @@ public virtual void WriteUTF(string str) EnsureAvailable(len*3); for (var i = 0; i < len; i++) { - Pos += Bits.WriteUtf8Char(Buffer, Pos, str[i]); + Pos += Bits.WriteUtf8Char(_buffer, Pos, str[i]); } } } @@ -392,7 +397,7 @@ public virtual int Position() public virtual void Position(int newPos) { - if ((newPos > Buffer.Length) || (newPos < 0)) + if ((newPos > _buffer.Length) || (newPos < 0)) { throw new ArgumentException(); } @@ -401,21 +406,21 @@ public virtual void Position(int newPos) public virtual byte[] ToByteArray() { - if (Buffer == null || Pos == 0) + if (_buffer == null || Pos == 0) { return new byte[0]; } var newBuffer = new byte[Pos]; - Array.Copy(Buffer, 0, newBuffer, 0, Pos); + System.Buffer.BlockCopy(_buffer, 0, newBuffer, 0, Pos); return newBuffer; } public virtual void Clear() { Pos = 0; - if (Buffer != null && Buffer.Length > _initialSize*8) + if (_buffer != null && _buffer.Length > _initialSize*8) { - Buffer = new byte[_initialSize*8]; + _buffer = new byte[_initialSize*8]; } } @@ -432,7 +437,7 @@ public virtual ByteOrder GetByteOrder() public void Write(int b) { EnsureAvailable(1); - Buffer[Pos++] = unchecked((byte) (b)); + _buffer[Pos++] = unchecked((byte) (b)); } public void Write(byte[] b) @@ -451,7 +456,7 @@ public virtual void Write(byte[] b, int off, int len) return; } EnsureAvailable(len); - Array.Copy(b, off, Buffer, Pos, len); + System.Buffer.BlockCopy(b, off, _buffer, Pos, len); Pos += len; } @@ -462,19 +467,19 @@ public void Flush() public virtual void Close() { Pos = 0; - Buffer = null; + _buffer = null; } public virtual int Available() { - return Buffer != null ? Buffer.Length - Pos : 0; + return _buffer != null ? _buffer.Length - Pos : 0; } public override string ToString() { var sb = new StringBuilder(); sb.Append("ByteArrayObjectDataOutput"); - sb.Append("{size=").Append(Buffer != null ? Buffer.Length : 0); + sb.Append("{size=").Append(_buffer != null ? _buffer.Length : 0); sb.Append(", pos=").Append(Pos); sb.Append('}'); return sb.ToString(); @@ -484,16 +489,16 @@ internal void EnsureAvailable(int len) { if (Available() < len) { - if (Buffer != null) + if (_buffer != null) { - var newCap = Math.Max(Buffer.Length << 1, Buffer.Length + len); + var newCap = Math.Max(_buffer.Length << 1, _buffer.Length + len); var newBuffer = new byte[newCap]; - Array.Copy(Buffer, 0, newBuffer, 0, Pos); - Buffer = newBuffer; + System.Buffer.BlockCopy(_buffer, 0, newBuffer, 0, Pos); + _buffer = newBuffer; } else { - Buffer = new byte[len > _initialSize/2 ? len*2 : _initialSize]; + _buffer = new byte[len > _initialSize/2 ? len*2 : _initialSize]; } } } diff --git a/Hazelcast.Net/Hazelcast.IO/Bits.cs b/Hazelcast.Net/Hazelcast.IO/Bits.cs index 7453071ec9..7bfd8392dc 100644 --- a/Hazelcast.Net/Hazelcast.IO/Bits.cs +++ b/Hazelcast.Net/Hazelcast.IO/Bits.cs @@ -120,22 +120,12 @@ public static char ReadChar(byte[] buffer, int pos, bool bigEndian) public static char ReadCharB(byte[] buffer, int pos) { - unchecked - { - var byte1 = buffer[pos] & 0xFF; - var byte0 = buffer[pos + 1] & 0xFF; - return (char) ((byte1 << 8) + byte0); - } + return unchecked((char)(buffer[pos + 1] | buffer[pos] << 8)); } public static char ReadCharL(byte[] buffer, int pos) { - unchecked - { - var byte1 = buffer[pos] & 0xFF; - var byte0 = buffer[pos + 1] & 0xFF; - return (char) ((byte0 << 8) + byte1); - } + return unchecked((char)(buffer[pos] | buffer[pos + 1] << 8)); } public static int ReadInt(byte[] buffer, int pos, bool bigEndian) @@ -147,11 +137,10 @@ public static int ReadIntB(byte[] buffer, int pos) { unchecked { - var byte3 = (buffer[pos] & 0xFF) << 24; - var byte2 = (buffer[pos + 1] & 0xFF) << 16; - var byte1 = (buffer[pos + 2] & 0xFF) << 8; - var byte0 = buffer[pos + 3] & 0xFF; - return byte3 + byte2 + byte1 + byte0; + return buffer[pos] << 24 | + buffer[pos + 1] << 16 | + buffer[pos + 2] << 8 | + buffer[pos + 3]; } } @@ -159,11 +148,10 @@ public static int ReadIntL(byte[] buffer, int pos) { unchecked { - var byte3 = buffer[pos] & 0xFF; - var byte2 = (buffer[pos + 1] & 0xFF) << 8; - var byte1 = (buffer[pos + 2] & 0xFF) << 16; - var byte0 = (buffer[pos + 3] & 0xFF) << 24; - return byte3 + byte2 + byte1 + byte0; + return buffer[pos] | + buffer[pos + 1] << 8 | + buffer[pos + 2] << 16 | + buffer[pos + 3] << 24; } } @@ -176,15 +164,14 @@ public static long ReadLongB(byte[] buffer, int pos) { unchecked { - var byte7 = (long) buffer[pos] << 56; - var byte6 = (long) (buffer[pos + 1] & 0xFF) << 48; - var byte5 = (long) (buffer[pos + 2] & 0xFF) << 40; - var byte4 = (long) (buffer[pos + 3] & 0xFF) << 32; - var byte3 = (long) (buffer[pos + 4] & 0xFF) << 24; - var byte2 = (long) (buffer[pos + 5] & 0xFF) << 16; - var byte1 = (long) (buffer[pos + 6] & 0xFF) << 8; - long byte0 = buffer[pos + 7] & 0xFF; - return byte7 + byte6 + byte5 + byte4 + byte3 + byte2 + byte1 + byte0; + return (long)buffer[pos] << 56 | + (long)buffer[pos + 1] << 48 | + (long)buffer[pos + 2] << 40 | + (long)buffer[pos + 3] << 32 | + (long)buffer[pos + 4] << 24 | + (long)buffer[pos + 5] << 16 | + (long)buffer[pos + 6] << 8 | + buffer[pos + 7]; } } @@ -192,15 +179,14 @@ public static long ReadLongL(byte[] buffer, int pos) { unchecked { - long byte7 = buffer[pos] & 0xFF; - var byte6 = (long) (buffer[pos + 1] & 0xFF) << 8; - var byte5 = (long) (buffer[pos + 2] & 0xFF) << 16; - var byte4 = (long) (buffer[pos + 3] & 0xFF) << 24; - var byte3 = (long) (buffer[pos + 4] & 0xFF) << 32; - var byte2 = (long) (buffer[pos + 5] & 0xFF) << 40; - var byte1 = (long) (buffer[pos + 6] & 0xFF) << 48; - var byte0 = (long) (buffer[pos + 7] & 0xFF) << 56; - return byte7 + byte6 + byte5 + byte4 + byte3 + byte2 + byte1 + byte0; + return buffer[pos] | + (long)buffer[pos + 1] << 8 | + (long)buffer[pos + 2] << 16 | + (long)buffer[pos + 3] << 24 | + (long)buffer[pos + 4] << 32 | + (long)buffer[pos + 5] << 40 | + (long)buffer[pos + 6] << 48 | + (long)buffer[pos + 7] << 56; } } @@ -211,22 +197,12 @@ public static short ReadShort(byte[] buffer, int pos, bool bigEndian) public static short ReadShortB(byte[] buffer, int pos) { - unchecked - { - var byte1 = buffer[pos] & 0xFF; - var byte0 = buffer[pos + 1] & 0xFF; - return (short) ((byte1 << 8) + byte0); - } + return unchecked((short)(buffer[pos] << 8 | buffer[pos + 1])); } public static short ReadShortL(byte[] buffer, int pos) { - unchecked - { - var byte1 = buffer[pos] & 0xFF; - var byte0 = buffer[pos + 1] & 0xFF; - return (short) ((byte0 << 8) + byte1); - } + return unchecked((short)(buffer[pos] | buffer[pos + 1] << 8)); } public static char ReadUtf8Char(IDataInput input, byte firstByte) @@ -258,23 +234,23 @@ public static char ReadUtf8Char(IDataInput input, byte firstByte) } } - /// Sets n-th bit of the byte value - /// byte value - /// n-th bit - /// value - public static byte SetBit(byte value, int bit) - { - return unchecked((byte) (value | (1 << bit))); - } - - /// Sets n-th bit of the integer value - /// integer value - /// n-th bit - /// value - public static int SetBit(int value, int bit) - { - return unchecked(value | (1 << bit)); - } +// /// Sets n-th bit of the byte value +// /// byte value +// /// n-th bit +// /// value +// public static byte SetBit(byte value, int bit) +// { +// return unchecked((byte) (value | (1 << bit))); +// } +// +// /// Sets n-th bit of the integer value +// /// integer value +// /// n-th bit +// /// value +// public static int SetBit(int value, int bit) +// { +// return unchecked(value | (1 << bit)); +// } public static void WriteChar(byte[] buffer, int pos, char v, bool bigEndian) { @@ -292,8 +268,8 @@ public static void WriteCharB(byte[] buffer, int pos, char v) { unchecked { - buffer[pos] = (byte) ((v >> 8) & 0xFF); - buffer[pos + 1] = (byte) ((v) & 0xFF); + buffer[pos] = (byte) (v >> 8); + buffer[pos + 1] = (byte) v; } } @@ -301,8 +277,8 @@ public static void WriteCharL(byte[] buffer, int pos, char v) { unchecked { - buffer[pos] = (byte) ((v) & 0xFF); - buffer[pos + 1] = (byte) ((v >> 8) & 0xFF); + buffer[pos] = (byte)v; + buffer[pos + 1] = (byte)(v >> 8); } } @@ -322,21 +298,24 @@ public static void WriteIntB(byte[] buffer, int pos, int v) { unchecked { - buffer[pos] = (byte) (((int) (((uint) v) >> 24)) & 0xFF); - buffer[pos + 1] = (byte) (((int) (((uint) v) >> 16)) & 0xFF); - buffer[pos + 2] = (byte) (((int) (((uint) v) >> 8)) & 0xFF); - buffer[pos + 3] = (byte) ((v) & 0xFF); + uint unsignedValue = (uint)v; + buffer[pos] = (byte)(unsignedValue >> 24); + buffer[pos + 1] = (byte)(unsignedValue >> 16); + buffer[pos + 2] = (byte)(unsignedValue >>8); + buffer[pos + 3] = (byte)unsignedValue; } } public static void WriteIntL(byte[] buffer, int pos, int v) { + unchecked { - buffer[pos] = (byte) ((v) & 0xFF); - buffer[pos + 1] = (byte) (((int) (((uint) v) >> 8)) & 0xFF); - buffer[pos + 2] = (byte) (((int) (((uint) v) >> 16)) & 0xFF); - buffer[pos + 3] = (byte) (((int) (((uint) v) >> 24)) & 0xFF); + uint unsignedValue = (uint)v; + buffer[pos] = (byte)unsignedValue; + buffer[pos + 1] = (byte)(unsignedValue >> 8); + buffer[pos + 2] = (byte)(unsignedValue >> 16); + buffer[pos + 3] = (byte)(unsignedValue >> 24); } } @@ -356,14 +335,15 @@ public static void WriteLongB(byte[] buffer, int pos, long v) { unchecked { - buffer[pos] = ((byte) ((long) (((ulong) v) >> 56))); - buffer[pos + 1] = ((byte) ((long) (((ulong) v) >> 48))); - buffer[pos + 2] = ((byte) ((long) (((ulong) v) >> 40))); - buffer[pos + 3] = ((byte) ((long) (((ulong) v) >> 32))); - buffer[pos + 4] = ((byte) ((long) (((ulong) v) >> 24))); - buffer[pos + 5] = ((byte) ((long) (((ulong) v) >> 16))); - buffer[pos + 6] = ((byte) ((long) (((ulong) v) >> 8))); - buffer[pos + 7] = ((byte) (v)); + ulong unsignedValue = (ulong)v; + buffer[pos] = (byte)(unsignedValue >> 56); + buffer[pos + 1] = (byte)(unsignedValue >> 48); + buffer[pos + 2] = (byte)(unsignedValue >> 40); + buffer[pos + 3] = (byte)(unsignedValue >> 32); + buffer[pos + 4] = (byte)(unsignedValue >> 24); + buffer[pos + 5] = (byte)(unsignedValue >> 16); + buffer[pos + 6] = (byte)(unsignedValue >> 8); + buffer[pos + 7] = (byte)unsignedValue; } } @@ -371,14 +351,15 @@ public static void WriteLongL(byte[] buffer, int pos, long v) { unchecked { - buffer[pos] = ((byte) (v)); - buffer[pos + 1] = ((byte) ((long) (((ulong) v) >> 8))); - buffer[pos + 2] = ((byte) ((long) (((ulong) v) >> 16))); - buffer[pos + 3] = ((byte) ((long) (((ulong) v) >> 24))); - buffer[pos + 4] = ((byte) ((long) (((ulong) v) >> 32))); - buffer[pos + 5] = ((byte) ((long) (((ulong) v) >> 40))); - buffer[pos + 6] = ((byte) ((long) (((ulong) v) >> 48))); - buffer[pos + 7] = ((byte) ((long) (((ulong) v) >> 56))); + ulong unsignedValue = (ulong)v; + buffer[pos] = (byte)unsignedValue; + buffer[pos + 1] = (byte)(unsignedValue >> 8); + buffer[pos + 2] = (byte)(unsignedValue >> 16); + buffer[pos + 3] = (byte)(unsignedValue >> 24); + buffer[pos + 4] = (byte)(unsignedValue >> 32); + buffer[pos + 5] = (byte)(unsignedValue >> 40); + buffer[pos + 6] = (byte)(unsignedValue >> 48); + buffer[pos + 7] = (byte)(unsignedValue >> 56); } } @@ -398,8 +379,8 @@ public static void WriteShortB(byte[] buffer, int pos, short v) { unchecked { - buffer[pos] = (byte) (((short) (((ushort) v) >> 8)) & 0xFF); - buffer[pos + 1] = (byte) ((v) & 0xFF); + buffer[pos] = (byte)((ushort)v >> 8); + buffer[pos + 1] = (byte)v; } } @@ -407,8 +388,8 @@ public static void WriteShortL(byte[] buffer, int pos, short v) { unchecked { - buffer[pos] = (byte) ((v) & 0xFF); - buffer[pos + 1] = (byte) (((short) (((ushort) v) >> 8)) & 0xFF); + buffer[pos] = (byte)v; + buffer[pos + 1] = (byte)((ushort)v >> 8); } } diff --git a/Hazelcast.Net/Hazelcast.NearCache/BaseNearCache.cs b/Hazelcast.Net/Hazelcast.NearCache/BaseNearCache.cs index f2f2cb0b85..66fcf244b8 100644 --- a/Hazelcast.Net/Hazelcast.NearCache/BaseNearCache.cs +++ b/Hazelcast.Net/Hazelcast.NearCache/BaseNearCache.cs @@ -138,6 +138,7 @@ public bool Remove(IData key) public bool TryAdd(IData keyData, object value) { + //TODO this method is not thread safe yet var lazyValue = new Lazy(() => { var ncValue = ConvertToRecordValue(value); diff --git a/Hazelcast.Net/Hazelcast.Net.Ext/ByteBuffer.cs b/Hazelcast.Net/Hazelcast.Net.Ext/ByteBuffer.cs index 412f09b1fe..8ae6009ccd 100644 --- a/Hazelcast.Net/Hazelcast.Net.Ext/ByteBuffer.cs +++ b/Hazelcast.Net/Hazelcast.Net.Ext/ByteBuffer.cs @@ -111,7 +111,7 @@ public void Clear() public virtual void Compact() { - System.Array.Copy(_buffer, Position, _buffer, 0, Remaining()); + Buffer.BlockCopy(_buffer, Position, _buffer, 0, Remaining()); Position = Remaining(); Limit = Capacity(); _mark = -1; diff --git a/Hazelcast.Net/Hazelcast.Util/AbstractLazyDictionary.cs b/Hazelcast.Net/Hazelcast.Util/AbstractLazyDictionary.cs new file mode 100644 index 0000000000..a7ed83dc01 --- /dev/null +++ b/Hazelcast.Net/Hazelcast.Util/AbstractLazyDictionary.cs @@ -0,0 +1,222 @@ +// Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using Hazelcast.IO.Serialization; + +namespace Hazelcast.Util +{ + internal abstract class AbstractLazyDictionary : IEnumerable> + { + protected readonly ConcurrentQueue> _contentQueue; + protected readonly ISerializationService _serializationService; + + protected AbstractLazyDictionary(ConcurrentQueue> contentQueue, + ISerializationService serializationService) + { + _contentQueue = contentQueue; + _serializationService = serializationService; + } + + public bool Contains(KeyValuePair item) + { + var keyData = _serializationService.ToData(item.Key); + + foreach (var pair in _contentQueue) + { + if (pair.Key.Equals(keyData)) + { + var itemValueData = _serializationService.ToData(item.Value); + var pairValueData = _serializationService.ToData(pair.Value); + return itemValueData.Equals(pairValueData); + } + } + return false; + } + + public bool ContainsKey(TKey key) + { + var keyData = _serializationService.ToData(key); + foreach (var pair in _contentQueue) + { + if (pair.Key.Equals(keyData)) + { + return true; + } + } + return false; + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + var ix = arrayIndex; + using (var enumerator = GetEnumerator()) + { + while (enumerator.MoveNext()) + { + array[ix] = enumerator.Current; + ix++; + } + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public IEnumerator> GetEnumerator() + { + return new Enumerator(_contentQueue.GetEnumerator(), _serializationService); + } + + public int Count + { + get { return _contentQueue.Count; } + } + + public bool IsReadOnly + { + get { return true; } + } + + public void Add(KeyValuePair item) + { + throw new NotSupportedException("Readonly dictionary"); + } + + public void Clear() + { + throw new NotSupportedException("Readonly dictionary"); + } + + + public void Add(TKey key, TValue value) + { + throw new NotSupportedException("Readonly dictionary"); + } + + + public bool Remove(TKey key) + { + throw new NotSupportedException("Readonly dictionary"); + } + + public bool TryGetValue(TKey key, out TValue value) + { + KeyValuePair entry; + if (FindEntry(key, out entry)) + { + value = _serializationService.ToObject(entry.Value); + return true; + } + value = default(TValue); + return false; + } + + public TValue this[TKey key] + { + get + { + KeyValuePair entry; + if (FindEntry(key, out entry)) + { + return _serializationService.ToObject(entry.Value); + } + throw new KeyNotFoundException(); + } + set { throw new NotSupportedException("Readonly dictionary"); } + } + + public bool Remove(KeyValuePair item) + { + throw new NotSupportedException("Readonly Set"); + } + + private bool FindEntry(TKey key, out KeyValuePair entry) + { + var keyData = _serializationService.ToData(key); + try + { + entry = _contentQueue.First(pair => pair.Key.Equals(keyData)); + return true; + } + catch (Exception) + { + entry = default(KeyValuePair); + return false; + } + } + + private struct Enumerator : IEnumerator> + { + private readonly IEnumerator> _enumerator; + private readonly ISerializationService _ss; + + internal Enumerator(IEnumerator> enumerator, ISerializationService ss) + { + _enumerator = enumerator; + _ss = ss; + } + + object IEnumerator.Current + { + get { return Current; } + } + + public KeyValuePair Current + { + get + { + var current = _enumerator.Current; + var key = _ss.ToObject(current.Key); + var value = _ss.ToObject(current.Value); + return new KeyValuePair(key, value); + } + } + + public void Dispose() + { + _enumerator.Dispose(); + } + + public bool MoveNext() + { + return _enumerator.MoveNext(); + } + + void IEnumerator.Reset() + { + _enumerator.Reset(); + } + } + } +} \ No newline at end of file diff --git a/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazyDictionary.cs b/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazyDictionary.cs new file mode 100644 index 0000000000..049c76915a --- /dev/null +++ b/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazyDictionary.cs @@ -0,0 +1,57 @@ +// Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using Hazelcast.IO.Serialization; + +namespace Hazelcast.Util +{ + internal class ReadOnlyLazyDictionary : AbstractLazyDictionary, IDictionary + { + public ReadOnlyLazyDictionary(ConcurrentQueue> contentQueue, + ISerializationService serializationService) : base(contentQueue, serializationService) + { + } + + public ICollection Keys + { + get + { + var keyDatas = _contentQueue.Select(pair => pair.Key).ToList(); + return new ReadOnlyLazyList(keyDatas, _serializationService); + } + private set + { + throw new NotSupportedException("Readonly dictionary"); + } + } + + public ICollection Values + { + get + { + var valueDatas = _contentQueue.Select(pair => pair.Value).ToList(); + return new ReadOnlyLazyList(valueDatas, _serializationService); + } + private set + { + throw new NotSupportedException("Readonly dictionary"); + } + } + } +} \ No newline at end of file diff --git a/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazyEntrySet.cs b/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazyEntrySet.cs new file mode 100644 index 0000000000..00d2f1c0fe --- /dev/null +++ b/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazyEntrySet.cs @@ -0,0 +1,98 @@ +// Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using Hazelcast.IO.Serialization; + +namespace Hazelcast.Util +{ + internal class ReadOnlyLazyEntrySet : AbstractLazyDictionary, ISet> + { + public ReadOnlyLazyEntrySet(ConcurrentQueue> contentQueue, + ISerializationService serializationService) : base(contentQueue, serializationService) + { + } + + public bool SetEquals(IEnumerable> other) + { + if (other == null) + { + throw new ArgumentNullException("other"); + } + foreach (var pair in other) + { + if (!Contains(pair)) + { + return false; + } + } + return other == this || other.All(Contains); + } + + public void ExceptWith(IEnumerable> other) + { + throw new NotSupportedException("Readonly Set"); + } + + public void IntersectWith(IEnumerable> other) + { + throw new NotSupportedException("Readonly Set"); + } + + public bool IsProperSubsetOf(IEnumerable> other) + { + throw new NotSupportedException("Readonly Set"); + } + + public bool IsProperSupersetOf(IEnumerable> other) + { + throw new NotSupportedException("Readonly Set"); + } + + public bool IsSubsetOf(IEnumerable> other) + { + throw new NotSupportedException("Readonly Set"); + } + + public bool IsSupersetOf(IEnumerable> other) + { + throw new NotSupportedException("Readonly Set"); + } + + public bool Overlaps(IEnumerable> other) + { + throw new NotSupportedException("Readonly Set"); + } + + public void SymmetricExceptWith(IEnumerable> other) + { + throw new NotSupportedException("Readonly Set"); + } + + public void UnionWith(IEnumerable> other) + { + throw new NotSupportedException("Readonly Set"); + } + + bool ISet>.Add(KeyValuePair item) + { + throw new NotSupportedException("Readonly Set"); + } + + } +} \ No newline at end of file diff --git a/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazyList.cs b/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazyList.cs index 6f53b9df49..e23c2114f5 100644 --- a/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazyList.cs +++ b/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazyList.cs @@ -19,18 +19,19 @@ namespace Hazelcast.Util { - internal class ReadOnlyLazyList : IList + internal class ReadOnlyLazyList : IList where D : class { - private readonly IList list; + private readonly IList list; private readonly ISerializationService serializationService; - public ReadOnlyLazyList(IList list, ISerializationService serializationService) + public ReadOnlyLazyList(IList list, ISerializationService serializationService) { this.list = list; this.serializationService = serializationService; } #region IList + public IEnumerator GetEnumerator() { return new Enumerator(list, serializationService); @@ -43,18 +44,31 @@ IEnumerator IEnumerable.GetEnumerator() public void Add(T item) { - throw new NotSupportedException("Readonly list"); + throw new NotSupportedException("Readonly lazy list"); } public void Clear() { - throw new NotSupportedException("Readonly list"); + throw new NotSupportedException("Readonly lazy list"); } public bool Contains(T item) { - var data = serializationService.ToData(item); - return list.Contains(data); + var itemData = serializationService.ToData(item); + foreach (var dValue in list) + { + if (dValue is IData) + { + if (itemData.Equals(dValue)) + return true; + } + else + { + if (itemData.Equals(serializationService.ToData(dValue))) + return true; + } + } + return false; } public void CopyTo(T[] array, int arrayIndex) @@ -63,7 +77,7 @@ public void CopyTo(T[] array, int arrayIndex) var enumerator = GetEnumerator(); while (enumerator.MoveNext()) { - array[ix]=enumerator.Current; + array[ix] = enumerator.Current; ix++; } } @@ -80,16 +94,18 @@ public int Count public bool IsReadOnly { - get - { - return true; - } + get { return true; } } public int IndexOf(T item) { - var data = serializationService.ToData(item); - return list.IndexOf(data); + var ix = list.IndexOf(item as D); + if (ix < 0) + { + var data = serializationService.ToData(item); + return list.IndexOf(data as D); + } + return ix; } public void Insert(int index, T item) @@ -104,25 +120,20 @@ public void RemoveAt(int index) public T this[int index] { - get - { - return serializationService.ToObject(list[index]); - } - set - { - throw new NotSupportedException("Readonly list"); - } + get { return serializationService.ToObject(list[index]); } + set { throw new NotSupportedException("Readonly list"); } } + #endregion internal struct Enumerator : IEnumerator { - private readonly IList _dataList; + private readonly IList _dataList; private readonly ISerializationService _ss; private int _nextIndex; - private IData _current; + private D _current; - internal Enumerator(IList dataList, ISerializationService ss) + internal Enumerator(IList dataList, ISerializationService ss) { _dataList = dataList; _ss = ss; @@ -134,7 +145,8 @@ object IEnumerator.Current { get { - if( _nextIndex == 0 || _nextIndex == _dataList.Count + 1) { + if (_nextIndex == 0 || _nextIndex == _dataList.Count + 1) + { throw new InvalidOperationException(); } return Current; @@ -143,10 +155,7 @@ object IEnumerator.Current public T Current { - get - { - return _ss.ToObject(_current); - } + get { return _ss.ToObject(_current); } } public void Dispose() @@ -157,7 +166,7 @@ public bool MoveNext() { var localList = _dataList; - if ((uint)_nextIndex < (uint)localList.Count) + if ((uint) _nextIndex < (uint) localList.Count) { _current = localList[_nextIndex]; _nextIndex++; @@ -175,6 +184,4 @@ void IEnumerator.Reset() } } } - - -} +} \ No newline at end of file diff --git a/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazySet.cs b/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazySet.cs index 403a69ceda..47cda8a26e 100644 --- a/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazySet.cs +++ b/Hazelcast.Net/Hazelcast.Util/ReadOnlyLazySet.cs @@ -34,7 +34,7 @@ public ReadOnlyLazySet(IList list, ISerializationService serializationSer public IEnumerator GetEnumerator() { - return new ReadOnlyLazyList.Enumerator(list, serializationService); + return new ReadOnlyLazyList.Enumerator(list, serializationService); } IEnumerator IEnumerable.GetEnumerator() diff --git a/Hazelcast.Net/Properties/AssemblyInfo.cs b/Hazelcast.Net/Properties/AssemblyInfo.cs index d914d8ac69..f2c56aee4f 100644 --- a/Hazelcast.Net/Properties/AssemblyInfo.cs +++ b/Hazelcast.Net/Properties/AssemblyInfo.cs @@ -19,3 +19,4 @@ [assembly: CLSCompliant(true)] [assembly: ComVisible(false)] [assembly: InternalsVisibleTo(@"Hazelcast.Test, PublicKey=00240000048000009400000006020000002400005253413100040000010001004d81045a994968ac643918d7bbce405b2473471d8de6aed6bbffc0fe1874bfcabf3c0b437c6c5293a589bdcbe884c6d86934069b35deaf5ab2e770cbff41a20dd4014bb53e481c30bd3ead29437b02dec5916a717a4a2b4fd353e81238b89ae09e5ba0ab615c5fef7937aabab4e240c3dffe2b948047769eeb07f674589d0bb3")] +[assembly: InternalsVisibleTo(@"Hazelcast.Benchmark, PublicKey=00240000048000009400000006020000002400005253413100040000010001004d81045a994968ac643918d7bbce405b2473471d8de6aed6bbffc0fe1874bfcabf3c0b437c6c5293a589bdcbe884c6d86934069b35deaf5ab2e770cbff41a20dd4014bb53e481c30bd3ead29437b02dec5916a717a4a2b4fd353e81238b89ae09e5ba0ab615c5fef7937aabab4e240c3dffe2b948047769eeb07f674589d0bb3")] diff --git a/Hazelcast.Test/Hazelcast.Client.Test/AbstractLazyDictionaryTest.cs b/Hazelcast.Test/Hazelcast.Client.Test/AbstractLazyDictionaryTest.cs new file mode 100644 index 0000000000..df4a883f27 --- /dev/null +++ b/Hazelcast.Test/Hazelcast.Client.Test/AbstractLazyDictionaryTest.cs @@ -0,0 +1,163 @@ +// Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections; +using System.Collections.Concurrent; +using System.Collections.Generic; +using Hazelcast.IO.Serialization; +using Hazelcast.Util; +using NUnit.Framework; + +namespace Hazelcast.Client.Test +{ + [TestFixture] + public abstract class AbstractLazyDictionaryTest + { + internal abstract AbstractLazyDictionary TestCollection { get; } + + [Test] + public void Test_GetEnumerator() + { + var enumerator = TestCollection.GetEnumerator(); + var ix = 0; + while (enumerator.MoveNext()) + { + var pair = enumerator.Current; + Assert.AreEqual(pair.Key, ix); + Assert.AreEqual(pair.Value, ix.ToString()); + ix++; + } + } + + [Test] + public void Test_IEnumerable_GetEnumerator() + { + var enumerator = ((IEnumerable) TestCollection).GetEnumerator(); + var ix = 0; + while (enumerator.MoveNext()) + { + var pair = (KeyValuePair) enumerator.Current; + Assert.AreEqual(pair.Key, ix); + Assert.AreEqual(pair.Value, ix.ToString()); + ix++; + } + } + + [Test] + public void Test_Add_Pair() + { + Assert.Throws(() => { TestCollection.Add(new KeyValuePair(1, "")); }); + } + + [Test] + public void Test_Clear() + { + Assert.Throws(() => TestCollection.Clear()); + } + + [Test] + public void Test_Contains() + { + for (var i = 0; i < 4; i++) + { + Assert.True(TestCollection.Contains(new KeyValuePair(i, i.ToString()))); + } + } + + [Test] + public void Test_CopyTo() + { + var copyArray = new KeyValuePair[TestCollection.Count + 5]; + TestCollection.CopyTo(copyArray, 1); + for (var i = 0; i < 4; i++) + { + Assert.AreEqual(copyArray[i+1].Key, i); + Assert.AreEqual(copyArray[i+1].Value, i.ToString()); + } + } + + [Test] + public void Test_Remove_pair() + { + Assert.Throws(() => TestCollection.Remove(2)); + } + + [Test] + public void Test_Count() + { + Assert.AreEqual(4, TestCollection.Count); + } + + [Test] + public void Test_IsReadOnly() + { + Assert.True(TestCollection.IsReadOnly); + } + + [Test] + public void Test_Add_key_value() + { + Assert.Throws(() => { TestCollection.Add(1, ""); }); + } + + [Test] + public void Test_ContainsKey() + { + for (var i = 0; i < 4; i++) + { + Assert.True(TestCollection.ContainsKey(i)); + } + } + + [Test] + public void Test_Remove_key() + { + Assert.Throws(() => { TestCollection.Remove(1); }); + } + + [Test] + public void Test_TryGetValue() + { + for (int i = 0; i < 5; i++) + { + string val; + if (TestCollection.TryGetValue(i, out val)) + { + Assert.AreEqual(val, i.ToString()); + } + } + } + + [Test] + public void Test_Index_operator() + { + for (int i = 0; i < 4; i++) + { + string val = TestCollection[i]; + Assert.AreEqual(val, i.ToString()); + } + Assert.Throws(() => + { + var x = TestCollection[99]; + }); + Assert.Throws(() => + { + TestCollection[99] = "99"; + }); + } + + + } +} \ No newline at end of file diff --git a/Hazelcast.Test/Hazelcast.Client.Test/AggregatorAndProjectionTest.cs b/Hazelcast.Test/Hazelcast.Client.Test/AggregatorAndProjectionTest.cs index 9c41b3ef3d..1c48d92ecc 100644 --- a/Hazelcast.Test/Hazelcast.Client.Test/AggregatorAndProjectionTest.cs +++ b/Hazelcast.Test/Hazelcast.Client.Test/AggregatorAndProjectionTest.cs @@ -18,6 +18,7 @@ using Hazelcast.Client.Model; using Hazelcast.Config; using Hazelcast.Core; +using Hazelcast.IO.Serialization; using Hazelcast.Util; using NUnit.Framework; @@ -242,7 +243,7 @@ public void testAggregate_NullPredicate() public void test_SingleAttributeProjection() { FillMap
(); - var result = (ReadOnlyLazyList) map.Project(new SingleAttributeProjection("id")); + var result = (ReadOnlyLazyList) map.Project(new SingleAttributeProjection("id")); var expected = new HashSet {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; CollectionAssert.AreEquivalent(expected, result); } @@ -252,7 +253,7 @@ public void test_SingleAttributeProjection_withPredicate() { FillMap
(); var predicate = Predicates.IsGreaterThan("id", 5); - var result = (ReadOnlyLazyList) map.Project(new SingleAttributeProjection("id"), predicate); + var result = (ReadOnlyLazyList) map.Project(new SingleAttributeProjection("id"), predicate); var expected = new HashSet {6, 7, 8, 9}; CollectionAssert.AreEquivalent(expected, result); } diff --git a/Hazelcast.Test/Hazelcast.Client.Test/ClientMapTest.cs b/Hazelcast.Test/Hazelcast.Client.Test/ClientMapTest.cs index 9897b9fe18..a634cd94c0 100644 --- a/Hazelcast.Test/Hazelcast.Client.Test/ClientMapTest.cs +++ b/Hazelcast.Test/Hazelcast.Client.Test/ClientMapTest.cs @@ -485,7 +485,7 @@ public virtual void TestGetAllExtreme() Assert.AreEqual(map.Size(), keycount); var dictionary = map.GetAll(mm.Keys); - Assert.AreEqual(dictionary.Count, keycount); + Assert.AreEqual(keycount, dictionary.Count); } [Test] diff --git a/Hazelcast.Test/Hazelcast.Client.Test/ClientSslBaseTest.cs b/Hazelcast.Test/Hazelcast.Client.Test/ClientSslBaseTest.cs index 600ef462d2..bdd0c962a9 100644 --- a/Hazelcast.Test/Hazelcast.Client.Test/ClientSslBaseTest.cs +++ b/Hazelcast.Test/Hazelcast.Client.Test/ClientSslBaseTest.cs @@ -31,7 +31,7 @@ namespace Hazelcast.Client.Test { public abstract class ClientSSLBaseTest : HazelcastTestSupport { - protected const string ValidCertNameSigned = "member1.hazelcast-test.download"; + protected const string ValidCertNameSigned = "foobar.hazelcast.com"; protected const string Password = "password"; protected IHazelcastInstance Client { get; set; } diff --git a/Hazelcast.Test/Hazelcast.Client.Test/NonSmartRoutingTest.cs b/Hazelcast.Test/Hazelcast.Client.Test/NonSmartRoutingTest.cs index 64a9df6cd0..a24d4db0ef 100644 --- a/Hazelcast.Test/Hazelcast.Client.Test/NonSmartRoutingTest.cs +++ b/Hazelcast.Test/Hazelcast.Client.Test/NonSmartRoutingTest.cs @@ -105,7 +105,10 @@ public void TestPutAllWithNonSmartRouting() var resp = map.GetAll(toInsert.Keys); - Assert.AreEqual(toInsert, resp); + foreach (var pair in resp) + { + Assert.AreEqual(toInsert[pair.Key] , pair.Value); + } } [Test] diff --git a/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazyDictionaryTest.cs b/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazyDictionaryTest.cs new file mode 100644 index 0000000000..7b3e5c5235 --- /dev/null +++ b/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazyDictionaryTest.cs @@ -0,0 +1,67 @@ +// Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Collections.Concurrent; +using System.Collections.Generic; +using Hazelcast.IO.Serialization; +using Hazelcast.Util; +using NUnit.Framework; + +namespace Hazelcast.Client.Test +{ + [TestFixture] + public class ReadOnlyLazyDictionaryTest : AbstractLazyDictionaryTest + { + private ReadOnlyLazyDictionary _testCollection; + + [SetUp] + public void Init() + { + var _ss = new SerializationServiceBuilder().Build(); + var dataList = new ConcurrentQueue>(); + dataList.Enqueue(new KeyValuePair(_ss.ToData(0), "0")); + dataList.Enqueue(new KeyValuePair(_ss.ToData(1), _ss.ToData("1"))); + dataList.Enqueue(new KeyValuePair(_ss.ToData(2), _ss.ToData("2"))); + dataList.Enqueue(new KeyValuePair(_ss.ToData(3), "3")); + _testCollection = new ReadOnlyLazyDictionary(dataList, _ss); + } + + [Test] + public void Test_Keys() + { + var ix = 0; + foreach (var key in _testCollection.Keys) + { + Assert.AreEqual(key, ix); + ix++; + } + } + + [Test] + public void Test_Values() + { + var ix = 0; + foreach (var value in _testCollection.Values) + { + Assert.AreEqual(value, ix.ToString()); + ix++; + } + } + + internal override AbstractLazyDictionary TestCollection + { + get { return _testCollection; } + } + } +} \ No newline at end of file diff --git a/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazyEntrySetTest.cs b/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazyEntrySetTest.cs new file mode 100644 index 0000000000..0b25e640e6 --- /dev/null +++ b/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazyEntrySetTest.cs @@ -0,0 +1,58 @@ +// Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Collections.Concurrent; +using System.Collections.Generic; +using Hazelcast.IO.Serialization; +using Hazelcast.Util; +using NUnit.Framework; + +namespace Hazelcast.Client.Test +{ + [TestFixture] + public class ReadOnlyLazyEntrySetTest : AbstractLazyDictionaryTest + { + private ReadOnlyLazyEntrySet _testCollection; + + [SetUp] + public void Init() + { + var _ss = new SerializationServiceBuilder().Build(); + var dataList = new ConcurrentQueue>(); + dataList.Enqueue(new KeyValuePair(_ss.ToData(0), "0")); + dataList.Enqueue(new KeyValuePair(_ss.ToData(1), _ss.ToData("1"))); + dataList.Enqueue(new KeyValuePair(_ss.ToData(2), _ss.ToData("2"))); + dataList.Enqueue(new KeyValuePair(_ss.ToData(3), "3")); + _testCollection = new ReadOnlyLazyEntrySet(dataList, _ss); + } + + internal override AbstractLazyDictionary TestCollection + { + get { return _testCollection; } + } + + [Test] + public void Test_SetEquals() + { + ISet> otherSet = new HashSet> + { + new KeyValuePair(0, "0"), + new KeyValuePair(1, "1"), + new KeyValuePair(2, "2"), + new KeyValuePair(3, "3") + }; + Assert.True(_testCollection.SetEquals(otherSet)); + } + } +} \ No newline at end of file diff --git a/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazyListTest.cs b/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazyListTest.cs index b1d276d53f..b6e7127145 100644 --- a/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazyListTest.cs +++ b/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazyListTest.cs @@ -23,15 +23,22 @@ namespace Hazelcast.Client.Test [TestFixture] public class ReadOnlyLazyListTest { - private ReadOnlyLazyList testList; + private ReadOnlyLazyList testList; private ISerializationService _ss; [SetUp] public void Init() { _ss = new SerializationServiceBuilder().Build(); - var dataList = new List {_ss.ToData(0), _ss.ToData(1), _ss.ToData(2), _ss.ToData(3), _ss.ToData(4)}; - testList = new ReadOnlyLazyList(dataList, _ss); + var dataList = new List + { + 0,//_ss.ToData(0), + _ss.ToData(1), + _ss.ToData(2), + _ss.ToData(3), + _ss.ToData(4) + }; + testList = new ReadOnlyLazyList(dataList, _ss); } [TearDown] @@ -41,7 +48,7 @@ public void Destroy() } [Test] - public virtual void TestListGet() + public void TestListGet() { for (var i = 0; i < 5; i++) { @@ -50,25 +57,25 @@ public virtual void TestListGet() } [Test] - public virtual void TestListCount() + public void TestListCount() { Assert.AreEqual(testList.Count, 5); } [Test] - public virtual void TestListIsReadOnly() + public void TestListIsReadOnly() { Assert.True(testList.IsReadOnly); } [Test] - public virtual void TestListContains() + public void TestListContains() { Assert.True(testList.Contains(1)); } [Test] - public virtual void TestListCopyTo() + public void TestListCopyTo() { var copyArray = new int[testList.Count + 5]; testList.CopyTo(copyArray, 1); @@ -79,13 +86,13 @@ public virtual void TestListCopyTo() } [Test] - public virtual void TestListIndexOf() + public void TestListIndexOf() { Assert.AreEqual(testList.IndexOf(4), 4); } [Test] - public virtual void TestListEnum() + public void TestListEnum() { var enumerator = testList.GetEnumerator(); var ix = 0; diff --git a/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazySetTest.cs b/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazySetTest.cs index 2efcc5b788..740d8fdadf 100644 --- a/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazySetTest.cs +++ b/Hazelcast.Test/Hazelcast.Client.Test/ReadOnlyLazySetTest.cs @@ -30,7 +30,14 @@ public class ReadOnlyLazySetTest public void Init() { _ss = new SerializationServiceBuilder().Build(); - var dataList = new List{_ss.ToData(0), _ss.ToData(1), _ss.ToData(2), _ss.ToData(3), _ss.ToData(4)}; + var dataList = new List + { + _ss.ToData(0), + _ss.ToData(1), + _ss.ToData(2), + _ss.ToData(3), + _ss.ToData(4) + }; testSet = new ReadOnlyLazySet(dataList, _ss); } @@ -82,121 +89,81 @@ public void TestEnumerator() } [Test] - public void TestAdd() - { - Assert.Throws(() => + public void TestAdd() { - testSet.Add(4); - }); - } + Assert.Throws(() => { testSet.Add(4); }); + } [Test] - public void TestRemove() - { - Assert.Throws(() => + public void TestRemove() { - testSet.Remove(4); - }); - } + Assert.Throws(() => { testSet.Remove(4); }); + } [Test] - public void TestClear() - { - Assert.Throws(() => + public void TestClear() { - testSet.Clear(); - }); - } + Assert.Throws(() => { testSet.Clear(); }); + } [Test] - public void TestExceptWith() - { - Assert.Throws(() => + public void TestExceptWith() { - testSet.ExceptWith(null); - }); - } + Assert.Throws(() => { testSet.ExceptWith(null); }); + } [Test] - public void TestIntersectWith() - { - Assert.Throws(() => + public void TestIntersectWith() { - testSet.IntersectWith(null); - }); - } + Assert.Throws(() => { testSet.IntersectWith(null); }); + } [Test] - public void TestIsProperSubsetOf() - { - Assert.Throws(() => + public void TestIsProperSubsetOf() { - testSet.IsProperSubsetOf(null); - }); - } + Assert.Throws(() => { testSet.IsProperSubsetOf(null); }); + } [Test] - public void TestIsProperSupersetOf() - { - Assert.Throws(() => + public void TestIsProperSupersetOf() { - testSet.IsProperSupersetOf(null); - }); - } + Assert.Throws(() => { testSet.IsProperSupersetOf(null); }); + } [Test] - public void TestIsSubsetOf() - { - Assert.Throws(() => + public void TestIsSubsetOf() { - testSet.IsSubsetOf(null); - }); - } + Assert.Throws(() => { testSet.IsSubsetOf(null); }); + } [Test] - public void TestIsSupersetOf() - { - Assert.Throws(() => + public void TestIsSupersetOf() { - testSet.IsSupersetOf(null); - }); - } + Assert.Throws(() => { testSet.IsSupersetOf(null); }); + } [Test] - public void TestOverlaps() - { - Assert.Throws(() => + public void TestOverlaps() { - testSet.Overlaps(null); - }); - } + Assert.Throws(() => { testSet.Overlaps(null); }); + } [Test] - public void TestSymmetricExceptWith() - { - Assert.Throws(() => + public void TestSymmetricExceptWith() { - testSet.SymmetricExceptWith(null); - }); - } + Assert.Throws(() => { testSet.SymmetricExceptWith(null); }); + } [Test] - public void TestUnionWith() - { - Assert.Throws(() => + public void TestUnionWith() { - testSet.UnionWith(null); - }); - } + Assert.Throws(() => { testSet.UnionWith(null); }); + } [Test] - public void TestSetEquals() - { - Assert.Throws(() => + public void TestSetEquals() { - testSet.SetEquals(null); - }); - } - + Assert.Throws(() => { testSet.SetEquals(null); }); + } } } \ No newline at end of file diff --git a/start-rc.ps1 b/start-rc.ps1 index 8f9bc9bba1..e495585503 100644 --- a/start-rc.ps1 +++ b/start-rc.ps1 @@ -1,5 +1,5 @@ param( - [string]$serverVersion = "3.12-SNAPSHOT" + [string]$serverVersion = "3.12.1-SNAPSHOT" ) $hazelcastTestVersion=$serverVersion diff --git a/start-rc.sh b/start-rc.sh index ae4e704c95..9a96ec47e6 100644 --- a/start-rc.sh +++ b/start-rc.sh @@ -1,6 +1,6 @@ #!/bin/sh -HZ_VERSION="3.11.2-SNAPSHOT" +HZ_VERSION="3.12.1-SNAPSHOT" HAZELCAST_TEST_VERSION=${HZ_VERSION} HAZELCAST_VERSION=${HZ_VERSION}