Permalink
Browse files

have a new outlook on how columns should be structured, to support th…

…e main Cassandra types, namely Ascii, UTF8, tyes, LexicalUUID, TimeUUID, and Long
  • Loading branch information...
nberardi committed Apr 27, 2010
1 parent 8f81f56 commit a47e07dfa56390d8188175f94200a5fbf6fc5838
@@ -15,45 +15,81 @@ private static void Main(string[] args)
{
using (var db = new CassandraContext("Blog", "localhost"))
{
+ // create post details
dynamic postDetails = new FluentSuperColumn();
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();
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;
+ // attach the post to the database
Console.WriteLine("attaching record");
db.Attach(post);
+ // save the changes
Console.WriteLine("saving changes");
db.SaveChanges();
+ // get the table/column family to pull back the post
var postsTable = db.GetColumnFamily("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));
+ // display all the fields in the Post property
Console.WriteLine("display post");
foreach (var col in samePost.Post)
Console.WriteLine("{0}: {1}", col.Name, samePost.Post[col.Name]);
+ // display all the fields in the Tags property
Console.WriteLine("display tags");
foreach (var tag in samePost.Tags)
Console.WriteLine("{0}: {1}", tag.Name, samePost.Tags[tag.Name]);
+
+ // now lets add some comments
+ dynamic comment = new FluentSuperColumn();
+ comment.Author = "Nick Berardi";
+ comment.CommentedOn = DateTime.Now;
+ comment.Text = "Wow this is great.";
+
+ dynamic comment2 = new FluentSuperColumn();
+ 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;
+
+ db.Attach(comment);
+ db.Attach(comment2);
+
+ db.SaveChanges();
}
Console.WriteLine("Done");
@@ -8,31 +8,6 @@ namespace FluentCassandra
{
internal static class BinaryHelper
{
- public static T GetObject<T>(this byte[] bytes)
- {
- return (T)GetObject(bytes, typeof(T));
- }
-
- public static object GetObject(this byte[] bytes, Type type)
- {
- var converter = new BinaryConverter();
-
- if (!converter.CanConvertTo(type))
- throw new NotSupportedException(type + " is not supported for binary serialization.");
-
- return converter.ConvertTo(bytes, type);
- }
-
- public static byte[] GetBytes(this object obj)
- {
- var converter = new BinaryConverter();
-
- if (!converter.CanConvertFrom(obj.GetType()))
- throw new NotSupportedException(obj.GetType() + " is not supported for binary serialization.");
-
- return (byte[])converter.ConvertFrom(obj);
- }
-
public static byte[] GetNameBytes(this IFluentColumn column)
{
if (column.IsSuperColumn())
@@ -11,6 +11,7 @@ namespace FluentCassandra
/// <seealso href="http://wiki.apache.org/cassandra/API"/>
public class CassandraColumnFamily
{
+ private CassandraContext _context;
private CassandraKeyspace _keyspace;
private IConnection _connection;
@@ -19,8 +20,9 @@ public class CassandraColumnFamily
/// </summary>
/// <param name="keyspace"></param>
/// <param name="connection"></param>
- public CassandraColumnFamily(CassandraKeyspace keyspace, IConnection connection, string columnFamily)
+ public CassandraColumnFamily(CassandraContext context, CassandraKeyspace keyspace, IConnection connection, string columnFamily)
{
+ _context = context;
_keyspace = keyspace;
_connection = connection;
FamilyName = columnFamily;
@@ -442,7 +444,8 @@ protected IFluentColumnFamily GetSingle(string key, string superColumnName, Slic
);
var record = ObjectHelper.ConvertToFluentColumnFamily(key, FamilyName, superColumnName, output);
-
+ _context.Attach(record);
+ record.MutationTracker.Clear();
return record;
}
@@ -546,7 +549,12 @@ protected IEnumerable<IFluentColumnFamily> Get(List<string> keys, string superCo
);
foreach (var record in output)
- yield return ObjectHelper.ConvertToFluentColumnFamily(record.Key, FamilyName, superColumnName, record.Value);
+ {
+ var family = ObjectHelper.ConvertToFluentColumnFamily(record.Key, FamilyName, superColumnName, record.Value);
+ _context.Attach(family);
+ family.MutationTracker.Clear();
+ yield return family;
+ }
}
/*
@@ -655,7 +663,12 @@ protected IEnumerable<IFluentColumnFamily> GetRange(KeyRange keyRange, string su
);
foreach (var record in output)
- yield return ObjectHelper.ConvertToFluentColumnFamily(record.Key, FamilyName, superColumnName, record.Columns);
+ {
+ var family = ObjectHelper.ConvertToFluentColumnFamily(record.Key, FamilyName, superColumnName, record.Columns);
+ _context.Attach(family);
+ family.MutationTracker.Clear();
+ yield return family;
+ }
}
}
}
@@ -19,8 +19,8 @@ public class CassandraColumnFamily<T> : CassandraColumnFamily
/// </summary>
/// <param name="keyspace"></param>
/// <param name="connection"></param>
- public CassandraColumnFamily(CassandraKeyspace keyspace, IConnection connection)
- : base(keyspace, connection, CassandraConfiguration.GetMapFor<T>().ColumnFamily)
+ public CassandraColumnFamily(CassandraContext context, CassandraKeyspace keyspace, IConnection connection)
+ : base(context, keyspace, connection, CassandraConfiguration.GetMapFor<T>().ColumnFamily)
{
_config = CassandraConfiguration.GetMapFor<T>();
}
@@ -81,22 +81,22 @@ protected Cassandra.Client GetClient()
}
/// <summary>
- /// Gets a typed column family.
+ ///
/// </summary>
- /// <typeparam name="T">Type of column family.</typeparam>
/// <returns></returns>
- public CassandraColumnFamily<T> GetColumnFamily<T>()
+ public CassandraColumnFamily GetColumnFamily(string columnFamily)
{
- return _keyspace.GetColumnFamily<T>();
+ return new CassandraColumnFamily(this, _keyspace, _connection, columnFamily);
}
/// <summary>
- ///
+ /// Gets a typed column family.
/// </summary>
+ /// <typeparam name="T">Type of column family.</typeparam>
/// <returns></returns>
- public CassandraColumnFamily GetColumnFamily(string columnFamily)
+ public CassandraColumnFamily<T> GetColumnFamily<T>()
{
- return _keyspace.GetColumnFamily(columnFamily);
+ return new CassandraColumnFamily<T>(this, _keyspace, _connection);
}
public void Attach(IFluentRecord record)
@@ -39,24 +39,5 @@ public string KeyspaceName
{
get { return _keyspaceName; }
}
-
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public CassandraColumnFamily GetColumnFamily(string columnFamily)
- {
- return new CassandraColumnFamily(this, _connection, columnFamily);
- }
-
- /// <summary>
- /// Gets a typed column family.
- /// </summary>
- /// <typeparam name="T">Type of column family.</typeparam>
- /// <returns></returns>
- public CassandraColumnFamily<T> GetColumnFamily<T>()
- {
- return new CassandraColumnFamily<T>(this, _connection);
- }
}
}
@@ -74,7 +74,8 @@
<Compile Include="Apache\Cassandra\TimedOutException.cs" />
<Compile Include="Apache\Cassandra\TokenRange.cs" />
<Compile Include="Apache\Cassandra\UnavailableException.cs" />
- <Compile Include="BinaryConverter.cs" />
+ <Compile Include="TypeConverters\AsciiType.cs" />
+ <Compile Include="TypeConverters\BinaryConverter.cs" />
<Compile Include="CassandraColumnFamily.cs" />
<Compile Include="CassandraContext.cs" />
<Compile Include="CassandraKeyRange.cs" />
@@ -104,8 +105,11 @@
<Compile Include="FluentColumnList`1.cs" />
<Compile Include="FluentColumnParent.cs" />
<Compile Include="FluentColumnPath.cs" />
+ <Compile Include="TypeConverters\BytesType.cs" />
<Compile Include="IFluentColumnFamily.cs" />
<Compile Include="IFluentRecord.cs" />
+ <Compile Include="TypeConverters\CassandraType.cs" />
+ <Compile Include="TypeConverters\LexicalUUIDType.cs" />
<Compile Include="ObjectHelper.cs" />
<Compile Include="FluentRecord.cs" />
<Compile Include="FluentSuperColumn.cs" />
@@ -124,6 +128,7 @@
<Compile Include="MutationType.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="BinaryHelper.cs" />
+ <Compile Include="TypeConverters\LongType.cs" />
<Compile Include="Thrift\Collections\THashSet.cs" />
<Compile Include="Thrift\Protocol\TBase.cs" />
<Compile Include="Thrift\Protocol\TBinaryProtocol.cs" />
@@ -154,6 +159,8 @@
<Compile Include="Thrift\Transport\TTransport.cs" />
<Compile Include="Thrift\Transport\TTransportException.cs" />
<Compile Include="Thrift\Transport\TTransportFactory.cs" />
+ <Compile Include="TypeConverters\TimeUUIDType.cs" />
+ <Compile Include="TypeConverters\UTF8Type.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Thrift\Collections\.svn\all-wcprops" />
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
+using FluentCassandra.Types;
namespace FluentCassandra
{
@@ -12,15 +13,28 @@ namespace FluentCassandra
/// <seealso href="http://wiki.apache.org/cassandra/API"/>
public class FluentColumn : IFluentColumn
{
+ private CassandraType _nameType;
+ private BytesType _valueType;
+
public FluentColumn()
{
Timestamp = DateTimeOffset.UtcNow;
}
+ private CassandraType Type
+ {
+ get
+ {
+ if (_nameType == null)
+ _nameType = Family.CompareWith;
+ return _nameType;
+ }
+ }
+
/// <summary>
/// The column name.
/// </summary>
- public string Name { get; set; }
+ public object Name { get; set; }
private object _value;
private byte[] _valueCache;
@@ -44,7 +58,7 @@ public object GetValue()
/// <returns></returns>
public object GetValue(Type type)
{
- _value = ValueBytes.GetObject(type);
+ _value = _valueType.GetObject(ValueBytes, type);
return _value;
}
@@ -55,7 +69,7 @@ public object GetValue(Type type)
/// <returns></returns>
public T GetValue<T>()
{
- _value = ValueBytes.GetObject<T>();
+ _value = _valueType.GetObject<T>(ValueBytes);
return (T)_value;
}
@@ -82,7 +96,7 @@ public DateTimeOffset Timestamp
/// </summary>
internal byte[] NameBytes
{
- get { return Name.GetBytes(); }
+ get { return _nameType.GetBytes(Name); }
}
/// <summary>
@@ -93,7 +107,7 @@ internal byte[] ValueBytes
get
{
if (_valueCache == null && _value != null)
- _valueCache = _value.GetBytes();
+ _valueCache = _valueType.GetBytes(_value);
return _valueCache;
}
set { _valueCache = value; }
@@ -5,6 +5,7 @@
using System.Dynamic;
using System.Linq.Expressions;
using System.ComponentModel;
+using FluentCassandra.Types;
namespace FluentCassandra
{
@@ -16,6 +17,7 @@ public FluentColumnFamily(string key, string columnFamily)
{
Key = key;
FamilyName = columnFamily;
+ CompareWith = new BytesType();
_columns = new FluentColumnList<FluentColumn>(new FluentColumnParent(this, null));
}
@@ -35,6 +37,11 @@ public FluentColumnFamily(string key, string columnFamily)
/// </summary>
public ColumnType ColumnType { get { return ColumnType.Normal; } }
+ /// <summary>
+ ///
+ /// </summary>
+ public CassandraType CompareWith { get; set; }
+
/// <summary>
///
/// </summary>
Oops, something went wrong.

0 comments on commit a47e07d

Please sign in to comment.