示例#1
0
        public override async Task <KeyspaceMetadata> GetKeyspaceAsync(string name)
        {
            var ks = await base.GetKeyspaceAsync(name).ConfigureAwait(false);

            if (ks != null)
            {
                return(ks);
            }

            // Maybe its a virtual keyspace
            IEnumerable <Row> rs;

            try
            {
                rs = await Cc.QueryAsync(string.Format(SelectSingleVirtualKeyspace, name), true)
                     .ConfigureAwait(false);
            }
            catch (InvalidQueryException)
            {
                // Incorrect version reported by the server: virtual keyspaces/tables are not yet supported
                return(null);
            }

            var row = rs.FirstOrDefault();

            return(row != null?ParseVirtualKeyspaceRow(row) : null);
        }
示例#2
0
 public override Task <UdtColumnInfo> GetUdtDefinition(string keyspaceName, string typeName)
 {
     return(Cc
            .QueryAsync(string.Format(SelectUdts, keyspaceName, typeName), true)
            .Then(rs =>
     {
         var row = rs.FirstOrDefault();
         if (row == null)
         {
             return TaskHelper.ToTask <UdtColumnInfo>(null);
         }
         var udt = new UdtColumnInfo(row.GetValue <string>("keyspace_name") + "." + row.GetValue <string>("type_name"));
         var fieldTypeTasks = row.GetValue <string[]>("field_types")
                              .Select(name => DataTypeParser.ParseTypeName(_udtResolver, keyspaceName, name))
                              .ToArray();
         return Task.Factory.ContinueWhenAll(fieldTypeTasks, tasks =>
         {
             var ex = tasks.Select(t => t.Exception).FirstOrDefault(e => e != null);
             if (ex != null)
             {
                 throw ex.InnerException;
             }
             var fieldNames = row.GetValue <string[]>("field_names");
             for (var i = 0; i < fieldNames.Length && i < tasks.Length; i++)
             {
                 var field = tasks[i].Result;
                 field.Name = fieldNames[i];
                 udt.Fields.Add(field);
             }
             return udt;
         });
     }));
 }
示例#3
0
        public override async Task <TableMetadata> GetTableAsync(string keyspaceName, string tableName)
        {
            var table = await base.GetTableAsync(keyspaceName, tableName).ConfigureAwait(false);

            if (table != null)
            {
                return(table);
            }

            IEnumerable <Row> tableRs;

            try
            {
                // Maybe its a virtual table
                tableRs = await Cc.QueryAsync(string.Format(SelectVirtualTable, keyspaceName, tableName), true)
                          .ConfigureAwait(false);
            }
            catch (InvalidQueryException)
            {
                // Incorrect version reported by the server: virtual keyspaces/tables are not yet supported
                return(null);
            }

            var columnsRs = await Cc.QueryAsync(string.Format(SelectVirtualColumns, keyspaceName, tableName), true)
                            .ConfigureAwait(false);

            return(await ParseTableOrView(_ => new TableMetadata(tableName, null, true), tableRs, columnsRs)
                   .ConfigureAwait(false));
        }
示例#4
0
        public override Task <AggregateMetadata> GetAggregate(string keyspaceName, string aggregateName, string signatureString)
        {
            var query = string.Format(SelectAggregates, keyspaceName, aggregateName, signatureString);

            return(Cc
                   .QueryAsync(query, true)
                   .ContinueSync(rs =>
            {
                var row = rs.FirstOrDefault();
                if (row == null)
                {
                    return null;
                }

                var emptyArray = new string[0];
                var aggregate = new AggregateMetadata
                {
                    Name = row.GetValue <string>("aggregate_name"),
                    KeyspaceName = row.GetValue <string>("keyspace_name"),
                    Signature = row.GetValue <string[]>("signature") ?? emptyArray,
                    StateFunction = row.GetValue <string>("state_func"),
                    StateType = DataTypeParser.ParseFqTypeName(row.GetValue <string>("state_type")),
                    FinalFunction = row.GetValue <string>("final_func"),
                    ReturnType = DataTypeParser.ParseFqTypeName(row.GetValue <string>("return_type")),
                    ArgumentTypes = (row.GetValue <string[]>("argument_types") ?? emptyArray).Select(s => DataTypeParser.ParseFqTypeName(s)).ToArray(),
                };
                var initConditionRaw = Deserialize(Cc, row.GetValue <byte[]>("initcond"), aggregate.StateType.TypeCode, aggregate.StateType.TypeInfo);
                if (initConditionRaw != null)
                {
                    aggregate.InitialCondition = initConditionRaw.ToString();
                }
                return aggregate;
            }));
        }
