Exemplo n.º 1
0
 private RowSet ProcessRowset(IOutput outp, string originCqlQuery)
 {
     using (outp)
     {
         if (outp is OutputError)
         {
             DriverException ex = (outp as OutputError).CreateException();
             _logger.Error(ex);
             throw ex;
         }
         else if (outp is OutputVoid)
         {
             return(null);
         }
         else if (outp is OutputSchemaChange)
         {
             return(null);
         }
         else if (outp is OutputRows)
         {
             var queryHandler = new QueryRequestHandler();
             return(queryHandler.ProcessResponse(outp, _session));
         }
         else
         {
             var ex = new DriverInternalError("Unexpected output kind");
             _logger.Error(ex);
             throw ex;
         }
     }
 }
        /// <summary>
        /// Gets the definition of a User defined type
        /// </summary>
        public UdtColumnInfo GetUdtDefinition(string keyspace, string typeName)
        {
            var rs  = Query(String.Format(SelectUdts + " WHERE keyspace_name='{0}' AND type_name = '{1}';", keyspace, typeName));
            var row = rs.FirstOrDefault();

            if (row == null)
            {
                return(null);
            }
            var udt        = new UdtColumnInfo(row.GetValue <string>("keyspace_name") + "." + row.GetValue <string>("type_name"));
            var fieldNames = row.GetValue <List <string> >("field_names");
            var fieldTypes = row.GetValue <List <string> >("field_types");

            if (fieldNames.Count != fieldTypes.Count)
            {
                var ex = new DriverInternalError("Field names and types for UDT do not match");
                _logger.Error(ex);
                throw ex;
            }
            for (var i = 0; i < fieldNames.Count; i++)
            {
                var field = TypeCodec.ParseDataType(fieldTypes[i]);
                field.Name = fieldNames[i];
                udt.Fields.Add(field);
            }
            return(udt);
        }
Exemplo n.º 3
0
 private void ProcessPrepareQuery(IOutput outp, out RowSetMetadata metadata, out byte[] queryId)
 {
     using (outp)
     {
         if (outp is OutputError)
         {
             var ex = (outp as OutputError).CreateException();
             _logger.Error(ex);
             throw ex;
         }
         else if (outp is OutputPrepared)
         {
             queryId  = (outp as OutputPrepared).QueryID;
             metadata = (outp as OutputPrepared).Metadata;
             _logger.Info("Prepared Query has been successfully processed.");
             return; //ok
         }
         else
         {
             var ex = new DriverInternalError("Unexpected output kind");
             _logger.Error("Prepared Query has returned an unexpected output kind.", ex);
             throw ex;
         }
     }
 }
        private void conn_CassandraEvent(object sender, CassandraEventArgs e)
        {
            if (e is TopologyChangeEventArgs)
            {
                var tce = e as TopologyChangeEventArgs;
                if (tce.What == TopologyChangeEventArgs.Reason.NewNode)
                {
                    SetupControlConnection(true);
                    _cluster.Metadata.AddHost(tce.Address);
                    return;
                }
                else if (tce.What == TopologyChangeEventArgs.Reason.RemovedNode)
                {
                    _cluster.Metadata.RemoveHost(tce.Address);
                    SetupControlConnection(!tce.Address.Equals(listeningOnHost));
                    return;
                }
            }
            else if (e is StatusChangeEventArgs)
            {
                var sce = e as StatusChangeEventArgs;
                if (sce.What == StatusChangeEventArgs.Reason.Up)
                {
                    _cluster.Metadata.BringUpHost(sce.Address, this);
                    return;
                }
                else if (sce.What == StatusChangeEventArgs.Reason.Down)
                {
                    _cluster.Metadata.SetDownHost(sce.Address, this);
                    return;
                }
            }
            else if (e is SchemaChangeEventArgs)
            {
                var ssc = e as SchemaChangeEventArgs;

                if (ssc.What == SchemaChangeEventArgs.Reason.Created)
                {
                    SubmitSchemaRefresh(string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, null);
                    return;
                }
                else if (ssc.What == SchemaChangeEventArgs.Reason.Dropped)
                {
                    SubmitSchemaRefresh(string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, null);
                    return;
                }
                else if (ssc.What == SchemaChangeEventArgs.Reason.Updated)
                {
                    SubmitSchemaRefresh(ssc.Keyspace, string.IsNullOrEmpty(ssc.Table) ? null : ssc.Table);
                    return;
                }
            }

            var ex = new DriverInternalError("Unknown Event");

            _logger.Error(ex);
            throw ex;
        }
