private static PagingPredicateHolder BuildHolder(ISerializationService serializationService, PagingPredicate pagingPredicate, IData partitionKeyData) { var anchorList = pagingPredicate.AnchorList; var anchorDataList = new List <KeyValuePair <IData, IData> >(anchorList.Count); var pageList = new List <int>(anchorList.Count); foreach (var pair in anchorList) { pageList.Add(pair.Key); var anchorEntry = pair.Value; anchorDataList.Add(new KeyValuePair <IData, IData>(serializationService.ToData(anchorEntry.Key), serializationService.ToData(anchorEntry.Value))); } var anchorDataListHolder = new AnchorDataListHolder(pageList, anchorDataList); var predicateData = serializationService.ToData(pagingPredicate.Predicate); var comparatorData = serializationService.ToData(pagingPredicate.Comparer); if (!pagingPredicate.IterationType.HasValue) { throw new InvalidOperationException("The paging predicate does not specify an iteration type."); } return(new PagingPredicateHolder(anchorDataListHolder, predicateData, comparatorData, pagingPredicate.PageSize, pagingPredicate.Page, (byte)pagingPredicate.IterationType, partitionKeyData)); }
/// <inheritdoc /> public bool ContainsKey(TKey key) { if (key == null) { throw new ArgumentNullException(nameof(key)); } // fast: use key entries if (_keyEntries.ContainsKey(key)) { return(true); } // slower: serialize var keyData = _serializationService.ToData(key); // exit if no corresponding entry if (!_entries.TryGetValue(keyData, out var entry)) { return(false); } // else, while we're at it, update the entry + key entries if (!entry.HasKey) { entry.Key = key; } _keyEntries.Add(key, entry); return(true); }
public void Init() { _ss = new SerializationServiceBuilder().Build(); var dataList = new List <IData> { _ss.ToData(0), _ss.ToData(1), _ss.ToData(2), _ss.ToData(3), _ss.ToData(4) }; testList = new ReadOnlyLazyList <int>(dataList, _ss); }
private static void AssertRepeatedSerialisationGivesSameByteArrays(ISerializationService ss, IPortable p) { var data1 = ss.ToData(p); for (var k = 0; k < 100; k++) { var data2 = ss.ToData(p); Assert.AreEqual(data1, data2); } }
public void Init() { serializationService = new SerializationServiceBuilder().Build(); testString = "Test String"; dataString = serializationService.ToData(testString); testInt = 666; dataInt = serializationService.ToData(testInt); var member = new Member(new Address("localhost", 5701), ""); dataAwareEntryEvent = new DataAwareEntryEvent <string, int?>("source", member, EntryEventType.Added, dataString, dataInt, dataInt, null, serializationService); }
public void Init() { serializationService = new SerializationServiceBuilder().Build(); testString = "Test String"; dataString = serializationService.ToData(testString); testInt = 666; dataInt = serializationService.ToData(testInt); IMember member = new MemberInfo(new Address("localhost", 5701), Guid.Empty, null, false, new MemberVersion(4, 0, 0)); dataAwareEntryEvent = new DataAwareEntryEvent <string, int?>("source", member, EntryEventType.Added, dataString, dataInt, dataInt, null, serializationService); }
public bool Contains(KeyValuePair <TKey, TValue> item) { var keyData = _serializationService.ToData(item.Key); foreach (var pair in _content) { if (pair.Key.Equals(keyData)) { var itemValueData = _serializationService.ToData(item.Value); var pairValueData = _serializationService.ToData(pair.Value); return(itemValueData.Equals(pairValueData)); } } return(false); }
static void Mainzz(string[] args) { var clientConfig = new ClientConfig(); clientConfig.GetNetworkConfig().AddAddress("127.0.0.1"); clientConfig.GetSerializationConfig().AddDataSerializableFactory(1, new MyDataSerializableFactory()); IHazelcastInstance client = HazelcastClient.NewHazelcastClient(clientConfig); //All cluster operations that you can do with ordinary HazelcastInstance IMap <string, DataSerializableType> map = client.GetMap <string, DataSerializableType>("imap"); ISerializationService service = ((HazelcastClientProxy)client).GetSerializationService(); object obj = new DataSerializableType(1000, 1000); long start = Clock.CurrentTimeMillis(); var data = service.ToData(obj); var dataSerializableType = service.ToObject <DataSerializableType>(data); long diff = Clock.CurrentTimeMillis() - start; Console.WriteLine("Serialization time:" + diff); Console.ReadKey(); }
public static PagingPredicateHolder Of(IPredicate predicate, ISerializationService serializationService) { if (predicate is null) { return(null); } if (predicate is PartitionPredicate partitionPredicate) { if (partitionPredicate.Target is PagingPredicate partitionPagingPredicate) { var partitionKeyData = serializationService.ToData(partitionPredicate.PartitionKey); return(BuildHolder(serializationService, partitionPagingPredicate, partitionKeyData)); } throw new InvalidOperationException("PartitionPredicate Target is not a PagingPredicate."); } if (predicate is PagingPredicate pagingPredicate) { return(BuildHolder(serializationService, pagingPredicate, null)); } throw new InvalidOperationException("Predicate is neither a PartitionPredicate nor a PagingPredicate."); }
/// <exception cref="System.IO.IOException" /> internal static void TestDifferentClassVersionsUsingDataWriteAndRead(ISerializationService serializationService, ISerializationService serializationService2) { NamedPortable portableV1 = new NamedPortable("portable-v1", 111); IData dataV1 = serializationService.ToData(portableV1); // emulate socket write by writing data to stream var @out = serializationService.CreateObjectDataOutput(1024); @out.WriteData(dataV1); var bytes = @out.ToByteArray(); // emulate socket read by reading data from stream var @in = serializationService2.CreateObjectDataInput(bytes); dataV1 = @in.ReadData(); // serialize new portable version var portableV2 = new NamedPortableV2("portable-v2", 123, 500); var dataV2 = serializationService2.ToData(portableV2); NamedPortable v1FromV2 = serializationService.ToObject <NamedPortable>(dataV2); Assert.AreEqual(portableV2.name, v1FromV2.name); Assert.AreEqual(portableV2.k, v1FromV2.k); NamedPortableV2 v2FromV1 = serializationService2.ToObject <NamedPortableV2>(dataV1); Assert.AreEqual(portableV1.name, v2FromV1.name); Assert.AreEqual(portableV1.k, v2FromV1.k); Assert.AreEqual(v2FromV1.v, 0); }
internal static void TestPreDefinedDifferentVersions(ISerializationService serializationService, ISerializationService serializationService2, MainPortable mainPortable) { var data = serializationService.ToData(mainPortable); Assert.AreEqual(mainPortable, serializationService2.ToObject <MainPortable>(data)); }
/// <exception cref="System.IO.IOException" /> internal static void TestDifferentClassVersionsUsingDataWriteAndRead(ISerializationService serializationService, ISerializationService serializationService2) { var p1 = new NamedPortable("portable-v1", 111); var data = serializationService.ToData(p1); // emulate socket write by writing data to stream var @out = serializationService.CreateObjectDataOutput(1024); @out.WriteData(data); var bytes = @out.ToByteArray(); // emulate socket read by reading data from stream var @in = serializationService2.CreateObjectDataInput(bytes); data = @in.ReadData(); // read data var object1 = serializationService2.ToObject <object>(data); // serialize new portable version var p2 = new NamedPortableV2("portable-v2", 123); var data2 = serializationService2.ToData(p2); // de-serialize back using old version var object2 = serializationService.ToObject <object>(data2); Assert.IsNotNull(object1, "object1 should not be null!"); Assert.IsNotNull(object2, "object2 should not be null!"); Assert.IsInstanceOf <NamedPortableV2>(object1, "Should be instance of NamedPortableV2: " + object1.GetType()); Assert.IsInstanceOf <NamedPortable>(object2, "Should be instance of NamedPortable: " + object2.GetType()); }
public void TestSerializeDeserializeJsonValue() { var jsonValue = new HazelcastJsonValue("{ \"key\": \"value\" }"); var jsonData = _serializationService.ToData(jsonValue); var jsonDeserialized = _serializationService.ToObject <HazelcastJsonValue>(jsonData); Assert.AreEqual(jsonValue, jsonDeserialized); }
private int GetPartitionIdOrDefault(IData key) { // `name` is used to determine partition ID of map-wide events like clear() // since key is `null`, we are using `name` to find the partition ID if (key == null) { key = _serializationService.ToData(_nearCache.Name); } return(_partitioner.GetPartitionId(key.PartitionHash)); }
internal static void TestDifferentClassVersions(ISerializationService serializationService, ISerializationService serializationService2) { var p1 = new NamedPortable("named-portable", 123); var data = serializationService.ToData(p1); var p2 = new NamedPortableV2("named-portable", 123); var data2 = serializationService2.ToData(p2); var o1 = serializationService2.ToObject <NamedPortableV2>(data); var o2 = serializationService.ToObject <NamedPortable>(data2); Assert.AreEqual(o1.name, o2.name); }
private T AssertPredicate <T>(T predicate, int classId) where T : IPredicate { Assert.That(predicate.FactoryId, Is.EqualTo(FactoryIds.PredicateFactoryId)); Assert.That(predicate.ClassId, Is.EqualTo(classId)); Assert.Throws <ArgumentNullException>(() => predicate.WriteData(null)); Assert.Throws <ArgumentNullException>(() => predicate.ReadData(null)); using var output = new ByteArrayObjectDataOutput(1024, _serializationService, Endianness.Unspecified); predicate.WriteData(output); T p = default; if (typeof(T) != typeof(PagingPredicate) && typeof(T) != typeof(PartitionPredicate)) { using var input = new ByteArrayObjectDataInput(output.Buffer, _serializationService, Endianness.Unspecified); p = (T)Activator.CreateInstance(typeof(T)); p.ReadData(input); Assert.That(predicate.Equals(p)); Assert.That(predicate.Equals(predicate)); Assert.That(predicate.Equals(null), Is.False); Assert.That(Equals(predicate, p)); Assert.That(Equals(predicate, predicate)); Assert.That(Equals(predicate, null), Is.False); var type = typeof(T); MethodInfo staticEquals; do { staticEquals = type.GetMethod("Equals", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); type = type.BaseType; } while (staticEquals == null && type != typeof(object)); Assert.That(staticEquals, Is.Not.Null); Assert.That((bool)staticEquals.Invoke(null, new object[] { predicate, p })); Assert.That((bool)staticEquals.Invoke(null, new object[] { predicate, predicate })); Assert.That((bool)staticEquals.Invoke(null, new object[] { predicate, null }), Is.False); var data = _serializationService.ToData(predicate); p = _serializationService.ToObject <T>(data); Assert.That(predicate.Equals(p)); } _ = predicate.GetHashCode(); Console.WriteLine($"{typeof(T)}: {predicate}"); return(p); }
private AnchorDataListHolder GetAnchorDataListHolder(ISerializationService serializationService, out List <int> pageList, out List <KeyValuePair <IData, IData> > dataList) { pageList = new List <int> { 0, 1, 2, 3 }; dataList = new List <KeyValuePair <IData, IData> > { new KeyValuePair <IData, IData>(serializationService.ToData(1), serializationService.ToData(2)), new KeyValuePair <IData, IData>(serializationService.ToData(2), serializationService.ToData(3)), new KeyValuePair <IData, IData>(serializationService.ToData(3), serializationService.ToData(4)), new KeyValuePair <IData, IData>(serializationService.ToData(4), serializationService.ToData(5)), }; return(new AnchorDataListHolder(pageList, dataList)); }
// tries to authenticate // returns a result if successful // returns null if failed due to credentials (may want to retry) // throws if anything else went wrong private async ValueTask <AuthenticationResult> TryAuthenticateAsync(MemberConnection client, string clusterName, Guid clusterClientId, string clusterClientName, ISet <string> labels, ICredentialsFactory credentialsFactory, ISerializationService serializationService, CancellationToken cancellationToken) { const string clientType = "CSP"; // CSharp var serializationVersion = serializationService.GetVersion(); var clientVersion = ClientVersion; var credentials = credentialsFactory.NewCredentials(); ClientMessage requestMessage; switch (credentials) { case IPasswordCredentials passwordCredentials: requestMessage = ClientAuthenticationCodec.EncodeRequest(clusterName, passwordCredentials.Name, passwordCredentials.Password, clusterClientId, clientType, serializationVersion, clientVersion, clusterClientName, labels); break; case ITokenCredentials tokenCredentials: requestMessage = ClientAuthenticationCustomCodec.EncodeRequest(clusterName, tokenCredentials.GetToken(), clusterClientId, clientType, serializationVersion, clientVersion, clusterClientName, labels); break; default: var bytes = serializationService.ToData(credentials).ToByteArray(); requestMessage = ClientAuthenticationCustomCodec.EncodeRequest(clusterName, bytes, clusterClientId, clientType, serializationVersion, clientVersion, clusterClientName, labels); break; } cancellationToken.ThrowIfCancellationRequested(); HConsole.WriteLine(this, "Send auth request"); var responseMessage = await client.SendAsync(requestMessage, cancellationToken).CAF(); HConsole.WriteLine(this, "Rcvd auth response"); var response = ClientAuthenticationCodec.DecodeResponse(responseMessage); return((AuthenticationStatus)response.Status switch { AuthenticationStatus.Authenticated => new AuthenticationResult(response.ClusterId, response.MemberUuid, response.Address, response.ServerHazelcastVersion, response.FailoverSupported, response.PartitionCount, response.SerializationVersion), AuthenticationStatus.CredentialsFailed => null, // could want to retry AuthenticationStatus.NotAllowedInCluster => throw new AuthenticationException("Client is not allowed in cluster."), AuthenticationStatus.SerializationVersionMismatch => throw new AuthenticationException("Serialization mismatch."), _ => throw new AuthenticationException($"Received unsupported status code {response.Status}.") });
private void AssertAggregator <TAggregator>(TAggregator aggregator, int classId) where TAggregator : IAggregator { Assert.That(aggregator.FactoryId, Is.EqualTo(FactoryIds.AggregatorDsFactoryId)); Assert.That(aggregator.ClassId, Is.EqualTo(classId)); Assert.Throws <ArgumentException>(() => _ = Aggregator.Count("")); Assert.Throws <ArgumentException>(() => _ = Aggregator.Count(null)); Assert.Throws <ArgumentNullException>(() => aggregator.WriteData(null)); Assert.Throws <ArgumentNullException>(() => aggregator.ReadData(null)); using var output = new ByteArrayObjectDataOutput(1024, _serializationService, Endianness.Unspecified); aggregator.WriteData(output); using var input = new ByteArrayObjectDataInput(output.Buffer, _serializationService, Endianness.Unspecified); var a = (TAggregator)Activator.CreateInstance(typeof(TAggregator)); a.ReadData(input); Assert.That(a.AttributePath, Is.EqualTo(aggregator.AttributePath)); var data = _serializationService.ToData(aggregator); IAggregator x = null; if (typeof(TAggregator).IsGenericType) { // doh - cannot deserialize generic types? if (typeof(TAggregator).GetGenericTypeDefinition() == typeof(MaxAggregator <>)) { x = _serializationService.ToObject <MaxAggregator <object> >(data); } else if (typeof(TAggregator).GetGenericTypeDefinition() == typeof(MinAggregator <>)) { x = _serializationService.ToObject <MinAggregator <object> >(data); } else { Assert.Fail("Unsupported generic aggregator type."); } } else { x = _serializationService.ToObject <TAggregator>(data); } Assert.That(x.AttributePath, Is.EqualTo(aggregator.AttributePath)); }
public bool Contains(T item) { var itemData = serializationService.ToData(item); foreach (var dValue in _content) { if (dValue is IData) { if (itemData.Equals(dValue)) { return(true); } } else { if (itemData.Equals(serializationService.ToData(dValue))) { return(true); } } } return(false); }
private static PagingPredicateHolder OfInternal(PartitionPredicate partitionPredicate, ISerializationService serializationService) { if (partitionPredicate == null) { return(null); } var pagingPredicate = (PagingPredicate)partitionPredicate.GetTarget(); var partitionKeyData = serializationService.ToData(partitionPredicate.GetPartitionKey()); return(BuildHolder(serializationService, pagingPredicate, partitionKeyData)); }
public virtual void TestLargeStringEncodeDecode() { var sb = new StringBuilder(); var i = 0; var j = 0; while (j < TestStrSize) { var ch = i++ % char.MaxValue; if (char.IsLetter((char)ch)) { sb.Append(ch); j++; } } var actualStr = sb.ToString(); var strBytes = Encoding.UTF8.GetBytes(actualStr); var actualDataBytes = _serializationService.ToData(actualStr).ToByteArray(); var expectedDataByte = ToDataByte(strBytes, actualStr.Length); var decodedStr = (string)_serializationService.ToObject <object>(new HeapData(expectedDataByte)); Assert.AreEqual(expectedDataByte, actualDataBytes, "Deserialized byte array do not match utf-8 encoding"); Assert.AreEqual(decodedStr, actualStr); }
internal static void TestDifferentClassVersions(ISerializationService serializationService, ISerializationService serializationService2) { NamedPortable portableV1 = new NamedPortable("named-portable", 123); IData dataV1 = serializationService.ToData(portableV1); NamedPortableV2 portableV2 = new NamedPortableV2("named-portable", 123, 500); IData dataV2 = serializationService2.ToData(portableV2); NamedPortable v1FromV2 = serializationService.ToObject <NamedPortable>(dataV2); Assert.AreEqual(portableV2.name, v1FromV2.name); Assert.AreEqual(portableV2.k, v1FromV2.k); NamedPortableV2 v2FromV1 = serializationService2.ToObject <NamedPortableV2>(dataV1); Assert.AreEqual(portableV1.name, v2FromV1.name); Assert.AreEqual(portableV1.k, v2FromV1.k); Assert.AreEqual(v2FromV1.v, 0); //Assert.IsNull(v2FromV1.v); }
public virtual void TestNestedPortableVersionedSerializer() { SerializationServiceBuilder builder1 = new SerializationServiceBuilder(); builder1.SetPortableVersion(6); builder1.AddPortableFactory(1, new MyPortableFactory()); ISerializationService ss1 = builder1.Build(); SerializationServiceBuilder builder2 = new SerializationServiceBuilder(); builder2.SetPortableVersion(6); builder2.AddPortableFactory(1, new MyPortableFactory()); ISerializationService ss2 = builder2.Build(); //make sure ss2 cached class definition of Child ss2.ToData(new Child("ubeyd")); //serialized parent from ss1 Parent parent = new Parent(new Child("ubeyd")); IData data = ss1.ToData(parent); // cached class definition of Child and the class definition from data coming from ss1 should be compatible Assert.AreEqual(parent, ss2.ToObject <Parent>(data)); }
internal static void TestPreDefinedDifferentVersions(ISerializationService serializationService, ISerializationService serializationService2, MainPortable mainPortable) { var data = serializationService.ToData(mainPortable); Assert.AreEqual(mainPortable, serializationService2.ToObject<MainPortable>(data)); }
/// <exception cref="System.IO.IOException" /> internal static void TestDifferentClassVersionsUsingDataWriteAndRead(ISerializationService serializationService, ISerializationService serializationService2) { var p1 = new NamedPortable("portable-v1", 111); var data = serializationService.ToData(p1); // emulate socket write by writing data to stream var @out = serializationService.CreateObjectDataOutput(1024); @out.WriteData(data); var bytes = @out.ToByteArray(); // emulate socket read by reading data from stream var @in = serializationService2.CreateObjectDataInput(bytes); data = @in.ReadData(); // read data var object1 = serializationService2.ToObject<object>(data); // serialize new portable version var p2 = new NamedPortableV2("portable-v2", 123); var data2 = serializationService2.ToData(p2); // de-serialize back using old version var object2 = serializationService.ToObject<object>(data2); Assert.IsNotNull(object1, "object1 should not be null!"); Assert.IsNotNull(object2, "object2 should not be null!"); Assert.IsInstanceOf<NamedPortableV2>(object1, "Should be instance of NamedPortableV2: " + object1.GetType()); Assert.IsInstanceOf<NamedPortable>(object2, "Should be instance of NamedPortable: " + object2.GetType()); }
internal static void TestDifferentClassVersions(ISerializationService serializationService, ISerializationService serializationService2) { var p1 = new NamedPortable("named-portable", 123); var data = serializationService.ToData(p1); var p2 = new NamedPortableV2("named-portable", 123); var data2 = serializationService2.ToData(p2); var o1 = serializationService2.ToObject<NamedPortableV2>(data); var o2 = serializationService.ToObject<NamedPortable>(data2); Assert.AreEqual(o1.name, o2.name); }
public bool Contains(T item) { var data = serializationService.ToData(item); return(list.Contains(data)); }
private static void AssertRepeatedSerialisationGivesSameByteArrays(ISerializationService ss, IPortable p) { var data1 = ss.ToData(p); for (var k = 0; k < 100; k++) { var data2 = ss.ToData(p); Assert.AreEqual(data1, data2); } }