Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 426 lines (358 sloc) 12.837 kb
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Apache.Cassandra;
using FluentCassandra.Types;
using System.IO;
using System.IO.Compression;

namespace FluentCassandra.Operations
{
internal static class Helper
{
public static List<byte[]> ToByteArrayList(List<CassandraObject> list)
{
return list.Select(x => x.TryToBigEndian()).ToList();
}

public static KeyRange CreateKeyRange(CassandraKeyRange range)
{
if (range.StartKey == null && range.EndKey == null && range.StartToken == null && range.EndToken == null)
range = new CassandraKeyRange("", "", null, null, range.Count);

return new KeyRange {
Start_key = range.StartKey.TryToBigEndian(),
End_key = range.EndKey.TryToBigEndian(),
Start_token = range.StartToken,
End_token = range.EndToken,
Count = range.Count
};
}

public static IndexClause CreateIndexClause(CassandraIndexClause index)
{
return new IndexClause {
Start_key = index.StartKey.TryToBigEndian(),
Count = index.Count,
Expressions = index.CompiledExpressions
};
}

public static ColumnParent CreateColumnParent(CassandraColumnParent parent)
{
return new ColumnParent {
Column_family = parent.ColumnFamily,
Super_column = parent.SuperColumn.TryToBigEndian()
};
}

public static ColumnPath CreateColumnPath(CassandraColumnPath path)
{
return new ColumnPath {
Column = path.Column.TryToBigEndian(),
Column_family = path.ColumnFamily,
Super_column = path.SuperColumn.TryToBigEndian()
};
}

public static CassandraSlicePredicate SetSchemaForSlicePredicate(CassandraSlicePredicate predicate, CassandraColumnFamilySchema schema, bool forSuperColumn = false)
{
CassandraType columnType = forSuperColumn ? schema.SuperColumnNameType : schema.ColumnNameType;

if (predicate is CassandraRangeSlicePredicate)
{
var x = (CassandraRangeSlicePredicate)predicate;
var start = CassandraObject.GetCassandraObjectFromObject(x.Start, columnType);
var finish = CassandraObject.GetCassandraObjectFromObject(x.Finish, columnType);

return new CassandraRangeSlicePredicate(start, finish, x.Reversed, x.Count);
}
else if (predicate is CassandraColumnSlicePredicate)
{
var x = (CassandraColumnSlicePredicate)predicate;
var cols = x.Columns.ToList();

for (int i = 0; i < cols.Count; i++)
cols[i] = CassandraObject.GetCassandraObjectFromObject(cols[i], columnType);

return new CassandraColumnSlicePredicate(cols);
}

return null;
}

public static SlicePredicate CreateSlicePredicate(CassandraSlicePredicate predicate)
{
if (predicate is CassandraRangeSlicePredicate)
{
var x = (CassandraRangeSlicePredicate)predicate;
return new SlicePredicate {
Slice_range = new SliceRange {
Start = x.Start.TryToBigEndian() ?? new byte[0],
Finish = x.Finish.TryToBigEndian() ?? new byte[0],
Reversed = x.Reversed,
Count = x.Count
}
};
}
else if (predicate is CassandraColumnSlicePredicate)
{
var x = (CassandraColumnSlicePredicate)predicate;
return new SlicePredicate {
Column_names = x.Columns.Select(o => o.TryToBigEndian()).ToList()
};
}

return null;
}

public static Column CreateColumn(CassandraColumn column)
{
var ccol = new Column {
Name = column.Name.TryToBigEndian(),
Value = column.Value.TryToBigEndian(),
Timestamp = column.Timestamp.ToCassandraTimestamp()
};

if (column.Ttl.HasValue && column.Ttl.Value > 0)
ccol.Ttl = column.Ttl.Value;

return ccol;
}

public static CounterColumn CreateCounterColumn(CassandraCounterColumn column)
{
return new CounterColumn {
Name = column.Name.TryToBigEndian(),
Value = column.Value
};
}

public static byte[] TryToBigEndian(this CassandraObject value)
{
if (value == null)
return null;

return value.ToBigEndian();
}

public static IFluentBaseColumn ConvertToFluentBaseColumn(ColumnOrSuperColumn col, CassandraColumnFamilySchema schema = null)
{
if (col.Super_column != null)
return ConvertSuperColumnToFluentSuperColumn(col.Super_column, schema);
else if (col.Column != null)
return ConvertColumnToFluentColumn(col.Column, schema);
else if (col.Counter_super_column != null)
return ConvertColumnToFluentCounterColumn(col.Counter_column, schema);
else if (col.Counter_column != null)
return ConvertSuperColumnToFluentCounterSuperColumn(col.Counter_super_column, schema);
else
return null;
}

public static FluentCounterColumn ConvertColumnToFluentCounterColumn(CounterColumn col, CassandraColumnFamilySchema schema = null)
{
var colSchema = new CassandraColumnSchema();

if (schema != null)
{
var name = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Name, schema.ColumnNameType);
colSchema = schema.Columns.Where(x => x.Name == name).FirstOrDefault();

if (colSchema == null)
{
colSchema = new CassandraColumnSchema();
colSchema.NameType = schema.ColumnNameType;
colSchema.Name = name;
colSchema.ValueType = schema.DefaultColumnValueType;
}
}

var fcol = new FluentCounterColumn(colSchema) {
ColumnName = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Name, colSchema.NameType),
ColumnValue = col.Value
};

return fcol;
}