Exemplo n.º 5
0
        private RowSet ProcessRowset(IOutput outp)
        {
            bool ok = false;

            try
            {
                if (outp is OutputError)
                {
                    var ex = (outp as OutputError).CreateException();
                    _logger.Error(ex);
                    throw ex;
                }
                else if (outp is OutputVoid)
                {
                    return(new RowSet(outp as OutputVoid, this));
                }
                else if (outp is OutputSchemaChange)
                {
                    return(new RowSet(outp as OutputSchemaChange, this));
                }
                else if (outp is OutputSetKeyspace)
                {
                    SetKeyspace((outp as OutputSetKeyspace).Value);
                    return(new RowSet(outp as OutputSetKeyspace, this));
                }
                else if (outp is OutputRows)
                {
                    ok = true;
                    return(new RowSet(outp as OutputRows, this, true));
                }
                else
                {
                    var ex = new DriverInternalError("Unexpected output kind");
                    _logger.Error(ex);
                    throw ex;
                }
            }
            finally
            {
                if (!ok)
                {
                    outp.Dispose();
                }
            }
        }
Exemplo n.º 6
0
        private void SetupEventListener()
        {
            var triedHosts      = new List <IPAddress>();
            var innerExceptions = new Dictionary <IPAddress, List <Exception> >();

            var hostsIter = _session._policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();

            if (!hostsIter.MoveNext())
            {
                var ex = new NoHostAvailableException(new Dictionary <IPAddress, List <Exception> >());
                _logger.Error(ex);
                throw ex;
            }

            int streamId;
            var nconn = _session.Connect(null, hostsIter, triedHosts, innerExceptions, out streamId);

            listeningOnHost = nconn.GetHostAdress();

            Exception theExc = null;

            nconn.CassandraEvent += new CassandraEventHandler(conn_CassandraEvent);
            using (var ret = nconn.RegisterForCassandraEvent(streamId,
                                                             CassandraEventType.TopologyChange | CassandraEventType.StatusChange | CassandraEventType.SchemaChange))
            {
                if (!(ret is OutputVoid))
                {
                    if (ret is OutputError)
                    {
                        theExc = (ret as OutputError).CreateException();
                    }
                    else
                    {
                        theExc = new DriverInternalError("Expected Error on Output");
                    }
                }
            }

            if (theExc != null)
            {
                _logger.Error(theExc);
                throw theExc;
            }
        }
Exemplo n.º 7
0
        private void SetupEventListener()
        {
            var triedHosts      = new List <IPAddress>();
            var innerExceptions = new Dictionary <IPAddress, List <Exception> >();

            IEnumerator <Host> hostsIter = _session.Policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();

            if (!hostsIter.MoveNext())
            {
                var ex = new NoHostAvailableException(new Dictionary <IPAddress, List <Exception> >());
                _logger.Error(ex);
                throw ex;
            }

            _activeConnection.Value = _session.Connect(hostsIter, triedHosts, innerExceptions, out _lockingStreamId);

            int streamId = _activeConnection.Value.AllocateStreamId();

            Exception theExc = null;

            _activeConnection.Value.CassandraEvent += conn_CassandraEvent;
            using (IOutput ret = _activeConnection.Value.RegisterForCassandraEvent(streamId,
                                                                                   CassandraEventType.TopologyChange | CassandraEventType.StatusChange |
                                                                                   CassandraEventType.SchemaChange))
            {
                if (!(ret is OutputVoid))
                {
                    if (ret is OutputError)
                    {
                        theExc = (ret as OutputError).CreateException();
                    }
                    else
                    {
                        theExc = new DriverInternalError("Expected Error on Output");
                    }
                }
            }

            if (theExc != null)
            {
                _logger.Error(theExc);
                throw theExc;
            }
        }
Exemplo n.º 8
0
 private IDictionary <string, string[]> ProcessExecuteOptions(IOutput outp)
 {
     using (outp)
     {
         if (outp is OutputError)
         {
             var ex = (outp as OutputError).CreateException();
             _logger.Error(ex);
             throw ex;
         }
         else if (outp is OutputOptions)
         {
             return((outp as OutputOptions).Options);
         }
         else
         {
             var ex = new DriverInternalError("Unexpected output kind");
             _logger.Error("Prepared Query has returned an unexpected output kind.", ex);
             throw ex;
         }
     }
 }
