Browse files

cleaned up session, context, and keyspace objects

  • Loading branch information...
1 parent 2645bc7 commit 3254baf23a313a13aca9d72fcbe66cc80045d1ee @nberardi nberardi committed Jan 31, 2012
View
18 src/BaseCassandraColumnFamily.cs
@@ -70,26 +70,20 @@ public void RemoveAllRows()
/// <returns></returns>
public TResult ExecuteOperation<TResult>(ColumnFamilyOperation<TResult> action, bool? throwOnError = null)
{
+ if (!throwOnError.HasValue)
+ throwOnError = ThrowErrors;
+
CassandraSession localSession = null;
if (CassandraSession.Current == null)
localSession = _context.OpenSession();
action.Context = _context;
- action.Session = localSession;
+ action.ColumnFamily = this;
try
{
- LastError = null;
-
- TResult result;
- action.ColumnFamily = this;
- bool success = action.TryExecute(out result);
-
- if (!success)
- LastError = action.Error;
-
- if (!success && (throwOnError ?? ThrowErrors))
- throw action.Error;
+ var result = localSession.ExecuteOperation(action, throwOnError);
+ LastError = localSession.LastError;
return result;
}
View
131 src/CassandraContext.cs
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
+using System.Linq;
+using System.Linq;
+using Apache.Cassandra;
+using FluentCassandra.Connections;
using FluentCassandra.Linq;
-using FluentCassandra.Types;
using FluentCassandra.Operations;
-using FluentCassandra.Connections;
+using FluentCassandra.Types;
namespace FluentCassandra
{
@@ -49,6 +52,8 @@ public CassandraContext(ConnectionBuilder connectionBuilder)
_connectionBuilder = connectionBuilder;
_trackers = new List<IFluentMutationTracker>();
+
+ Keyspace = new CassandraKeyspace(connectionBuilder.Keyspace, this);
}
/// <summary>
@@ -79,6 +84,115 @@ public CassandraColumnFamily<CompareWith> GetColumnFamily<CompareWith>(string co
/// <summary>
///
/// </summary>
+ public CassandraKeyspace Keyspace { get; private set; }
+
+ #region Cassandra System For Server
+
+ public string AddKeyspace(KsDef definition)
+ {
+ return ExecuteOperation(new SimpleOperation<string>(ctx => {
+ return ctx.Session.GetClient(setKeyspace: false).system_add_keyspace(definition);
+ }));
+ }
+
+ public string UpdateKeyspace(KsDef definition)
+ {
+ return ExecuteOperation(new SimpleOperation<string>(ctx => {
+ return ctx.Session.GetClient(setKeyspace: false).system_update_keyspace(definition);
+ }));
+ }
+
+ public string DropKeyspace(string keyspace)
+ {
+ return ExecuteOperation(new SimpleOperation<string>(ctx => {
+ return ctx.Session.GetClient(setKeyspace: false).system_drop_keyspace(keyspace);
+ }));
+ }
+
+ public string AddColumnFamily(CfDef definition)
+ {
+ Keyspace.ClearCachedKeyspaceDescription();
+
+ return ExecuteOperation(new SimpleOperation<string>(ctx => {
+ return ctx.Session.GetClient().system_add_column_family(definition);
+ }));
+ }
+
+ public string UpdateColumnFamily(CfDef definition)
+ {
+ Keyspace.ClearCachedKeyspaceDescription();
+
+ return ExecuteOperation(new SimpleOperation<string>(ctx => {
+ return ctx.Session.GetClient().system_update_column_family(definition);
+ }));
+ }
+
+ public string DropColumnFamily(string columnFamily)
+ {
+ Keyspace.ClearCachedKeyspaceDescription();
+
+ return ExecuteOperation(new SimpleOperation<string>(ctx => {
+ return ctx.Session.GetClient().system_drop_column_family(columnFamily);
+ }));
+ }
+
+ #endregion
+
+ #region Cassandra Descriptions For Server
+
+ public bool KeyspaceExists(string keyspaceName)
+ {
+ return DescribeKeyspaces().Any(keyspace => keyspace.KeyspaceName == keyspaceName);
+ }
+
+ 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));
+ }));
+ }
+
+ public string DescribeClusterName()
+ {
+ return ExecuteOperation(new SimpleOperation<string>(ctx => {
+ return ctx.Session.GetClient(setKeyspace: false).describe_cluster_name();
+ }));
+ }
+
+ public Dictionary<string, List<string>> DescribeSchemaVersions()
+ {
+ return ExecuteOperation(new SimpleOperation<Dictionary<string, List<string>>>(ctx => {
+ return ctx.Session.GetClient(setKeyspace: false).describe_schema_versions();
+ }));
+ }
+
+ public string DescribeVersion()
+ {
+ return ExecuteOperation(new SimpleOperation<string>(ctx => {
+ return ctx.Session.GetClient(setKeyspace: false).describe_version();
+ }));
+ }
+
+ public string DescribePartitioner()
+ {
+ return ExecuteOperation(new SimpleOperation<string>(ctx => {
+ return ctx.Session.GetClient(setKeyspace: false).describe_partitioner();
+ }));
+ }
+
+ public string DescribeSnitch()
+ {
+ return ExecuteOperation(new SimpleOperation<string>(ctx => {
+ return ctx.Session.GetClient(setKeyspace: false).describe_snitch();
+ }));
+ }
+
+ #endregion
+
+ /// <summary>
+ ///
+ /// </summary>
/// <param name="record"></param>
public void Attach(IFluentRecord record)
{
@@ -184,20 +298,11 @@ public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwO
localSession = OpenSession();
action.Context = this;
- action.Session = localSession;
try
{
- LastError = null;
-
- TResult result;
- bool success = action.TryExecute(out result);
-
- if (!success)
- LastError = action.Error;
-
- if (!success && (throwOnError ?? ThrowErrors))
- throw action.Error;
+ var result = localSession.ExecuteOperation(action, throwOnError);
+ LastError = localSession.LastError;
return result;
}
View
96 src/CassandraKeyspace.cs
@@ -6,30 +6,42 @@
using System.Diagnostics;
using FluentCassandra.Types;
using System.Text;
+using FluentCassandra.Operations;
namespace FluentCassandra
{
public class CassandraKeyspace
{
private readonly string _keyspaceName;
+ private KsDef _cachedKeyspaceDescription;
+ private CassandraContext _context;
- public CassandraKeyspace(KsDef definition)
+ /// <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)
+ public CassandraKeyspace(string keyspaceName, CassandraContext context)
{
+ if (keyspaceName == null)
+ throw new ArgumentNullException("keyspaceName");
+
_keyspaceName = keyspaceName;
+ _context = context;
}
/// <summary>
@@ -40,22 +52,24 @@ public string KeyspaceName
get { return _keyspaceName; }
}
+ /// <summary>
+ ///
+ /// </summary>
+ /// <returns></returns>
public override string ToString()
{
return KeyspaceName;
}
- private KsDef _cachedKeyspaceDescription;
-
public void TryCreateSelf(Server server)
{
- if (CassandraSession.KeyspaceExists(server, KeyspaceName))
+ if (_context.KeyspaceExists(KeyspaceName))
{
Debug.WriteLine(KeyspaceName + " already exists", "keyspace");
return;
}
- string result = CassandraSession.AddKeyspace(server, new KsDef {
+ string result = _context.AddKeyspace(new KsDef {
Name = KeyspaceName,
Strategy_class = "org.apache.cassandra.locator.SimpleStrategy",
Replication_factor = 1,
@@ -64,30 +78,30 @@ public void TryCreateSelf(Server server)
Debug.WriteLine(result, "keyspace setup");
}
- public void TryCreateColumnFamily<CompareWith>(Server server, string columnFamilyName)
+ public void TryCreateColumnFamily<CompareWith>(string columnFamilyName)
where CompareWith : CassandraType
{
- if (ColumnFamilyExists(server, columnFamilyName))
+ if (ColumnFamilyExists(columnFamilyName))
{
Debug.WriteLine(columnFamilyName + " already exists", "keyspace setup");
return;
}
var comparatorType = GetCassandraComparatorType(typeof(CompareWith));
- string result = AddColumnFamily(server, new CfDef {
+ string result = _context.AddColumnFamily(new CfDef {
Name = columnFamilyName,
Keyspace = KeyspaceName,
Comparator_type = comparatorType
});
Debug.WriteLine(result, "keyspace setup");
}
- public void TryCreateColumnFamily<CompareWith, CompareSubcolumnWith>(Server server, string columnFamilyName)
+ public void TryCreateColumnFamily<CompareWith, CompareSubcolumnWith>(string columnFamilyName)
where CompareWith : CassandraType
where CompareSubcolumnWith : CassandraType
{
- if (ColumnFamilyExists(server, columnFamilyName))
+ if (ColumnFamilyExists(columnFamilyName))
{
Debug.WriteLine(columnFamilyName + " already exists", "keyspace setup");
return;
@@ -96,7 +110,7 @@ public void TryCreateColumnFamily<CompareWith>(Server server, string columnFamil
var comparatorType = GetCassandraComparatorType(typeof(CompareWith));
var subComparatorType = GetCassandraComparatorType(typeof(CompareSubcolumnWith));
- string result = AddColumnFamily(server, new CfDef {
+ string result = _context.AddColumnFamily(new CfDef {
Name = columnFamilyName,
Keyspace = KeyspaceName,
Column_type = "Super",
@@ -127,63 +141,39 @@ private string GetCassandraComparatorType(Type comparatorType)
return comparatorTypeName;
}
- #region Cassandra System For Server
-
- public CfDef GetColumnFamily(Server server, string columnFamily)
+ public CfDef GetColumnFamily(string columnFamily)
{
- return Describe(server).Cf_defs.FirstOrDefault(cf => cf.Name == columnFamily);
+ return Describe().Cf_defs.FirstOrDefault(cf => cf.Name == columnFamily);
}
- public string AddColumnFamily(Server server, CfDef definition)
+ public bool ColumnFamilyExists(string columnFamilyName)
{
- _cachedKeyspaceDescription = null;
-
- using (var session = new CassandraSession(new ConnectionBuilder(KeyspaceName, server.Host, server.Port)))
- return session.GetClient().system_add_column_family(definition);
+ return Describe().Cf_defs.Any(columnFamily => columnFamily.Name == columnFamilyName);
}
- public string UpdateColumnFamily(Server server, CfDef definition)
+ public void ClearCachedKeyspaceDescription()
{
_cachedKeyspaceDescription = null;
-
- using (var session = new CassandraSession(new ConnectionBuilder(KeyspaceName, server.Host, server.Port)))
- return session.GetClient().system_update_column_family(definition);
}
- public string DropColumnFamily(Server server, string columnFamily)
- {
- _cachedKeyspaceDescription = null;
+ #region Cassandra Keyspace Server Operations
- using (var session = new CassandraSession(new ConnectionBuilder(KeyspaceName, server.Host, server.Port)))
- return session.GetClient().system_drop_column_family(columnFamily);
- }
-
- #endregion
-
- #region Cassandra Descriptions For Server
-
- public bool ColumnFamilyExists(Server server, string columnFamilyName)
- {
- return Describe(server).Cf_defs.Any(columnFamily => columnFamily.Name == columnFamilyName);
- }
-
- public IEnumerable<CassandraTokenRange> DescribeRing(Server server)
+ public KsDef Describe()
{
- using (var session = new CassandraSession(new ConnectionBuilder(KeyspaceName, server.Host, server.Port)))
- {
- var tokenRanges = session.GetClient(setKeyspace: false).describe_ring(KeyspaceName);
+ if (_cachedKeyspaceDescription == null)
+ _cachedKeyspaceDescription = _context.ExecuteOperation(new SimpleOperation<Apache.Cassandra.KsDef>(ctx => {
+ return ctx.Session.GetClient().describe_keyspace(KeyspaceName);
+ }));
- foreach (var tokenRange in tokenRanges)
- yield return new CassandraTokenRange(tokenRange.Start_token, tokenRange.End_token, tokenRange.Endpoints);
- }
+ return _cachedKeyspaceDescription;
}
- public KsDef Describe(Server server)
+ public IEnumerable<CassandraTokenRange> DescribeRing()
{
- if (_cachedKeyspaceDescription == null)
- _cachedKeyspaceDescription = CassandraSession.GetKeyspace(server, KeyspaceName);
-
- return _cachedKeyspaceDescription;
+ return _context.ExecuteOperation(new SimpleOperation<IEnumerable<CassandraTokenRange>>(ctx => {
+ var tokenRanges = ctx.Session.GetClient(setKeyspace: false).describe_ring(KeyspaceName);
+ return tokenRanges.Select(tr => new CassandraTokenRange(tr.Start_token, tr.End_token, tr.Endpoints));
+ }));
}
#endregion
View
141 src/CassandraSession.cs
@@ -18,97 +18,6 @@ public static CassandraSession Current
internal set { _current = value; }
}
- #region Cassandra System For Server
-
- public static KsDef GetKeyspace(Server server, string keyspace)
- {
- using (var session = new CassandraSession(new ConnectionBuilder(keyspace, server.Host, server.Port)))
- return session.GetClient().describe_keyspace(keyspace);
- }
-
- public static string AddKeyspace(Server server, KsDef definition)
- {
- using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
- return session.GetClient(setKeyspace: false).system_add_keyspace(definition);
- }
-
- public static string UpdateKeyspace(Server server, KsDef definition)
- {
- using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
- return session.GetClient(setKeyspace: false).system_update_keyspace(definition);
- }
-
- public static string DropKeyspace(Server server, string keyspace)
- {
- using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
- return session.GetClient(setKeyspace: false).system_drop_keyspace(keyspace);
- }
-
- #endregion
-
- #region Cassandra Descriptions For Server
-
- public static bool KeyspaceExists(Server server, string keyspaceName)
- {
- return DescribeKeyspaces(server).Any(keyspace => keyspace.KeyspaceName == keyspaceName);
- }
-
- public static IEnumerable<CassandraKeyspace> DescribeKeyspaces(Server server)
- {
- using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
- {
- IEnumerable<KsDef> keyspaces = session.GetClient(setKeyspace: false).describe_keyspaces();
- return keyspaces.Select(keyspace => new CassandraKeyspace(keyspace)).ToList();
- }
- }
-
- public static string DescribeClusterName(Server server)
- {
- using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
- {
- string response = session.GetClient(setKeyspace: false).describe_cluster_name();
- return response;
- }
- }
-
- public static Dictionary<string, List<string>> DescribeSchemaVersions(Server server)
- {
- using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
- {
- var response = session.GetClient(setKeyspace: false).describe_schema_versions();
- return response;
- }
- }
-
- public static string DescribeVersion(Server server)
- {
- using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
- {
- string response = session.GetClient(setKeyspace: false).describe_version();
- return response;
- }
- }
-
- public static string DescribePartitioner(Server server)
- {
- using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
- {
- string response = session.GetClient(setKeyspace: false).describe_partitioner();
- return response;
- }
- }
-
- public static string DescribeSnitch(Server server)
- {
- using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
- {
- string response = session.GetClient(setKeyspace: false).describe_snitch();
- return response;
- }
- }
-
- #endregion
-
private IConnection _connection;
public CassandraSession(ConnectionBuilder connectionBuilder)
@@ -125,7 +34,6 @@ public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel
ConnectionProvider = connectionProvider;
ReadConsistency = read;
WriteConsistency = write;
- Keyspace = new CassandraKeyspace(connectionProvider.Builder.Keyspace);
IsAuthenticated = false;
Current = this;
@@ -149,11 +57,6 @@ public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel
/// <summary>
///
/// </summary>
- public CassandraKeyspace Keyspace { get; private set; }
-
- /// <summary>
- ///
- /// </summary>
public bool IsAuthenticated { get; private set; }
/// <summary>
@@ -170,7 +73,11 @@ internal CassandraClientWrapper GetClient(bool setKeyspace = true)
_connection.Open();
if (setKeyspace)
- _connection.SetKeyspace(Keyspace.KeyspaceName);
+ _connection.SetKeyspace(ConnectionProvider.Builder.Keyspace);
+
+ var builder = ConnectionProvider.Builder;
+ if (!String.IsNullOrWhiteSpace(builder.Username) && !String.IsNullOrWhiteSpace(builder.Password))
+ Login(builder.Username, builder.Password);
return new CassandraClientWrapper(_connection.Client);
}
@@ -211,6 +118,44 @@ public void Login(string username, string password)
}
}
+ /// <summary>
+ /// The last error that occured during the execution of an operation.
+ /// </summary>
+ public CassandraException LastError { get; private set; }
+
+ /// <summary>
+ /// Indicates if errors should be thrown when occuring on opperation.
+ /// </summary>
+ public bool ThrowErrors { get; set; }
+
+ /// <summary>
+ /// Execute the column family operation against the connection to the server.
+ /// </summary>
+ /// <typeparam name="TResult"></typeparam>
+ /// <param name="action"></param>
+ /// <param name="throwOnError"></param>
+ /// <returns></returns>
+ public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwOnError = null)
+ {
+ if (!throwOnError.HasValue)
+ throwOnError = ThrowErrors;
+
+ action.Session = this;
+
+ LastError = null;
+
+ TResult result;
+ bool success = action.TryExecute(out result);
+
+ if (!success)
+ LastError = action.Error;
+
+ if (!success && (throwOnError ?? ThrowErrors))
+ throw action.Error;
+
+ return result;
+ }
+
#region IDisposable Members
/// <summary>
View
2 src/Connections/IConnectionProvider.cs
@@ -6,6 +6,8 @@ public interface IConnectionProvider
{
ConnectionBuilder Builder { get; }
+ IServerManager Servers { get; }
+
IConnection CreateConnection();
IConnection Open();
View
1 src/Connections/NormalConnectionProvider.cs
@@ -1,6 +1,5 @@
using System;
using System.Net.Sockets;
-using System.Threading;
namespace FluentCassandra.Connections
{
View
83 src/Operations/CassandraClientWrapper.cs
@@ -14,24 +14,6 @@ public CassandraClientWrapper(Apache.Cassandra.Cassandra.Iface client)
_client = client;
}
- /// <summary>
- /// Execute the column family operation against the connection to the server.
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="action"></param>
- /// <param name="throwOnError"></param>
- /// <returns></returns>
- private TResult ExecuteOperation<TResult>(Operation<TResult> action)
- {
- TResult result;
- bool success = action.TryExecute(out result);
-
- if (!success)
- throw action.Error;
-
- return result;
- }
-
#region Iface Members
public void login(Apache.Cassandra.AuthenticationRequest auth_request)
@@ -148,115 +130,82 @@ public void batch_mutate(Dictionary<byte[], Dictionary<string, List<Apache.Cassa
public void truncate(string cfname)
{
- ExecuteOperation(new SimpleOperation<Void>(delegate {
- _client.truncate(cfname);
- return new Void();
- }));
+ _client.truncate(cfname);
}
public Dictionary<string, List<string>> describe_schema_versions()
{
- return ExecuteOperation(new SimpleOperation<Dictionary<string, List<string>>>(delegate {
- return _client.describe_schema_versions();
- }));
+ return _client.describe_schema_versions();
}
public List<Apache.Cassandra.KsDef> describe_keyspaces()
{
- return ExecuteOperation(new SimpleOperation<List<Apache.Cassandra.KsDef>>(delegate {
- return _client.describe_keyspaces();
- }));
+ return _client.describe_keyspaces();
}
public string describe_cluster_name()
{
- return ExecuteOperation(new SimpleOperation<string>(delegate {
- return _client.describe_cluster_name();
- }));
+ return _client.describe_cluster_name();
}
public string describe_version()
{
- return ExecuteOperation(new SimpleOperation<string>(delegate {
- return _client.describe_version();
- }));
+ return _client.describe_version();
}
public List<Apache.Cassandra.TokenRange> describe_ring(string keyspace)
{
- return ExecuteOperation(new SimpleOperation<List<Apache.Cassandra.TokenRange>>(delegate {
- return _client.describe_ring(keyspace);
- }));
+ return _client.describe_ring(keyspace);
}
public string describe_partitioner()
{
- return ExecuteOperation(new SimpleOperation<string>(delegate {
- return _client.describe_partitioner();
- }));
+ return _client.describe_partitioner();
}
public string describe_snitch()
{
- return ExecuteOperation(new SimpleOperation<string>(delegate {
- return _client.describe_snitch();
- }));
+ return _client.describe_snitch();
}
public Apache.Cassandra.KsDef describe_keyspace(string keyspace)
{
- return ExecuteOperation(new SimpleOperation<Apache.Cassandra.KsDef>(delegate {
- return _client.describe_keyspace(keyspace);
- }));
+ return _client.describe_keyspace(keyspace);
}
public List<string> describe_splits(string cfName, string start_token, string end_token, int keys_per_split)
{
- return ExecuteOperation(new SimpleOperation<List<string>>(delegate {
- return _client.describe_splits(cfName, start_token, end_token, keys_per_split);
- }));
+ return _client.describe_splits(cfName, start_token, end_token, keys_per_split);
}
public string system_add_column_family(Apache.Cassandra.CfDef cf_def)
{
- return ExecuteOperation(new SimpleOperation<string>(delegate {
- return _client.system_add_column_family(cf_def);
- }));
+ return _client.system_add_column_family(cf_def);
}
public string system_drop_column_family(string column_family)
{
- return ExecuteOperation(new SimpleOperation<string>(delegate {
- return _client.system_drop_column_family(column_family);
- }));
+ return _client.system_drop_column_family(column_family);
}
public string system_add_keyspace(Apache.Cassandra.KsDef ks_def)
{
- return ExecuteOperation(new SimpleOperation<string>(delegate {
- return _client.system_add_keyspace(ks_def);
- }));
+ return _client.system_add_keyspace(ks_def);
}
public string system_drop_keyspace(string keyspace)
{
- return ExecuteOperation(new SimpleOperation<string>(delegate {
- return _client.system_drop_keyspace(keyspace);
- }));
+ return _client.system_drop_keyspace(keyspace);
}
public string system_update_keyspace(Apache.Cassandra.KsDef ks_def)
{
- return ExecuteOperation(new SimpleOperation<string>(delegate {
- return _client.system_update_keyspace(ks_def);
- }));
+ return _client.system_update_keyspace(ks_def);
}
public string system_update_column_family(Apache.Cassandra.CfDef cf_def)
{
- return ExecuteOperation(new SimpleOperation<string>(delegate {
- return _client.system_update_column_family(cf_def);
- }));
+ return _client.system_update_column_family(cf_def);
}
public Apache.Cassandra.CqlResult execute_cql_query(byte[] query, Apache.Cassandra.Compression compression)
View
6 src/Operations/SimpleOperation.cs
@@ -5,16 +5,16 @@ namespace FluentCassandra.Operations
{
public class SimpleOperation<TResult> : Operation<TResult>
{
- private readonly Func<TResult> _operation;
+ private readonly Func<SimpleOperation<TResult>, TResult> _operation;
- public SimpleOperation(Func<TResult> operation)
+ public SimpleOperation(Func<SimpleOperation<TResult>, TResult> operation)
{
_operation = operation;
}
public override TResult Execute()
{
- return _operation();
+ return _operation(this);
}
}
}
View
20 test/FluentCassandra.Sandbox/Program.cs
@@ -15,14 +15,18 @@ internal class Program
private static void SetupKeyspace()
{
- if (CassandraSession.KeyspaceExists(Server, KeyspaceName))
- CassandraSession.DropKeyspace(Server, KeyspaceName);
-
- var keyspace = new CassandraKeyspace(KeyspaceName);
- keyspace.TryCreateSelf(Server);
- keyspace.TryCreateColumnFamily<UTF8Type>(Server, "Posts");
- keyspace.TryCreateColumnFamily<LongType>(Server, "Tags");
- keyspace.TryCreateColumnFamily<TimeUUIDType, UTF8Type>(Server, "Comments");
+ using (var db = new CassandraContext(keyspace: KeyspaceName, server: Server))
+ {
+
+ if (db.KeyspaceExists(KeyspaceName))
+ db.DropKeyspace(KeyspaceName);
+
+ var keyspace = db.Keyspace;
+ keyspace.TryCreateSelf(Server);
+ keyspace.TryCreateColumnFamily<UTF8Type>("Posts");
+ keyspace.TryCreateColumnFamily<LongType>("Tags");
+ keyspace.TryCreateColumnFamily<TimeUUIDType, UTF8Type>("Comments");
+ }
}
#endregion
View
39 test/FluentCassandra.StressTest/Program.cs
@@ -34,25 +34,26 @@ private static void SendDebugToConsole()
private static void SetupKeyspace()
{
- if (!CassandraSession.KeyspaceExists(server, keyspaceName))
- CassandraSession.AddKeyspace(server, new KsDef {
- Name = keyspaceName,
- Replication_factor = 1,
- Strategy_class = "org.apache.cassandra.locator.SimpleStrategy",
- Cf_defs = new List<CfDef>()
- });
-
- var keyspace = new CassandraKeyspace(keyspaceName);
-
- if (!keyspace.ColumnFamilyExists(server, "Posts"))
- keyspace.AddColumnFamily(server, new CfDef {
- Name = "Posts",
- Keyspace = keyspaceName,
- Column_type = "Super",
- Comparator_type = "UTF8Type",
- Subcomparator_type = "UTF8Type",
- Comment = "Used for blog posts."
- });
+ using (var db = new CassandraContext(keyspace: keyspaceName, server: server))
+ {
+ if (!db.KeyspaceExists(keyspaceName))
+ db.AddKeyspace(new KsDef {
+ Name = keyspaceName,
+ Replication_factor = 1,
+ Strategy_class = "org.apache.cassandra.locator.SimpleStrategy",
+ Cf_defs = new List<CfDef>()
+ });
+
+ if (!db.Keyspace.ColumnFamilyExists("Posts"))
+ db.AddColumnFamily(new CfDef {
+ Name = "Posts",
+ Keyspace = keyspaceName,
+ Column_type = "Super",
+ Comparator_type = "UTF8Type",
+ Subcomparator_type = "UTF8Type",
+ Comment = "Used for blog posts."
+ });
+ }
}
private static void Main(string[] args)
View
10 test/FluentCassandra.Tests/Linq/LinqToCassandraTests.cs
@@ -19,15 +19,13 @@ public void TestInit()
var keyspaceName = "Testing";
var server = new Server("localhost");
- try { CassandraSession.DropKeyspace(server, keyspaceName); }
- catch { }
-
- var keyspace = new CassandraKeyspace(keyspaceName);
- keyspace.TryCreateSelf(server);
-
_db = new CassandraContext(keyspace: keyspaceName, server: server);
_db.ThrowErrors = true;
+ try { _db.DropKeyspace(keyspaceName); }
+ catch { }
+
+ _db.Keyspace.TryCreateSelf(server);
_db.ExecuteNonQuery(@"
CREATE COLUMNFAMILY Users (
KEY int PRIMARY KEY,
View
36 test/FluentCassandra.Tests/_CassandraSetup.cs
@@ -22,27 +22,27 @@ public _CassandraSetup()
var keyspaceName = "Testing";
var server = new Server("localhost");
- if (CassandraSession.KeyspaceExists(server, keyspaceName))
- CassandraSession.DropKeyspace(server, keyspaceName);
-
- var keyspace = new CassandraKeyspace(keyspaceName);
- keyspace.TryCreateSelf(server);
- keyspace.TryCreateColumnFamily<AsciiType>(server, "Standard");
- keyspace.TryCreateColumnFamily<AsciiType, AsciiType>(server, "Super");
- keyspace.TryCreateColumnFamily<AsciiType>(server, "StandardAsciiType");
- keyspace.TryCreateColumnFamily<BytesType>(server, "StandardBytesType");
- keyspace.TryCreateColumnFamily<IntegerType>(server, "StandardIntegerType");
- keyspace.TryCreateColumnFamily<LexicalUUIDType>(server, "StandardLexicalUUIDType");
- keyspace.TryCreateColumnFamily<LongType>(server, "StandardLongType");
- keyspace.TryCreateColumnFamily<TimeUUIDType>(server, "StandardTimeUUIDType");
- keyspace.TryCreateColumnFamily<UTF8Type>(server, "StandardUTF8Type");
- keyspace.TryCreateColumnFamily<UUIDType>(server, "StandardUUIDType");
- keyspace.TryCreateColumnFamily<CompositeType<LongType, UTF8Type>>(server, "StandardCompositeType");
- keyspace.TryCreateColumnFamily<DynamicCompositeType>(server, "StandardDynamicCompositeType");
-
DB = new CassandraContext(keyspaceName, server);
DB.ThrowErrors = true;
+ if (DB.KeyspaceExists(keyspaceName))
+ DB.DropKeyspace(keyspaceName);
+
+ var keyspace = DB.Keyspace;
+ keyspace.TryCreateSelf(server);
+ keyspace.TryCreateColumnFamily<AsciiType>("Standard");
+ keyspace.TryCreateColumnFamily<AsciiType, AsciiType>("Super");
+ keyspace.TryCreateColumnFamily<AsciiType>("StandardAsciiType");
+ keyspace.TryCreateColumnFamily<BytesType>("StandardBytesType");
+ keyspace.TryCreateColumnFamily<IntegerType>("StandardIntegerType");
+ keyspace.TryCreateColumnFamily<LexicalUUIDType>("StandardLexicalUUIDType");
+ keyspace.TryCreateColumnFamily<LongType>("StandardLongType");
+ keyspace.TryCreateColumnFamily<TimeUUIDType>("StandardTimeUUIDType");
+ keyspace.TryCreateColumnFamily<UTF8Type>("StandardUTF8Type");
+ keyspace.TryCreateColumnFamily<UUIDType>("StandardUUIDType");
+ keyspace.TryCreateColumnFamily<CompositeType<LongType, UTF8Type>>("StandardCompositeType");
+ keyspace.TryCreateColumnFamily<DynamicCompositeType>("StandardDynamicCompositeType");
+
Family = DB.GetColumnFamily<AsciiType>("Standard");
SuperFamily = DB.GetColumnFamily<AsciiType, AsciiType>("Super");
View
9 utility/FluentCassandra.LinqPad/CassandraDriver.cs
@@ -3,9 +3,8 @@
using System.Linq;
using System.Reflection;
using FluentCassandra.Linq;
-using LINQPad.Extensibility.DataContext;
using LINQPad;
-using FluentCassandra.Connections;
+using LINQPad.Extensibility.DataContext;
namespace FluentCassandra.LinqPad
{
@@ -26,10 +25,10 @@ private void CacheDefinitionIfNessisary(CassandraConnectionInfo info)
if (_schema != null)
return;
- var server = new Server(info.Host, info.Port, info.Timeout);
- var keyspace = new CassandraKeyspace(info.Keyspace);
+ var context = info.CreateContext();
+ var keyspace = context.Keyspace;
- _schema = keyspace.Describe(server);
+ _schema = keyspace.Describe();
}
/// <summary>Returns the text to display in the root Schema Explorer node for a given connection info.</summary>
View
7 utility/FluentCassandra.LinqPad/SchemaBuilder.cs
@@ -7,7 +7,6 @@
using System.Linq;
using System.Reflection;
using System.Text;
-using FluentCassandra.Connections;
using FluentCassandra.Types;
using LINQPad.Extensibility.DataContext;
using Microsoft.CSharp;
@@ -18,10 +17,10 @@ internal class SchemaBuilder
{
internal static List<ExplorerItem> GetSchemaAndBuildAssembly(CassandraConnectionInfo info, string driverFolder, AssemblyName name, ref string nameSpace, ref string typeName)
{
- var server = new Server(info.Host, info.Port, info.Timeout);
- var keyspace = new CassandraKeyspace(info.Keyspace);
+ var context = info.CreateContext();
+ var keyspace = context.Keyspace;
- var def = keyspace.Describe(server);
+ var def = keyspace.Describe();
var code = GenerateCode(def, nameSpace, typeName);
var schema = GetSchema(def);

0 comments on commit 3254baf

Please sign in to comment.