示例#5
0
        public override Task <FunctionMetadata> GetFunction(string keyspaceName, string functionName, string signatureString)
        {
            var query = string.Format(SelectFunctions, keyspaceName, functionName, signatureString);

            return(Cc
                   .QueryAsync(query, true)
                   .ContinueSync(rs =>
            {
                var row = rs.FirstOrDefault();
                if (row == null)
                {
                    return null;
                }
                var emptyArray = new string[0];
                return new FunctionMetadata
                {
                    Name = row.GetValue <string>("function_name"),
                    KeyspaceName = row.GetValue <string>("keyspace_name"),
                    Signature = row.GetValue <string[]>("signature") ?? emptyArray,
                    ArgumentNames = row.GetValue <string[]>("argument_names") ?? emptyArray,
                    Body = row.GetValue <string>("body"),
                    CalledOnNullInput = row.GetValue <bool>("called_on_null_input"),
                    Language = row.GetValue <string>("language"),
                    ReturnType = DataTypeParser.ParseFqTypeName(row.GetValue <string>("return_type")),
                    ArgumentTypes = (row.GetValue <string[]>("argument_types") ?? emptyArray).Select(s => DataTypeParser.ParseFqTypeName(s)).ToArray()
                };
            }));
        }
示例#6
0
        public override async Task <KeyspaceMetadata> GetKeyspaceAsync(string name)
        {
            var rs = await Cc.QueryAsync(string.Format(SelectSingleKeyspace, name), true).ConfigureAwait(false);

            var row = rs.FirstOrDefault();

            return(row != null?ParseKeyspaceRow(row) : null);
        }
示例#7
0
        public override Task <FunctionMetadata> GetFunctionAsync(string keyspaceName, string functionName, string signatureString)
        {
            var query = string.Format(SelectFunctions, keyspaceName, functionName, signatureString);

            return(Cc
                   .QueryAsync(query, true)
                   .Then(rs =>
            {
                var row = rs.FirstOrDefault();
                if (row == null)
                {
                    return TaskHelper.ToTask <FunctionMetadata>(null);
                }
                var argumentTypes = row.GetValue <string[]>("argument_types") ?? new string[0];
                var parseTasks = new Task <ColumnDesc> [1 + argumentTypes.Length];
                parseTasks[0] = DataTypeParser.ParseTypeName(_udtResolver, row.GetValue <string>("keyspace_name"), row.GetValue <string>("return_type"));
                for (var i = 0; i < argumentTypes.Length; i++)
                {
                    parseTasks[1 + i] = DataTypeParser.ParseTypeName(_udtResolver, row.GetValue <string>("keyspace_name"), argumentTypes[i]);
                }
                return Task.Factory.ContinueWhenAll(parseTasks, tasks =>
                {
                    var ex = tasks.Select(t => t.Exception).FirstOrDefault(e => e != null);
                    if (ex != null)
                    {
                        throw ex.InnerException;
                    }

                    var result = new FunctionMetadata
                    {
                        Name = row.GetValue <string>("function_name"),
                        KeyspaceName = row.GetValue <string>("keyspace_name"),
                        Signature = argumentTypes,
                        ArgumentNames = row.GetValue <string[]>("argument_names") ?? new string[0],
                        Body = row.GetValue <string>("body"),
                        CalledOnNullInput = row.GetValue <bool>("called_on_null_input"),
                        Language = row.GetValue <string>("language"),
                        ReturnType = tasks[0].Result,
                        ArgumentTypes = tasks.Skip(1).Select(t => t.Result).ToArray()
                    };

                    if (row.GetColumn("deterministic") != null)
                    {
                        // DSE 6.0+
                        result.Deterministic = row.GetValue <bool>("deterministic");
                        result.Monotonic = row.GetValue <bool>("monotonic");
                        result.MonotonicOn = row.GetValue <string[]>("monotonic_on");
                    }

                    return result;
                });
            }));
        }
示例#8
0
        public override async Task <MaterializedViewMetadata> GetViewAsync(string keyspaceName, string viewName)
        {
            var getTableTask   = Cc.QueryAsync(string.Format(SelectSingleView, viewName, keyspaceName), true);
            var getColumnsTask = Cc.QueryAsync(string.Format(SelectColumns, viewName, keyspaceName), true);

            var tableRs = await getTableTask.ConfigureAwait(false);

            var columnsRs = await getColumnsTask.ConfigureAwait(false);

            return(await ParseTableOrView(
                       viewRow => new MaterializedViewMetadata(viewName, viewRow.GetValue <string>("where_clause")),
                       tableRs,
                       columnsRs).ConfigureAwait(false));
        }
