This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
CassandraKeyspace.cs
168 lines (143 loc) · 4.23 KB
/
CassandraKeyspace.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using FluentCassandra.Apache.Cassandra;
using FluentCassandra.Operations;
using FluentCassandra.Types;
namespace FluentCassandra
{
public class CassandraKeyspace
{
private readonly string _keyspaceName;
private readonly CassandraContext _context;
private CassandraKeyspaceSchema _cachedSchema;
public CassandraKeyspace(string keyspaceName, CassandraContext context)
{
if (keyspaceName == null)
throw new ArgumentNullException("keyspaceName");
_keyspaceName = keyspaceName;
_context = context;
}
public CassandraKeyspace(CassandraKeyspaceSchema schema, CassandraContext context)
{
if (schema == null)
throw new ArgumentNullException("schema");
if (schema.Name == null)
throw new ArgumentException("Must specify the keyspace name.");
_keyspaceName = schema.Name;
_cachedSchema = schema;
_context = context;
}
/// <summary>
///
/// </summary>
public string KeyspaceName
{
get { return _keyspaceName; }
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
return KeyspaceName;
}
public void TryCreateSelf()
{
var schema = GetSchema();
try
{
string result = _context.AddKeyspace(schema);
Debug.WriteLine(result, "keyspace setup");
}
catch(Exception exc)
{
if (_context.ThrowErrors)
throw exc;
}
}
public void TryCreateColumnFamily(CassandraColumnFamilySchema schema)
{
try
{
schema.KeyspaceName = KeyspaceName;
string result = _context.AddColumnFamily(schema);
Debug.WriteLine(result, "column family setup");
}
catch (Exception exc)
{
if (_context.ThrowErrors)
throw exc;
}
}
[Obsolete("Use \"TryCreateColumnFamily\" class with out generic type")]
public void TryCreateColumnFamily<CompareWith>(string columnFamilyName)
where CompareWith : CassandraObject
{
TryCreateColumnFamily(new CassandraColumnFamilySchema {
ColumnNameType = typeof(CompareWith),
FamilyName = columnFamilyName
});
}
[Obsolete("Use \"TryCreateColumnFamily\" class with out generic type")]
public void TryCreateColumnFamily<CompareWith, CompareSubcolumnWith>(string columnFamilyName)
where CompareWith : CassandraObject
where CompareSubcolumnWith : CassandraObject
{
TryCreateColumnFamily(new CassandraColumnFamilySchema(type: ColumnType.Super) {
ColumnNameType = typeof(CompareWith),
SuperColumnNameType = typeof(CompareSubcolumnWith),
FamilyName = columnFamilyName
});
}
public KsDef GetDescription()
{
return _context.ExecuteOperation(new SimpleOperation<Apache.Cassandra.KsDef>(ctx => {
return ctx.Session.GetClient().describe_keyspace(KeyspaceName);
}));
}
public CassandraColumnFamilySchema GetColumnFamilySchema(string columnFamily, bool onlyCheckCache = false)
{
if (onlyCheckCache && _cachedSchema == null)
return null;
return GetSchema().ColumnFamilies.FirstOrDefault(cf => cf.FamilyName == columnFamily);
}
public bool ColumnFamilyExists(string columnFamilyName)
{
return GetSchema().ColumnFamilies.Any(cf => String.Equals(cf.FamilyName, columnFamilyName, StringComparison.OrdinalIgnoreCase));
}
public void ClearCachedKeyspaceSchema()
{
_cachedSchema = null;
}
#region Cassandra Keyspace Server Operations
public CassandraKeyspaceSchema GetSchema()
{
if (_cachedSchema == null)
try
{
_cachedSchema = new CassandraKeyspaceSchema(_context.ExecuteOperation(new SimpleOperation<Apache.Cassandra.KsDef>(ctx => {
return ctx.Session.GetClient().describe_keyspace(KeyspaceName);
})));
}
catch (CassandraOperationException exc)
{
Debug.WriteLine(exc);
_cachedSchema = new CassandraKeyspaceSchema {
Name = KeyspaceName
};
}
return _cachedSchema;
}
public IEnumerable<CassandraTokenRange> DescribeRing()
{
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
}
}