コード例 #1
0
 public BatchResult[] Batch(IList<ICommandData> commandDatas)
 {
     if (connection.State != ConnectionState.Open) {
         connection.Open();
     }
     var results = new BatchResult[commandDatas.Count];
     if (commandDatas.Count == 0) return results;
     using (var cmd = connection.CreateCommand()) {
         if (transaction != null) {
             cmd.Transaction = transaction;
         }
         var batchCommandBuilder = new StringBuilder();
         var addParamWithValue = new Action<string, object>((x, y) => cmd.Parameters.AddWithValue(x, y));
         for (int i = 0; i < commandDatas.Count; i++) {
             var command = commandDatas[i];
             if (command is DeleteCommandData) {
                 results[i] = Delete(command as DeleteCommandData, batchCommandBuilder, addParamWithValue, i);
             } else if (command is PutCommandData) {
                 results[i] = Put(command as PutCommandData, batchCommandBuilder, addParamWithValue, i);
             } else {
                 throw new InvalidOperationException("Unknown Command Type: " + command.GetType());
             }
         }
         cmd.CommandText = batchCommandBuilder.ToString();
         var count = cmd.ExecuteNonQuery();
         if (count != commandDatas.Count) {
             throw new InvalidOperationException("Concurrency Exception?");
         }
     }
     return results;
 }
コード例 #2
0
ファイル: CommandExtensions.cs プロジェクト: j2jensen/ravendb
        public static BatchResult ExecuteBatch(this ICommandData self, DocumentDatabase database, string[] participatingIds = null)
        {
            var batchResult = new BatchResult();

            Execute(self, database, batchResult, participatingIds);

            batchResult.Method = self.Method;
            batchResult.Key = self.Key;
            batchResult.Etag = self.Etag;
            batchResult.Metadata = self.Metadata;
            batchResult.AdditionalData = self.AdditionalData;

            return batchResult;
        }
コード例 #3
0
 public BatchResult[] Batch(IList<ICommandData> commandDatas)
 {
     var results = new BatchResult[commandDatas.Count];
     for (int i = 0; i < commandDatas.Count; i++) {
         var command = commandDatas[i];
         if (command is DeleteCommandData) {
             results[i] = Delete(command as DeleteCommandData);
         } else if (command is PutCommandData) {
             results[i] = Put(command as PutCommandData);
         } else {
             throw new InvalidOperationException("Unknown Command Type: " + command.GetType());
         }
     }
     return results;
 }
コード例 #4
0
ファイル: CommandExtensions.cs プロジェクト: j2jensen/ravendb
        private static void Execute(ICommandData self, DocumentDatabase database, BatchResult batchResult, string[] participatingIds = null)
        {
            var deleteCommandData = self as DeleteCommandData;
            if (deleteCommandData != null)
            {
                var result = database.Documents.Delete(deleteCommandData.Key, deleteCommandData.Etag, deleteCommandData.TransactionInformation,participatingIds);

                if (batchResult != null)
                    batchResult.Deleted = result;

                return;
            }

            var putCommandData = self as PutCommandData;
            if (putCommandData != null)
            {
                var putResult = database.Documents.Put(putCommandData.Key, putCommandData.Etag, putCommandData.Document, putCommandData.Metadata, putCommandData.TransactionInformation, participatingIds);
                putCommandData.Etag = putResult.ETag;
                putCommandData.Key = putResult.Key;

                return;
            }

            var patchCommandData = self as PatchCommandData;
            if (patchCommandData != null)
            {
                var result = database.Patches.ApplyPatch(patchCommandData.Key, patchCommandData.Etag,
                                                 patchCommandData.Patches, patchCommandData.PatchesIfMissing, patchCommandData.Metadata,
                                                 patchCommandData.TransactionInformation,
                                                 skipPatchIfEtagMismatch: patchCommandData.SkipPatchIfEtagMismatch, participatingIds: participatingIds);

                if (batchResult != null)
                    batchResult.PatchResult = result.PatchResult;

                var doc = database.Documents.Get(patchCommandData.Key, patchCommandData.TransactionInformation);
                if (doc != null)
                {
                    database.TransactionalStorage.ExecuteImmediatelyOrRegisterForSynchronization(() =>
                    {
                        patchCommandData.Metadata = doc.Metadata;
                        patchCommandData.Etag = doc.Etag;
                    });
                }
                return;
            }

            var advPatchCommandData = self as ScriptedPatchCommandData;
            if (advPatchCommandData != null)
            {
                var result = database.Patches.ApplyPatch(advPatchCommandData.Key, advPatchCommandData.Etag,
                                                 advPatchCommandData.Patch, advPatchCommandData.PatchIfMissing, advPatchCommandData.Metadata,
                                                 advPatchCommandData.TransactionInformation, advPatchCommandData.DebugMode, participatingIds);

                if (batchResult != null)
                    batchResult.PatchResult = result.Item1.PatchResult;

                advPatchCommandData.AdditionalData = new RavenJObject { { "Debug", new RavenJArray(result.Item2) } };
                if(advPatchCommandData.DebugMode)
                {
                    advPatchCommandData.AdditionalData["Document"] = result.Item1.Document;
                    advPatchCommandData.AdditionalData["Actions"] = result.Item1.DebugActions;
                    return;
                }

                var doc = database.Documents.Get(advPatchCommandData.Key, advPatchCommandData.TransactionInformation);
                if (doc != null)
                {
                    database.TransactionalStorage.ExecuteImmediatelyOrRegisterForSynchronization(() =>
                    {
                        advPatchCommandData.Metadata = doc.Metadata;
                        advPatchCommandData.Etag = doc.Etag;
                    });
                }
            }
        }
