Beispiel #1
0
        private static PagingPredicateHolder BuildHolder(SerializationService 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);
        }
Beispiel #3
0
        private static void AssertRepeatedSerialisationGivesSameByteArrays(SerializationService ss, IPortable p)
        {
            var data1 = ss.ToData(p);

            for (var k = 0; k < 100; k++)
            {
                var data2 = ss.ToData(p);
                Assert.AreEqual(data1, data2);
            }
        }
Beispiel #4
0
        public static PagingPredicateHolder Of(IPredicate predicate, SerializationService 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.");
        }
Beispiel #5
0
        private async Task <IReadOnlyList <TValue> > GetValuesAsync(IPredicate predicate, CancellationToken cancellationToken)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }
            var pagingPredicate = UnwrapPagingPredicate(predicate);

            if (pagingPredicate != null)
            {
                pagingPredicate.IterationType = IterationType.Value;

                var pagingPredicateHolder = PagingPredicateHolder.Of(predicate, SerializationService);
                var requestMessage        = MapValuesWithPagingPredicateCodec.EncodeRequest(Name, pagingPredicateHolder);
                var responseMessage       = await Cluster.Messaging.SendAsync(requestMessage, cancellationToken).CAF();

                var response = MapValuesWithPagingPredicateCodec.DecodeResponse(responseMessage);
                pagingPredicate.UpdateAnchors(response.AnchorDataList.AsAnchorIterator(SerializationService));
                return(new ReadOnlyLazyList <TValue>(response.Response, SerializationService));
            }

            {
                var requestMessage  = MapValuesWithPredicateCodec.EncodeRequest(Name, ToData(predicate));
                var responseMessage = await(predicate is PartitionPredicate pp
                    ? Cluster.Messaging.SendToKeyPartitionOwnerAsync(requestMessage, SerializationService.ToData(pp.PartitionKey), cancellationToken)
                    : Cluster.Messaging.SendAsync(requestMessage, cancellationToken))
                                      .CAF();
                var response = MapValuesWithPredicateCodec.DecodeResponse(responseMessage).Response;
                return(new ReadOnlyLazyList <TValue>(response, SerializationService));
            }
        }
        internal static void TestPreDefinedDifferentVersions(SerializationService serializationService,
                                                             SerializationService serializationService2, MainPortable mainPortable)
        {
            var data = serializationService.ToData(mainPortable);

            Assert.AreEqual(mainPortable, serializationService2.ToObject <MainPortable>(data));
        }
        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));
 }
        private static string GetKey(int partitionId, int partitionCount, SerializationService serializationService)
        {
            int GetHash(string value) => serializationService.ToData(value).PartitionHash;

            var key = "key0";

            for (var i = 1; i < 100 && GetHash(key) % partitionCount != partitionId; i++)
            {
                key = "key" + i;
            }
            return(key);
        }
Beispiel #10
0
        private T AssertPredicate <T>(T predicate, int classId)
            where T : IIdentifiedDataSerializable
        {
            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 ObjectDataOutput(1024, _serializationService, Endianness.BigEndian);
            predicate.WriteData(output);

            T p = default;

            if (typeof(T) != typeof(PagingPredicate) && typeof(T) != typeof(PartitionPredicate))
            {
                using var input = new ObjectDataInput(output.Buffer, _serializationService, Endianness.BigEndian);
                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);
        }
        // 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, 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).CfAwait();

            HConsole.WriteLine(this, "Rcvd auth response");
            var response = ClientAuthenticationCodec.DecodeResponse(responseMessage);

            HConsole.WriteLine(this, "Auth response is: " + (AuthenticationStatus)response.Status);

            return((AuthenticationStatus)response.Status switch
            {
                AuthenticationStatus.Authenticated
                => new AuthenticationResult(response.ClusterId, response.MemberUuid, response.Address, response.ServerHazelcastVersion, response.FailoverSupported, response.PartitionCount, response.SerializationVersion, credentials.Name),

                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}.")
            });
