Beispiel #1
0
        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);
            }

            var client = Session.GetClient();

            if (Atomic && client.describe_version() >= RpcApiVersion.Cassandra120)
            {
                client.atomic_batch_mutate(
                    mutationMap,
                    Session.WriteConsistency);
            }
            else
            {
                client.batch_mutate(
                    mutationMap,
                    Session.WriteConsistency);
            }

            return(new Void());
        }