Exemplo n.º 9
0
        private void SetupEventListener()
        {
            var triedHosts = new List<IPAddress>();
            var innerExceptions = new Dictionary<IPAddress, List<Exception>>();

            var hostsIter = _session._policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();

            if (!hostsIter.MoveNext())
            {
                var ex = new NoHostAvailableException(new Dictionary<IPAddress, List<Exception>>());
                _logger.Error(ex);
                throw ex;
            }

            _activeConnection.Value = _session.Connect(hostsIter, triedHosts, innerExceptions, out _lockingStreamId);

            int streamId = _activeConnection.Value.AllocateStreamId();

            Exception theExc = null;

            _activeConnection.Value.CassandraEvent += new CassandraEventHandler(conn_CassandraEvent);
            using (var ret = _activeConnection.Value.RegisterForCassandraEvent(streamId,
                CassandraEventType.TopologyChange | CassandraEventType.StatusChange | CassandraEventType.SchemaChange))
            {
                if (!(ret is OutputVoid))
                {
                    if (ret is OutputError)
                        theExc = (ret as OutputError).CreateException();
                    else
                        theExc = new DriverInternalError("Expected Error on Output");
                }
            }

            if (theExc != null)
            {
                _logger.Error(theExc);
                throw theExc;
            }
        }
Exemplo n.º 10
0
 private IDictionary<string, string[]> ProcessExecuteOptions(IOutput outp)
 {
     using (outp)
     {
         if (outp is OutputError)
         {
             var ex = (outp as OutputError).CreateException();
             _logger.Error(ex);
             throw ex;
         }
         else if (outp is OutputOptions)
         {
             return (outp as OutputOptions).Options;
         }
         else
         {
             var ex = new DriverInternalError("Unexpected output kind");
             _logger.Error("Prepared Query has returned an unexpected output kind.", ex);
             throw ex;
         }
     }
 }
Exemplo n.º 11
0
        private ColumnTypeCode convertToColumnTypeCode(string type, out object[] collectionValueTp)
        {
            object[] obj;
            collectionValueTp = new object[2];
            if (type.StartsWith("org.apache.cassandra.db.marshal.ListType"))
            {
                collectionValueTp[0] = convertToColumnTypeCode(type.Replace("org.apache.cassandra.db.marshal.ListType(", "").Replace(")", ""), out obj);
                return ColumnTypeCode.List;
            }
            if (type.StartsWith("org.apache.cassandra.db.marshal.SetType"))
            {
                collectionValueTp[0] = convertToColumnTypeCode(type.Replace("org.apache.cassandra.db.marshal.SetType(", "").Replace(")", ""), out obj);
                return ColumnTypeCode.Set;
            }

            if (type.StartsWith("org.apache.cassandra.db.marshal.MapType"))
            {
                collectionValueTp[0] = convertToColumnTypeCode(type.Replace("org.apache.cassandra.db.marshal.MapType(", "").Replace(")", "").Split(',')[0], out obj);
                collectionValueTp[1] = convertToColumnTypeCode(type.Replace("org.apache.cassandra.db.marshal.MapType(", "").Replace(")", "").Split(',')[1], out obj);
                return ColumnTypeCode.Map;
            }

            collectionValueTp = null;
            switch (type)
            {
                case "org.apache.cassandra.db.marshal.UTF8Type":
                    return ColumnTypeCode.Text;
                case "org.apache.cassandra.db.marshal.UUIDType":
                    return ColumnTypeCode.Uuid;
                case "org.apache.cassandra.db.marshal.Int32Type":
                    return ColumnTypeCode.Int;
                case "org.apache.cassandra.db.marshal.BytesType":
                    return ColumnTypeCode.Blob;
                case "org.apache.cassandra.db.marshal.FloatType":
                    return ColumnTypeCode.Float;
                case "org.apache.cassandra.db.marshal.DoubleType":
                    return ColumnTypeCode.Double;
                case "org.apache.cassandra.db.marshal.BooleanType":
                    return ColumnTypeCode.Boolean;
                case "org.apache.cassandra.db.marshal.InetAddressType":
                    return ColumnTypeCode.Inet;
                case "org.apache.cassandra.db.marshal.DateType":
                    return ColumnTypeCode.Timestamp;
                case "org.apache.cassandra.db.marshal.LongType":
                    return ColumnTypeCode.Bigint;
                case "org.apache.cassandra.db.marshal.DecimalType":
                    return ColumnTypeCode.Decimal;
                case "org.apache.cassandra.db.marshal.IntegerType":
                    return ColumnTypeCode.Varint;
                default:
                    var ex = new DriverInternalError("Unsupported data type:" + type);
                    _logger.Error(string.Format("Unsupported data type: {0}", type), ex);
                    throw ex;
            }
        }
