/// <summary>
        ///  Returns metadata of specified table in this keyspace.
        /// </summary>
        /// <param name="tableName"> the name of table to retrieve </param>
        /// <returns>the metadata for table <c>tableName</c> in this keyspace if it
        ///  exists, <c>null</c> otherwise.</returns>
        public TableMetadata GetTableMetadata(string tableName)
        {
            TableMetadata table;
            if (_tables.TryGetValue(tableName, out table))
            {
                //The table metadata is available in local cache
                return table;
            }
            var keyspaceName = Name;
            var cols = new Dictionary<string, TableColumn>();
            TableOptions options = null;
            var tableMetadataRow = _cc.Query(String.Format(SelectSingleTable, tableName, keyspaceName), true).FirstOrDefault();
            if (tableMetadataRow == null)
            {
                return null;
            }
            var columnsMetadata = _cc.Query(String.Format(SelectColumns, tableName, keyspaceName), true);
            foreach (var row in columnsMetadata)
            {
                var dataType = TypeCodec.ParseDataType(row.GetValue<string>("validator"));
                var dsc = new TableColumn
                {
                    Name = row.GetValue<string>("column_name"),
                    Keyspace = row.GetValue<string>("keyspace_name"),
                    Table = row.GetValue<string>("columnfamily_name"),
                    TypeCode = dataType.TypeCode,
                    TypeInfo = dataType.TypeInfo,
                    SecondaryIndexName = row.GetValue<string>("index_name"),
                    SecondaryIndexType = row.GetValue<string>("index_type"),
                    KeyType =
                        row.GetValue<string>("index_name") != null
                            ? KeyType.SecondaryIndex
                            : KeyType.None,
                };
                cols.Add(dsc.Name, dsc);
            }
            
            var colNames = tableMetadataRow.GetValue<string>("column_aliases");
            var rowKeys = colNames.Substring(1, colNames.Length - 2).Split(',');
            for (var i = 0; i < rowKeys.Length; i++)
            {
                if (rowKeys[i].StartsWith("\""))
                {
                    rowKeys[i] = rowKeys[i].Substring(1, rowKeys[i].Length - 2).Replace("\"\"", "\"");
                }
            }
            if (rowKeys.Length > 0 && rowKeys[0] != string.Empty)
            {
                bool isCompact = true;
                var comparator = tableMetadataRow.GetValue<string>("comparator");
                //Remove reversed type
                comparator = comparator.Replace(TypeCodec.ReversedTypeName, "");
                if (comparator.StartsWith(TypeCodec.CompositeTypeName))
                {
                    comparator = comparator.Replace(TypeCodec.CompositeTypeName, "");
                    isCompact = false;
                }

                var rg = new Regex(@"org\.apache\.cassandra\.db\.marshal\.\w+");
                var rowKeysTypes = rg.Matches(comparator);

                for (var i = 0; i < rowKeys.Length; i++)
                {
                    var keyName = rowKeys[i];
                    var dataType = TypeCodec.ParseDataType(rowKeysTypes[i].ToString());
                    var dsc = new TableColumn
                    {
                        Name = keyName,
                        Keyspace = tableMetadataRow.GetValue<string>("keyspace_name"),
                        Table = tableMetadataRow.GetValue<string>("columnfamily_name"),
                        TypeCode = dataType.TypeCode,
                        TypeInfo = dataType.TypeInfo,
                        KeyType = KeyType.Clustering,
                    };
                    cols[dsc.Name] = dsc;
                }

                options = new TableOptions
                {
                    isCompactStorage = isCompact,
                    bfFpChance = tableMetadataRow.GetValue<double>("bloom_filter_fp_chance"),
                    caching = tableMetadataRow.GetValue<string>("caching"),
                    comment = tableMetadataRow.GetValue<string>("comment"),
                    gcGrace = tableMetadataRow.GetValue<int>("gc_grace_seconds"),
                    localReadRepair = tableMetadataRow.GetValue<double>("local_read_repair_chance"),
                    readRepair = tableMetadataRow.GetValue<double>("read_repair_chance"),
                    compactionOptions = GetCompactionStrategyOptions(tableMetadataRow),
                    compressionParams =
                        (SortedDictionary<string, string>)Utils.ConvertStringToMap(tableMetadataRow.GetValue<string>("compression_parameters"))
                };
                //replicate_on_write column not present in C* >= 2.1
                if (tableMetadataRow.GetColumn("replicate_on_write") != null)
                {
                    options.replicateOnWrite = tableMetadataRow.GetValue<bool>("replicate_on_write");
                }
            }
            //In Cassandra 1.2, the keys are not stored in the system.schema_columns table
            //But you can get it from system.schema_columnfamilies
            var keys = tableMetadataRow.GetValue<string>("key_aliases")
                            .Replace("[", "")
                            .Replace("]", "")
                            .Split(',');
            var keyTypes = tableMetadataRow.GetValue<string>("key_validator")
                                .Replace("org.apache.cassandra.db.marshal.CompositeType", "")
                                .Replace("(", "")
                                .Replace(")", "")
                                .Split(',');
            var partitionKeys = new TableColumn[keys.Length];
            for (var i = 0; i < keys.Length; i++)
            {
                var name = keys[i].Replace("\"", "").Trim();
                var dataType = TypeCodec.ParseDataType(keyTypes[i].Trim());
                var c = new TableColumn()
                {
                    Name = name,
                    Keyspace = tableMetadataRow.GetValue<string>("keyspace_name"),
                    Table = tableMetadataRow.GetValue<string>("columnfamily_name"),
                    TypeCode = dataType.TypeCode,
                    TypeInfo = dataType.TypeInfo,
                    KeyType = KeyType.Partition
                };
                cols[name] = c;
                partitionKeys[i] = c;
            }

            table = new TableMetadata(tableName, cols.Values.ToArray(), partitionKeys, options);
            //Cache it
            _tables.AddOrUpdate(tableName, table, (k, o) => table);
            return table;
        }
        /// <summary>
        ///  Returns metadata of specified table in this keyspace.
        /// </summary>
        /// <param name="tableName"> the name of table to retrieve </param>
        /// <returns>the metadata for table <c>tableName</c> in this keyspace if it
        ///  exists, <c>null</c> otherwise.</returns>
        public TableMetadata GetTableMetadata(string tableName)
        {
            TableMetadata table;

            if (_tables.TryGetValue(tableName, out table))
            {
                //The table metadata is available in local cache
                return(table);
            }
            var keyspaceName     = Name;
            var columns          = new Dictionary <string, TableColumn>();
            var partitionKeys    = new List <Tuple <int, TableColumn> >();
            var tableMetadataRow = _cc.Query(String.Format(SelectSingleTable, tableName, keyspaceName), true).FirstOrDefault();

            if (tableMetadataRow == null)
            {
                return(null);
            }
            //Read table options
            var options = new TableOptions
            {
                isCompactStorage  = false,
                bfFpChance        = tableMetadataRow.GetValue <double>("bloom_filter_fp_chance"),
                caching           = tableMetadataRow.GetValue <string>("caching"),
                comment           = tableMetadataRow.GetValue <string>("comment"),
                gcGrace           = tableMetadataRow.GetValue <int>("gc_grace_seconds"),
                localReadRepair   = tableMetadataRow.GetValue <double>("local_read_repair_chance"),
                readRepair        = tableMetadataRow.GetValue <double>("read_repair_chance"),
                compactionOptions = GetCompactionStrategyOptions(tableMetadataRow),
                compressionParams =
                    (SortedDictionary <string, string>)Utils.ConvertStringToMap(tableMetadataRow.GetValue <string>("compression_parameters"))
            };

            //replicate_on_write column not present in C* >= 2.1
            if (tableMetadataRow.GetColumn("replicate_on_write") != null)
            {
                options.replicateOnWrite = tableMetadataRow.GetValue <bool>("replicate_on_write");
            }

            var columnsMetadata = _cc.Query(String.Format(SelectColumns, tableName, keyspaceName), true);

            foreach (var row in columnsMetadata)
            {
                var dataType = TypeCodec.ParseDataType(row.GetValue <string>("validator"));
                var col      = new TableColumn
                {
                    Name               = row.GetValue <string>("column_name"),
                    Keyspace           = row.GetValue <string>("keyspace_name"),
                    Table              = row.GetValue <string>("columnfamily_name"),
                    TypeCode           = dataType.TypeCode,
                    TypeInfo           = dataType.TypeInfo,
                    SecondaryIndexName = row.GetValue <string>("index_name"),
                    SecondaryIndexType = row.GetValue <string>("index_type"),
                    KeyType            =
                        row.GetValue <string>("index_name") != null
                            ? KeyType.SecondaryIndex
                            : KeyType.None,
                };
                if (row.GetColumn("type") != null && row.GetValue <string>("type") == "partition_key")
                {
                    partitionKeys.Add(Tuple.Create(row.GetValue <int?>("component_index") ?? 0, col));
                }
                columns.Add(col.Name, col);
            }
            var comparator          = tableMetadataRow.GetValue <string>("comparator");
            var comparatorComposite = false;

            if (comparator.StartsWith(TypeCodec.CompositeTypeName))
            {
                comparator          = comparator.Replace(TypeCodec.CompositeTypeName, "");
                comparatorComposite = true;
            }
            //Remove reversed type
            comparator = comparator.Replace(TypeCodec.ReversedTypeName, "");
            if (partitionKeys.Count == 0 && tableMetadataRow.GetColumn("key_aliases") != null)
            {
                //In C* 1.2, keys are not stored on the schema_columns table
                var colNames = tableMetadataRow.GetValue <string>("column_aliases");
                var rowKeys  = colNames.Substring(1, colNames.Length - 2).Split(',');
                for (var i = 0; i < rowKeys.Length; i++)
                {
                    if (rowKeys[i].StartsWith("\""))
                    {
                        rowKeys[i] = rowKeys[i].Substring(1, rowKeys[i].Length - 2).Replace("\"\"", "\"");
                    }
                }
                if (rowKeys.Length > 0 && rowKeys[0] != string.Empty)
                {
                    var rg           = new Regex(@"org\.apache\.cassandra\.db\.marshal\.\w+");
                    var rowKeysTypes = rg.Matches(comparator);

                    for (var i = 0; i < rowKeys.Length; i++)
                    {
                        var keyName  = rowKeys[i];
                        var dataType = TypeCodec.ParseDataType(rowKeysTypes[i].ToString());
                        var dsc      = new TableColumn
                        {
                            Name     = keyName,
                            Keyspace = tableMetadataRow.GetValue <string>("keyspace_name"),
                            Table    = tableMetadataRow.GetValue <string>("columnfamily_name"),
                            TypeCode = dataType.TypeCode,
                            TypeInfo = dataType.TypeInfo,
                            KeyType  = KeyType.Clustering,
                        };
                        columns[dsc.Name] = dsc;
                    }
                }
                var keys = tableMetadataRow.GetValue <string>("key_aliases")
                           .Replace("[", "")
                           .Replace("]", "")
                           .Split(',');
                var keyTypes = tableMetadataRow.GetValue <string>("key_validator")
                               .Replace("org.apache.cassandra.db.marshal.CompositeType", "")
                               .Replace("(", "")
                               .Replace(")", "")
                               .Split(',');


                for (var i = 0; i < keys.Length; i++)
                {
                    var name     = keys[i].Replace("\"", "").Trim();
                    var dataType = TypeCodec.ParseDataType(keyTypes[i].Trim());
                    var c        = new TableColumn()
                    {
                        Name     = name,
                        Keyspace = tableMetadataRow.GetValue <string>("keyspace_name"),
                        Table    = tableMetadataRow.GetValue <string>("columnfamily_name"),
                        TypeCode = dataType.TypeCode,
                        TypeInfo = dataType.TypeInfo,
                        KeyType  = KeyType.Partition
                    };
                    columns[name] = c;
                    partitionKeys.Add(Tuple.Create(i, c));
                }
            }

            options.isCompactStorage = tableMetadataRow.GetColumn("is_dense") != null && tableMetadataRow.GetValue <bool>("is_dense");
            if (!options.isCompactStorage)
            {
                //is_dense column does not exist in previous versions of Cassandra
                //also, compact pk, ck and val appear as is_dense false
                // clusteringKeys != comparator types - 1
                // or not composite (comparator)
                options.isCompactStorage = !comparatorComposite;
            }

            table = new TableMetadata(
                tableName, columns.Values.ToArray(),
                partitionKeys.OrderBy(p => p.Item1).Select(p => p.Item2).ToArray(),
                options);
            //Cache it
            _tables.AddOrUpdate(tableName, table, (k, o) => table);
            return(table);
        }
        /// <summary>
        ///  Returns metadata of specified table in this keyspace.
        /// </summary>
        /// <param name="tableName"> the name of table to retrieve </param>
        /// <returns>the metadata for table <c>tableName</c> in this keyspace if it
        ///  exists, <c>null</c> otherwise.</returns>
        public TableMetadata GetTableMetadata(string tableName)
        {
            TableMetadata table;
            if (_tables.TryGetValue(tableName, out table))
            {
                //The table metadata is available in local cache
                return table;
            }
            var keyspaceName = Name;
            var columns = new Dictionary<string, TableColumn>();
            var partitionKeys = new List<Tuple<int, TableColumn>>();
            var tableMetadataRow = _cc.Query(String.Format(SelectSingleTable, tableName, keyspaceName), true).FirstOrDefault();
            if (tableMetadataRow == null)
            {
                return null;
            }
            //Read table options
            var options = new TableOptions
            {
                isCompactStorage = false,
                bfFpChance = tableMetadataRow.GetValue<double>("bloom_filter_fp_chance"),
                caching = tableMetadataRow.GetValue<string>("caching"),
                comment = tableMetadataRow.GetValue<string>("comment"),
                gcGrace = tableMetadataRow.GetValue<int>("gc_grace_seconds"),
                localReadRepair = tableMetadataRow.GetValue<double>("local_read_repair_chance"),
                readRepair = tableMetadataRow.GetValue<double>("read_repair_chance"),
                compactionOptions = GetCompactionStrategyOptions(tableMetadataRow),
                compressionParams =
                    (SortedDictionary<string, string>)Utils.ConvertStringToMap(tableMetadataRow.GetValue<string>("compression_parameters"))
            };
            //replicate_on_write column not present in C* >= 2.1
            if (tableMetadataRow.GetColumn("replicate_on_write") != null)
            {
                options.replicateOnWrite = tableMetadataRow.GetValue<bool>("replicate_on_write");
            }

            var columnsMetadata = _cc.Query(String.Format(SelectColumns, tableName, keyspaceName), true);
            foreach (var row in columnsMetadata)
            {
                var dataType = TypeCodec.ParseDataType(row.GetValue<string>("validator"));
                var col = new TableColumn
                {
                    Name = row.GetValue<string>("column_name"),
                    Keyspace = row.GetValue<string>("keyspace_name"),
                    Table = row.GetValue<string>("columnfamily_name"),
                    TypeCode = dataType.TypeCode,
                    TypeInfo = dataType.TypeInfo,
                    SecondaryIndexName = row.GetValue<string>("index_name"),
                    SecondaryIndexType = row.GetValue<string>("index_type"),
                    KeyType =
                        row.GetValue<string>("index_name") != null
                            ? KeyType.SecondaryIndex
                            : KeyType.None,
                };
                if (row.GetColumn("type") != null && row.GetValue<string>("type") == "partition_key")
                {
                    partitionKeys.Add(Tuple.Create(row.GetValue<int?>("component_index") ?? 0, col));
                }
                columns.Add(col.Name, col);
            }
            var comparator = tableMetadataRow.GetValue<string>("comparator");
            var comparatorComposite = false;
            if (comparator.StartsWith(TypeCodec.CompositeTypeName))
            {
                comparator = comparator.Replace(TypeCodec.CompositeTypeName, "");
                comparatorComposite = true;
            }
            //Remove reversed type
            comparator = comparator.Replace(TypeCodec.ReversedTypeName, "");
            if (partitionKeys.Count == 0 && tableMetadataRow.GetColumn("key_aliases") != null)
            {
                //In C* 1.2, keys are not stored on the schema_columns table
                var colNames = tableMetadataRow.GetValue<string>("column_aliases");
                var rowKeys = colNames.Substring(1, colNames.Length - 2).Split(',');
                for (var i = 0; i < rowKeys.Length; i++)
                {
                    if (rowKeys[i].StartsWith("\""))
                    {
                        rowKeys[i] = rowKeys[i].Substring(1, rowKeys[i].Length - 2).Replace("\"\"", "\"");
                    }
                }
                if (rowKeys.Length > 0 && rowKeys[0] != string.Empty)
                {
                    var rg = new Regex(@"org\.apache\.cassandra\.db\.marshal\.\w+");
                    var rowKeysTypes = rg.Matches(comparator);

                    for (var i = 0; i < rowKeys.Length; i++)
                    {
                        var keyName = rowKeys[i];
                        var dataType = TypeCodec.ParseDataType(rowKeysTypes[i].ToString());
                        var dsc = new TableColumn
                        {
                            Name = keyName,
                            Keyspace = tableMetadataRow.GetValue<string>("keyspace_name"),
                            Table = tableMetadataRow.GetValue<string>("columnfamily_name"),
                            TypeCode = dataType.TypeCode,
                            TypeInfo = dataType.TypeInfo,
                            KeyType = KeyType.Clustering,
                        };
                        columns[dsc.Name] = dsc;
                    }
                }
                var keys = tableMetadataRow.GetValue<string>("key_aliases")
                    .Replace("[", "")
                    .Replace("]", "")
                    .Split(',');
                var keyTypes = tableMetadataRow.GetValue<string>("key_validator")
                    .Replace("org.apache.cassandra.db.marshal.CompositeType", "")
                    .Replace("(", "")
                    .Replace(")", "")
                    .Split(',');
                
                
                for (var i = 0; i < keys.Length; i++)
                {
                    var name = keys[i].Replace("\"", "").Trim();
                    var dataType = TypeCodec.ParseDataType(keyTypes[i].Trim());
                    var c = new TableColumn()
                    {
                        Name = name,
                        Keyspace = tableMetadataRow.GetValue<string>("keyspace_name"),
                        Table = tableMetadataRow.GetValue<string>("columnfamily_name"),
                        TypeCode = dataType.TypeCode,
                        TypeInfo = dataType.TypeInfo,
                        KeyType = KeyType.Partition
                    };
                    columns[name] = c;
                    partitionKeys.Add(Tuple.Create(i, c));
                }
            }

            options.isCompactStorage = tableMetadataRow.GetColumn("is_dense") != null && tableMetadataRow.GetValue<bool>("is_dense");
            if (!options.isCompactStorage)
            {
                //is_dense column does not exist in previous versions of Cassandra
                //also, compact pk, ck and val appear as is_dense false
                // clusteringKeys != comparator types - 1
                // or not composite (comparator)
                options.isCompactStorage = !comparatorComposite;
            }

            table = new TableMetadata(
                tableName, columns.Values.ToArray(), 
                partitionKeys.OrderBy(p => p.Item1).Select(p => p.Item2).ToArray(), 
                options);
            //Cache it
            _tables.AddOrUpdate(tableName, table, (k, o) => table);
            return table;
        }
        public override Task <TableMetadata> GetTable(string keyspaceName, string tableName)
        {
            var columns        = new Dictionary <string, TableColumn>();
            var partitionKeys  = new List <Tuple <int, TableColumn> >();
            var clusteringKeys = new List <Tuple <int, Tuple <TableColumn, SortOrder> > >();

            return(Cc
                   .QueryAsync(string.Format(SelectSingleTable, tableName, keyspaceName), true)
                   .Then(rs =>
            {
                var tableMetadataRow = rs.FirstOrDefault();
                if (tableMetadataRow == null)
                {
                    return NullTableTask;
                }
                //Read table options
                var options = new TableOptions
                {
                    isCompactStorage = false,
                    bfFpChance = tableMetadataRow.GetValue <double>("bloom_filter_fp_chance"),
                    caching = tableMetadataRow.GetValue <string>("caching"),
                    comment = tableMetadataRow.GetValue <string>("comment"),
                    gcGrace = tableMetadataRow.GetValue <int>("gc_grace_seconds"),
                    localReadRepair = tableMetadataRow.GetValue <double>("local_read_repair_chance"),
                    readRepair = tableMetadataRow.GetValue <double>("read_repair_chance"),
                    compactionOptions = GetCompactionStrategyOptions(tableMetadataRow),
                    compressionParams =
                        (SortedDictionary <string, string>)Utils.ConvertStringToMap(tableMetadataRow.GetValue <string>("compression_parameters"))
                };
                //replicate_on_write column not present in C* >= 2.1
                if (tableMetadataRow.GetColumn("replicate_on_write") != null)
                {
                    options.replicateOnWrite = tableMetadataRow.GetValue <bool>("replicate_on_write");
                }
                return Cc
                .QueryAsync(string.Format(SelectColumns, tableName, keyspaceName), true)
                .ContinueSync(columnsMetadata =>
                {
                    foreach (var row in columnsMetadata)
                    {
                        var dataType = DataTypeParser.ParseFqTypeName(row.GetValue <string>("validator"));
                        var col = new TableColumn
                        {
                            Name = row.GetValue <string>("column_name"),
                            Keyspace = row.GetValue <string>("keyspace_name"),
                            Table = row.GetValue <string>("columnfamily_name"),
                            TypeCode = dataType.TypeCode,
                            TypeInfo = dataType.TypeInfo,
                                    #pragma warning disable 618
                            SecondaryIndexName = row.GetValue <string>("index_name"),
                            SecondaryIndexType = row.GetValue <string>("index_type"),
                            SecondaryIndexOptions = Utils.ParseJsonStringMap(row.GetValue <string>("index_options")),
                                    #pragma warning restore 618
                            KeyType =
                                row.GetValue <string>("index_name") != null
                                            ? KeyType.SecondaryIndex
                                            : KeyType.None
                        };
                        if (row.GetColumn("type") != null)
                        {
                            switch (row.GetValue <string>("type"))
                            {
                            case "partition_key":
                                partitionKeys.Add(Tuple.Create(row.GetValue <int?>("component_index") ?? 0, col));
                                col.KeyType = KeyType.Partition;
                                break;

                            case "clustering_key":
                                var sortOrder = dataType.IsReversed ? SortOrder.Descending : SortOrder.Ascending;
                                clusteringKeys.Add(Tuple.Create(row.GetValue <int?>("component_index") ?? 0, Tuple.Create(col, sortOrder)));
                                col.KeyType = KeyType.Clustering;
                                break;

                            case "static":
                                col.IsStatic = true;
                                break;
                            }
                        }
                        columns.Add(col.Name, col);
                    }
                    var comparator = tableMetadataRow.GetValue <string>("comparator");
                    if (tableMetadataRow.GetColumn("key_aliases") != null && partitionKeys.Count == 0)
                    {
                        //In C* 1.2, keys are not stored on the schema_columns table
                        var partitionKeyNames = Utils.ParseJsonStringArray(tableMetadataRow.GetValue <string>("key_aliases"));
                        var types = AdaptKeyTypes(tableMetadataRow.GetValue <string>("key_validator"));
                        for (var i = 0; i < partitionKeyNames.Length; i++)
                        {
                            var name = partitionKeyNames[i];
                            TableColumn c;
                            if (!columns.TryGetValue(name, out c))
                            {
                                c = new TableColumn
                                {
                                    Name = name,
                                    Keyspace = keyspaceName,
                                    Table = tableName,
                                    TypeCode = types[i].TypeCode,
                                    TypeInfo = types[i].TypeInfo,
                                    KeyType = KeyType.Partition
                                };
                                //The column is not part of columns metadata table
                                columns.Add(name, c);
                            }
                            partitionKeys.Add(Tuple.Create(i, c));
                        }
                        //In C* 1.2, keys are not stored on the schema_columns table
                        var clusteringKeyNames = Utils.ParseJsonStringArray(tableMetadataRow.GetValue <string>("column_aliases"));
                        if (clusteringKeyNames.Length > 0)
                        {
                            types = AdaptKeyTypes(comparator);
                            for (var i = 0; i < clusteringKeyNames.Length; i++)
                            {
                                var name = clusteringKeyNames[i];
                                TableColumn c;
                                var dataType = types[i];
                                if (!columns.TryGetValue(name, out c))
                                {
                                    c = new TableColumn
                                    {
                                        Name = name,
                                        Keyspace = keyspaceName,
                                        Table = tableName,
                                        TypeCode = dataType.TypeCode,
                                        TypeInfo = dataType.TypeInfo,
                                        KeyType = KeyType.Clustering
                                    };
                                    //The column is not part of columns metadata table
                                    columns.Add(name, c);
                                }
                                clusteringKeys.Add(Tuple.Create(i, Tuple.Create(c, dataType.IsReversed ? SortOrder.Descending : SortOrder.Ascending)));
                            }
                        }
                    }
                    options.isCompactStorage = tableMetadataRow.GetColumn("is_dense") != null && tableMetadataRow.GetValue <bool>("is_dense");
                    if (!options.isCompactStorage)
                    {
                        //is_dense column does not exist in previous versions of Cassandra
                        //also, compact pk, ck and val appear as is_dense false
                        // clusteringKeys != comparator types - 1
                        // or not composite (comparator)
                        options.isCompactStorage = !comparator.StartsWith(DataTypeParser.CompositeTypeName);
                    }
                    var result = new TableMetadata(tableName, GetIndexesFromColumns(columns.Values));
                    result.SetValues(
                        columns,
                        partitionKeys.OrderBy(p => p.Item1).Select(p => p.Item2).ToArray(),
                        clusteringKeys.OrderBy(p => p.Item1).Select(p => p.Item2).ToArray(),
                        options);
                    return result;
                });
            }));
        }
        /// <summary>
        ///  Returns metadata of specified table in this keyspace.
        /// </summary>
        /// <param name="tableName"> the name of table to retrieve </param>
        /// <returns>the metadata for table <c>tableName</c> in this keyspace if it
        ///  exists, <c>null</c> otherwise.</returns>
        public TableMetadata GetTableMetadata(string tableName)
        {
            TableMetadata table;

            if (_tables.TryGetValue(tableName, out table))
            {
                //The table metadata is available in local cache
                return(table);
            }
            var          keyspaceName     = Name;
            var          cols             = new Dictionary <string, TableColumn>();
            TableOptions options          = null;
            var          tableMetadataRow = _cc.Query(String.Format(SelectSingleTable, tableName, keyspaceName), true).FirstOrDefault();

            if (tableMetadataRow == null)
            {
                return(null);
            }
            var columnsMetadata = _cc.Query(String.Format(SelectColumns, tableName, keyspaceName), true);

            foreach (var row in columnsMetadata)
            {
                var dataType = TypeCodec.ParseDataType(row.GetValue <string>("validator"));
                var dsc      = new TableColumn
                {
                    Name               = row.GetValue <string>("column_name"),
                    Keyspace           = row.GetValue <string>("keyspace_name"),
                    Table              = row.GetValue <string>("columnfamily_name"),
                    TypeCode           = dataType.TypeCode,
                    TypeInfo           = dataType.TypeInfo,
                    SecondaryIndexName = row.GetValue <string>("index_name"),
                    SecondaryIndexType = row.GetValue <string>("index_type"),
                    KeyType            =
                        row.GetValue <string>("index_name") != null
                            ? KeyType.SecondaryIndex
                            : KeyType.None,
                };
                cols.Add(dsc.Name, dsc);
            }

            var colNames = tableMetadataRow.GetValue <string>("column_aliases");
            var rowKeys  = colNames.Substring(1, colNames.Length - 2).Split(',');

            for (var i = 0; i < rowKeys.Length; i++)
            {
                if (rowKeys[i].StartsWith("\""))
                {
                    rowKeys[i] = rowKeys[i].Substring(1, rowKeys[i].Length - 2).Replace("\"\"", "\"");
                }
            }
            if (rowKeys.Length > 0 && rowKeys[0] != string.Empty)
            {
                bool isCompact  = true;
                var  comparator = tableMetadataRow.GetValue <string>("comparator");
                //Remove reversed type
                comparator = comparator.Replace(TypeCodec.ReversedTypeName, "");
                if (comparator.StartsWith(TypeCodec.CompositeTypeName))
                {
                    comparator = comparator.Replace(TypeCodec.CompositeTypeName, "");
                    isCompact  = false;
                }

                var rg           = new Regex(@"org\.apache\.cassandra\.db\.marshal\.\w+");
                var rowKeysTypes = rg.Matches(comparator);

                for (var i = 0; i < rowKeys.Length; i++)
                {
                    var keyName  = rowKeys[i];
                    var dataType = TypeCodec.ParseDataType(rowKeysTypes[i].ToString());
                    var dsc      = new TableColumn
                    {
                        Name     = keyName,
                        Keyspace = tableMetadataRow.GetValue <string>("keyspace_name"),
                        Table    = tableMetadataRow.GetValue <string>("columnfamily_name"),
                        TypeCode = dataType.TypeCode,
                        TypeInfo = dataType.TypeInfo,
                        KeyType  = KeyType.Clustering,
                    };
                    cols[dsc.Name] = dsc;
                }

                options = new TableOptions
                {
                    isCompactStorage  = isCompact,
                    bfFpChance        = tableMetadataRow.GetValue <double>("bloom_filter_fp_chance"),
                    caching           = tableMetadataRow.GetValue <string>("caching"),
                    comment           = tableMetadataRow.GetValue <string>("comment"),
                    gcGrace           = tableMetadataRow.GetValue <int>("gc_grace_seconds"),
                    localReadRepair   = tableMetadataRow.GetValue <double>("local_read_repair_chance"),
                    readRepair        = tableMetadataRow.GetValue <double>("read_repair_chance"),
                    compactionOptions = GetCompactionStrategyOptions(tableMetadataRow),
                    compressionParams =
                        (SortedDictionary <string, string>)Utils.ConvertStringToMap(tableMetadataRow.GetValue <string>("compression_parameters"))
                };
                //replicate_on_write column not present in C* >= 2.1
                if (tableMetadataRow.GetColumn("replicate_on_write") != null)
                {
                    options.replicateOnWrite = tableMetadataRow.GetValue <bool>("replicate_on_write");
                }
            }
            //In Cassandra 1.2, the keys are not stored in the system.schema_columns table
            //But you can get it from system.schema_columnfamilies
            var keys = tableMetadataRow.GetValue <string>("key_aliases")
                       .Replace("[", "")
                       .Replace("]", "")
                       .Split(',');
            var keyTypes = tableMetadataRow.GetValue <string>("key_validator")
                           .Replace("org.apache.cassandra.db.marshal.CompositeType", "")
                           .Replace("(", "")
                           .Replace(")", "")
                           .Split(',');
            var partitionKeys = new TableColumn[keys.Length];

            for (var i = 0; i < keys.Length; i++)
            {
                var name     = keys[i].Replace("\"", "").Trim();
                var dataType = TypeCodec.ParseDataType(keyTypes[i].Trim());
                var c        = new TableColumn()
                {
                    Name     = name,
                    Keyspace = tableMetadataRow.GetValue <string>("keyspace_name"),
                    Table    = tableMetadataRow.GetValue <string>("columnfamily_name"),
                    TypeCode = dataType.TypeCode,
                    TypeInfo = dataType.TypeInfo,
                    KeyType  = KeyType.Partition
                };
                cols[name]       = c;
                partitionKeys[i] = c;
            }

            table = new TableMetadata(tableName, cols.Values.ToArray(), partitionKeys, options);
            //Cache it
            _tables.AddOrUpdate(tableName, table, (k, o) => table);
            return(table);
        }