示例#9
0
        public override Task <MaterializedViewMetadata> GetView(string keyspaceName, string viewName)
        {
            var getTableTask   = Cc.QueryAsync(string.Format(SelectSingleView, viewName, keyspaceName), true);
            var getColumnsTask = Cc.QueryAsync(string.Format(SelectColumns, viewName, keyspaceName), true);

            return(Task.Factory.ContinueWhenAll(new[] { getTableTask, getColumnsTask }, tasks =>
            {
                var ex = tasks.Select(t => t.Exception).FirstOrDefault(e => e != null);
                if (ex != null)
                {
                    throw ex.InnerException;
                }
                return ParseTableOrView(viewRow => new MaterializedViewMetadata(viewName, viewRow.GetValue <string>("where_clause")), tasks[0], tasks[1]);
            }, TaskContinuationOptions.ExecuteSynchronously).Unwrap());
        }
示例#10
0
        public override Task <TableMetadata> GetTable(string keyspaceName, string tableName)
        {
            var getTableTask   = Cc.QueryAsync(string.Format(SelectSingleTable, tableName, keyspaceName), true);
            var getColumnsTask = Cc.QueryAsync(string.Format(SelectColumns, tableName, keyspaceName), true);
            var getIndexesTask = Cc.QueryAsync(string.Format(SelectIndexes, tableName, keyspaceName), true);

            return(Task.Factory.ContinueWhenAll(new[] { getTableTask, getColumnsTask, getIndexesTask }, tasks =>
            {
                var ex = tasks.Select(t => t.Exception).FirstOrDefault(e => e != null);
                if (ex != null)
                {
                    throw ex.InnerException;
                }
                return ParseTableOrView(_ => new TableMetadata(tableName, GetIndexes(tasks[2].Result)), tasks[0], tasks[1]);
            }, TaskContinuationOptions.ExecuteSynchronously).Unwrap());
        }
示例#11
0
        public override Task <AggregateMetadata> GetAggregateAsync(string keyspaceName, string aggregateName, string signatureString)
        {
            var query = string.Format(SelectAggregates, keyspaceName, aggregateName, signatureString);

            return(Cc
                   .QueryAsync(query, true)
                   .Then(rs =>
            {
                var row = rs.FirstOrDefault();
                if (row == null)
                {
                    return TaskHelper.ToTask <AggregateMetadata>(null);
                }
                var argumentTypes = row.GetValue <string[]>("argument_types") ?? new string[0];
                //state_type + return_type + amount of argument types
                var parseTasks = new Task <ColumnDesc> [2 + argumentTypes.Length];
                parseTasks[0] = DataTypeParser.ParseTypeName(_udtResolver, row.GetValue <string>("keyspace_name"), row.GetValue <string>("state_type"));
                parseTasks[1] = DataTypeParser.ParseTypeName(_udtResolver, row.GetValue <string>("keyspace_name"), row.GetValue <string>("return_type"));
                for (var i = 0; i < argumentTypes.Length; i++)
                {
                    parseTasks[2 + i] = DataTypeParser.ParseTypeName(_udtResolver, row.GetValue <string>("keyspace_name"), argumentTypes[i]);
                }
                return Task.Factory.ContinueWhenAll(parseTasks, tasks =>
                {
                    var ex = tasks.Select(t => t.Exception).FirstOrDefault(e => e != null);
                    if (ex != null)
                    {
                        throw ex.InnerException;
                    }
                    return new AggregateMetadata
                    {
                        Name = row.GetValue <string>("aggregate_name"),
                        KeyspaceName = row.GetValue <string>("keyspace_name"),
                        StateFunction = row.GetValue <string>("state_func"),
                        FinalFunction = row.GetValue <string>("final_func"),
                        InitialCondition = row.GetValue <string>("initcond"),
                        Deterministic = row.GetColumn("deterministic") != null &&
                                        row.GetValue <bool>("deterministic"),
                        Signature = argumentTypes,
                        StateType = tasks[0].Result,
                        ReturnType = tasks[1].Result,
                        ArgumentTypes = tasks.Skip(2).Select(t => t.Result).ToArray()
                    };
                }, TaskContinuationOptions.ExecuteSynchronously);
            }));
        }
