/// <summary>
 /// Creates request to update a mapping in given shard map.
 /// </summary>
 /// <param name="operationCode">Operation code.</param>
 /// <param name="shardMapManager">Shard map manager object.</param>
 /// <param name="operationId">Operation Id.</param>
 /// <param name="undoStartState">Undo start state.</param>
 /// <param name="root">Xml representation of the object.</param>
 /// <param name="originalShardVersionRemoves">Original shard version for removes.</param>
 /// <param name="originalShardVersionAdds">Original shard version for adds.</param>
 /// <returns>The store operation.</returns>
 public virtual IStoreOperation CreateUpdateMappingOperation(
     StoreOperationCode operationCode,
     ShardMapManager shardMapManager,
     Guid operationId,
     StoreOperationState undoStartState,
     XElement root,
     Guid originalShardVersionRemoves,
     Guid originalShardVersionAdds)
 {
     return(new UpdateMappingOperation(
                shardMapManager,
                operationId,
                undoStartState,
                operationCode,
                StoreObjectFormatterXml.ReadIStoreShardMap(root.Element("ShardMap")),
                StoreObjectFormatterXml.ReadIStoreMapping(
                    root.Element("Steps").Element("Step").Element("Mapping"),
                    root.Element("Removes").Element("Shard")),
                StoreObjectFormatterXml.ReadIStoreMapping(
                    root.Element("Steps").Element("Step").Element("Update").Element("Mapping"),
                    root.Element("Adds").Element("Shard")),
                root.Element("PatternForKill").Value,
                StoreObjectFormatterXml.ReadLock(root.Element("Steps").Element("Step").Element("Lock")),
                originalShardVersionRemoves,
                originalShardVersionAdds));
 }
 /// <summary>
 /// Creates request to remove shard from given shard map.
 /// </summary>
 /// <param name="shardMapManager">Shard map manager object.</param>
 /// <param name="operationId">Operation Id.</param>
 /// <param name="undoStartState">Undo start state.</param>
 /// <param name="root">Xml representation of the object.</param>
 /// <returns>The store operation.</returns>
 public virtual IStoreOperation CreateRemoveShardOperation(
     ShardMapManager shardMapManager,
     Guid operationId,
     StoreOperationState undoStartState,
     XElement root)
 {
     return(new RemoveShardOperation(
                shardMapManager,
                operationId,
                undoStartState,
                StoreObjectFormatterXml.ReadIStoreShardMap(root.Element("ShardMap")),
                StoreObjectFormatterXml.ReadIStoreShard(root.Element("Steps").Element("Step").Element("Shard"))));
 }
 /// <summary>
 /// Creates request to add a mapping in given shard map.
 /// </summary>
 /// <param name="operationCode">Operation code.</param>
 /// <param name="shardMapManager">Shard map manager object.</param>
 /// <param name="operationId">Operation Id.</param>
 /// <param name="undoStartState">Undo start state.</param>
 /// <param name="root">Xml representation of the object.</param>
 /// <param name="originalShardVersionAdds">Original shard version.</param>
 /// <returns>The store operation.</returns>
 public virtual IStoreOperation CreateAddMappingOperation(
     StoreOperationCode operationCode,
     ShardMapManager shardMapManager,
     Guid operationId,
     StoreOperationState undoStartState,
     XElement root,
     Guid originalShardVersionAdds)
 {
     return(new AddMappingOperation(
                shardMapManager,
                operationId,
                undoStartState,
                operationCode,
                StoreObjectFormatterXml.ReadIStoreShardMap(root.Element("ShardMap")),
                StoreObjectFormatterXml.ReadIStoreMapping(
                    root.Element("Steps").Element("Step").Element("Mapping"),
                    root.Element("Adds").Element("Shard")),
                originalShardVersionAdds));
 }
        /// <summary>
        /// Creates request to replace a set of mappings with new set in given shard map.
        /// </summary>
        /// <param name="operationCode">Operation code.</param>
        /// <param name="shardMapManager">Shard map manager object.</param>
        /// <param name="operationId">Operation Id.</param>
        /// <param name="undoStartState">Undo start state.</param>
        /// <param name="root">Xml representation of the object.</param>
        /// <param name="originalShardVersionAdds">Original shard Id of source.</param>
        /// <returns>The store operation.</returns>
        public virtual IStoreOperation CreateReplaceMappingsOperation(
            StoreOperationCode operationCode,
            ShardMapManager shardMapManager,
            Guid operationId,
            StoreOperationState undoStartState,
            XElement root,
            Guid originalShardVersionAdds)
        {
            XElement removeShard = root.Element("Removes").Element("Shard");
            XElement addShard    = root.Element("Adds").Element("Shard");

            return(new ReplaceMappingsOperation(
                       shardMapManager,
                       operationId,
                       undoStartState,
                       operationCode,
                       StoreObjectFormatterXml.ReadIStoreShardMap(root.Element("ShardMap")),
                       root.Element("Steps").Elements("Step").Where(e => e.Attribute("Kind").Value == "1").Select(
                           xe => new
            {
                Index = Int32.Parse(xe.Attribute("Id").Value),
                Mapping = StoreObjectFormatterXml.ReadIStoreMapping(xe.Element("Mapping"), removeShard),
                Lock = StoreObjectFormatterXml.ReadLock(xe.Element("Lock"))
            })
                       .OrderBy(el => el.Index)
                       .Select(el => new Tuple <IStoreMapping, Guid>(el.Mapping, el.Lock))
                       .ToArray(),
                       root.Element("Steps").Elements("Step").Where(e => e.Attribute("Kind").Value == "3").Select(
                           xe => new
            {
                Index = Int32.Parse(xe.Attribute("Id").Value),
                Mapping = StoreObjectFormatterXml.ReadIStoreMapping(xe.Element("Mapping"), addShard),
                Lock = StoreObjectFormatterXml.ReadLock(xe.Element("Lock"))
            })
                       .OrderBy(el => el.Index)
                       .Select(el => new Tuple <IStoreMapping, Guid>(el.Mapping, el.Lock))
                       .ToArray(),
                       originalShardVersionAdds));
        }