Permalink
Browse files

cleaning up some tests and getting example to work as part of some pr…

…e-poco-support cleanup
  • Loading branch information...
1 parent 5cb0086 commit f77d3d9df9b8c07043a5176371c3312062827d07 @nberardi nberardi committed May 13, 2010
@@ -6,6 +6,7 @@
using Thrift.Protocol;
using FluentCassandra.Configuration;
+using FluentCassandra.Types;
namespace FluentCassandra.Sandbox
{
@@ -15,26 +16,26 @@ private static void Main(string[] args)
{
using (var db = new CassandraContext("Blog", "localhost"))
{
+ // create post
+ dynamic post = new FluentSuperColumnFamily<AsciiType, AsciiType>(
+ key: "first-post",
+ columnFamily: "Posts"
+ );
+
// create post details
- dynamic postDetails = new FluentSuperColumn();
+ dynamic postDetails = post.CreateSuperColumn();
postDetails.Title = "My First Cassandra Post";
postDetails.Body = "Blah. Blah. Blah. about my first post on how great Cassandra is to work with.";
postDetails.Author = "Nick Berardi";
postDetails.PostedOn = DateTimeOffset.Now;
// create post tags
- dynamic tags = new FluentSuperColumn();
+ dynamic tags = post.CreateSuperColumn();
tags[0] = "Cassandra";
tags[1] = ".NET";
tags[2] = "Database";
tags[3] = "NoSQL";
- // create post
- dynamic post = new FluentSuperColumnFamily(
- key: "first-post",
- columnFamily: "Posts"
- );
-
// add properties to post
post.Post = postDetails;
post.Tags = tags;
@@ -48,13 +49,10 @@ private static void Main(string[] args)
db.SaveChanges();
// get the table/column family to pull back the post
- var postsTable = db.GetColumnFamily("Posts");
+ var postsTable = db.GetColumnFamily<AsciiType, AsciiType>("Posts");
// we want to pull back the post we just saved, and we want the Post and Tags columns that we added
- dynamic samePost = postsTable.GetSingle("first-post", new[] { "Post", "Tags" });
-
- // we need to hint at the type of non string fields since the data is all stored as binary
- samePost.Post.SetHint("PostedOn", typeof(DateTimeOffset));
+ dynamic samePost = postsTable.GetSingle("first-post", new AsciiType[] { "Post", "Tags" });
// display all the fields in the Post property
Console.WriteLine("display post");
@@ -66,22 +64,22 @@ private static void Main(string[] args)
foreach (var tag in samePost.Tags)
Console.WriteLine("{0}: {1}", tag.Name, samePost.Tags[tag.Name]);
+ dynamic comments = new FluentSuperColumnFamily<TimeUUIDType, AsciiType>(
+ key: "first-post",
+ columnFamily: "Comments"
+ );
+
// now lets add some comments
- dynamic comment = new FluentSuperColumn();
+ dynamic comment = comments.CreateSuperColumn();
comment.Author = "Nick Berardi";
comment.CommentedOn = DateTime.Now;
comment.Text = "Wow this is great.";
- dynamic comment2 = new FluentSuperColumn();
+ dynamic comment2 = comments.CreateSuperColumn();
comment2.Author = "Joe Somebody";
comment2.CommentedOn = DateTime.Now;
comment2.Text = "I agree with you Nick. -- Joe";
- dynamic comments = new FluentSuperColumnFamily(
- key: "first-post",
- columnFamily: "Comments"
- );
-
// the comments are stored by time based Guid under the same key
comments[GuidGenerator.GenerateTimeBasedGuid()] = comment;
comments[GuidGenerator.GenerateTimeBasedGuid()] = comment2;
@@ -48,6 +48,8 @@ public void Constructor_Test()
// assert
Assert.AreEqual(2, actual.Columns.Count);
+ Assert.AreSame(col1.Family, actual);
+ Assert.AreSame(col2.Family, actual);
}
[TestMethod]
@@ -48,6 +48,8 @@ public void Constructor_Test()
// assert
Assert.AreEqual(2, actual.Columns.Count);
+ Assert.AreSame(col1.Family, actual);
+ Assert.AreSame(col2.Family, actual);
}
[TestMethod]
@@ -67,6 +69,8 @@ public void Constructor_Dynamic_Test()
Assert.AreEqual(col1, actual["Test1"]);
Assert.AreEqual(col2, actual.Test2);
Assert.AreEqual(col2, actual["Test2"]);
+ Assert.AreSame(col1.Family, actual);
+ Assert.AreSame(col2.Family, actual);
}
[TestMethod]
@@ -51,7 +51,13 @@ public DateTimeOffset Timestamp
/// </summary>
public IFluentColumnFamily<CompareWith> Family
{
- get { return _family; }
+ get
+ {
+ if (_family == null && _parent != null)
+ _family = _parent.ColumnFamily as IFluentColumnFamily<CompareWith>;
+
+ return _family;
+ }
internal set
{
_family = value;
@@ -62,6 +62,15 @@ internal FluentColumnFamily(string key, string columnFamily, IEnumerable<IFluent
/// <summary>
///
/// </summary>
+ /// <returns></returns>
+ public IFluentColumn<CompareWith> CreateColumn()
+ {
+ return new FluentColumn<CompareWith>();
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
public override IList<IFluentColumn<CompareWith>> Columns
{
get { return _columns; }
@@ -47,7 +47,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
/// <returns></returns>
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
- string index0 = indexes[0].ToString();
+ object index0 = indexes[0];
return TryGetColumn(index0, out result);
}
@@ -79,7 +79,7 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
/// <returns></returns>
public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
{
- string index0 = indexes[0].ToString();
+ object index0 = indexes[0];
return TrySetColumn(index0, value);
}
@@ -125,9 +125,9 @@ protected void OnColumnMutated(MutationType type, IFluentBaseColumn column)
#region IFluentRecord Members
- IList<IFluentBaseColumn> IFluentRecord.Columns
+ IEnumerable<IFluentBaseColumn> IFluentRecord.Columns
{
- get { return (IList<IFluentBaseColumn>)Columns; }
+ get { return Columns.OfType<IFluentBaseColumn>(); }
}
public IFluentMutationTracker MutationTracker
@@ -45,6 +45,15 @@ internal FluentSuperColumn(IEnumerable<IFluentColumn<CompareSubcolumnWith>> colu
public CompareWith Name { get; set; }
/// <summary>
+ ///
+ /// </summary>
+ /// <returns></returns>
+ public IFluentColumn<CompareSubcolumnWith> CreateColumn()
+ {
+ return new FluentColumn<CompareSubcolumnWith>();
+ }
+
+ /// <summary>
/// The columns in the super column.
/// </summary>
public override IList<IFluentColumn<CompareSubcolumnWith>> Columns
@@ -57,7 +66,13 @@ public override IList<IFluentColumn<CompareSubcolumnWith>> Columns
/// </summary>
public IFluentSuperColumnFamily<CompareWith, CompareSubcolumnWith> Family
{
- get { return _family; }
+ get
+ {
+ if (_family == null && _parent != null)
+ _family = _parent.ColumnFamily as IFluentSuperColumnFamily<CompareWith, CompareSubcolumnWith>;
+
+ return _family;
+ }
internal set
{
_family = value;
@@ -130,9 +145,9 @@ public override bool TrySetColumn(object name, object value)
return true;
}
- #region IFluentSuperColumn Members
+ #region IFluentSuperColumn Members
- IList<IFluentColumn> IFluentSuperColumn.Columns { get { return (IList<IFluentColumn>)Columns; } }
+ IEnumerable<IFluentColumn> IFluentSuperColumn.Columns { get { return _columns.OfType<IFluentColumn>(); } }
#endregion
@@ -151,8 +166,10 @@ private void UpdateParent(FluentColumnParent parent)
{
_parent = parent;
_columns.Parent = parent;
+
+ var columnParent = new FluentColumnParent(Family, this);
foreach (var col in Columns)
- ((IFluentBaseColumn)col).SetParent(parent);
+ col.SetParent(columnParent);
}
#endregion
@@ -64,6 +64,15 @@ internal FluentSuperColumnFamily(string key, string columnFamily, IEnumerable<IF
/// <summary>
///
/// </summary>
+ /// <returns></returns>
+ public IFluentSuperColumn<CompareWith, CompareSubcolumnWith> CreateSuperColumn()
+ {
+ return new FluentSuperColumn<CompareWith, CompareSubcolumnWith>();
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
public override IList<IFluentSuperColumn<CompareWith, CompareSubcolumnWith>> Columns
{
get { return _columns; }
@@ -7,7 +7,7 @@ namespace FluentCassandra
{
public interface IFluentRecord
{
- IList<IFluentBaseColumn> Columns { get; }
+ IEnumerable<IFluentBaseColumn> Columns { get; }
IFluentMutationTracker MutationTracker { get; }
}
}
@@ -7,6 +7,6 @@ namespace FluentCassandra
{
public interface IFluentSuperColumn : IFluentBaseColumn, IFluentRecord
{
- new IList<IFluentColumn> Columns { get; }
+ new IEnumerable<IFluentColumn> Columns { get; }
}
}
@@ -147,10 +147,10 @@ public static ColumnOrSuperColumn CreateColumnOrSuperColumn(IFluentBaseColumn co
var colY = (IFluentSuperColumn)column;
var superColumn = new SuperColumn {
Name = colY.Name,
- Columns = new List<Column>(colY.Columns.Count)
+ Columns = new List<Column>()
};
- foreach (var col in colY.Columns)
+ foreach (var col in colY.Columns.OfType<IFluentColumn>())
superColumn.Columns.Add(CreateColumn(col));
return new ColumnOrSuperColumn {
@@ -27,7 +27,7 @@ public override CassandraType SetValue(object obj)
var converter = Converter;
if (!converter.CanConvertFrom(obj.GetType()))
- throw new InvalidCastException(GetType() + " cannot be cast to " + TypeCode);
+ throw new InvalidCastException(obj.GetType() + " cannot be cast to " + TypeCode);
_value = (string)converter.ConvertFrom(obj);
@@ -10,23 +10,26 @@ internal class AsciiTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
- return sourceType == typeof(byte[]) || sourceType == typeof(string);
+ if (Type.GetTypeCode(sourceType) != TypeCode.Object)
+ return true;
+
+ return sourceType == typeof(byte[]);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
- return destinationType == typeof(byte[]) || destinationType == typeof(string);
+ if (Type.GetTypeCode(destinationType) != TypeCode.Object)
+ return true;
+
+ return destinationType == typeof(byte[]);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is byte[])
return Encoding.ASCII.GetString((byte[])value);
- if (value is string)
- return (string)value;
-
- return null;
+ return Convert.ChangeType(value, typeof(string));
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
@@ -37,10 +40,7 @@ public override object ConvertTo(ITypeDescriptorContext context, System.Globaliz
if (destinationType == typeof(byte[]))
return Encoding.ASCII.GetBytes((string)value);
- if (destinationType == typeof(string))
- return (string)value;
-
- return null;
+ return Convert.ChangeType(value, destinationType);
}
}
}
@@ -27,7 +27,7 @@ public override CassandraType SetValue(object obj)
var converter = Converter;
if (!converter.CanConvertFrom(obj.GetType()))
- throw new InvalidCastException(GetType() + " cannot be cast to " + TypeCode);
+ throw new InvalidCastException(obj.GetType() + " cannot be cast to " + TypeCode);
_value = (byte[])converter.ConvertFrom(obj);
@@ -27,7 +27,7 @@ public override CassandraType SetValue(object obj)
var converter = Converter;
if (!converter.CanConvertFrom(obj.GetType()))
- throw new InvalidCastException(GetType() + " cannot be cast to " + TypeCode);
+ throw new InvalidCastException(obj.GetType() + " cannot be cast to " + TypeCode);
_value = (Guid)converter.ConvertFrom(obj);
@@ -27,7 +27,7 @@ public override CassandraType SetValue(object obj)
var converter = Converter;
if (!converter.CanConvertFrom(obj.GetType()))
- throw new InvalidCastException(GetType() + " cannot be cast to " + TypeCode);
+ throw new InvalidCastException(obj.GetType() + " cannot be cast to " + TypeCode);
_value = (long)converter.ConvertFrom(obj);
@@ -27,7 +27,7 @@ public override CassandraType SetValue(object obj)
var converter = Converter;
if (!converter.CanConvertFrom(obj.GetType()))
- throw new InvalidCastException(GetType() + " cannot be cast to " + TypeCode);
+ throw new InvalidCastException(obj.GetType() + " cannot be cast to " + TypeCode);
_value = (Guid)converter.ConvertFrom(obj);
@@ -27,7 +27,7 @@ public override CassandraType SetValue(object obj)
var converter = Converter;
if (!converter.CanConvertFrom(obj.GetType()))
- throw new InvalidCastException(GetType() + " cannot be cast to " + TypeCode);
+ throw new InvalidCastException(obj.GetType() + " cannot be cast to " + TypeCode);
_value = (string)converter.ConvertFrom(obj);
Oops, something went wrong.

0 comments on commit f77d3d9

Please sign in to comment.