Example #1
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()
                };
            }));
        }
Example #2
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;
            }));
        }
Example #3
0
        private static ColumnDesc[] AdaptKeyTypes(string typesString)
        {
            if (typesString == null)
            {
                return(new ColumnDesc[0]);
            }
            var indexes = new List <int>();

            for (var i = 1; i < typesString.Length; i++)
            {
                if (typesString[i] == ',')
                {
                    indexes.Add(i + 1);
                }
            }
            if (typesString.StartsWith(CompositeTypeName))
            {
                indexes.Insert(0, CompositeTypeName.Length + 1);
                indexes.Add(typesString.Length);
            }
            else
            {
                indexes.Insert(0, 0);
                //we are talking about indexes
                //the next valid start indexes would be at length + 1
                indexes.Add(typesString.Length + 1);
            }
            var types = new ColumnDesc[indexes.Count - 1];

            for (var i = 0; i < types.Length; i++)
            {
                types[i] = DataTypeParser.ParseFqTypeName(typesString, indexes[i], indexes[i + 1] - indexes[i] - 1);
            }
            return(types);
        }
Example #4
0
        public void ParseFqTypeName_Should_Parse_Custom_Types()
        {
            var typeNames = new[]
            {
                "org.apache.cassandra.db.marshal.MyCustomType",
                "com.datastax.dse.whatever.TypeName"
            };

            foreach (var typeName in typeNames)
            {
                var type = DataTypeParser.ParseFqTypeName(typeName);
                Assert.AreEqual(ColumnTypeCode.Custom, type.TypeCode);
                var info = (CustomColumnInfo)type.TypeInfo;
                Assert.AreEqual(typeName, info.CustomTypeName);
            }
        }
Example #5
0
        public void Parse_DataType_Name_Frozen_Test()
        {
            var dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.FrozenType(org.apache.cassandra.db.marshal.ListType(org.apache.cassandra.db.marshal.TimeUUIDType))");

            Assert.AreEqual(ColumnTypeCode.List, dataType.TypeCode);
            Assert.IsInstanceOf <ListColumnInfo>(dataType.TypeInfo);
            Assert.AreEqual(ColumnTypeCode.Timeuuid, ((ListColumnInfo)dataType.TypeInfo).ValueTypeCode);

            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.MapType(org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.FrozenType(org.apache.cassandra.db.marshal.ListType(org.apache.cassandra.db.marshal.Int32Type)))");
            Assert.AreEqual(ColumnTypeCode.Map, dataType.TypeCode);
            Assert.IsInstanceOf <MapColumnInfo>(dataType.TypeInfo);
            Assert.AreEqual(ColumnTypeCode.Varchar, ((MapColumnInfo)dataType.TypeInfo).KeyTypeCode);
            Assert.AreEqual(ColumnTypeCode.List, ((MapColumnInfo)dataType.TypeInfo).ValueTypeCode);
            var subType = (ListColumnInfo)(((MapColumnInfo)dataType.TypeInfo).ValueTypeInfo);

            Assert.AreEqual(ColumnTypeCode.Int, subType.ValueTypeCode);
        }
