예제 #1
0
 internal TableMetadata(string name, TableColumn[] tableColumns, TableColumn[] partitionKeys, TableOptions options)
 {
     Name = name;
     TableColumns = tableColumns;
     PartitionKeys = partitionKeys;
     Options = options;
 }
 internal void SetValues(IDictionary<string, TableColumn> columns, TableColumn[] partitionKeys, Tuple<TableColumn, SortOrder>[] clusteringKeys, TableOptions options)
 {
     ColumnsByName = columns;
     TableColumns = columns.Values.ToArray();
     PartitionKeys = partitionKeys;
     ClusteringKeys = clusteringKeys;
     Options = options;
 }
예제 #3
0
        public TableMetadata GetTableMetadata(string tableName, string keyspaceName)
        {
            object[] collectionValuesTypes;
            List<TableColumn> cols = new List<TableColumn>();
            TableOptions Options = null;
            {
                int streamId = _activeConnection.Value.AllocateStreamId();
                using (var rows = ProcessRowset(_activeConnection.Value.Query(streamId,
                        string.Format(SelectColumns + " WHERE columnfamily_name='{0}' AND keyspace_name='{1}';",
                                    tableName, keyspaceName), ConsistencyLevel.Default, false)))
                {
                    foreach (var row in rows.GetRows())
                    {
                        var tp_code = convertToColumnTypeCode(row.GetValue<string>("validator"), out collectionValuesTypes);
                        var dsc = new TableColumn()
                            {
                                Name = row.GetValue<string>("column_name"),
                                Keyspace = row.GetValue<string>("keyspace_name"),
                                Table = row.GetValue<string>("columnfamily_name"),
                                TypeCode = tp_code,
                                SecondaryIndexName = row.GetValue<string>("index_name"),
                                SecondaryIndexType = row.GetValue<string>("index_type"),
                                KeyType =
                                    row.GetValue<string>("index_name") != null
                                        ? KeyType.SecondaryIndex
                                        : KeyType.None,
                            };

                        if (tp_code == ColumnTypeCode.List)
                            dsc.TypeInfo = new ListColumnInfo()
                                {
                                    ValueTypeCode = (ColumnTypeCode)collectionValuesTypes[0]
                                };
                        else if (tp_code == ColumnTypeCode.Map)
                            dsc.TypeInfo = new MapColumnInfo()
                                {
                                    KeyTypeCode = (ColumnTypeCode)collectionValuesTypes[0],
                                    ValueTypeCode = (ColumnTypeCode)collectionValuesTypes[1]
                                };
                        else if (tp_code == ColumnTypeCode.Set)
                            dsc.TypeInfo = new SetColumnInfo()
                                {
                                    KeyTypeCode = (ColumnTypeCode)collectionValuesTypes[0]
                                };

                        cols.Add(dsc);
                    }
                }
            }
            {
                int streamId = _activeConnection.Value.AllocateStreamId();
                using (var rows = ProcessRowset(_activeConnection.Value.Query(streamId,
                    string.Format(
                                SelectColumnFamilies + " WHERE columnfamily_name='{0}' AND keyspace_name='{1}';",
                                tableName, keyspaceName),ConsistencyLevel.Default,false)))
                {
                    foreach (var row in rows.GetRows()) // There is only one row!
                    {
                        var colNames = row.GetValue<string>("column_aliases");
                        var rowKeys = colNames.Substring(1, colNames.Length - 2).Split(',');
                        for (int 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;
                            string comparator = row.GetValue<string>("comparator");
                            if (comparator.StartsWith("org.apache.cassandra.db.marshal.CompositeType"))
                            {
                                comparator = comparator.Replace("org.apache.cassandra.db.marshal.CompositeType", "");
                                isCompact = false;
                            }

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

                            int i = 0;
                            foreach (var keyName in rowKeys)
                            {
                                var tp_code = convertToColumnTypeCode(rowKeysTypes[i].ToString(),
                                                                      out collectionValuesTypes);
                                var dsc = new TableColumn()
                                    {
                                        Name = keyName.ToString(),
                                        Keyspace = row.GetValue<string>("keyspace_name"),
                                        Table = row.GetValue<string>("columnfamily_name"),
                                        TypeCode = tp_code,
                                        KeyType = KeyType.Clustering,
                                    };
                                if (tp_code == ColumnTypeCode.List)
                                    dsc.TypeInfo = new ListColumnInfo()
                                        {
                                            ValueTypeCode = (ColumnTypeCode)collectionValuesTypes[0]
                                        };
                                else if (tp_code == ColumnTypeCode.Map)
                                    dsc.TypeInfo = new MapColumnInfo()
                                        {
                                            KeyTypeCode = (ColumnTypeCode)collectionValuesTypes[0],
                                            ValueTypeCode = (ColumnTypeCode)collectionValuesTypes[1]
                                        };
                                else if (tp_code == ColumnTypeCode.Set)
                                    dsc.TypeInfo = new SetColumnInfo()
                                        {
                                            KeyTypeCode = (ColumnTypeCode)collectionValuesTypes[0]
                                        };
                                cols.Add(dsc);
                                i++;
                            }

                            Options = new TableOptions()
                            {
                                isCompactStorage = isCompact,
                                bfFpChance = row.GetValue<double>("bloom_filter_fp_chance"),
                                caching = row.GetValue<string>("caching"),
                                comment = row.GetValue<string>("comment"),
                                gcGrace = row.GetValue<int>("gc_grace_seconds"),
                                localReadRepair = row.GetValue<double>("local_read_repair_chance"),
                                readRepair = row.GetValue<double>("read_repair_chance"),
                                replicateOnWrite = row.GetValue<bool>("replicate_on_write"),
                                compactionOptions = (SortedDictionary<string, string>)getCompactionStrategyOptions(row),
                                compressionParams = (SortedDictionary<string, string>)Utils.ConvertStringToMap(row.GetValue<string>("compression_parameters"))
                            };

                        }
                        cols.Add(new TableColumn()
                            {
                                Name =
                                    row.GetValue<string>("key_aliases")
                                       .Replace("[\"", "")
                                       .Replace("\"]", "")
                                       .Replace("\"\"", "\""),
                                Keyspace = row.GetValue<string>("keyspace_name"),
                                Table = row.GetValue<string>("columnfamily_name"),
                                TypeCode =
                                    convertToColumnTypeCode(row.GetValue<string>("key_validator"), out collectionValuesTypes),
                                KeyType = KeyType.Partition
                            });
                    }
                }
            }
            return new TableMetadata(tableName, cols.ToArray(), Options);
        }
