Permalink
Browse files

some much needed refactoring of the schema

  • Loading branch information...
1 parent 2ba33bc commit 4526a65b9864de37481145d4071b17b9e2a075cf @nberardi nberardi committed Feb 7, 2012
@@ -70,6 +70,14 @@ public bool IsPartOfFamily(IFluentBaseColumnFamily family)
/// </summary>
public abstract void ClearCachedColumnFamilySchema();
+ /// <summary>
+ ///
+ /// </summary>
+ public void TryCreateSelf()
+ {
+ Context.Keyspace.TryCreateColumnFamily(GetSchema());
+ }
+
/// <summary>
/// Removes all the rows from the given column family.
/// </summary>
@@ -35,25 +35,12 @@ public FluentColumnFamily CreateRecord(CassandraType key)
public override CassandraColumnFamilySchema GetSchema()
{
- var def = Context.Keyspace.GetColumnFamilyDescription(FamilyName);
-
- if (def == null && _cachedSchema == null)
- _cachedSchema = new CassandraColumnFamilySchema();
+ var schema = Context.Keyspace.GetColumnFamilySchema(FamilyName);
if (_cachedSchema == null)
- {
- _cachedSchema = new CassandraColumnFamilySchema();
-
- var keyType = CassandraType.GetCassandraType(def.Key_validation_class);
- var colNameType = CassandraType.GetCassandraType(def.Default_validation_class);
-
- _cachedSchema.FamilyName = FamilyName;
- _cachedSchema.KeyType = keyType;
- _cachedSchema.Columns = def.Column_metadata.Select(col => new CassandraColumnSchema {
- Name = CassandraType.GetTypeFromDatabaseValue(col.Name, colNameType),
- ValueType = CassandraType.GetCassandraType(col.Validation_class)
- }).ToList();
- }
+ _cachedSchema = (schema == null)
+ ? new CassandraColumnFamilySchema(FamilyName, ColumnType.Standard)
+ : schema;
return _cachedSchema;
}
@@ -1,31 +1,72 @@
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 = CassandraType.GetTypeFromDatabaseValue<AsciiType>(new byte[] { 75, 69, 89 });
+ public static readonly AsciiType DefaultKeyName = "KEY";
- public CassandraColumnFamilySchema()
+ public CassandraColumnFamilySchema(CfDef def)
{
- KeyName = DefaultKeyName;
+ var familyType = ColumnType.Standard;
+ Enum.TryParse<ColumnType>(def.Column_type, out familyType);
+
+ var keyType = CassandraType.GetCassandraType(def.Key_validation_class);
+ var defaultColumnValueType = CassandraType.GetCassandraType(def.Default_validation_class);
+ Type 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 = CassandraType.GetTypeFromDatabaseValue<BytesType>(def.Key_alias);
+ KeyType = keyType;
+ 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;
KeyType = typeof(BytesType);
- SuperColumnNameType = typeof(BytesType);
+ SuperColumnNameType = type == ColumnType.Super ? typeof(BytesType) : null;
ColumnNameType = typeof(BytesType);
+ DefaultColumnValueType = typeof(BytesType);
Columns = new List<CassandraColumnSchema>();
}
+ public ColumnType FamilyType { get; set; }
public string FamilyName { get; set; }
- public CassandraType KeyName { get; set; }
+ public string FamilyDescription { get; set; }
+ public CassandraType KeyName { get; set; }
public Type KeyType { get; set; }
public Type SuperColumnNameType { get; set; }
public Type ColumnNameType { get; set; }
+ public Type DefaultColumnValueType { get; set; }
public IList<CassandraColumnSchema> Columns { get; set; }
}
@@ -1,15 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using FluentCassandra.Types;
-
-namespace FluentCassandra
-{
- public class CassandraColumnPathSchema
- {
- public string ColumnFamily { get; set; }
- public CassandraType SuperColumnNameType { get; set; }
- public CassandraType ColumnNameType { get; set; }
- }
-}
@@ -1,4 +1,5 @@
using System;
+using Apache.Cassandra;
using FluentCassandra.Types;
namespace FluentCassandra
@@ -11,33 +12,25 @@ public CassandraColumnSchema()
ValueType = typeof(BytesType);
}
- private Type _nameType;
+ public CassandraColumnSchema(ColumnDef def, Type columnNameType)
+ {
+ NameType = columnNameType;
+ Name = def.Name;
+ ValueType = CassandraType.GetCassandraType(def.Validation_class);
+ }
+
private CassandraType _name;
public CassandraType Name
{
get { return _name; }
set
{
- if (value != null)
- NameType = value.GetType();
-
- _name = value;
- }
- }
-
- public Type NameType
- {
- get
- {
- if (_nameType == null && Name != null)
- _nameType = Name.GetType();
-
- return _nameType;
+ _name = (CassandraType)value.GetValue(NameType);
}
- set { _nameType = value; }
}
- public virtual Type ValueType { get; set; }
+ public Type NameType { get; set; }
+ public Type ValueType { get; set; }
}
}
View
@@ -171,7 +171,7 @@ public IEnumerable<CassandraKeyspace> DescribeKeyspaces()
{
return ExecuteOperation(new SimpleOperation<IEnumerable<CassandraKeyspace>>(ctx => {
IEnumerable<KsDef> keyspaces = ctx.Session.GetClient(setKeyspace: false).describe_keyspaces();
- return keyspaces.Select(keyspace => new CassandraKeyspace(keyspace, this));
+ return keyspaces.Select(keyspace => new CassandraKeyspace(new CassandraKeyspaceSchema(keyspace), this));
}));
}
View
@@ -12,34 +12,22 @@ namespace FluentCassandra
public class CassandraKeyspace
{
private readonly string _keyspaceName;
- private KsDef _cachedKeyspaceDescription;
+ private CassandraKeyspaceSchema _cachedSchema;
private CassandraContext _context;
- /// <summary>
- ///
- /// </summary>
- /// <param name="definition"></param>
- public CassandraKeyspace(KsDef definition, CassandraContext context)
- {
- if (definition == null)
- throw new ArgumentNullException("definition");
-
- _keyspaceName = definition.Name;
- _cachedKeyspaceDescription = definition;
- _context = context;
- }
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="keyspaceName"></param>
- /// <param name="connecton"></param>
public CassandraKeyspace(string keyspaceName, CassandraContext context)
+ : this(new CassandraKeyspaceSchema { Name = keyspaceName }, context) { }
+
+ public CassandraKeyspace(CassandraKeyspaceSchema schema, CassandraContext context)
{
- if (keyspaceName == null)
- throw new ArgumentNullException("keyspaceName");
+ if (schema == null)
+ throw new ArgumentNullException("schema");
- _keyspaceName = keyspaceName;
+ if (schema.Name == null)
+ throw new ArgumentException("Must specify the keyspace name.");
+
+ _keyspaceName = schema.Name;
+ _cachedSchema = schema;
_context = context;
}
@@ -70,55 +58,63 @@ public void TryCreateSelf()
string result = _context.AddKeyspace(new KsDef {
Name = KeyspaceName,
- Strategy_class = "org.apache.cassandra.locator.SimpleStrategy",
+ Strategy_class = "org.apache.cassandra.locator.NetworkTopologyStrategy",
Replication_factor = 1,
Cf_defs = new List<CfDef>(0)
});
Debug.WriteLine(result, "keyspace setup");
}
- public void TryCreateColumnFamily<CompareWith>(string columnFamilyName)
- where CompareWith : CassandraType
+ public void TryCreateColumnFamily(CassandraColumnFamilySchema schema)
{
try
{
- var comparatorType = GetCassandraComparatorType(typeof(CompareWith));
-
- string result = _context.AddColumnFamily(new CfDef {
- Name = columnFamilyName,
+ var def = new CfDef {
Keyspace = KeyspaceName,
- Comparator_type = comparatorType
- });
- Debug.WriteLine(result, "keyspace setup");
+ Name = schema.FamilyName,
+ Comment = schema.FamilyDescription,
+ Column_type = schema.FamilyType.ToString(),
+ Key_alias = schema.KeyName.ToBigEndian(),
+ Key_validation_class = GetCassandraComparatorType(schema.KeyType),
+ Comparator_type = GetCassandraComparatorType(schema.ColumnNameType),
+ Default_validation_class = GetCassandraComparatorType(schema.DefaultColumnValueType)
+ };
+
+ if (schema.FamilyType == ColumnType.Super)
+ {
+ def.Comparator_type = GetCassandraComparatorType(schema.SuperColumnNameType);
+ def.Subcomparator_type = GetCassandraComparatorType(schema.ColumnNameType);
+ }
+
+ string result = _context.AddColumnFamily(def);
+ Debug.WriteLine(result, "column family setup");
}
catch
{
- Debug.WriteLine(columnFamilyName + " already exists", "keyspace setup");
+ Debug.WriteLine(schema.FamilyName + " already exists", "column family setup");
}
}
+ [Obsolete("Use \"TryCreateColumnFamily\" class with out generic type")]
+ public void TryCreateColumnFamily<CompareWith>(string columnFamilyName)
+ where CompareWith : CassandraType
+ {
+ TryCreateColumnFamily(new CassandraColumnFamilySchema {
+ ColumnNameType = typeof(CompareWith),
+ FamilyName = columnFamilyName
+ });
+ }
+
+ [Obsolete("Use \"TryCreateColumnFamily\" class with out generic type")]
public void TryCreateColumnFamily<CompareWith, CompareSubcolumnWith>(string columnFamilyName)
where CompareWith : CassandraType
where CompareSubcolumnWith : CassandraType
{
- try
- {
- var comparatorType = GetCassandraComparatorType(typeof(CompareWith));
- var subComparatorType = GetCassandraComparatorType(typeof(CompareSubcolumnWith));
-
- string result = _context.AddColumnFamily(new CfDef {
- Name = columnFamilyName,
- Keyspace = KeyspaceName,
- Column_type = "Super",
- Comparator_type = comparatorType,
- Subcomparator_type = subComparatorType
- });
- Debug.WriteLine(result, "keyspace setup");
- }
- catch
- {
- Debug.WriteLine(columnFamilyName + " already exists", "keyspace setup");
- }
+ TryCreateColumnFamily(new CassandraColumnFamilySchema(type: ColumnType.Super) {
+ ColumnNameType = typeof(CompareWith),
+ SuperColumnNameType = typeof(CompareSubcolumnWith),
+ FamilyName = columnFamilyName
+ });
}
private string GetCassandraComparatorType(Type comparatorType)
@@ -142,31 +138,38 @@ private string GetCassandraComparatorType(Type comparatorType)
return comparatorTypeName;
}
- public CfDef GetColumnFamilyDescription(string columnFamily)
+ public KsDef GetDescription()
{
- return Describe().Cf_defs.FirstOrDefault(cf => cf.Name == columnFamily);
+ return _context.ExecuteOperation(new SimpleOperation<Apache.Cassandra.KsDef>(ctx => {
+ return ctx.Session.GetClient().describe_keyspace(KeyspaceName);
+ }));
+ }
+
+ public CassandraColumnFamilySchema GetColumnFamilySchema(string columnFamily)
+ {
+ return GetSchema().ColumnFamilies.FirstOrDefault(cf => cf.FamilyName == columnFamily);
}
public bool ColumnFamilyExists(string columnFamilyName)
{
- return Describe().Cf_defs.Any(columnFamily => columnFamily.Name == columnFamilyName);
+ return GetSchema().ColumnFamilies.Any(cf => cf.FamilyName == columnFamilyName);
}
public void ClearCachedKeyspaceDescription()
{
- _cachedKeyspaceDescription = null;
+ _cachedSchema = null;
}
#region Cassandra Keyspace Server Operations
- public KsDef Describe()
+ public CassandraKeyspaceSchema GetSchema()
{
- if (_cachedKeyspaceDescription == null)
- _cachedKeyspaceDescription = _context.ExecuteOperation(new SimpleOperation<Apache.Cassandra.KsDef>(ctx => {
+ if (_cachedSchema == null)
+ _cachedSchema = new CassandraKeyspaceSchema(_context.ExecuteOperation(new SimpleOperation<Apache.Cassandra.KsDef>(ctx => {
return ctx.Session.GetClient().describe_keyspace(KeyspaceName);
- }));
+ })));
- return _cachedKeyspaceDescription;
+ return _cachedSchema;
}
public IEnumerable<CassandraTokenRange> DescribeRing()
Oops, something went wrong.

0 comments on commit 4526a65

Please sign in to comment.