Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 114 lines (88 sloc) 3.469 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
using System;
using System.Collections.Generic;
using System.Linq;
using Apache.Cassandra;
using FluentCassandra.Types;

namespace FluentCassandra
{
public class CassandraColumnFamilySchema
{
public static readonly AsciiType DefaultKeyName = "KEY";
public static readonly CassandraType DefaultKeyNameType = CassandraType.AsciiType;

private readonly CfDef _def;

public CassandraColumnFamilySchema(CfDef def)
{
_def = def;

KeyspaceName = def.Keyspace;

var familyType = ColumnType.Standard;
Enum.TryParse<ColumnType>(def.Column_type, out familyType);

var defaultKeyValueType = CassandraType.GetCassandraType(def.Key_validation_class);
var defaultColumnValueType = CassandraType.GetCassandraType(def.Default_validation_class);

CassandraType columnNameType, superColumnNameType;

if (familyType == ColumnType.Super)
{
superColumnNameType = CassandraType.GetCassandraType(def.Comparator_type);
columnNameType = CassandraType.GetCassandraType(def.Subcomparator_type);
}
else
{
superColumnNameType = null;
columnNameType = CassandraType.GetCassandraType(def.Comparator_type);
}

FamilyType = familyType;
FamilyName = def.Name;
FamilyDescription = def.Comment;

KeyName = CassandraObject.GetCassandraObjectFromDatabaseByteArray(def.Key_alias, DefaultKeyNameType);
KeyValueType = defaultKeyValueType;

SuperColumnNameType = superColumnNameType;
ColumnNameType = columnNameType;
DefaultColumnValueType = defaultColumnValueType;

Columns = def.Column_metadata.Select(col => new CassandraColumnSchema(col, columnNameType)).ToList();
}

public CassandraColumnFamilySchema(string name = null, ColumnType type = ColumnType.Standard)
{
FamilyType = type;
FamilyName = name;
FamilyDescription = null;

KeyName = DefaultKeyName;
KeyValueType = CassandraType.BytesType;

SuperColumnNameType = type == ColumnType.Super ? CassandraType.BytesType : null;
ColumnNameType = CassandraType.BytesType;
DefaultColumnValueType = CassandraType.BytesType;

Columns = new List<CassandraColumnSchema>();
}

internal string KeyspaceName { get; set; }

public ColumnType FamilyType { get; set; }
public string FamilyName { get; set; }
public string FamilyDescription { get; set; }

public CassandraObject KeyName { get; set; }
public CassandraType KeyValueType { get; set; }

public CassandraType SuperColumnNameType { get; set; }
public CassandraType ColumnNameType { get; set; }
public CassandraType DefaultColumnValueType { get; set; }

public IList<CassandraColumnSchema> Columns { get; set; }

public static implicit operator CfDef(CassandraColumnFamilySchema schema)
{
var def = new CfDef {
Keyspace = schema.KeyspaceName,
Name = schema.FamilyName,
Comment = schema.FamilyDescription,
Column_type = schema.FamilyType.ToString(),
Key_alias = schema.KeyName.ToBigEndian(),
Key_validation_class = schema.KeyValueType.DatabaseType,
Comparator_type = schema.ColumnNameType.DatabaseType,
Default_validation_class = schema.DefaultColumnValueType.DatabaseType
};

if (schema.FamilyType == ColumnType.Super)
{
def.Comparator_type = schema.SuperColumnNameType.DatabaseType;
def.Subcomparator_type = schema.ColumnNameType.DatabaseType;
}

return def;
}

public static implicit operator CassandraColumnFamilySchema(CfDef def)
{
return new CassandraColumnFamilySchema(def);
}
}
}
Something went wrong with that request. Please try again.