public static FluentColumn ConvertColumnToFluentColumn(Column col, CassandraColumnFamilySchema schema = null)
{
var colSchema = new CassandraColumnSchema();

if (schema != null)
{
var name = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Name, schema.ColumnNameType);
colSchema = schema.Columns.Where(x => x.Name == name).FirstOrDefault();

if (colSchema == null)
{
colSchema = new CassandraColumnSchema();
colSchema.NameType = schema.ColumnNameType;
colSchema.Name = name;
colSchema.ValueType = schema.DefaultColumnValueType;
}
}

return ConvertColumnToFluentColumn(col, colSchema);
}

public static FluentColumn ConvertColumnToFluentColumn(Column col, CassandraColumnSchema colSchema)
{
colSchema = colSchema ?? new CassandraColumnSchema();

var fcol = new FluentColumn(colSchema) {
ColumnName = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Name, colSchema.NameType),
ColumnValue = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Value, colSchema.ValueType),
ColumnTimestamp = TimestampHelper.FromCassandraTimestamp(col.Timestamp),
};

if (col.__isset.ttl)
fcol.ColumnSecondsUntilDeleted = col.Ttl;

return fcol;
}

public static FluentSuperColumn ConvertSuperColumnToFluentCounterSuperColumn(CounterSuperColumn col, CassandraColumnFamilySchema schema = null)
{
var superColSchema = new CassandraColumnSchema {
Name = col.Name
};

if (schema != null)
superColSchema = new CassandraColumnSchema {
NameType = schema.SuperColumnNameType,
Name = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Name, schema.SuperColumnNameType),
ValueType = schema.ColumnNameType
};

var superCol = new FluentSuperColumn(superColSchema) {
ColumnName = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Name, superColSchema.NameType)
};

((ILoadable)superCol).BeginLoad();
foreach (var xcol in col.Columns)
superCol.Columns.Add(ConvertColumnToFluentCounterColumn(xcol, schema));
((ILoadable)superCol).EndLoad();

return superCol;
}

public static FluentSuperColumn ConvertSuperColumnToFluentSuperColumn(SuperColumn col, CassandraColumnFamilySchema schema = null)
{
var superColSchema = new CassandraColumnSchema {
Name = col.Name
};

if (schema != null)
superColSchema = new CassandraColumnSchema {
NameType = schema.SuperColumnNameType,
Name = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Name, schema.SuperColumnNameType),
ValueType = schema.ColumnNameType
};

var superCol = new FluentSuperColumn(superColSchema) {
ColumnName = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Name, superColSchema.NameType)
};

((ILoadable)superCol).BeginLoad();
foreach (var xcol in col.Columns)
superCol.Columns.Add(ConvertColumnToFluentColumn(xcol, schema));
((ILoadable)superCol).EndLoad();

return superCol;
}

public static IEnumerable<Mutation> CreateDeletedColumnMutation(IEnumerable<FluentMutation> mutation)
{
foreach (var col in mutation)
{
var deletion = new Deletion {
Timestamp = col.ColumnTimestamp.ToCassandraTimestamp(),
Predicate = CreateSlicePredicate(new[] { col.Column.ColumnName })
};

yield return new Mutation {
Deletion = deletion
};
}
}

