Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 224 lines (180 sloc) 6.282 kB
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
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 c…
nberardi authored
6 using FluentCassandra.Operations;
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
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
50d830b @nberardi added thrift from latest build
nberardi authored
21 #region Cassandra System For Server
22
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
23 public static KsDef GetKeyspace(Server server, string keyspace)
24 {
25 using (var session = new CassandraSession(new ConnectionBuilder(keyspace, server.Host, server.Port)))
26 return session.GetClient().describe_keyspace(keyspace);
27 }
28
50d830b @nberardi added thrift from latest build
nberardi authored
29 public static string AddKeyspace(Server server, KsDef definition)
30 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
31 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
32 return session.GetClient(setKeyspace: false).system_add_keyspace(definition);
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
33 }
34
35 public static string UpdateKeyspace(Server server, KsDef definition)
36 {
37 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
38 return session.GetClient(setKeyspace: false).system_update_keyspace(definition);
50d830b @nberardi added thrift from latest build
nberardi authored
39 }
40
41 public static string DropKeyspace(Server server, string keyspace)
42 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
43 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
44 return session.GetClient(setKeyspace: false).system_drop_keyspace(keyspace);
50d830b @nberardi added thrift from latest build
nberardi authored
45 }
46
47 #endregion
48
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
49 #region Cassandra Descriptions For Server
50
ce017c8 @nberardi all tests working for 0.7
nberardi authored
51 public static bool KeyspaceExists(Server server, string keyspaceName)
52 {
53 return DescribeKeyspaces(server).Any(keyspace => keyspace.KeyspaceName == keyspaceName);
54 }
55
50d830b @nberardi added thrift from latest build
nberardi authored
56 public static IEnumerable<CassandraKeyspace> DescribeKeyspaces(Server server)
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
57 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
58 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
59 {
74b23c0 @nberardi now compiles against 0.7 beta-2 thrift interface
nberardi authored
60 IEnumerable<KsDef> keyspaces = session.GetClient(setKeyspace: false).describe_keyspaces();
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
61
62 foreach (var keyspace in keyspaces)
63 yield return new CassandraKeyspace(keyspace);
64 }
65 }
66
50d830b @nberardi added thrift from latest build
nberardi authored
67 public static string DescribeClusterName(Server server)
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
68 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
69 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
70 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
71 string response = session.GetClient(setKeyspace: false).describe_cluster_name();
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
72 return response;
73 }
74 }
75
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
76 public static Dictionary<string, List<string>> DescribeSchemaVersions(Server server)
77 {
78 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
79 {
80 var response = session.GetClient(setKeyspace: false).describe_schema_versions();
81 return response;
82 }
83 }
84
50d830b @nberardi added thrift from latest build
nberardi authored
85 public static string DescribeVersion(Server server)
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
86 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
87 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
88 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
89 string response = session.GetClient(setKeyspace: false).describe_version();
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
90 return response;
91 }
92 }
93
50d830b @nberardi added thrift from latest build
nberardi authored
94 public static string DescribePartitioner(Server server)
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
95 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
96 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
97 {
ce017c8 @nberardi all tests working for 0.7
nberardi authored
98 string response = session.GetClient(setKeyspace: false).describe_partitioner();
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
99 return response;
100 }
101 }
102
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
103 public static string DescribeSnitch(Server server)
104 {
105 using (var session = new CassandraSession(new ConnectionBuilder(null, server.Host, server.Port)))
106 {
107 string response = session.GetClient(setKeyspace: false).describe_snitch();
108 return response;
109 }
110 }
111
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
112 #endregion
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
113
114 private bool _disposed;
115 private IConnection _connection;
116
117 public CassandraSession()
118 : this(CassandraContext.CurrentConnectionBuilder) { }
119
120 public CassandraSession(ConnectionBuilder connectionBuilder)
121 : this(ConnectionProviderFactory.Get(connectionBuilder), connectionBuilder.ReadConsistency, connectionBuilder.WriteConsistency) { }
122
123 public CassandraSession(ConsistencyLevel read, ConsistencyLevel write)
124 : this(ConnectionProviderFactory.Get(CassandraContext.CurrentConnectionBuilder), read, write) { }
125
126 public CassandraSession(IConnectionProvider connectionProvider, ConsistencyLevel read, ConsistencyLevel write)
127 {
128 if (Current != null)
129 throw new CassandraException("Cannot create a new session while there is one already active.");
130
131 ConnectionProvider = connectionProvider;
132 ReadConsistency = read;
133 WriteConsistency = write;
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
134 Keyspace = new CassandraKeyspace(connectionProvider.Builder.Keyspace);
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
135
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection st…
nberardi authored
136 IsAuthenticated = false;
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
137 Current = this;
138 }
139
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
140 public IConnectionProvider ConnectionProvider { get; private set; }
141
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
142 public ConsistencyLevel ReadConsistency { get; private set; }
143
144 public ConsistencyLevel WriteConsistency { get; private set; }
145
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
146 public CassandraKeyspace Keyspace { get; private set; }
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
147
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection st…
nberardi authored
148 public bool IsAuthenticated { get; private set; }
149
321da49 @nberardi refactoring of way operations happen, and created a wrapper for the c…
nberardi authored
150 internal CassandraClientWrapper GetClient(bool setKeyspace = true)
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
151 {
50d830b @nberardi added thrift from latest build
nberardi authored
152 if (_connection == null)
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
153 _connection = ConnectionProvider.Open();
50d830b @nberardi added thrift from latest build
nberardi authored
154
155 if (!_connection.IsOpen)
156 _connection.Open();
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
157
ce017c8 @nberardi all tests working for 0.7
nberardi authored
158 if (setKeyspace)
23a0cbf @nberardi fixed build error
nberardi authored
159 _connection.SetKeyspace(Keyspace.KeyspaceName);
ce017c8 @nberardi all tests working for 0.7
nberardi authored
160
321da49 @nberardi refactoring of way operations happen, and created a wrapper for the c…
nberardi authored
161 return new CassandraClientWrapper(_connection.Client);
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
162 }
163
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection st…
nberardi authored
164 public void Login(string username, string password)
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
165 {
e93edc7 @nberardi improved the login API, and allowed it to be set in the connection st…
nberardi authored
166 var auth = new AuthenticationRequest {
167 Credentials = new Dictionary<string, string> { { "username", username }, { "password", password } }
168 };
169
170 try
171 {
172 GetClient().login(auth);
173 IsAuthenticated = true;
174 }
175 catch (Exception exc)
176 {
177 IsAuthenticated = false;
178 throw new CassandraException("Login failed.", exc);
179 }
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
180 }
181
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
182 #region IDisposable Members
183
184 /// <summary>
185 ///
186 /// </summary>
187 public void Dispose()
188 {
189 Dispose(true);
046825e @nberardi updated connections to have a more standarding naming connvetion with…
nberardi authored
190 GC.SuppressFinalize(this);
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
191 }
192
193 /// <summary>
194 /// The dispose.
195 /// </summary>
196 /// <param name="disposing">
197 /// The disposing.
198 /// </param>
199 protected virtual void Dispose(bool disposing)
200 {
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
201 if (!_disposed && disposing)
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
202 {
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
203 if (_connection != null)
204 ConnectionProvider.Close(_connection);
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
205
206 if (Current == this)
207 Current = null;
208 }
209
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
210 _disposed = true;
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
211 }
212
213 /// <summary>
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
214 /// Finalizes an instance of the <see cref="CassandraSession"/> class.
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
215 /// </summary>
216 ~CassandraSession()
217 {
365b683 @nberardi still working on getting the app to compile after the breaking change…
nberardi authored
218 Dispose(false);
e4efc4c @nberardi added support for session based configurations, as well as dynamic pr…
nberardi authored
219 }
220
221 #endregion
222 }
223 }
Something went wrong with that request. Please try again.