Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

67 lines (51 sloc) 2.012 kB
using System;
using System.Collections.Generic;
using System.Linq;
using Apache.Cassandra;
namespace FluentCassandra.Operations
{
public class BatchMutate : Operation<Void>
{
/*
* batch_mutate(keyspace, mutation_map, consistency_level)
*/
public IEnumerable<FluentMutation> Mutations { get; private set; }
public override Void Execute()
{
if (Mutations.Count() == 0)
return new Void();
var mutationMap = new Dictionary<byte[], Dictionary<string, List<Mutation>>>();
foreach (var key in Mutations.GroupBy(x => x.Column.Family.Key))
{
var keyMutations = new Dictionary<string, List<Mutation>>();
foreach (var columnFamily in key.GroupBy(x => x.Column.Family.FamilyName))
{
var columnFamilyMutations = columnFamily
.Where(m => m.Type == MutationType.Added || m.Type == MutationType.Changed)
.Select(m => Helper.CreateInsertedOrChangedMutation(m))
.ToList();
var superColumnsNeedingDeleted = columnFamily
.Where(m => m.Type == MutationType.Removed && m.Column.GetParent().SuperColumn != null);
foreach (var superColumn in superColumnsNeedingDeleted.GroupBy(x => x.Column.GetParent().SuperColumn.ColumnName))
columnFamilyMutations.AddRange(Helper.CreateDeletedSuperColumnMutation(superColumn));
var columnsNeedingDeleted = columnFamily
.Where(m => m.Type == MutationType.Removed && m.Column.GetParent().SuperColumn == null);
if (columnsNeedingDeleted.Count() > 0)
columnFamilyMutations.AddRange(Helper.CreateDeletedColumnMutation(columnsNeedingDeleted));
keyMutations.Add(columnFamily.Key, columnFamilyMutations);
}
mutationMap.Add(key.Key.TryToBigEndian(), keyMutations);
}
// Dictionary<string : key, Dicationary<string : columnFamily, List<Mutation>>>
Session.GetClient().batch_mutate(
mutationMap,
Session.WriteConsistency
);
return new Void();
}
public BatchMutate(IEnumerable<FluentMutation> mutations)
{
Mutations = mutations;
}
}
}
Jump to Line
Something went wrong with that request. Please try again.