Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 169 lines (143 sloc) 4.312 kB
1adec35 @nberardi connections now are object oriented, and basic keyspace and column fa…
nberardi authored
1 using System;
2 using System.Collections.Generic;
b9d2f58 @nberardi all tests pass after most recent changes
nberardi authored
3 using System.Diagnostics;
1adec35 @nberardi connections now are object oriented, and basic keyspace and column fa…
nberardi authored
4 using System.Linq;
5 using Apache.Cassandra;
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
6 using FluentCassandra.Operations;
b9d2f58 @nberardi all tests pass after most recent changes
nberardi authored
7 using FluentCassandra.Types;
1adec35 @nberardi connections now are object oriented, and basic keyspace and column fa…
nberardi authored
8
9 namespace FluentCassandra
10 {
11 public class CassandraKeyspace
12 {
13 private readonly string _keyspaceName;
bfe118e @nberardi got rid of the CassandraSession.Current since it was an anti-pattern …
nberardi authored
14 private readonly CassandraContext _context;
15
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
16 private CassandraKeyspaceSchema _cachedSchema;
1adec35 @nberardi connections now are object oriented, and basic keyspace and column fa…
nberardi authored
17
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
18 public CassandraKeyspace(string keyspaceName, CassandraContext context)
fc725a6 @nberardi fixed issues discovered while running sandbox
nberardi authored
19 {
20 if (keyspaceName == null)
21 throw new ArgumentNullException("keyspaceName");
22
23 _keyspaceName = keyspaceName;
24 _context = context;
25 }
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
26
27 public CassandraKeyspace(CassandraKeyspaceSchema schema, CassandraContext context)
1adec35 @nberardi connections now are object oriented, and basic keyspace and column fa…
nberardi authored
28 {
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
29 if (schema == null)
30 throw new ArgumentNullException("schema");
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
31
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
32 if (schema.Name == null)
33 throw new ArgumentException("Must specify the keyspace name.");
34
35 _keyspaceName = schema.Name;
36 _cachedSchema = schema;
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
37 _context = context;
1adec35 @nberardi connections now are object oriented, and basic keyspace and column fa…
nberardi authored
38 }
39
40 /// <summary>
41 ///
42 /// </summary>
43 public string KeyspaceName
44 {
45 get { return _keyspaceName; }
46 }
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
47
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
48 /// <summary>
49 ///
50 /// </summary>
51 /// <returns></returns>
8c2e9f5 @nberardi composite types are now working
nberardi authored
52 public override string ToString()
53 {
54 return KeyspaceName;
55 }
56
b9d2f58 @nberardi all tests pass after most recent changes
nberardi authored
57 public void TryCreateSelf()
21aa083 @nberardi updated sandbox program to be broken up into logical parts
nberardi authored
58 {
d576460 @nberardi made the type system more robus to support parsing of complex types f…
nberardi authored
59 var schema = GetSchema();
60
61 try
21aa083 @nberardi updated sandbox program to be broken up into logical parts
nberardi authored
62 {
ea9226f @nberardi added support for CQL 3 and selecting the CQL version
nberardi authored
63 string result = _context.AddKeyspace(schema);
d576460 @nberardi made the type system more robus to support parsing of complex types f…
nberardi authored
64 Debug.WriteLine(result, "keyspace setup");
65 }
3b61e2b @nberardi moved from the obsolete property replication_factor on the KsDef obje…
nberardi authored
66 catch(Exception exc)
d576460 @nberardi made the type system more robus to support parsing of complex types f…
nberardi authored
67 {
bfe118e @nberardi got rid of the CassandraSession.Current since it was an anti-pattern …
nberardi authored
68 if (_context.ThrowErrors)
69 throw exc;
21aa083 @nberardi updated sandbox program to be broken up into logical parts
nberardi authored
70 }
71 }
72
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
73 public void TryCreateColumnFamily(CassandraColumnFamilySchema schema)
21aa083 @nberardi updated sandbox program to be broken up into logical parts
nberardi authored
74 {
b9d2f58 @nberardi all tests pass after most recent changes
nberardi authored
75 try
76 {
ea9226f @nberardi added support for CQL 3 and selecting the CQL version
nberardi authored
77 schema.KeyspaceName = KeyspaceName;
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
78
ea9226f @nberardi added support for CQL 3 and selecting the CQL version
nberardi authored
79 string result = _context.AddColumnFamily(schema);
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
80 Debug.WriteLine(result, "column family setup");
b9d2f58 @nberardi all tests pass after most recent changes
nberardi authored
81 }
3b61e2b @nberardi moved from the obsolete property replication_factor on the KsDef obje…
nberardi authored
82 catch (Exception exc)
21aa083 @nberardi updated sandbox program to be broken up into logical parts
nberardi authored
83 {
bfe118e @nberardi got rid of the CassandraSession.Current since it was an anti-pattern …
nberardi authored
84 if (_context.ThrowErrors)
85 throw exc;
21aa083 @nberardi updated sandbox program to be broken up into logical parts
nberardi authored
86 }
87 }
88
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
89 [Obsolete("Use \"TryCreateColumnFamily\" class with out generic type")]
90 public void TryCreateColumnFamily<CompareWith>(string columnFamilyName)
d576460 @nberardi made the type system more robus to support parsing of complex types f…
nberardi authored
91 where CompareWith : CassandraObject
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
92 {
93 TryCreateColumnFamily(new CassandraColumnFamilySchema {
94 ColumnNameType = typeof(CompareWith),
95 FamilyName = columnFamilyName
96 });
97 }
98
99 [Obsolete("Use \"TryCreateColumnFamily\" class with out generic type")]
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
100 public void TryCreateColumnFamily<CompareWith, CompareSubcolumnWith>(string columnFamilyName)
d576460 @nberardi made the type system more robus to support parsing of complex types f…
nberardi authored
101 where CompareWith : CassandraObject
102 where CompareSubcolumnWith : CassandraObject
21aa083 @nberardi updated sandbox program to be broken up into logical parts
nberardi authored
103 {
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
104 TryCreateColumnFamily(new CassandraColumnFamilySchema(type: ColumnType.Super) {
105 ColumnNameType = typeof(CompareWith),
106 SuperColumnNameType = typeof(CompareSubcolumnWith),
107 FamilyName = columnFamilyName
108 });
21aa083 @nberardi updated sandbox program to be broken up into logical parts
nberardi authored
109 }
110
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
111 public KsDef GetDescription()
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
112 {
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
113 return _context.ExecuteOperation(new SimpleOperation<Apache.Cassandra.KsDef>(ctx => {
114 return ctx.Session.GetClient().describe_keyspace(KeyspaceName);
115 }));
116 }
117
fdc415c @nberardi cleaned up the object conventions and also implimented the use of sch…
nberardi authored
118 public CassandraColumnFamilySchema GetColumnFamilySchema(string columnFamily, bool onlyCheckCache = false)
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
119 {
fdc415c @nberardi cleaned up the object conventions and also implimented the use of sch…
nberardi authored
120 if (onlyCheckCache && _cachedSchema == null)
121 return null;
122
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
123 return GetSchema().ColumnFamilies.FirstOrDefault(cf => cf.FamilyName == columnFamily);
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
124 }
125
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
126 public bool ColumnFamilyExists(string columnFamilyName)
ce017c8 @nberardi all tests working for 0.7
nberardi authored
127 {
ff16fb5 @nberardi supporting key aliases in CQL
nberardi authored
128 return GetSchema().ColumnFamilies.Any(cf => String.Equals(cf.FamilyName, columnFamilyName, StringComparison.OrdinalIgnoreCase));
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
129 }
130
fc725a6 @nberardi fixed issues discovered while running sandbox
nberardi authored
131 public void ClearCachedKeyspaceSchema()
7ba4ce5 @nberardi added support for a couple more features that were added to the clien…
nberardi authored
132 {
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
133 _cachedSchema = null;
ce017c8 @nberardi all tests working for 0.7
nberardi authored
134 }
135
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
136 #region Cassandra Keyspace Server Operations
ce017c8 @nberardi all tests working for 0.7
nberardi authored
137
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
138 public CassandraKeyspaceSchema GetSchema()
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
139 {
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
140 if (_cachedSchema == null)
fc725a6 @nberardi fixed issues discovered while running sandbox
nberardi authored
141 try
142 {
143 _cachedSchema = new CassandraKeyspaceSchema(_context.ExecuteOperation(new SimpleOperation<Apache.Cassandra.KsDef>(ctx => {
144 return ctx.Session.GetClient().describe_keyspace(KeyspaceName);
145 })));
146 }
ff16fb5 @nberardi supporting key aliases in CQL
nberardi authored
147 catch (CassandraOperationException exc)
fc725a6 @nberardi fixed issues discovered while running sandbox
nberardi authored
148 {
ff16fb5 @nberardi supporting key aliases in CQL
nberardi authored
149 Debug.WriteLine(exc);
fc725a6 @nberardi fixed issues discovered while running sandbox
nberardi authored
150 _cachedSchema = new CassandraKeyspaceSchema {
151 Name = KeyspaceName
152 };
153 }
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
154
4526a65 @nberardi some much needed refactoring of the schema
nberardi authored
155 return _cachedSchema;
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
156 }
157
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
158 public IEnumerable<CassandraTokenRange> DescribeRing()
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
159 {
3254baf @nberardi cleaned up session, context, and keyspace objects
nberardi authored
160 return _context.ExecuteOperation(new SimpleOperation<IEnumerable<CassandraTokenRange>>(ctx => {
161 var tokenRanges = ctx.Session.GetClient(setKeyspace: false).describe_ring(KeyspaceName);
162 return tokenRanges.Select(tr => new CassandraTokenRange(tr.Start_token, tr.End_token, tr.Endpoints));
163 }));
1651f02 @nberardi added descriptive opperations so that you can query the server for in…
nberardi authored
164 }
165
166 #endregion
1adec35 @nberardi connections now are object oriented, and basic keyspace and column fa…
nberardi authored
167 }
168 }
Something went wrong with that request. Please try again.