Exemplo n.º 12
0
 private RowSet ProcessRowset(IOutput outp)
 {
     bool ok = false;
     try
     {
         if (outp is OutputError)
         {
             var ex = (outp as OutputError).CreateException();
             _logger.Error(ex);
             throw ex;
         }
         else if (outp is OutputVoid)
             return null;
         else if (outp is OutputSchemaChange)
             return null;
         else if (outp is OutputRows)
         {
             ok = true;
             return new RowSet(outp as OutputRows, null, true);
         }
         else
         {
             var ex = new DriverInternalError("Unexpected output kind");
             _logger.Error(ex);
             throw ex;
         }
     }
     finally
     {
         if (!ok)
             outp.Dispose();
     }
 }
Exemplo n.º 13
0
 /// <summary>
 /// Gets the definition of a User defined type
 /// </summary>
 public UdtColumnInfo GetUdtDefinition(string keyspace, string typeName)
 {
     var rs = Query(String.Format(SelectUdts + " WHERE keyspace_name='{0}' AND type_name = '{1}';", keyspace, typeName));
     var row = rs.FirstOrDefault();
     if (row == null)
     {
         return null;
     }
     var udt = new UdtColumnInfo(row.GetValue<string>("keyspace_name") + "." + row.GetValue<string>("type_name"));
     var fieldNames = row.GetValue<List<string>>("field_names");
     var fieldTypes = row.GetValue<List<string>>("field_types");
     if (fieldNames.Count != fieldTypes.Count)
     {
         var ex = new DriverInternalError("Field names and types for UDT do not match");
         _logger.Error(ex);
         throw ex;
     }
     for (var i = 0; i < fieldNames.Count; i++)
     {
         var field = TypeCodec.ParseDataType(fieldTypes[i]);
         field.Name = fieldNames[i];
         udt.Fields.Add(field);
     }
     return udt;
 }
