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
/
FluentSuperColumn.cs
312 lines (260 loc) · 7.12 KB
/
FluentSuperColumn.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.Collections.Generic;
using System.Linq;
using FluentCassandra.Types;
namespace FluentCassandra
{
[Obsolete("Use \"FluentSuperColumn\" class with out generic type")]
public class FluentSuperColumn<CompareWith, CompareSubcolumnWith> : FluentSuperColumn
where CompareWith : CassandraObject
where CompareSubcolumnWith : CassandraObject
{
public FluentSuperColumn()
: base(new CassandraColumnSchema {
NameType = typeof(CompareWith),
ValueType = typeof(CompareSubcolumnWith)
}) { }
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public class FluentSuperColumn : FluentRecord<FluentColumn>, IFluentBaseColumn, IFluentRecordExpression
{
private CassandraObject _name;
private FluentColumnParent _parent;
private FluentSuperColumnFamily _family;
private FluentColumnList<FluentColumn> _columns;
private CassandraColumnSchema _schema;
/// <summary>
///
/// </summary>
public FluentSuperColumn(CassandraColumnSchema schema = null)
{
SetSchema(schema);
_columns = new FluentColumnList<FluentColumn>(GetPath());
}
/// <summary>
///
/// </summary>
/// <param name="columns"></param>
internal FluentSuperColumn(CassandraColumnSchema schema, IEnumerable<FluentColumn> columns)
{
SetSchema(schema);
_columns = new FluentColumnList<FluentColumn>(GetPath(), columns);
}
/// <summary>
/// The column name.
/// </summary>
public CassandraObject ColumnName
{
get { return _name; }
set { _name = value.GetValue(GetSchema().NameType); }
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public FluentColumn CreateColumn()
{
return new FluentColumn(GetColumnSchema(""));
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public FluentColumn CreateColumn(CassandraObject name)
{
return new FluentColumn(GetColumnSchema(name)) {
ColumnName = name
};
}
/// <summary>
/// The columns in the super column.
/// </summary>
public override IList<FluentColumn> Columns
{
get { return _columns; }
}
/// <summary>
///
/// </summary>
public FluentSuperColumnFamily Family
{
get
{
if (_family == null && _parent != null)
_family = _parent.ColumnFamily as FluentSuperColumnFamily;
return _family;
}
internal set
{
_family = value;
UpdateParent(GetParent());
}
}
/// <summary>
///
/// </summary>
/// <param name="columnName"></param>
/// <returns></returns>
public CassandraObject this[CassandraObject columnName]
{
get
{
object value;
if (!TryGetColumn(columnName, out value))
throw new CassandraException(String.Format("Column, {0}, could not be found.", columnName));
return (CassandraObject)value;
}
set
{
TrySetColumn(columnName, value);
}
}
/// <summary>
///
/// </summary>
public CassandraColumnSchema GetSchema()
{
if (_schema == null)
_schema = new CassandraColumnSchema { Name = ColumnName, ValueType = typeof(BytesType) };
return _schema;
}
/// <summary>
///
/// </summary>
/// <param name="schema"></param>
public void SetSchema(CassandraColumnSchema schema)
{
if (schema == null)
schema = new CassandraColumnSchema { Name = ColumnName, ValueType = typeof(BytesType) };
_schema = schema;
}
/// <summary>
/// Gets the path.
/// </summary>
/// <returns></returns>
public FluentColumnPath GetPath()
{
return new FluentColumnPath(Family, this, (FluentColumn)null);
}
/// <summary>
/// Gets the parent.
/// </summary>
/// <returns></returns>
public FluentColumnParent GetParent()
{
return _parent ?? new FluentColumnParent(Family, this);
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="result"></param>
/// <returns></returns>
private CassandraObject GetColumnValue(object name)
{
var col = Columns.FirstOrDefault(c => c.ColumnName == name);
if (col == null)
return NullType.Value;
var schema = GetColumnSchema(name);
return col.ColumnValue.GetValue(schema.ValueType);
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private CassandraColumnSchema GetColumnSchema(object name)
{
var schema = GetSchema();
var valueType = CassandraType.BytesType;
if (Family != null)
valueType = Family.GetSchema().DefaultColumnValueType;
// mock up a fake schema to send to the fluent column
return new CassandraColumnSchema { NameType = schema.ValueType, ValueType = valueType };
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="result"></param>
/// <returns></returns>
public override bool TryGetColumn(object name, out object result)
{
result = GetColumnValue(name);
return !(result is NullType);
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
public override bool TrySetColumn(object name, object value)
{
var col = Columns.FirstOrDefault(c => c.ColumnName == name);
var mutationType = MutationType.Changed;
// if column doesn't exist create it and add it to the columns
if (col == null)
{
var schema = GetColumnSchema(name);
mutationType = MutationType.Added;
col = new FluentColumn(schema);
col.ColumnName = CassandraObject.GetCassandraObjectFromObject(name, schema.NameType);
_columns.SupressChangeNotification = true;
_columns.Add(col);
_columns.SupressChangeNotification = false;
}
// set the column value
col.ColumnValue = CassandraObject.GetCassandraObjectFromObject(value);
// notify the tracker that the column has changed
OnColumnMutated(mutationType, col);
return true;
}
public override bool TryRemoveColumn(object name)
{
var col = Columns.FirstOrDefault(c => c.ColumnName == name);
if (col != null)
{
Columns.Remove(col);
return true;
}
var schema = GetColumnSchema(name);
var mutationType = MutationType.Removed;
col = new FluentColumn(schema);
col.ColumnName = CassandraObject.GetCassandraObjectFromObject(name, schema.NameType);
col.SetParent(GetPath());
// notify the tracker that the column has changed
OnColumnMutated(mutationType, col);
return true;
}
public void SetParent(FluentColumnParent parent)
{
UpdateParent(parent);
}
private void UpdateParent(FluentColumnParent parent)
{
if (!(parent.ColumnFamily is FluentSuperColumnFamily))
throw new ArgumentException("ColumnFamily must be of type FluentSuperColumnFamily.", "parent");
_parent = parent;
var columnParent = GetPath();
_columns.Parent = columnParent;
foreach (var col in Columns)
col.SetParent(columnParent);
if (!SuppressMutationTracking) {
MutationTracker.Clear();
foreach (var col in Columns)
MutationTracker.ColumnMutated(MutationType.Added, col);
}
}
#region IFluentBaseColumn Members
IFluentBaseColumnFamily IFluentBaseColumn.Family { get { return Family; } }
#endregion
public override string ToString()
{
return String.Format("{0} = {1} columns", ColumnName, Columns.Count);
}
}
}