Beispiel #12
0
        private void AssertAggregator <TResult>(AggregatorBase <TResult> aggregator, int classId)
        {
            var aggregatorType = aggregator.GetType();

            Assert.That(aggregator.FactoryId, Is.EqualTo(FactoryIds.AggregatorDsFactoryId));
            Assert.That(aggregator.ClassId, Is.EqualTo(classId));

            Assert.Throws <ArgumentException>(() => _ = Aggregators.Count(""));
            Assert.Throws <ArgumentException>(() => _ = Aggregators.Count(null));

            Assert.Throws <ArgumentNullException>(() => aggregator.WriteData(null));
            Assert.Throws <ArgumentNullException>(() => aggregator.ReadData(null));

            using var output = new ObjectDataOutput(1024, _serializationService, Endianness.BigEndian);
            aggregator.WriteData(output);

            using var input = new ObjectDataInput(output.Buffer, _serializationService, Endianness.BigEndian);
            var a = (AggregatorBase <TResult>)Activator.CreateInstance(aggregatorType);

            a.ReadData(input);

            Assert.That(a.AttributePath, Is.EqualTo(aggregator.AttributePath));

            var data = _serializationService.ToData(aggregator);

            if (aggregatorType.IsGenericType)
            {
                // doh - cannot deserialize generic types?
                IAggregator <object> x = null;

                if (aggregatorType.GetGenericTypeDefinition() == typeof(MaxAggregator <>))
                {
                    x = _serializationService.ToObject <MaxAggregator <object> >(data);
                }
                else if (aggregatorType.GetGenericTypeDefinition() == typeof(MinAggregator <>))
                {
                    x = _serializationService.ToObject <MinAggregator <object> >(data);
                }
                else
                {
                    Assert.Fail("Unsupported generic aggregator type.");
                }

                Assert.That(x.AttributePath, Is.EqualTo(aggregator.AttributePath));
            }
            else
            {
                var x = _serializationService.ToObject <IAggregator <TResult> >(data);
                Assert.That(x.AttributePath, Is.EqualTo(aggregator.AttributePath));
            }
        }
        private AnchorDataListHolder GetAnchorDataListHolder(SerializationService 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));
        }
 public virtual void Before()
 {
     service1 = (SerializationService) new SerializationServiceBuilder()
         .AddPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID, new PortableFactoryFunc(
             i => new MorphingBasePortable())).Build();
     service2 = (SerializationService) new SerializationServiceBuilder()
         .AddPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID,
             new PortableFactoryFunc(
                 i => new MorphingPortable())).Build();
     var data = service1.ToData(new MorphingBasePortable(unchecked(1), true, (char) 2, 3, 4, 5, 1f, 2d, "test"));
     var input = service2.CreateObjectDataInput(data);
     var portableSerializer = service2.GetPortableSerializer();
     reader = portableSerializer.CreateMorphingReader(input);
 }
        public virtual void Before()
        {
            service1 = (SerializationService) new SerializationServiceBuilder(new NullLoggerFactory())
                       .AddPortableFactory(SerializationTestsConstants.PORTABLE_FACTORY_ID, new PortableFactoryFunc(
                                               i => new MorphingPortableBase())).Build();
            service2 = (SerializationService) new SerializationServiceBuilder(new NullLoggerFactory())
                       .AddPortableFactory(SerializationTestsConstants.PORTABLE_FACTORY_ID,
                                           new PortableFactoryFunc(
                                               i => new MorphingPortable())).Build();
            var data  = service1.ToData(new MorphingPortableBase(unchecked (1), true, (char)2, 3, 4, 5, 1f, 2d, "test"));
            var input = service2.CreateObjectDataInput(data);
            var portableSerializer = service2.PortableSerializer;

            reader = portableSerializer.CreateMorphingReader(input);
        }
Beispiel #16
0
        public 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);
            var decodedStr       = (string)_serializationService.ToObject <object>(new HeapData(expectedDataByte));

            Assert.AreEqual(decodedStr, actualStr);
            Assert.AreEqual(expectedDataByte, actualDataBytes, "Deserialized byte array do not match utf-8 encoding");
        }
        /// <exception cref="System.IO.IOException" />
        internal static void TestDifferentClassVersionsUsingDataWriteAndRead(SerializationService serializationService,
                                                                             SerializationService serializationService2)
        {
            var portableV1 = new NamedPortable("portable-v1", 111);
            var dataV1     = serializationService.ToData(portableV1);

            // 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 TestDifferentClassVersions(SerializationService serializationService,
                                                        SerializationService 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(new NullLoggerFactory());

            builder1.SetPortableVersion(6);
            builder1.AddPortableFactory(1, new MyPortableFactory());
            SerializationService ss1 = builder1.Build();

            SerializationServiceBuilder builder2 = new SerializationServiceBuilder(new NullLoggerFactory());

            builder2.SetPortableVersion(6);
            builder2.AddPortableFactory(1, new MyPortableFactory());
            SerializationService 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));
        }