Пример #1
0
        /// <summary>
        /// Initialize a new Pool and Cluster instance with the specified contact points.
        /// The cluster will be initialized with a default configuration.
        /// </summary>
        internal MetaCluster(ArrayRig <IPEndPoint> entryPoints, string username = null, string password = null)
        {
            _lock     = new Lock();
            Keyspaces = new Dictionary <string, Keyspace>();

            // reference the entry points
            EntryPoints = entryPoints;

            // get the existing type definitions
            var typeDefinitions = new Cassandra.Serialization.TypeSerializerDefinitions();

            foreach (Type type in Generic.GetTypes <Cassandra.Serialization.TypeSerializer>())
            {
                // if the type is in the Cassandra assembly - skip
                if (type.FullName.StartsWith("Cassandra.Serialization.", StringComparison.Ordinal))
                {
                    continue;
                }

                // call 'Define' for all type definitions
                var  methodInfo = typeDefinitions.GetType().GetMethod("Define", BindingFlags.Instance | BindingFlags.Public);
                Type baseType   = type.BaseType;
                while (!baseType.IsGenericType)
                {
                    baseType = baseType.BaseType;
                }
                methodInfo = methodInfo.MakeGenericMethod(baseType.GetGenericArguments()[0]);

                methodInfo.Invoke(typeDefinitions, new [] {
                    type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, CallingConventions.Any, new Type[0], null).Invoke(null)
                });
            }

            // initialize a cluster connection
            Builder builder = Cluster.Builder()
                              .WithTypeSerializers(typeDefinitions)
                              .WithLoadBalancingPolicy(new LoadBalancer(this))
                              .WithCompression(CompressionType.Snappy)
                              .AddContactPoints(entryPoints.ToArray());

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                builder = builder.WithCredentials(username, password);
            }

            Cluster = builder.Build();

            if (Cluster == null)
            {
                Log.Warning("The cluster builder failed in MetaCluster.");
            }
        }
Пример #2
0
        //-------------------------------------------//

        /// <summary>
        /// Initialize a new Cluster instance with the specified contact point addresses.
        /// A Cassandra.Cluster will be initialized with a default configuration.
        /// </summary>
        internal MetaCluster(ArrayRig <string> entryPointAddresses, int port = 0, string username = null, string password = null)
        {
            _lock     = new Lock();
            Keyspaces = new Dictionary <string, Keyspace>();

            // initialize a collection of end points
            EntryPoints = new ArrayRig <IPEndPoint>(entryPointAddresses.Count);
            foreach (var entryPointString in entryPointAddresses)
            {
                IPAddress  address;
                IPEndPoint endpoint;
                if (entryPointString.TryParseEndPoint(out endpoint))
                {
                    EntryPoints.Add(endpoint);
                }
                else if (port == 0)
                {
                    Log.Warning("An ip address could not be derived : '" + entryPointString + "'.");
                }
                else if (IPAddress.TryParse(entryPointString, out address))
                {
                    endpoint = new IPEndPoint(address, port);
                    EntryPoints.Add(endpoint);
                }
            }

            if (EntryPoints.Count == 0)
            {
                Log.Warning("A cluster wasn't initialized. There were zero valid entry points.");
                return;
            }

            // get the existing type definitions
            var typeDefinitions = new Cassandra.Serialization.TypeSerializerDefinitions();

            foreach (Type type in Generic.GetTypes <Cassandra.Serialization.TypeSerializer>(type => !type.IsAbstract && !type.IsInterface))
            {
                // call 'Define' for all type definitions
                var methodInfo = typeDefinitions.GetType().GetMethod("Define", BindingFlags.Public);
                var types      = type.GetGenericArguments();
                if (types.Length != 0)
                {
                    methodInfo = methodInfo.MakeGenericMethod(types[0]);
                    methodInfo.Invoke(typeDefinitions, new [] { Activator.CreateInstance(type, false) });
                }
            }

            // initialize a cluster connection
            Builder builder = Cluster.Builder()
                              .WithTypeSerializers(typeDefinitions)
                              .WithLoadBalancingPolicy(new LoadBalancer(this))
                              .WithCompression(CompressionType.Snappy)
                              .AddContactPoints(EntryPoints.ToArray());

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                builder = builder.WithCredentials(username, password);
            }

            Cluster = builder.Build();
        }