public Action <int> SetupKeyspace(Action <int> dx)
 {
     return(new Action <int>((streamId) =>
     {
         if (currentKs != selectedKs)
         {
             Evaluate(new QueryRequest(streamId, CqlQueryTools.GetUseKeyspaceCQL(selectedKs), ConsistencyLevel.Default, false), streamId, new Action <ResponseFrame>((frame3) =>
             {
                 var response = FrameParser.Parse(frame3);
                 if (response is ResultResponse)
                 {
                     currentKs = selectedKs;
                     dx(streamId);
                 }
                 else
                 {
                     _protocolErrorHandlerAction(new ErrorActionParam()
                     {
                         AbstractResponse = response, StreamId = streamId
                     });
                 }
             }));
         }
         else
         {
             dx(streamId);
         }
     }));
 }
예제 #2
0
 public Action SetupKeyspace(AsyncResult <IOutput> jar, Action dx)
 {
     return(() =>
     {
         if (!_currentKs.Value.Equals(_selectedKs.Value))
         {
             Evaluate(new QueryRequest(jar.StreamId, CqlQueryTools.GetUseKeyspaceCql(_selectedKs.Value), false, QueryProtocolOptions.Default),
                      jar.StreamId, frame3 =>
             {
                 AbstractResponse response = FrameParser.Parse(frame3);
                 if (response is ResultResponse)
                 {
                     _currentKs.Value = _selectedKs.Value;
                     dx();
                 }
                 else
                 {
                     _protocolErrorHandlerAction(new ErrorActionParam {
                         AbstractResponse = response, Jar = jar
                     });
                 }
             });
         }
         else
         {
             dx();
         }
     });
 }
 public Action SetupKeyspace(AsyncResult <IOutput> jar, Action dx)
 {
     return(new Action(() =>
     {
         if (!currentKs.Value.Equals(selectedKs.Value))
         {
             Evaluate(new QueryRequest(jar.StreamId, CqlQueryTools.GetUseKeyspaceCQL(selectedKs.Value), ConsistencyLevel.Default, false), jar.StreamId, new Action <ResponseFrame>((frame3) =>
             {
                 var response = FrameParser.Parse(frame3);
                 if (response is ResultResponse)
                 {
                     currentKs.Value = selectedKs.Value;
                     dx();
                 }
                 else
                 {
                     _protocolErrorHandlerAction(new ErrorActionParam()
                     {
                         AbstractResponse = response, Jar = jar
                     });
                 }
             }));
         }
         else
         {
             dx();
         }
     }));
 }
예제 #4
0
 /// <inheritdoc />
 public void ChangeKeyspace(string keyspace)
 {
     if (this.Keyspace != keyspace)
     {
         this.Execute(new SimpleStatement(CqlQueryTools.GetUseKeyspaceCql(keyspace)));
         this.Keyspace = keyspace;
     }
 }
예제 #5
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();
                }
            }
        }
예제 #6
0
        /// <summary>
        ///  Returns a CQL query representing this keyspace. This method returns a single
        ///  'CREATE KEYSPACE' query with the options corresponding to this name
        ///  definition.
        /// </summary>
        ///
        /// <returns>the 'CREATE KEYSPACE' query corresponding to this name.
        ///  <see>#ExportAsString</see></returns>
        public string AsCqlQuery()
        {
            var sb = new StringBuilder();

            sb.Append("CREATE KEYSPACE ").Append(CqlQueryTools.CqlIdentifier(Name)).Append(" WITH ");
            sb.Append("REPLICATION = { 'class' : '").Append(Replication["class"]).Append("'");
            foreach (var rep in Replication)
            {
                if (rep.Key == "class")
                {
                    continue;
                }
                sb.Append(", '").Append(rep.Key).Append("': '").Append(rep.Value).Append("'");
            }
            sb.Append(" } AND DURABLE_WRITES = ").Append(DurableWrites);
            sb.Append(";");
            return(sb.ToString());
        }
예제 #7
0
 public void DeleteKeyspaceIfExists(string keyspace_name)
 {
     if (_binaryProtocolVersion > 1)
     {
         WaitForSchemaAgreement(Execute(CqlQueryTools.GetDropKeyspaceCql(keyspace_name, true)));
         _logger.Info("Keyspace [" + keyspace_name + "] has been successfully DELETED");
     }
     else
     {
         try
         {
             DeleteKeyspace(keyspace_name);
         }
         catch (InvalidQueryException)
         {
             _logger.Info(string.Format("Cannot DELETE keyspace:  {0}  because it not exists.", keyspace_name));
         }
     }
 }
예제 #8
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);
                    nconn.SetKeyspace(keyspaceId);
                }
            }
            catch (Exception ex)
            {
                if (nconn != null)
                {
                    nconn.Dispose();
                }

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

            _logger.Info("Allocated new connection");

            return(nconn);
        }