Exemplo n.º 14
0
        private void conn_CassandraEvent(object sender, CassandraEventArgs e)
        {
            var act = new Action<object>((_) =>
            {
                if (e is TopologyChangeEventArgs)
                {
                    var tce = e as TopologyChangeEventArgs;
                    if (tce.What == TopologyChangeEventArgs.Reason.NewNode)
                    {
                        SetupControlConnection(true);
                        _cluster.Metadata.AddHost(tce.Address);
                        return;
                    }
                    else if (tce.What == TopologyChangeEventArgs.Reason.RemovedNode)
                    {
                        _cluster.Metadata.RemoveHost(tce.Address);
                        SetupControlConnection(_activeConnection.Value == null ? false : !tce.Address.Equals(_activeConnection.Value.GetHostAdress()));
                        return;
                    }
                }
                else if (e is StatusChangeEventArgs)
                {
                    var sce = e as StatusChangeEventArgs;
                    if (sce.What == StatusChangeEventArgs.Reason.Up)
                    {
                        _cluster.Metadata.BringUpHost(sce.Address, this);
                        return;
                    }
                    else if (sce.What == StatusChangeEventArgs.Reason.Down)
                    {
                        _cluster.Metadata.SetDownHost(sce.Address, this);
                        return;
                    }
                }
                else if (e is SchemaChangeEventArgs)
                {
                    var ssc = e as SchemaChangeEventArgs;

                    if (ssc.What == SchemaChangeEventArgs.Reason.Created)
                    {
                        SubmitSchemaRefresh(string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, null);
                        _cluster.Metadata.FireSchemaChangedEvent(SchemaChangedEventArgs.Kind.Created, string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, ssc.Table);
                        return;
                    }
                    else if (ssc.What == SchemaChangeEventArgs.Reason.Dropped)
                    {
                        SubmitSchemaRefresh(string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, null);
                        _cluster.Metadata.FireSchemaChangedEvent(SchemaChangedEventArgs.Kind.Dropped, string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, ssc.Table);
                        return;
                    }
                    else if (ssc.What == SchemaChangeEventArgs.Reason.Updated)
                    {
                        SubmitSchemaRefresh(ssc.Keyspace, string.IsNullOrEmpty(ssc.Table) ? null : ssc.Table);
                        _cluster.Metadata.FireSchemaChangedEvent(SchemaChangedEventArgs.Kind.Updated, string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, ssc.Table);
                        return;
                    }
                }

                var ex = new DriverInternalError("Unknown Event");
                _logger.Error(ex);
                throw ex;
            });
            act.BeginInvoke(null, (ar) => { act.EndInvoke(ar); }, null);
        }
        private void conn_CassandraEvent(object sender, CassandraEventArgs e)
        {
            if (e is TopologyChangeEventArgs)
            {
                var tce = e as TopologyChangeEventArgs;
                if (tce.What == TopologyChangeEventArgs.Reason.NewNode)
                {
                    if (CCEvent != null)
                        CCEvent.Invoke(this, new CCEventArgs() {IPAddress = tce.Address, What = CCEventArgs.Kind.Add});
                    CheckConnectionUp(tce.Address);
                    return;
                }
                else if (tce.What == TopologyChangeEventArgs.Reason.RemovedNode)
                {
                    if (CCEvent != null)
                        CCEvent.Invoke(this, new CCEventArgs() { IPAddress = tce.Address, What = CCEventArgs.Kind.Remove });
                    CheckConnectionDown(tce.Address);
                    return;
                }
            }
            else if (e is StatusChangeEventArgs)
            {
                var sce = e as StatusChangeEventArgs;
                if (sce.What == StatusChangeEventArgs.Reason.Up)
                {
                    if (CCEvent != null)
                        CCEvent.Invoke(this, new CCEventArgs() { IPAddress = sce.Address, What = CCEventArgs.Kind.Add });
                    CheckConnectionUp(sce.Address);
                    return;
                }
                else if (sce.What == StatusChangeEventArgs.Reason.Down)
                {
                    if (CCEvent != null)
                        CCEvent.Invoke(this, new CCEventArgs() { IPAddress = sce.Address, What = CCEventArgs.Kind.Down });
                    CheckConnectionDown(sce.Address);
                    return;
                }
            }
            else if (e is SchemaChangeEventArgs)
            {
                var ssc = e as SchemaChangeEventArgs;

                if (ssc.What == SchemaChangeEventArgs.Reason.Created)
                {
                    SubmitSchemaRefresh(string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, null);
                    return;
                }
                else if (ssc.What == SchemaChangeEventArgs.Reason.Dropped)
                {
                    SubmitSchemaRefresh(string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, null);
                    return;
                }
                else if (ssc.What == SchemaChangeEventArgs.Reason.Updated)
                {
                    SubmitSchemaRefresh(ssc.Keyspace, string.IsNullOrEmpty(ssc.Table) ? null : ssc.Table);
                    return;
                }
            }

            var ex = new DriverInternalError("Unknown Event");
            _logger.Error(ex);
            throw ex;
        }
        private void SetupEventListeners(CassandraConnection nconn)
        {
            Exception theExc = null;

            nconn.CassandraEvent += new CassandraEventHandler(conn_CassandraEvent);
            using (var ret = nconn.RegisterForCassandraEvent(
                CassandraEventType.TopologyChange | CassandraEventType.StatusChange | CassandraEventType.SchemaChange))
            {
                if (!(ret is OutputVoid))
                {
                    if (ret is OutputError)
                        theExc = (ret as OutputError).CreateException();
                    else
                        theExc = new DriverInternalError("Expected Error on Output");
                }
            }

            if (theExc != null)
            {
                _logger.Error(theExc);
                throw theExc;
            }
        }
Exemplo n.º 17
0
        private void conn_CassandraEvent(object sender, CassandraEventArgs e)
        {
            var act = new Action <object>((_) =>
            {
                if (e is TopologyChangeEventArgs)
                {
                    var tce = e as TopologyChangeEventArgs;
                    if (tce.What == TopologyChangeEventArgs.Reason.NewNode)
                    {
                        SetupControlConnection(true);
                        _cluster.Metadata.AddHost(tce.Address);
                        return;
                    }
                    else if (tce.What == TopologyChangeEventArgs.Reason.RemovedNode)
                    {
                        _cluster.Metadata.RemoveHost(tce.Address);
                        SetupControlConnection(_activeConnection.Value == null ? false : !tce.Address.Equals(_activeConnection.Value.GetHostAdress()));
                        return;
                    }
                }
                else if (e is StatusChangeEventArgs)
                {
                    var sce = e as StatusChangeEventArgs;
                    if (sce.What == StatusChangeEventArgs.Reason.Up)
                    {
                        _cluster.Metadata.BringUpHost(sce.Address, this);
                        return;
                    }
                    else if (sce.What == StatusChangeEventArgs.Reason.Down)
                    {
                        _cluster.Metadata.SetDownHost(sce.Address, this);
                        return;
                    }
                }
                else if (e is SchemaChangeEventArgs)
                {
                    var ssc = e as SchemaChangeEventArgs;

                    if (ssc.What == SchemaChangeEventArgs.Reason.Created)
                    {
                        SubmitSchemaRefresh(string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, null);
                        return;
                    }
                    else if (ssc.What == SchemaChangeEventArgs.Reason.Dropped)
                    {
                        SubmitSchemaRefresh(string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, null);
                        return;
                    }
                    else if (ssc.What == SchemaChangeEventArgs.Reason.Updated)
                    {
                        SubmitSchemaRefresh(ssc.Keyspace, string.IsNullOrEmpty(ssc.Table) ? null : ssc.Table);
                        return;
                    }
                }

                var ex = new DriverInternalError("Unknown Event");
                _logger.Error(ex);
                throw ex;
            });

            act.BeginInvoke(null, (ar) => { act.EndInvoke(ar); }, null);
        }