Example #6
0
        public void ParseDataTypeNameSingleTest()
        {
            var dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.Int32Type");

            Assert.AreEqual(ColumnTypeCode.Int, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.UUIDType");
            Assert.AreEqual(ColumnTypeCode.Uuid, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.UTF8Type");
            Assert.AreEqual(ColumnTypeCode.Varchar, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.BytesType");
            Assert.AreEqual(ColumnTypeCode.Blob, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.FloatType");
            Assert.AreEqual(ColumnTypeCode.Float, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.DoubleType");
            Assert.AreEqual(ColumnTypeCode.Double, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.BooleanType");
            Assert.AreEqual(ColumnTypeCode.Boolean, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.InetAddressType");
            Assert.AreEqual(ColumnTypeCode.Inet, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.DateType");
            Assert.AreEqual(ColumnTypeCode.Timestamp, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.TimestampType");
            Assert.AreEqual(ColumnTypeCode.Timestamp, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.LongType");
            Assert.AreEqual(ColumnTypeCode.Bigint, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.DecimalType");
            Assert.AreEqual(ColumnTypeCode.Decimal, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.IntegerType");
            Assert.AreEqual(ColumnTypeCode.Varint, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.CounterColumnType");
            Assert.AreEqual(ColumnTypeCode.Counter, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.TimeUUIDType");
            Assert.AreEqual(ColumnTypeCode.Timeuuid, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.AsciiType");
            Assert.AreEqual(ColumnTypeCode.Ascii, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.SimpleDateType");
            Assert.AreEqual(ColumnTypeCode.Date, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.TimeType");
            Assert.AreEqual(ColumnTypeCode.Time, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.ShortType");
            Assert.AreEqual(ColumnTypeCode.SmallInt, dataType.TypeCode);
            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.ByteType");
            Assert.AreEqual(ColumnTypeCode.TinyInt, dataType.TypeCode);
        }
Example #7
0
        public void Parse_DataType_Name_Udt_Test()
        {
            var typeText =
                "org.apache.cassandra.db.marshal.UserType(" +
                "tester,70686f6e65,616c696173:org.apache.cassandra.db.marshal.UTF8Type,6e756d626572:org.apache.cassandra.db.marshal.UTF8Type" +
                ")";
            var dataType = DataTypeParser.ParseFqTypeName(typeText);

            Assert.AreEqual(ColumnTypeCode.Udt, dataType.TypeCode);
            //Udt name
            Assert.AreEqual("phone", dataType.Name);
            Assert.IsInstanceOf <UdtColumnInfo>(dataType.TypeInfo);
            var subTypes = ((UdtColumnInfo)dataType.TypeInfo).Fields;

            Assert.AreEqual(2, subTypes.Count);
            Assert.AreEqual("alias", subTypes[0].Name);
            Assert.AreEqual(ColumnTypeCode.Varchar, subTypes[0].TypeCode);
            Assert.AreEqual("number", subTypes[1].Name);
            Assert.AreEqual(ColumnTypeCode.Varchar, subTypes[1].TypeCode);
        }
Example #8
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;
     }));
 }
Example #9
0
        public void Parse_DataType_Name_Udt_Nested_Test()
        {
            var typeText =
                "org.apache.cassandra.db.marshal.UserType(" +
                "tester," +
                "61646472657373," +
                "737472656574:org.apache.cassandra.db.marshal.UTF8Type," +
                "5a4950:org.apache.cassandra.db.marshal.Int32Type," +
                "70686f6e6573:org.apache.cassandra.db.marshal.SetType(" +
                "org.apache.cassandra.db.marshal.UserType(" +
                "tester," +
                "70686f6e65," +
                "616c696173:org.apache.cassandra.db.marshal.UTF8Type," +
                "6e756d626572:org.apache.cassandra.db.marshal.UTF8Type))" +
                ")";
            var dataType = DataTypeParser.ParseFqTypeName(typeText);

            Assert.AreEqual(ColumnTypeCode.Udt, dataType.TypeCode);
            Assert.IsInstanceOf <UdtColumnInfo>(dataType.TypeInfo);
            Assert.AreEqual("address", dataType.Name);
            Assert.AreEqual("tester.address", ((UdtColumnInfo)dataType.TypeInfo).Name);
            var subTypes = ((UdtColumnInfo)dataType.TypeInfo).Fields;

            Assert.AreEqual(3, subTypes.Count);
            Assert.AreEqual("street,ZIP,phones", String.Join(",", subTypes.Select(s => s.Name)));
            Assert.AreEqual(ColumnTypeCode.Varchar, subTypes[0].TypeCode);
            Assert.AreEqual(ColumnTypeCode.Set, subTypes[2].TypeCode);
            //field name
            Assert.AreEqual("phones", subTypes[2].Name);

            var phonesSubType = (UdtColumnInfo)((SetColumnInfo)subTypes[2].TypeInfo).KeyTypeInfo;

            Assert.AreEqual("tester.phone", phonesSubType.Name);
            Assert.AreEqual(2, phonesSubType.Fields.Count);
            Assert.AreEqual("alias", phonesSubType.Fields[0].Name);
            Assert.AreEqual("number", phonesSubType.Fields[1].Name);
        }
Example #10
0
        public void Parse_DataType_Name_Multiple_Test()
        {
            var dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.ListType(org.apache.cassandra.db.marshal.Int32Type)");

            Assert.AreEqual(ColumnTypeCode.List, dataType.TypeCode);
            Assert.IsInstanceOf <ListColumnInfo>(dataType.TypeInfo);
            Assert.AreEqual(ColumnTypeCode.Int, ((ListColumnInfo)dataType.TypeInfo).ValueTypeCode);

            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.SetType(org.apache.cassandra.db.marshal.UUIDType)");
            Assert.AreEqual(ColumnTypeCode.Set, dataType.TypeCode);
            Assert.IsInstanceOf <SetColumnInfo>(dataType.TypeInfo);
            Assert.AreEqual(ColumnTypeCode.Uuid, ((SetColumnInfo)dataType.TypeInfo).KeyTypeCode);

            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.SetType(org.apache.cassandra.db.marshal.TimeUUIDType)");
            Assert.AreEqual(ColumnTypeCode.Set, dataType.TypeCode);
            Assert.IsInstanceOf <SetColumnInfo>(dataType.TypeInfo);
            Assert.AreEqual(ColumnTypeCode.Timeuuid, ((SetColumnInfo)dataType.TypeInfo).KeyTypeCode);

            dataType = DataTypeParser.ParseFqTypeName("org.apache.cassandra.db.marshal.MapType(org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.LongType)");
            Assert.AreEqual(ColumnTypeCode.Map, dataType.TypeCode);
            Assert.IsInstanceOf <MapColumnInfo>(dataType.TypeInfo);
            Assert.AreEqual(ColumnTypeCode.Varchar, ((MapColumnInfo)dataType.TypeInfo).KeyTypeCode);
            Assert.AreEqual(ColumnTypeCode.Bigint, ((MapColumnInfo)dataType.TypeInfo).ValueTypeCode);
        }
Example #11
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;
                });
            }));
        }