예제 #9
0
 /// <summary>
 ///  Deletes specified keyspace from current cluster.
 ///  If keyspace with specified name does not exist, then exception will be thrown.
 /// </summary>
 /// <param name="keyspace_name">Name of keyspace to be deleted.</param>
 public void DeleteKeyspace(string keyspace_name)
 {
     Cluster.WaitForSchemaAgreement(
         Query(CqlQueryTools.GetDropKeyspaceCQL(keyspace_name), ConsistencyLevel.Default));
     _logger.Info("Keyspace [" + keyspace_name + "] has been successfully DELETED");
 }
예제 #10
0
 /// <summary>
 ///  Creates new keyspace in current cluster.
 /// </summary>
 /// <param name="keyspace_name">Name of keyspace to be created.</param>
 /// <param name="replication">Replication property for this keyspace.
 /// To set it, refer to the <see cref="ReplicationStrategies"/> class methods.
 /// It is a dictionary of replication property sub-options where key is a sub-option name and value is a value for that sub-option.
 /// <p>Default value is <code>'SimpleStrategy'</code> with <code>'replication_factor' = 1</code></p></param>
 /// <param name="durable_writes">Whether to use the commit log for updates on this keyspace. Default is set to <code>true</code>.</param>
 public void CreateKeyspace(string keyspace_name, Dictionary <string, string> replication = null, bool durable_writes = true)
 {
     Cluster.WaitForSchemaAgreement(
         Query(CqlQueryTools.GetCreateKeyspaceCQL(keyspace_name, replication, durable_writes), ConsistencyLevel.Default));
     _logger.Info("Keyspace [" + keyspace_name + "] has been successfully CREATED.");
 }
예제 #11
0
 /// <inheritdoc />
 public void DeleteKeyspace(string keyspaceName)
 {
     Execute(CqlQueryTools.GetDropKeyspaceCql(keyspaceName, false));
 }
예제 #12
0
 /// <inheritdoc />
 public void CreateKeyspace(string keyspace, Dictionary <string, string> replication = null, bool durableWrites = true)
 {
     WaitForSchemaAgreement(Execute(CqlQueryTools.GetCreateKeyspaceCql(keyspace, replication, durableWrites, false)));
     _logger.Info("Keyspace [" + keyspace + "] has been successfully CREATED.");
 }
예제 #13
0
 public static string GetDropKeyspaceCQL(string keyspace)
 {
     return(string.Format(
                @"DROP KEYSPACE {0}"
                , CqlQueryTools.CqlIdentifier(keyspace)));
 }
예제 #14
0
        public void PrintTo(TextWriter stream,
                            string delim            = "\t|",
                            string rowDelim         = "\r\n",
                            bool printHeader        = true,
                            bool printFooter        = true,
                            string separ            = "-------------------------------------------------------------------------------",
                            string lasLFrm          = "Returned {0} rows.",
                            CellEncoder cellEncoder = null
                            )
        {
            if (printHeader)
            {
                bool first = true;
                foreach (var column in Columns)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        stream.Write(delim);
                    }

                    stream.Write(column.Name);
                }
                stream.Write(rowDelim);
                stream.Write(separ);
                stream.Write(rowDelim);
            }
            int i = 0;

            foreach (var row in GetRows())
            {
                bool first = true;
                for (int j = 0; j < Columns.Length; j++)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        stream.Write(delim);
                    }

                    if (row[j] is System.Array || (row[j].GetType().IsGenericType&& row[j] is System.Collections.IEnumerable))
                    {
                        cellEncoder = delegate(object collection)
                        {
                            string result = "<Collection>";
                            if (collection.GetType() == typeof(byte[]))
                            {
                                result += CqlQueryTools.ToHex((byte[])collection);
                            }
                            else
                            {
                                foreach (var val in (collection as System.Collections.IEnumerable))
                                {
                                    result += val.ToString() + ",";
                                }
                            }
                            return(result.Substring(0, result.Length - 1) + "</Collection>");
                        }
                    }
                    ;

                    stream.Write(cellEncoder == null ? row[j] : cellEncoder(row[j]));
                }
                stream.Write(rowDelim);
                i++;
            }
            if (printFooter)
            {
                stream.Write(separ);
                stream.Write(rowDelim);
                stream.Write(string.Format(lasLFrm, i));
                stream.Write(rowDelim);
            }
        }
    }
예제 #15
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)
 {
     Execute(CqlQueryTools.GetUseKeyspaceCQL(keyspace_name));
 }
예제 #16
0
 public static string GetUseKeyspaceCQL(string keyspace)
 {
     return(string.Format(
                @"USE {0}"
                , CqlQueryTools.CqlIdentifier(keyspace)));
 }
예제 #17
0
 public void DeleteKeyspace(string keyspace_name)
 {
     WaitForSchemaAgreement(Execute(CqlQueryTools.GetDropKeyspaceCql(keyspace_name, false)));
     _logger.Info("Keyspace [" + keyspace_name + "] has been successfully DELETED");
 }