Exemplo n.º 18
0
        private ColumnTypeCode convertToColumnTypeCode(string type, out object[] collectionValueTp)
        {
            object[] obj;
            collectionValueTp = new object[2];
            if (type.StartsWith("org.apache.cassandra.db.marshal.ListType"))
            {
                collectionValueTp[0] = convertToColumnTypeCode(type.Replace("org.apache.cassandra.db.marshal.ListType(", "").Replace(")", ""), out obj);
                return(ColumnTypeCode.List);
            }
            if (type.StartsWith("org.apache.cassandra.db.marshal.SetType"))
            {
                collectionValueTp[0] = convertToColumnTypeCode(type.Replace("org.apache.cassandra.db.marshal.SetType(", "").Replace(")", ""), out obj);
                return(ColumnTypeCode.Set);
            }

            if (type.StartsWith("org.apache.cassandra.db.marshal.MapType"))
            {
                collectionValueTp[0] = convertToColumnTypeCode(type.Replace("org.apache.cassandra.db.marshal.MapType(", "").Replace(")", "").Split(',')[0], out obj);
                collectionValueTp[1] = convertToColumnTypeCode(type.Replace("org.apache.cassandra.db.marshal.MapType(", "").Replace(")", "").Split(',')[1], out obj);
                return(ColumnTypeCode.Map);
            }

            collectionValueTp = null;
            switch (type)
            {
            case "org.apache.cassandra.db.marshal.UTF8Type":
                return(ColumnTypeCode.Text);

            case "org.apache.cassandra.db.marshal.UUIDType":
                return(ColumnTypeCode.Uuid);

            case "org.apache.cassandra.db.marshal.Int32Type":
                return(ColumnTypeCode.Int);

            case "org.apache.cassandra.db.marshal.BytesType":
                return(ColumnTypeCode.Blob);

            case "org.apache.cassandra.db.marshal.FloatType":
                return(ColumnTypeCode.Float);

            case "org.apache.cassandra.db.marshal.DoubleType":
                return(ColumnTypeCode.Double);

            case "org.apache.cassandra.db.marshal.BooleanType":
                return(ColumnTypeCode.Boolean);

            case "org.apache.cassandra.db.marshal.InetAddressType":
                return(ColumnTypeCode.Inet);

            case "org.apache.cassandra.db.marshal.DateType":
                return(ColumnTypeCode.Timestamp);

            case "org.apache.cassandra.db.marshal.LongType":
                return(ColumnTypeCode.Bigint);

            case "org.apache.cassandra.db.marshal.DecimalType":
                return(ColumnTypeCode.Decimal);

            case "org.apache.cassandra.db.marshal.IntegerType":
                return(ColumnTypeCode.Varint);

            default:
                var ex = new DriverInternalError("Unsupported data type:" + type);
                _logger.Error(string.Format("Unsupported data type: {0}", type), ex);
                throw ex;
            }
        }
        private void SetupEventListener()
        {
            var triedHosts = new List<IPAddress>();
            var innerExceptions = new Dictionary<IPAddress, Exception>();

            var hostsIter = _session._policies.LoadBalancingPolicy.NewQueryPlan(null).GetEnumerator();

            if (!hostsIter.MoveNext())
            {
                var ex = new NoHostAvailableException(new Dictionary<IPAddress, Exception>());
                _logger.Error(ex);
                throw ex;
            }

            var nconn = _session.Connect(null, hostsIter, triedHosts, innerExceptions);
            listeningOnHost = nconn.GetHostAdress();

            Exception theExc = null;

            nconn.CassandraEvent += new CassandraEventHandler(conn_CassandraEvent);
            using (var ret = nconn.RegisterForCassandraEvent(
                CassandraEventType.TopologyChange | CassandraEventType.StatusChange | CassandraEventType.SchemaChange))
            {
                if (!(ret is OutputVoid))
                {
                    if (ret is OutputError)
                        theExc = (ret as OutputError).CreateException();
                    else
                        theExc = new DriverInternalError("Expected Error on Output");
                }
            }

            if (theExc != null)
            {
                _logger.Error(theExc);
                throw theExc;
            }
        }