コード例 #5
0
		private static void Execute(ICommandData self, DocumentDatabase database, BatchResult batchResult)
		{
			var deleteCommandData = self as DeleteCommandData;
			if (deleteCommandData != null)
			{
				var result = database.Delete(deleteCommandData.Key, deleteCommandData.Etag, deleteCommandData.TransactionInformation);

				if (batchResult != null)
					batchResult.Deleted = result;

				return;
			}

			var putCommandData = self as PutCommandData;
			if (putCommandData != null)
			{
				var putResult = database.Put(putCommandData.Key, putCommandData.Etag, putCommandData.Document, putCommandData.Metadata, putCommandData.TransactionInformation);
				putCommandData.Etag = putResult.ETag;
				putCommandData.Key = putResult.Key;

				return;
			}

			var patchCommandData = self as PatchCommandData;
			if (patchCommandData != null)
			{
				var result = database.ApplyPatch(patchCommandData.Key, patchCommandData.Etag,
												 patchCommandData.Patches, patchCommandData.PatchesIfMissing, patchCommandData.Metadata,
												 patchCommandData.TransactionInformation);

				if (batchResult != null)
					batchResult.PatchResult = result.PatchResult;

				var doc = database.Get(patchCommandData.Key, patchCommandData.TransactionInformation);
				if (doc != null)
				{
					patchCommandData.Metadata = doc.Metadata;
					patchCommandData.Etag = doc.Etag;
				}
				return;
			}

			var advPatchCommandData = self as ScriptedPatchCommandData;
			if (advPatchCommandData != null)
			{
				var result = database.ApplyPatch(advPatchCommandData.Key, advPatchCommandData.Etag,
												 advPatchCommandData.Patch, advPatchCommandData.PatchIfMissing, advPatchCommandData.Metadata,
												 advPatchCommandData.TransactionInformation, advPatchCommandData.DebugMode);

				if (batchResult != null)
					batchResult.PatchResult = result.Item1.PatchResult;

				advPatchCommandData.AdditionalData = new RavenJObject { { "Debug", new RavenJArray(result.Item2) } };
				if(advPatchCommandData.DebugMode)
				{
					advPatchCommandData.AdditionalData["Document"] = result.Item1.Document;
					return;
				}

				var doc = database.Get(advPatchCommandData.Key, advPatchCommandData.TransactionInformation);
				if (doc != null)
				{
					advPatchCommandData.Metadata = doc.Metadata;
					advPatchCommandData.Etag = doc.Etag;
				}
				return;
			}
		}