Permalink
Browse files

fixed issues discovered while running sandbox

  • Loading branch information...
1 parent 0592960 commit fc725a6f669e0ccdcba9314cc6d785653828a224 @nberardi nberardi committed Feb 11, 2012
@@ -15,7 +15,7 @@ public CassandraColumnSchema()
public CassandraColumnSchema(ColumnDef def, CassandraType columnNameType)
{
NameType = columnNameType;
- Name = def.Name;
+ Name = CassandraObject.GetTypeFromDatabaseValue(def.Name, columnNameType);
ValueType = CassandraObject.ParseType(def.Validation_class);
}
View
@@ -69,9 +69,9 @@ public CassandraColumnFamily GetColumnFamily(string columnFamily)
/// </summary>
/// <typeparam name="CompareWith"></typeparam>
/// <returns></returns>
- public CassandraColumnFamily GetSuperColumnFamily(string columnFamily)
+ public CassandraSuperColumnFamily GetSuperColumnFamily(string columnFamily)
{
- return new CassandraColumnFamily(this, columnFamily);
+ return new CassandraSuperColumnFamily(this, columnFamily);
}
/// <summary>
@@ -133,7 +133,7 @@ public string DropKeyspace(string keyspace)
public string AddColumnFamily(CfDef definition)
{
- Keyspace.ClearCachedKeyspaceDescription();
+ Keyspace.ClearCachedKeyspaceSchema();
return ExecuteOperation(new SimpleOperation<string>(ctx => {
return ctx.Session.GetClient().system_add_column_family(definition);
@@ -142,7 +142,7 @@ public string AddColumnFamily(CfDef definition)
public string UpdateColumnFamily(CfDef definition)
{
- Keyspace.ClearCachedKeyspaceDescription();
+ Keyspace.ClearCachedKeyspaceSchema();
return ExecuteOperation(new SimpleOperation<string>(ctx => {
return ctx.Session.GetClient().system_update_column_family(definition);
@@ -151,7 +151,7 @@ public string UpdateColumnFamily(CfDef definition)
public string DropColumnFamily(string columnFamily)
{
- Keyspace.ClearCachedKeyspaceDescription();
+ Keyspace.ClearCachedKeyspaceSchema();
return ExecuteOperation(new SimpleOperation<string>(ctx => {
return ctx.Session.GetClient().system_drop_column_family(columnFamily);
View
@@ -15,7 +15,13 @@ public class CassandraKeyspace
private CassandraContext _context;
public CassandraKeyspace(string keyspaceName, CassandraContext context)
- : this(new CassandraKeyspaceSchema { Name = keyspaceName }, context) { }
+ {
+ if (keyspaceName == null)
+ throw new ArgumentNullException("keyspaceName");
+
+ _keyspaceName = keyspaceName;
+ _context = context;
+ }
public CassandraKeyspace(CassandraKeyspaceSchema schema, CassandraContext context)
{
@@ -136,7 +142,7 @@ public bool ColumnFamilyExists(string columnFamilyName)
return GetSchema().ColumnFamilies.Any(cf => cf.FamilyName == columnFamilyName);
}
- public void ClearCachedKeyspaceDescription()
+ public void ClearCachedKeyspaceSchema()
{
_cachedSchema = null;
}
@@ -146,9 +152,18 @@ public void ClearCachedKeyspaceDescription()
public CassandraKeyspaceSchema GetSchema()
{
if (_cachedSchema == null)
- _cachedSchema = new CassandraKeyspaceSchema(_context.ExecuteOperation(new SimpleOperation<Apache.Cassandra.KsDef>(ctx => {
- return ctx.Session.GetClient().describe_keyspace(KeyspaceName);
- })));
+ try
+ {
+ _cachedSchema = new CassandraKeyspaceSchema(_context.ExecuteOperation(new SimpleOperation<Apache.Cassandra.KsDef>(ctx => {
+ return ctx.Session.GetClient().describe_keyspace(KeyspaceName);
+ })));
+ }
+ catch (CassandraOperationException)
+ {
+ _cachedSchema = new CassandraKeyspaceSchema {
+ Name = KeyspaceName
+ };
+ }
return _cachedSchema;
}
View
@@ -45,7 +45,7 @@ public CassandraObject ColumnName
get { return _name; }
set
{
- _name = (CassandraObject)value.GetValue(GetSchema().NameType);
+ _name = value.GetValue(GetSchema().NameType);
ColumnTimestamp = DateTimeOffset.UtcNow;
}
}
@@ -58,7 +58,7 @@ public CassandraObject ColumnValue
get { return _value; }
set
{
- _value = (CassandraObject)value.GetValue(GetSchema().ValueType);
+ _value = value.GetValue(GetSchema().ValueType);
ColumnTimestamp = DateTimeOffset.UtcNow;
}
}
@@ -151,10 +151,10 @@ public void SetSchema(CassandraColumnSchema schema)
schema = new CassandraColumnSchema();
if (_name != null)
- _name = (CassandraObject)_name.GetValue(schema.NameType);
+ _name = _name.GetValue(schema.NameType);
if (_value != null)
- _value = (CassandraObject)_value.GetValue(schema.ValueType);
+ _value = _value.GetValue(schema.ValueType);
_schema = schema;
}
@@ -61,10 +61,7 @@ internal FluentColumnFamily(CassandraObject key, string columnFamily, CassandraC
public CassandraObject Key
{
get { return _key; }
- set
- {
- _key = (CassandraObject)value.GetValue(GetSchema().KeyType);
- }
+ set { _key = value.GetValue(GetSchema().KeyType); }
}
/// <summary>
@@ -224,13 +221,13 @@ public override bool TryGetColumn(object name, out object result)
/// <returns></returns>
public override bool TrySetColumn(object name, object value)
{
+ var schema = GetColumnSchema(name);
var col = Columns.FirstOrDefault(c => c.ColumnName == name);
var mutationType = MutationType.Changed;
// if column doesn't exisit create it and add it to the columns
if (col == null)
{
- var schema = GetColumnSchema(name);
mutationType = MutationType.Added;
col = new FluentColumn(schema);
@@ -242,7 +239,7 @@ public override bool TrySetColumn(object name, object value)
}
// set the column value
- col.ColumnValue = CassandraObject.GetTypeFromObject(value);
+ col.ColumnValue = CassandraObject.GetTypeFromObject(value, schema.ValueType);
// notify the tracker that the column has changed
OnColumnMutated(mutationType, col);
@@ -27,12 +27,14 @@ public FluentColumnList(FluentColumnParent parent)
public FluentColumnList(FluentColumnParent parent, IEnumerable<T> columns)
{
Parent = parent;
+ Columns = new List<T>();
// make sure all columns have the same parent
foreach (var col in columns)
- col.SetParent(Parent);
-
- Columns = columns.ToList();
+ {
+ col.SetParent(parent);
+ Columns.Add(col);
+ }
SupressChangeNotification = false;
}
View
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
-using System.Linq;
-using System.Dynamic;
using System.ComponentModel;
-using FluentCassandra.Types;
+using System.Dynamic;
+using System.Linq;
namespace FluentCassandra
{
@@ -111,7 +110,7 @@ public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object
public IEnumerator<IFluentBaseColumn> GetEnumerator()
{
- return ((IFluentRecord)Columns).GetEnumerator();
+ return Columns.Cast<IFluentBaseColumn>().GetEnumerator();
}
#endregion
View
@@ -24,6 +24,8 @@ public FluentSuperColumn()
public class FluentSuperColumn : FluentRecord<FluentColumn>, IFluentBaseColumn, IFluentRecordExpression
{
private CassandraObject _name;
+ private FluentColumnParent _parent;
+ private FluentSuperColumnFamily _family;
private FluentColumnList<FluentColumn> _columns;
private CassandraColumnSchema _schema;
@@ -54,10 +56,7 @@ internal FluentSuperColumn(CassandraColumnSchema schema, IEnumerable<FluentColum
public CassandraObject ColumnName
{
get { return _name; }
- set
- {
- _name = (CassandraObject)value.GetValue(GetSchema().NameType);
- }
+ set { _name = value.GetValue(GetSchema().NameType); }
}
/// <summary>
@@ -93,8 +92,18 @@ public override IList<FluentColumn> Columns
/// </summary>
public FluentSuperColumnFamily Family
{
- get;
- internal set;
+ get
+ {
+ if (_family == null && _parent != null)
+ _family = _parent.ColumnFamily as FluentSuperColumnFamily;
+
+ return _family;
+ }
+ internal set
+ {
+ _family = value;
+ UpdateParent(GetParent());
+ }
}
/// <summary>
@@ -153,7 +162,7 @@ public FluentColumnPath GetPath()
/// <returns></returns>
public FluentColumnParent GetParent()
{
- return new FluentColumnParent(Family, null);
+ return _parent ?? new FluentColumnParent(Family, this);
}
/// <summary>
@@ -170,7 +179,7 @@ private CassandraObject GetColumnValue(object name)
return NullType.Value;
var schema = GetColumnSchema(name);
- return (CassandraObject)col.ColumnValue.GetValue(schema.ValueType);
+ return col.ColumnValue.GetValue(schema.ValueType);
}
/// <summary>
@@ -181,9 +190,13 @@ private CassandraObject GetColumnValue(object name)
private CassandraColumnSchema GetColumnSchema(object name)
{
var schema = GetSchema();
+ var valueType = CassandraType.BytesType;
+
+ if (Family != null)
+ valueType = Family.GetSchema().DefaultColumnValueType;
// mock up a fake schema to send to the fluent column
- return new CassandraColumnSchema { NameType = schema.ValueType, ValueType = typeof(BytesType) };
+ return new CassandraColumnSchema { NameType = schema.ValueType, ValueType = valueType };
}
/// <summary>
@@ -233,18 +246,17 @@ public override bool TrySetColumn(object name, object value)
return true;
}
- #region IFluentBaseColumn Members
-
- IFluentBaseColumnFamily IFluentBaseColumn.Family { get { return Family; } }
-
- void IFluentBaseColumn.SetParent(FluentColumnParent parent)
+ public void SetParent(FluentColumnParent parent)
{
UpdateParent(parent);
}
private void UpdateParent(FluentColumnParent parent)
{
- Family = parent.ColumnFamily as FluentSuperColumnFamily;
+ if (!(parent.ColumnFamily is FluentSuperColumnFamily))
+ throw new ArgumentException("ColumnFamily must be of type FluentSuperColumnFamily.", "parent");
+
+ _parent = parent;
var columnParent = GetPath();
_columns.Parent = columnParent;
@@ -255,6 +267,10 @@ private void UpdateParent(FluentColumnParent parent)
ResetMutationAndAddAllColumns();
}
+ #region IFluentBaseColumn Members
+
+ IFluentBaseColumnFamily IFluentBaseColumn.Family { get { return Family; } }
+
#endregion
}
}
@@ -63,10 +63,7 @@ internal FluentSuperColumnFamily(CassandraObject key, string columnFamily, Cassa
public CassandraObject Key
{
get { return _key; }
- set
- {
- _key = (CassandraObject)value.GetValue(GetSchema().KeyType);
- }
+ set { _key = value.GetValue(GetSchema().KeyType); }
}
/// <summary>
@@ -85,7 +82,10 @@ public CassandraObject Key
/// <returns></returns>
public FluentSuperColumn CreateSuperColumn()
{
- return new FluentSuperColumn(GetColumnSchema(""));
+ var col = new FluentSuperColumn(GetColumnSchema(null));
+ col.SetParent(GetSelf());
+
+ return col;
}
/// <summary>
@@ -94,9 +94,12 @@ public FluentSuperColumn CreateSuperColumn()
/// <returns></returns>
public FluentSuperColumn CreateSuperColumn(CassandraObject name)
{
- return new FluentSuperColumn(GetColumnSchema(name)) {
+ var col = new FluentSuperColumn(GetColumnSchema(name)) {
ColumnName = name
};
+ col.SetParent(GetSelf());
+
+ return col;
}
/// <summary>
@@ -170,10 +173,15 @@ private CassandraColumnSchema GetColumnSchema(object name)
var schema = GetSchema();
// mock up a fake schema to send to the fluent column
- return new CassandraColumnSchema {
- Name = CassandraObject.GetTypeFromObject(name, schema.ColumnNameType),
- NameType = schema.SuperColumnNameType,
- ValueType = schema.ColumnNameType };
+ var colSchema = new CassandraColumnSchema {
+ NameType = schema.SuperColumnNameType,
+ ValueType = schema.DefaultColumnValueType
+ };
+
+ if (name != null)
+ colSchema.Name = CassandraObject.GetTypeFromObject(name, schema.SuperColumnNameType);
+
+ return colSchema;
}
/// <summary>
@@ -201,11 +209,12 @@ public override bool TrySetColumn(object name, object value)
if (!(value is FluentSuperColumn))
throw new ArgumentException("Value must be of type " + typeof(FluentSuperColumn) + ", because this column family is of type Super.", "value");
+ var schema = GetSchema();
var col = GetColumnValue(name);
var mutationType = col == null ? MutationType.Added : MutationType.Changed;
col = (FluentSuperColumn)value;
- col.ColumnName = CassandraObject.GetTypeFromObject(name, GetSchema().ColumnNameType);
+ col.ColumnName = CassandraObject.GetTypeFromObject(name, schema.SuperColumnNameType);
int index = Columns.IndexOf(col);
@@ -126,10 +126,11 @@ IQueryable IQueryProvider.CreateQuery(Expression expression)
/// <returns></returns>
TResult IQueryProvider.Execute<TResult>(Expression expression)
{
- if (!typeof(TResult).IsAssignableFrom(typeof(IFluentBaseColumnFamily)))
+ if (!typeof(TResult).IsAssignableFrom(typeof(ICqlRow)))
throw new CassandraException("'TElement' must inherit from IFluentBaseColumnFamily");
- return (TResult)new CqlQuery(expression, this).FirstOrDefault();
+ var result = new CqlQuery(expression, this);
+ return (TResult)Enumerable.FirstOrDefault(result);
}
/// <summary>
Oops, something went wrong.

0 comments on commit fc725a6

Please sign in to comment.