public static IEnumerable<Mutation> CreateDeletedSuperColumnMutation(IEnumerable<FluentMutation> mutation)
{
foreach (var col in mutation)
{
var superColumn = col.Column.GetPath().SuperColumn.ColumnName.TryToBigEndian();

var deletion = new Deletion {
Timestamp = col.ColumnTimestamp.ToCassandraTimestamp(),
Super_column = superColumn,
Predicate = CreateSlicePredicate(new[] { col.Column.ColumnName })
};

yield return new Mutation {
Deletion = deletion
};
}
}

public static Mutation CreateInsertedOrChangedMutation(FluentMutation mutation)
{
switch (mutation.Type)
{
case MutationType.Added:
case MutationType.Changed:
var column = mutation.Column;

if (column is FluentColumn)
{
return new Mutation {
Column_or_supercolumn = new ColumnOrSuperColumn {
Column = CreateColumn((FluentColumn)column)
}
};
}
else if (column is FluentSuperColumn)
{
var colY = (FluentSuperColumn)column;
var superColumn = new SuperColumn {
Name = colY.ColumnName.TryToBigEndian(),
Columns = new List<Column>()
};

foreach (var col in colY.MutationTracker.GetMutations().Select(x => x.Column).OfType<FluentColumn>())
superColumn.Columns.Add(CreateColumn(col));

return new Mutation {
Column_or_supercolumn = new ColumnOrSuperColumn {
Super_column = superColumn
}
};
}
break;
}

return null;
}

public static Column CreateColumn(FluentColumn column)
{
var col = new Column {
Name = column.ColumnName.TryToBigEndian(),
Value = column.ColumnValue.TryToBigEndian(),
Timestamp = column.ColumnTimestamp.ToCassandraTimestamp()
};

if (column.ColumnSecondsUntilDeleted.HasValue)
col.Ttl = column.ColumnSecondsUntilDeleted.Value;

return col;
}

public static ColumnOrSuperColumn CreateColumnOrSuperColumn(IFluentBaseColumn column)
{
if (column is FluentColumn)
{
return new ColumnOrSuperColumn {
Column = CreateColumn((FluentColumn)column)
};
}
else if (column is FluentSuperColumn)
{
var colY = (FluentSuperColumn)column;
var superColumn = new SuperColumn {
Name = colY.ColumnName.TryToBigEndian(),
Columns = new List<Column>()
};

foreach (var col in colY.Columns.OfType<FluentColumn>())
superColumn.Columns.Add(CreateColumn(col));

return new ColumnOrSuperColumn {
Super_column = superColumn
};
}
else
{
return null;
}
}

public static SlicePredicate CreateSlicePredicate(IEnumerable<CassandraObject> columnNames)
{
return new SlicePredicate {
Column_names = columnNames.Select(o => o.TryToBigEndian()).ToList()
};
}

public static byte[] ZlibCompress(byte[] data)
{
// Note: even though Cassandra calls this GZIP compression it is actually ZLIB compression provided by the
// Java Inflator class http://docs.oracle.com/javase/1.4.2/docs/api/java/util/zip/Inflater.html
// If it wasn't for this post explaining how to get Deflator to mimic ZLIB compression none of the following
// code would work http://tlzprgmr.wordpress.com/2010/03/17/net-deflatestreamzlib-compatibility/
using (MemoryStream outStream = new MemoryStream())
{
// zlib header
outStream.WriteByte(0x58);
outStream.WriteByte(0x85);

// zlib body
using (var compressor = new DeflateStream(outStream, CompressionMode.Compress, true))
compressor.Write(data, 0, data.Length);

// zlib checksum - a naive implementation of adler-32 checksum
const uint A32Mod = 65521;
uint s1 = 1, s2 = 0;
foreach (byte b in data)
{
s1 = (s1 + b) % A32Mod;
s2 = (s2 + s1) % A32Mod;
}

int adler32 = unchecked((int)((s2 << 16) + s1));
outStream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(adler32)), 0, sizeof(uint));

// zlib compatible compressed query
var bytes = outStream.ToArray();
outStream.Close();

return bytes;
}
}
}
}
Something went wrong with that request. Please try again.