예제 #4
0
        public void checkMetadata(string TableName = null, string KeyspaceName = null, TableOptions tableOptions = null)
        {
            CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
            try
            {
                Session = CCMCluster.Session;
                Cluster = CCMCluster.Cluster;
                Session.CreateKeyspaceIfNotExists(Keyspace);
                Session.ChangeKeyspace(Keyspace);

                checkPureMetadata(TableName, KeyspaceName, tableOptions);
            }
            finally
            {
                CCMCluster.Discard();
            }
        }
예제 #5
0
        public void checkPureMetadata(string TableName = null, string KeyspaceName = null, TableOptions tableOptions = null)
        {
            Dictionary<string, ColumnTypeCode> columns = new Dictionary
                               <string, ColumnTypeCode>()
                {
                    {"q0uuid", ColumnTypeCode.Uuid},
                    {"q1timestamp", ColumnTypeCode.Timestamp},
                    {"q2double", ColumnTypeCode.Double},
                    {"q3int32", ColumnTypeCode.Int},
                    {"q4int64", ColumnTypeCode.Bigint},
                    {"q5float", ColumnTypeCode.Float},
                    {"q6inet", ColumnTypeCode.Inet},
                    {"q7boolean", ColumnTypeCode.Boolean},
                    {"q8inet", ColumnTypeCode.Inet},
                    {"q9blob", ColumnTypeCode.Blob},
            #if NET_40_OR_GREATER
                         {"q10varint", ColumnTypeCode.Varint},
                         {"q11decimal", ColumnTypeCode.Decimal},
            #endif
                    {"q12list", ColumnTypeCode.List},
                    {"q13set", ColumnTypeCode.Set},
                    {"q14map", ColumnTypeCode.Map}
                    //{"q12counter", Metadata.ColumnTypeCode.Counter}, A table that contains a counter can only contain counters
                };

            string tablename = TableName ?? "table" + Guid.NewGuid().ToString("N");
            StringBuilder sb = new StringBuilder(@"CREATE TABLE " + tablename + " (");
            Randomm urndm = new Randomm(DateTimeOffset.Now.Millisecond);

            foreach (var col in columns)
                sb.Append(col.Key + " " + col.Value.ToString() +
                          (((col.Value == ColumnTypeCode.List) ||
                            (col.Value == ColumnTypeCode.Set) ||
                            (col.Value == ColumnTypeCode.Map))
                               ? "<int" + (col.Value == ColumnTypeCode.Map ? ",varchar>" : ">")
                               : "") + ", ");

            sb.Append("PRIMARY KEY(");
            int rowKeys = urndm.Next(1, columns.Count - 3);

            for (int i = 0; i < rowKeys; i++)
                sb.Append(columns.Keys.Where(key => key.StartsWith("q" + i.ToString())).First() +
                          ((i == rowKeys - 1) ? "" : ", "));
            var opt = tableOptions != null ? " WITH " + tableOptions.ToString() : "";
            sb.Append("))" + opt + ";");

            Session.Cluster.WaitForSchemaAgreement(
                QueryTools.ExecuteSyncNonQuery(Session, sb.ToString())

            );

            var table = this.Cluster.Metadata.GetTable(KeyspaceName ?? Keyspace, tablename);
            foreach (var metaCol in table.TableColumns)
            {
                Assert.True(columns.Keys.Contains(metaCol.Name));
                Assert.True(metaCol.TypeCode ==
                            columns.Where(tpc => tpc.Key == metaCol.Name).First().Value);
                Assert.True(metaCol.Table == tablename);
                Assert.True(metaCol.Keyspace == (KeyspaceName ?? Keyspace));
            }
            if (tableOptions != null)
                Assert.True(tableOptions.Equals(table.Options));
        }