Exemplo n.º 20
0
 private CqlRowSet ProcessRowset(IOutput outp)
 {
     bool ok = false;
     try
     {
         if (outp is OutputError)
         {
             var ex = (outp as OutputError).CreateException();
             _logger.Error(ex);
             throw ex;
         }
         else if (outp is OutputVoid)
             return new CqlRowSet(outp as OutputVoid, this);
         else if (outp is OutputSchemaChange)
             return new CqlRowSet(outp as OutputSchemaChange, this);
         else if (outp is OutputSetKeyspace)
         {
             SetKeyspace(CqlQueryTools.CqlIdentifier((outp as OutputSetKeyspace).Value));
             return new CqlRowSet(outp as OutputSetKeyspace, this);
         }
         else if (outp is OutputRows)
         {
             ok = true;
             return new CqlRowSet(outp as OutputRows, this, true);
         }
         else
         {
             var ex = new DriverInternalError("Unexpected output kind");
             _logger.Error(ex);
             throw ex;
         }
     }
     finally
     {
         if (!ok)
             outp.Dispose();
     }
 }
Exemplo n.º 21
0
 /// <summary>
 ///  Switches to the specified keyspace.
 /// </summary>
 /// <param name="keyspace_name">Name of keyspace that is to be used.</param>
 public void ChangeKeyspace(string keyspace_name)
 {
     lock (_connectionPool)
     {
         foreach (var kv in _connectionPool)
         {
             foreach (var conn in kv.Value)
             {
                 if (conn.IsHealthy)
                 {
                 retry:
                     try
                     {
                         var keyspaceId = CqlQueryTools.CqlIdentifier(keyspace_name);
                         string retKeyspaceId;
                         try
                         {
                             retKeyspaceId = ProcessSetKeyspace(conn.Query(GetUseKeyspaceCQL(keyspace_name), ConsistencyLevel.Ignore,false));
                         }
                         catch (QueryValidationException)
                         {
                             _logger.Error("Cannot execute USE query. Query is invalid, unauthorized or syntactically incorrect.");
                             throw;
                         }
                         if (CqlQueryTools.CqlIdentifier(retKeyspaceId) != keyspaceId)
                         {
                             var ex = new DriverInternalError("USE query returned " + retKeyspaceId + ". We expected " + keyspaceId + ".");
                             _logger.Error(ex);
                             throw ex;
                         }
                     }
                     catch (Cassandra.CassandraConnection.StreamAllocationException)
                     {
                         _logger.Warning("Cannot allocate stream during keyspace change. Retrying..");
                         goto retry;
                     }
                 }
             }
         }
         this._keyspace = keyspace_name;
         _logger.Info("Changed keyspace to [" + this._keyspace + "]");
     }
 }
Exemplo n.º 22
0
 //private void ProcessRegisterForEvent(IOutput outp)
 //{
 //    using (outp)
 //    {
 //        if (!(outp is OutputVoid))
 //        {
 //            if (outp is OutputError)
 //            {
 //                var ex = (outp as OutputError).CreateException();
 //                _logger.Error(ex);
 //                throw ex;
 //            }
 //            else
 //            {
 //                var ex = new DriverInternalError("Unexpected output kind: " + outp.GetType().Name);
 //                _logger.Error(ex);
 //                throw ex;
 //            }
 //        }
 //        else
 //        {
 //            _logger.Info("Checked register for event - ok");
 //            return; //ok
 //        }
 //    }
 //}
 private string ProcessSetKeyspace(IOutput outp)
 {
     using (outp)
     {
         if (outp is OutputError)
         {
             var ex = (outp as OutputError).CreateException();
             _logger.Error(ex);
             throw ex;
         }
         else if (outp is OutputSetKeyspace)
         {
             return (outp as OutputSetKeyspace).Value;
         }
         else
         {
             var ex = new DriverInternalError("Unexpected output kind");
             _logger.Error(ex);
             throw ex;
         }
     }
 }