示例#12
0
        public override async Task <TableMetadata> GetTableAsync(string keyspaceName, string tableName)
        {
            var getTableTask   = Cc.QueryAsync(string.Format(SelectSingleTable, tableName, keyspaceName), true);
            var getColumnsTask = Cc.QueryAsync(string.Format(SelectColumns, tableName, keyspaceName), true);
            var getIndexesTask = Cc.QueryAsync(string.Format(SelectIndexes, tableName, keyspaceName), true);

            await Task.WhenAll(getTableTask, getColumnsTask, getIndexesTask).ConfigureAwait(false);

            var indexesRs = await getIndexesTask.ConfigureAwait(false);

            var tableRs = await getTableTask.ConfigureAwait(false);

            var columnsRs = await getColumnsTask.ConfigureAwait(false);

            var indexes = GetIndexes(indexesRs);

            return(await ParseTableOrView(_ => new TableMetadata(tableName, indexes), tableRs, columnsRs)
                   .ConfigureAwait(false));
        }
示例#13
0
        public override async Task <ICollection <string> > GetKeyspacesNamesAsync()
        {
            // Start the task to get the keyspace names in parallel
            var keyspacesTask    = base.GetKeyspacesNamesAsync();
            var virtualKeyspaces = Enumerable.Empty <string>();

            try
            {
                var rs = await Cc.QueryAsync(SelectVirtualKeyspaceNames, true).ConfigureAwait(false);

                virtualKeyspaces = rs.Select(r => r.GetValue <string>(0));
            }
            catch (InvalidQueryException)
            {
                // Incorrect version reported by the server: virtual keyspaces/tables are not yet supported
            }

            var keyspaces = await keyspacesTask.ConfigureAwait(false);

            // Yield the keyspaces followed by the virtual keyspaces
            return(keyspaces.Concat(virtualKeyspaces).ToArray());
        }
示例#14
0
        public override async Task <IEnumerable <KeyspaceMetadata> > GetKeyspacesAsync(bool retry)
        {
            // Start the task to get the keyspaces in parallel
            var keyspacesTask    = base.GetKeyspacesAsync(retry);
            var virtualKeyspaces = Enumerable.Empty <KeyspaceMetadata>();

            try
            {
                var rs = await Cc.QueryAsync(SchemaParserV3.SelectVirtualKeyspaces, retry).ConfigureAwait(false);

                virtualKeyspaces = rs.Select(ParseVirtualKeyspaceRow);
            }
            catch (InvalidQueryException)
            {
                // Incorrect version reported by the server: virtual keyspaces/tables are not yet supported
            }

            var keyspaces = await keyspacesTask.ConfigureAwait(false);

            // Yield the keyspaces followed by the virtual keyspaces
            return(keyspaces.Concat(virtualKeyspaces));
        }
示例#15
0
 public override Task <UdtColumnInfo> GetUdtDefinition(string keyspaceName, string typeName)
 {
     return(Cc
            .QueryAsync(string.Format(SelectUdts, keyspaceName, typeName), true)
            .ContinueSync(rs =>
     {
         var row = rs.FirstOrDefault();
         if (row == null)
         {
             return null;
         }
         var udt = new UdtColumnInfo(row.GetValue <string>("keyspace_name") + "." + row.GetValue <string>("type_name"));
         var fieldNames = row.GetValue <string[]>("field_names");
         var fieldTypes = row.GetValue <string[]>("field_types");
         for (var i = 0; i < fieldNames.Length && i < fieldTypes.Length; i++)
         {
             var field = DataTypeParser.ParseFqTypeName(fieldTypes[i]);
             field.Name = fieldNames[i];
             udt.Fields.Add(field);
         }
         return udt;
     }));
 }
示例#16
0
        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;
                });
            }));
        }
示例#17
0
        public override async Task <IEnumerable <KeyspaceMetadata> > GetKeyspacesAsync(bool retry)
        {
            var rs = await Cc.QueryAsync(SelectKeyspaces, retry).ConfigureAwait(false);

            return(rs.Select(ParseKeyspaceRow));
        }
示例#18
0
        public override async Task <ICollection <string> > GetKeyspacesNamesAsync()
        {
            var rs = await Cc.QueryAsync(SelectKeyspacesNames, true).ConfigureAwait(false);

            return(rs.Select(r => r.GetValue <string>(0)).ToArray());
        }
示例#19
0
 public override Task <IEnumerable <KeyspaceMetadata> > GetKeyspaces(bool retry)
 {
     return(Cc
            .QueryAsync(SelectKeyspaces, retry)
            .ContinueSync(rs => rs.Select(ParseKeyspaceRow)));
 }
示例#20
0
 public override Task <KeyspaceMetadata> GetKeyspace(string name)
 {
     return(Cc
            .QueryAsync(string.Format(SelectSingleKeyspace, name), true)
            .ContinueSync(rs => ParseKeyspaceRow(rs.FirstOrDefault())));
 }