예제 #6
0
        public TableMetadata GetTableMetadata(string tableName, string keyspaceName)
        {
            var cols = new Dictionary<string, TableColumn>();
            TableOptions options = null;
            {
                var cqlQuery = string.Format(SelectColumns + " WHERE columnfamily_name='{0}' AND keyspace_name='{1}';", tableName, keyspaceName);
                var rows = Query(cqlQuery);
                foreach (var row in rows)
                {
                    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 cqlQuery = string.Format(SelectColumnFamilies + " WHERE columnfamily_name='{0}' AND keyspace_name='{1}';", tableName, keyspaceName);
                var rows = Query(cqlQuery);
                var row = rows.First();
                var colNames = row.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 = row.GetValue<string>("comparator");
                    if (comparator.StartsWith("org.apache.cassandra.db.marshal.CompositeType"))
                    {
                        comparator = comparator.Replace("org.apache.cassandra.db.marshal.CompositeType", "");
                        isCompact = false;
                    }

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

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

                    options = new TableOptions
                    {
                        isCompactStorage = isCompact,
                        bfFpChance = row.GetValue<double>("bloom_filter_fp_chance"),
                        caching = row.GetValue<string>("caching"),
                        comment = row.GetValue<string>("comment"),
                        gcGrace = row.GetValue<int>("gc_grace_seconds"),
                        localReadRepair = row.GetValue<double>("local_read_repair_chance"),
                        readRepair = row.GetValue<double>("read_repair_chance"),
                        compactionOptions = getCompactionStrategyOptions(row),
                        compressionParams =
                            (SortedDictionary<string, string>) Utils.ConvertStringToMap(row.GetValue<string>("compression_parameters"))
                    };
                    //replicate_on_write column not present in C* >= 2.1
                    if (row.GetColumn("replicate_on_write") != null)
                    {
                        options.replicateOnWrite = row.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 = row.GetValue<string>("key_aliases")
                              .Replace("[", "")
                              .Replace("]", "")
                              .Split(',');
                var keyTypes = row.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());
                    cols[name] = new TableColumn()
                    {
                        Name = name,
                        Keyspace = row.GetValue<string>("keyspace_name"),
                        Table = row.GetValue<string>("columnfamily_name"),
                        TypeCode = dataType.TypeCode,
                        TypeInfo = dataType.TypeInfo,
                        KeyType = KeyType.Partition
                    };
                }
            }
            return new TableMetadata(tableName, cols.Values.ToArray(), options);
        }
예제 #7
0
        /// <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;
        }
예제 #9
0
        /// <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);
        }
예제 #10
0
        public TableMetadata GetTableMetadata(string tableName, string keyspaceName)
        {
            var          cols    = new Dictionary <string, TableColumn>();
            TableOptions options = null;

            {
                var cqlQuery = string.Format(SelectColumns + " WHERE columnfamily_name='{0}' AND keyspace_name='{1}';", tableName, keyspaceName);
                var rows     = Query(cqlQuery);
                foreach (var row in rows)
                {
                    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 cqlQuery = string.Format(SelectColumnFamilies + " WHERE columnfamily_name='{0}' AND keyspace_name='{1}';", tableName, keyspaceName);
                var rows     = Query(cqlQuery);
                var row      = rows.First();
                var colNames = row.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("\"\"", "\"");
                    }
                }
                //Needs cleanup
                if (rowKeys.Length > 0 && rowKeys[0] != string.Empty)
                {
                    bool isCompact  = true;
                    var  comparator = row.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+");
                    MatchCollection rowKeysTypes = rg.Matches(comparator);

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

                    options = new TableOptions
                    {
                        isCompactStorage  = isCompact,
                        bfFpChance        = row.GetValue <double>("bloom_filter_fp_chance"),
                        caching           = row.GetValue <string>("caching"),
                        comment           = row.GetValue <string>("comment"),
                        gcGrace           = row.GetValue <int>("gc_grace_seconds"),
                        localReadRepair   = row.GetValue <double>("local_read_repair_chance"),
                        readRepair        = row.GetValue <double>("read_repair_chance"),
                        compactionOptions = getCompactionStrategyOptions(row),
                        compressionParams =
                            (SortedDictionary <string, string>)Utils.ConvertStringToMap(row.GetValue <string>("compression_parameters"))
                    };
                    //replicate_on_write column not present in C* >= 2.1
                    if (row.GetColumn("replicate_on_write") != null)
                    {
                        options.replicateOnWrite = row.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 = row.GetValue <string>("key_aliases")
                           .Replace("[", "")
                           .Replace("]", "")
                           .Split(',');
                var keyTypes = row.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());
                    cols[name] = new TableColumn()
                    {
                        Name     = name,
                        Keyspace = row.GetValue <string>("keyspace_name"),
                        Table    = row.GetValue <string>("columnfamily_name"),
                        TypeCode = dataType.TypeCode,
                        TypeInfo = dataType.TypeInfo,
                        KeyType  = KeyType.Partition
                    };
                }
            }
            return(new TableMetadata(tableName, cols.Values.ToArray(), options));
        }