Exemplo n.º 23
0
        CassandraConnection AllocateConnection(IPAddress endPoint, out Exception outExc)
        {
            CassandraConnection nconn = null;
            outExc = null;

            try
            {
                nconn = new CassandraConnection(this, endPoint, _protocolOptions, _socketOptions, _clientOptions, _authProvider);

                var options = nconn.ExecuteOptions();

                if (!string.IsNullOrEmpty(_keyspace))
                {
                    var keyspaceId = CqlQueryTools.CqlIdentifier(_keyspace);
                    string retKeyspaceId;
                    try
                    {
                        retKeyspaceId = ProcessSetKeyspace(nconn.Query(GetUseKeyspaceCQL(keyspaceId), ConsistencyLevel.Ignore,false));
                    }
                    catch (InvalidException)
                    {
                        throw;
                    }
                    catch (QueryValidationException)
                    {
                        return null;
                    }

                    if (CqlQueryTools.CqlIdentifier(retKeyspaceId) != CqlQueryTools.CqlIdentifier(keyspaceId))
                    {
                        var ex = new DriverInternalError("USE query returned " + retKeyspaceId + ". We expected " + keyspaceId + ".");
                        _logger.Error(ex);
                        throw ex;
                    }

                    lock(_preparedQueries)
                        foreach (var prepQ in _preparedQueries)
                        {
                            try
                            {
                                byte[] queryid;
                                RowSetMetadata metadata;
                                ProcessPrepareQuery(nconn.PrepareQuery(prepQ.Key), out metadata, out queryid);
                            }
                            catch (QueryValidationException)
                            {
                                return null;
                            }
                            //TODO: makesure that ids are equal;
                        }
                }
            }
            catch (Exception ex)
            {
                if (nconn != null)
                    nconn.Dispose();

                if (CassandraConnection.IsStreamRelatedException(ex))
                {
                    outExc = ex;
                    return null;
                }
                else
                    throw ex;
            }

            _logger.Info("Allocated new connection");

            return nconn;
        }
Exemplo n.º 24
0
 private void ProcessPrepareQuery(IOutput outp, out RowSetMetadata metadata, out byte[] queryId)
 {
     using (outp)
     {
         if (outp is OutputError)
         {
             var ex = (outp as OutputError).CreateException();
             _logger.Error(ex);
             throw ex;
         }
         else if (outp is OutputPrepared)
         {
             queryId = (outp as OutputPrepared).QueryID;
             metadata = (outp as OutputPrepared).Metadata;
             _logger.Info("Prepared Query has been successfully processed.");
             return; //ok
         }
         else
         {
             var ex = new DriverInternalError("Unexpected output kind");
             _logger.Error("Prepared Query has returned an unexpected output kind.", ex);
             throw ex;
         }
     }
 }
        private void conn_CassandraEvent(object sender, CassandraEventArgs e)
        {
            if (e is TopologyChangeEventArgs)
            {
                var tce = e as TopologyChangeEventArgs;
                if (tce.What == TopologyChangeEventArgs.Reason.NewNode)
                {
                    SetupControlConnection(true);
                    _cluster.Metadata.AddHost(tce.Address);
                    return;
                }
                else if (tce.What == TopologyChangeEventArgs.Reason.RemovedNode)
                {
                    _cluster.Metadata.RemoveHost(tce.Address);
                    SetupControlConnection(!tce.Address.Equals(listeningOnHost));
                    return;
                }
            }
            else if (e is StatusChangeEventArgs)
            {
                var sce = e as StatusChangeEventArgs;
                if (sce.What == StatusChangeEventArgs.Reason.Up)
                {
                    _cluster.Metadata.BringUpHost(sce.Address, this);
                    return;
                }
                else if (sce.What == StatusChangeEventArgs.Reason.Down)
                {
                    _cluster.Metadata.SetDownHost(sce.Address, this);
                    return;
                }
            }
            else if (e is SchemaChangeEventArgs)
            {
                var ssc = e as SchemaChangeEventArgs;

                if (ssc.What == SchemaChangeEventArgs.Reason.Created)
                {
                    SubmitSchemaRefresh(string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, null);
                    return;
                }
                else if (ssc.What == SchemaChangeEventArgs.Reason.Dropped)
                {
                    SubmitSchemaRefresh(string.IsNullOrEmpty(ssc.Keyspace) ? null : ssc.Keyspace, null);
                    return;
                }
                else if (ssc.What == SchemaChangeEventArgs.Reason.Updated)
                {
                    SubmitSchemaRefresh(ssc.Keyspace, string.IsNullOrEmpty(ssc.Table) ? null : ssc.Table);
                    return;
                }
            }

            var ex = new DriverInternalError("Unknown Event");
            _logger.Error(ex);
            throw ex;
        }