Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 217 lines (175 sloc) 5.264 kb
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using Apache.Cassandra;
b6643b0 @nberardi added round robin server manager to connection provider
nberardi authored
5 using FluentCassandra.Connections;
321da49 @nberardi refactoring of way operations happen, and created a wrapper for the cass...
nberardi authored
6 using FluentCassandra.Operations;
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
7
8 namespace FluentCassandra
9 {
10 public class CassandraSession : IDisposable
11 {
12 [ThreadStatic]
13 private static CassandraSession _current;
14
15 public static CassandraSession Current
16 {
17 get { return _current; }
18 internal set { _current = value; }
19 }
20
21 private IConnection _connection;
22
23 public CassandraSession(ConnectionBuilder connectionBuilder)
24 : this(ConnectionProviderFactory.Get(connectionBuilder), connectionBuilder.ReadConsistency, connectionBuilder.WriteConsistency) { }
25
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
26 public CassandraSession(ConnectionBuilder connectionBuilder, ConsistencyLevel read, ConsistencyLevel write)
27 : this(ConnectionProviderFactory.Get(connectionBuilder), read, write) { }
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
28
29 public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel read, ConsistencyLevel write)
30 {
31 if (Current != null)
32 throw new CassandraException("Cannot create a new session while there is one already active.");
33
34 ConnectionProvider = connectionProvider;
35 ReadConsistency = read;
36 WriteConsistency = write;
37
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection strin...
nberardi authored
38 IsAuthenticated = false;
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
39 Current = this;
40 }
41
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
42 /// <summary>
43 ///
44 /// </summary>
365b683 @nberardi still working on getting the app to compile after the breaking changes i...
nberardi authored
45 public IConnectionProvider ConnectionProvider { get; private set; }
46
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
47 /// <summary>
48 ///
49 /// </summary>
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
50 public ConsistencyLevel ReadConsistency { get; private set; }
51
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
52 /// <summary>
53 ///
54 /// </summary>
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
55 public ConsistencyLevel WriteConsistency { get; private set; }
56
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
57 /// <summary>
58 ///
59 /// </summary>
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection strin...
nberardi authored
60 public bool IsAuthenticated { get; private set; }
61
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
62 /// <summary>
63 ///
64 /// </summary>
65 /// <param name="setKeyspace"></param>
66 /// <returns></returns>
ea9226f @nberardi added support for CQL 3 and selecting the CQL version
nberardi authored
67 internal CassandraClientWrapper GetClient(bool setKeyspace = true, bool? setCqlVersion = null)
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
68 {
ea9226f @nberardi added support for CQL 3 and selecting the CQL version
nberardi authored
69 var builder = ConnectionProvider.Builder;
70 setCqlVersion = setCqlVersion ?? (builder.CqlVersion != null);
71
50d830b @nberardi added thrift from latest build
nberardi authored
72 if (_connection == null)
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
73 _connection = ConnectionProvider.Open();
50d830b @nberardi added thrift from latest build
nberardi authored
74
75 if (!_connection.IsOpen)
76 _connection.Open();
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
77
ce017c8 @nberardi all tests working for 0.7
nberardi authored
78 if (setKeyspace)
ea9226f @nberardi added support for CQL 3 and selecting the CQL version
nberardi authored
79 _connection.SetKeyspace(builder.Keyspace);
80
81 if (setCqlVersion.Value)
82 _connection.SetCqlVersion(builder.CqlVersion);
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
83
84 if (!String.IsNullOrWhiteSpace(builder.Username) && !String.IsNullOrWhiteSpace(builder.Password))
85 Login(builder.Username, builder.Password);
ce017c8 @nberardi all tests working for 0.7
nberardi authored
86
321da49 @nberardi refactoring of way operations happen, and created a wrapper for the cass...
nberardi authored
87 return new CassandraClientWrapper(_connection.Client);
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
88 }
89
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
90 /// <summary>
91 ///
92 /// </summary>
93 public void Login()
94 {
95 var builder = ConnectionProvider.Builder;
96
97 if (String.IsNullOrWhiteSpace(builder.Username) || String.IsNullOrWhiteSpace(builder.Password))
98 throw new CassandraException("No username and/or password was set in the connection string, please use Login(username, password) method.");
99
100 Login(builder.Username, builder.Password);
101 }
102
103 /// <summary>
104 ///
105 /// </summary>
106 /// <param name="username"></param>
107 /// <param name="password"></param>
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection strin...
nberardi authored
108 public void Login(string username, string password)
7ba4ce5 @nberardi added support for a couple more features that were added to the client i...
nberardi authored
109 {
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection strin...
nberardi authored
110 var auth = new AuthenticationRequest {
111 Credentials = new Dictionary<string, string> { { "username", username }, { "password", password } }
112 };
113
114 try
115 {
116 GetClient().login(auth);
117 IsAuthenticated = true;
118 }
119 catch (Exception exc)
120 {
121 IsAuthenticated = false;
122 throw new CassandraException("Login failed.", exc);
123 }
7ba4ce5 @nberardi added support for a couple more features that were added to the client i...
nberardi authored
124 }
125
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
126 /// <summary>
127 /// The last error that occured during the execution of an operation.
128 /// </summary>
129 public CassandraException LastError { get; private set; }
130
131 /// <summary>
132 /// Indicates if errors should be thrown when occuring on opperation.
133 /// </summary>
134 public bool ThrowErrors { get; set; }
135
136 /// <summary>
137 /// Execute the column family operation against the connection to the server.
138 /// </summary>
139 /// <typeparam name="TResult"></typeparam>
140 /// <param name="action"></param>
141 /// <param name="throwOnError"></param>
142 /// <returns></returns>
143 public TResult ExecuteOperation<TResult>(Operation<TResult> action, bool? throwOnError = null)
144 {
ac522e7 @nberardi added support for counter column
nberardi authored
145 if (WasDisposed)
146 throw new ObjectDisposedException(GetType().FullName);
147
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
148 if (!throwOnError.HasValue)
149 throwOnError = ThrowErrors;
150
151 LastError = null;
ac522e7 @nberardi added support for counter column
nberardi authored
152 action.Session = this;
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
153
154 TResult result;
155 bool success = action.TryExecute(out result);
156
157 if (!success)
158 LastError = action.Error;
159
160 if (!success && (throwOnError ?? ThrowErrors))
161 throw action.Error;
162
163 return result;
164 }
165
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
166 #region IDisposable Members
167
168 /// <summary>
169 ///
170 /// </summary>
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
171 public bool WasDisposed
172 {
173 get;
174 private set;
175 }
176
177 /// <summary>
178 ///
179 /// </summary>
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
180 public void Dispose()
181 {
182 Dispose(true);
046825e @nberardi updated connections to have a more standarding naming connvetion with ot...
nberardi authored
183 GC.SuppressFinalize(this);
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
184 }
185
186 /// <summary>
187 /// The dispose.
188 /// </summary>
189 /// <param name="disposing">
190 /// The disposing.
191 /// </param>
192 protected virtual void Dispose(bool disposing)
193 {
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
194 if (!WasDisposed && disposing)
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
195 {
365b683 @nberardi still working on getting the app to compile after the breaking changes i...
nberardi authored
196 if (_connection != null)
197 ConnectionProvider.Close(_connection);
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
198
199 if (Current == this)
200 Current = null;
201 }
202
2645bc7 @nberardi cleaned up the use of sessions in the code
nberardi authored
203 WasDisposed = true;
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
204 }
205
206 /// <summary>
365b683 @nberardi still working on getting the app to compile after the breaking changes i...
nberardi authored
207 /// Finalizes an instance of the <see cref="CassandraSession"/> class.
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
208 /// </summary>
209 ~CassandraSession()
210 {
365b683 @nberardi still working on getting the app to compile after the breaking changes i...
nberardi authored
211 Dispose(false);
e4efc4c @nberardi added support for session based configurations, as well as dynamic prope...
nberardi authored
212 }
213
214 #endregion
215 }
216 }
Something went wrong with that request. Please try again.