internal TableMetadata(BEBinaryReader reader) { List<TableColumn> coldat = new List<TableColumn>(); var flags = (FlagBits)reader.ReadInt32(); var numberOfcolumns = reader.ReadInt32(); this.TableColumns = new TableColumn[numberOfcolumns]; string gKsname = null; string gTablename = null; if ((flags & FlagBits.GlobalTablesSpec) == FlagBits.GlobalTablesSpec) { gKsname = reader.ReadString(); gTablename = reader.ReadString(); } for (int i = 0; i < numberOfcolumns; i++) { var col = new TableColumn(); if ((flags & FlagBits.GlobalTablesSpec) != FlagBits.GlobalTablesSpec) { col.Keyspace = reader.ReadString(); col.Table = reader.ReadString(); } else { col.Keyspace = gKsname; col.Table = gTablename; } col.Name = reader.ReadString(); col.TypeCode = (ColumnTypeCode)reader.ReadUInt16(); col.TypeInfo = GetColumnInfo(reader, col.TypeCode); coldat.Add(col); } TableColumns = coldat.ToArray(); }
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; }
/// <summary> /// From legacy columns /// </summary> internal static IndexMetadata FromTableColumn(TableColumn c) { //using obsolete properties #pragma warning disable 618 string target = null; if (c.SecondaryIndexOptions.ContainsKey("index_keys")) { target = string.Format("keys({0})", c.Name); } else if (c.SecondaryIndexOptions.ContainsKey("index_keys_and_values")) { target = string.Format("entries({0})", c.Name); } else if (c.TypeCode == ColumnTypeCode.List || c.TypeCode == ColumnTypeCode.Set || c.TypeCode == ColumnTypeCode.Map) { target = string.Format("values({0})", c.Name); } return new IndexMetadata(c.SecondaryIndexName, target, GetKindByName(c.SecondaryIndexType), c.SecondaryIndexOptions); #pragma warning restore 618 }
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); }
/// <summary> /// Sets the partition keys of the query /// </summary> /// <returns>True if it was possible to set the routing indexes for this query</returns> internal bool SetPartitionKeys(TableColumn[] keys) { var queryParameters = Metadata.Columns; var routingIndexes = new List<int>(); foreach (var key in keys) { //find the position of the key in the parameters for (var i = 0; i < queryParameters.Length; i++) { if (queryParameters[i].Name != key.Name) { continue; } routingIndexes.Add(i); break; } } if (routingIndexes.Count != keys.Length) { //The parameter names don't match the partition keys return false; } RoutingIndexes = routingIndexes.ToArray(); return true; }
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); }
/// <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; }
internal TableMetadata(string name, TableColumn[] tableColumns) { Name = name; TableColumns = tableColumns; }
/// <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 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)); }
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)); 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))); 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; }); })); }
public TableMetadata GetTableMetadata(string tableName, string keyspaceName) { object[] collectionValuesTypes; List<TableColumn> cols = new List<TableColumn>(); using ( var rows = _session.Query( string.Format(SelectColumns + " WHERE columnfamily_name='{0}' AND keyspace_name='{1}';", tableName, keyspaceName))) { 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); } } using ( var rows = _session.Query( string.Format( SelectColumnFamilies + " WHERE columnfamily_name='{0}' AND keyspace_name='{1}';", tableName, keyspaceName))) { foreach (var row in rows.GetRows()) { 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) { var rg = new Regex(@"org\.apache\.cassandra\.db\.marshal\.\w+"); var rowKeysTypes = rg.Matches(row.GetValue<string>("comparator")); int i = 0; foreach (var keyName in rowKeys) { var tp_code = convertToColumnTypeCode(rowKeysTypes[i + 1].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++; } } 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()); }