当前位置: 首页>>代码示例>>C#>>正文


C# Data.ScriptedPatchRequest类代码示例

本文整理汇总了C#中Raven.Abstractions.Data.ScriptedPatchRequest的典型用法代码示例。如果您正苦于以下问题:C# ScriptedPatchRequest类的具体用法?C# ScriptedPatchRequest怎么用?C# ScriptedPatchRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ScriptedPatchRequest类属于Raven.Abstractions.Data命名空间,在下文中一共展示了ScriptedPatchRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CheckinScript

		public void CheckinScript(ScriptedPatchRequest request, Jint.JintEngine context)
		{
			CachedResult value;
			if (cacheDic.TryGetValue(request, out value))
			{
				if (value.Queue.Count > 20)
					return;
				value.Queue.Enqueue(context);
				return;
			}
			cacheDic.AddOrUpdate(request, patchRequest =>
			{
				var queue = new ConcurrentQueue<Jint.JintEngine>();
				queue.Enqueue(context);
				return new CachedResult
				{
					Queue = queue,
					Timestamp = SystemTime.UtcNow,
					Usage = 1
				};
			}, (patchRequest, result) =>
			{
				result.Queue.Enqueue(context);
				return result;
			});
		}
开发者ID:nberardi,项目名称:ravendb,代码行数:26,代码来源:ScriptsCache.cs

示例2: PatcherCanOutputObjectsCorrectly

        public void PatcherCanOutputObjectsCorrectly()
        {
            var doc = RavenJObject.Parse("{}");
            const string script = @"output(undefined);
                                output(true);
                                output(2);
                                output(2.5);
                                output('string');
                                output(null);
                                output([2, 'c']);
                                output({'a': 'c', 'f': { 'x' : 2}});"
                                ;

            var patch = new ScriptedPatchRequest()
            {
                Script = script
            };
            using (var scope = new DefaultScriptedJsonPatcherOperationScope())
            {
                var patcher = new ScriptedJsonPatcher();
                patcher.Apply(scope, doc, patch);
                Assert.Equal(8, patcher.Debug.Count);
                Assert.Equal("undefined", patcher.Debug[0]);
                Assert.Equal("True", patcher.Debug[1]);
                Assert.Equal("2", patcher.Debug[2]);
                Assert.Equal("2.5", patcher.Debug[3]);
                Assert.Equal("string", patcher.Debug[4]);
                Assert.Equal("null", patcher.Debug[5]);
                Assert.Equal("[2,\"c\"]", patcher.Debug[6]);
                Assert.Equal("{\"a\":\"c\",\"f\":{\"x\":2}}", patcher.Debug[7]);
            }
        }
开发者ID:j2jensen,项目名称:ravendb,代码行数:32,代码来源:RavenDB_3264.cs

示例3: CheckinScript

        public void CheckinScript(ScriptedPatchRequest request, Engine context, RavenJObject customFunctions)
        {
            CachedResult cacheByCustomFunctions;

            var patchRequestAndCustomFunctionsTuple = new ScriptedPatchRequestAndCustomFunctionsToken(request, customFunctions);
            if (cacheDic.TryGetValue(patchRequestAndCustomFunctionsTuple, out cacheByCustomFunctions))
            {
                if (cacheByCustomFunctions.Queue.Count > 20)
                    return;
                cacheByCustomFunctions.Queue.Enqueue(context);
                return;
            }
            cacheDic.AddOrUpdate(patchRequestAndCustomFunctionsTuple, patchRequest =>
            {
                var queue = new ConcurrentQueue<Engine>();

                return new CachedResult
                {
                    Queue = queue,
                    Timestamp = SystemTime.UtcNow,
                    Usage = 1
                };
            }, (patchRequest, result) =>
            {
                result.Queue.Enqueue(context);
                return result;
            });
        }
开发者ID:j2jensen,项目名称:ravendb,代码行数:28,代码来源:ScriptsCache.cs

示例4: ApplySingleScript

		private RavenJObject ApplySingleScript(RavenJObject doc, ScriptedPatchRequest patch, int size, string docId, ScriptedJsonPatcherOperationScope scope)
		{
			Engine jintEngine;
			try
			{
				jintEngine = ScriptsCache.CheckoutScript(CreateEngine, patch);
			}
			catch (NotSupportedException e)
			{
				throw new ParseException("Could not parse script", e);
			}
			catch (JavaScriptException e)
			{
				throw new ParseException("Could not parse script", e);
			}
			catch (Exception e)
			{
				throw new ParseException("Could not parse: " + Environment.NewLine + patch.Script, e);
			}

			try
			{
				PrepareEngine(patch, docId, size, scope, jintEngine);

				var jsObject = scope.ToJsObject(jintEngine, doc);
			    jintEngine.Invoke("ExecutePatchScript", jsObject);

			    CleanupEngine(patch, jintEngine);

				OutputLog(jintEngine);

			    ScriptsCache.CheckinScript(patch, jintEngine);

				return scope.ConvertReturnValue(jsObject);
			}
			catch (ConcurrencyException)
			{
			    throw;
			}
			catch (Exception errorEx)
			{
				jintEngine.ResetStatementsCount();

				OutputLog(jintEngine);
				var errorMsg = "Unable to execute JavaScript: " + Environment.NewLine + patch.Script;
				var error = errorEx as JavaScriptException;
				if (error != null)
					errorMsg += Environment.NewLine + "Error: " + Environment.NewLine + string.Join(Environment.NewLine, error.Error);
				if (Debug.Count != 0)
					errorMsg += Environment.NewLine + "Debug information: " + Environment.NewLine +
								string.Join(Environment.NewLine, Debug);

				var targetEx = errorEx as TargetInvocationException;
				if (targetEx != null && targetEx.InnerException != null)
					throw new InvalidOperationException(errorMsg, targetEx.InnerException);

				throw new InvalidOperationException(errorMsg, errorEx);
			}
		}
开发者ID:ReginaBricker,项目名称:ravendb,代码行数:59,代码来源:ScriptedJsonPatcher.cs

示例5: UpdateByIndex

        public RavenJArray UpdateByIndex(string indexName, IndexQuery queryToUpdate, ScriptedPatchRequest patch, BulkOperationOptions options = null)
		{
            return PerformBulkOperation(indexName, queryToUpdate, options, (docId, tx) =>
			{
				var patchResult = database.Patches.ApplyPatch(docId, null, patch, tx);
				return new { Document = docId, Result = patchResult.Item1, Debug = patchResult.Item2 };
			});
		}
开发者ID:bbqchickenrobot,项目名称:ravendb,代码行数:8,代码来源:DatabaseBulkOperations.cs

示例6: ApplySingleScript

		private RavenJObject ApplySingleScript(RavenJObject doc, ScriptedPatchRequest patch)
		{
			JintEngine ctx;
			try
			{
				ctx = scriptsCache.CheckoutScript(patch);
			}
			catch (NotSupportedException)
			{
				throw;
			}
			catch (Exception e)
			{
				throw new InvalidOperationException("Could not parse: " + Environment.NewLine + patch.Script, e);
			}

			loadDocumentStatic = loadDocument;
			try
			{
				foreach (var kvp in patch.Values)
				{
					if(kvp.Value is RavenJToken)
					{
						ctx.SetParameter(kvp.Key, ToJsInstance(ctx.Global, (RavenJToken)kvp.Value));
					}
					else
					{
						var rjt = RavenJToken.FromObject(kvp.Value);
						var jsInstance = ToJsInstance(ctx.Global, rjt);
						ctx.SetParameter(kvp.Key, jsInstance);
					}
				}
				var jsObject = ToJsObject(ctx.Global, doc);
				ctx.CallFunction("ExecutePatchScript", jsObject);
				foreach (var kvp in patch.Values)
				{
					ctx.RemoveParameter(kvp.Key);
				}
				OutputLog(ctx);

				scriptsCache.CheckinScript(patch, ctx);

				return ToRavenJObject(jsObject);
			}
			catch (Exception errorEx)
			{
				OutputLog(ctx);
				throw new InvalidOperationException("Unable to execute JavaScript: " + Environment.NewLine + patch.Script +
					Environment.NewLine + "Debug information: " + Environment.NewLine + string.Join(Environment.NewLine, Debug), errorEx);
			}
			finally
			{
				loadDocumentStatic = null;
			}
		}
开发者ID:arelee,项目名称:ravendb,代码行数:55,代码来源:ScriptedJsonPatcher.cs

示例7: CanUseTrim

		public void CanUseTrim()
		{
			var doc = RavenJObject.Parse("{\"Email\":' [email protected] '}");
			const string script = "this.Email = this.Email.trim();";
			var patch = new ScriptedPatchRequest()
			{
				Script = script,
			};
			var result = new ScriptedJsonPatcher().Apply(doc, patch);
			Assert.Equal(result["Email"].Value<string>(), "[email protected]");
		}
开发者ID:robashton,项目名称:ravendb,代码行数:11,代码来源:AdvancedPatching.cs

示例8: ComplexVariableTest

		public void ComplexVariableTest()
		{
			const string email = "[email protected]";
			var doc = RavenJObject.Parse("{\"Email\":null}");
			const string script = "this.Email = data.Email;";
			var patch = new ScriptedPatchRequest()
			{
				Script = script,
				Values = { { "data", new { Email = email } } }
			};
			var result = new ScriptedJsonPatcher().Apply(doc, patch);
			Assert.Equal(result["Email"].Value<string>(),email);
		}
开发者ID:robashton,项目名称:ravendb,代码行数:13,代码来源:AdvancedPatching.cs

示例9: Apply

		public RavenJObject Apply(RavenJObject document, ScriptedPatchRequest patch)
		{
			if (document == null)
				return null;

			if (String.IsNullOrEmpty(patch.Script))
				throw new InvalidOperationException("Patch script must be non-null and not empty");

			var resultDocument = ApplySingleScript(document, patch);
			if (resultDocument != null)
				document = resultDocument;
			return document;
		}
开发者ID:thoemmi,项目名称:ravendb,代码行数:13,代码来源:ScriptedJsonPatcher.cs

示例10: ComplexVariableTest2

		public void ComplexVariableTest2()
		{
			const string email = "[email protected]";
			var doc = RavenJObject.Parse("{\"Contact\":null}");
			const string script = "this.Contact = contact;";
			var patch = new ScriptedPatchRequest()
			{
				Script = script,
				Values = { { "contact", new { Email = email } } }
			};
			var result = new ScriptedJsonPatcher().Apply(doc, patch);
			Assert.NotNull(result["Contact"]);
		}
开发者ID:arelee,项目名称:ravendb,代码行数:13,代码来源:AdvancedPatching.cs

示例11: Apply

	    public virtual RavenJObject Apply(ScriptedJsonPatcherOperationScope scope, RavenJObject document, ScriptedPatchRequest patch, int size = 0, string docId = null)
		{
			if (document == null)
				return null;

			if (String.IsNullOrEmpty(patch.Script))
				throw new InvalidOperationException("Patch script must be non-null and not empty");

				var resultDocument = ApplySingleScript(document, patch, size, docId, scope);
				if (resultDocument != null)
					document = resultDocument;
			
			return document;
		}
开发者ID:ReginaBricker,项目名称:ravendb,代码行数:14,代码来源:ScriptedJsonPatcher.cs

示例12: CheckoutScript

		public Engine CheckoutScript(Func<ScriptedPatchRequest, Engine> createEngine, ScriptedPatchRequest request)
		{
			CachedResult value;
			if (cacheDic.TryGetValue(request, out value))
			{
				Interlocked.Increment(ref value.Usage);
				Engine context;
				if (value.Queue.TryDequeue(out context))
				{
					return context;
				}
			}
			var result = createEngine(request);

			var cachedResult = new CachedResult
			{
				Usage = 1,
				Queue = new ConcurrentQueue<Engine>(),
				Timestamp = SystemTime.UtcNow
			};

			cacheDic.AddOrUpdate(request, cachedResult, (_, existing) =>
			{
				Interlocked.Increment(ref existing.Usage);
				return existing;
			});
			if (cacheDic.Count > CacheMaxSize)
			{
				foreach (var source in cacheDic
					.Where(x=>x.Value !=null)
					.OrderByDescending(x => x.Value.Usage)
					.ThenBy(x => x.Value.Timestamp)
					.Skip(CacheMaxSize - CacheMaxSize/10))
				{
					if (Equals(source.Key, request))
						continue; // we don't want to remove the one we just added
					CachedResult ignored;
					cacheDic.TryRemove(source.Key, out ignored);
				}
				foreach (var source in cacheDic.Where(x => x.Value == null))
				{
					CachedResult ignored;
					cacheDic.TryRemove(source.Key, out ignored);
				}
			}

			return result;
		}
开发者ID:felipeleusin,项目名称:ravendb,代码行数:48,代码来源:ScriptsCache.cs

示例13: PatchRequestShouldCreateDocIfNotExists

        public void PatchRequestShouldCreateDocIfNotExists()
        {
            using (var store = NewDocumentStore())
            {
                var patchExisting = new ScriptedPatchRequest
                {
                    Script = "this.Counter++;",
                };
                var patchDefault = new ScriptedPatchRequest
                {
                    Script = "this.Counter=100;",
                };
                var docId = "Docs/1";

                store.DatabaseCommands.Patch(docId, patchExisting, patchDefault, new RavenJObject());

                using (var session = store.OpenSession())
                {
                    Assert.NotNull(session.Load<TestDoc>(docId));
                }
            }
        }
开发者ID:j2jensen,项目名称:ravendb,代码行数:22,代码来源:PatchingWithDefault.cs

示例14: CreateEngine

		internal static JintEngine CreateEngine(ScriptedPatchRequest patch)
		{
			var wrapperScript = String.Format(@"
function ExecutePatchScript(docInner){{
  (function(doc){{
	{0}{1}
  }}).apply(docInner);
}};
", patch.Script, patch.Script.EndsWith(";") ? String.Empty : ";");

			var jintEngine = new JintEngine()
				.AllowClr(false)
				.SetDebugMode(false)
				.SetMaxRecursions(50)
				.SetMaxSteps(10*1000);


			jintEngine.Run(GetFromResources("Raven.Database.Json.Map.js"));

			jintEngine.Run(GetFromResources("Raven.Database.Json.lodash.js"));

			jintEngine.Run(GetFromResources("Raven.Database.Json.RavenDB.js"));

			jintEngine.SetFunction("LoadDocument", ((Func<string, object>)(value =>
			{
				var loadedDoc = loadDocumentStatic(value);
				if (loadedDoc == null)
					return null;
				loadedDoc[Constants.DocumentIdFieldName] = value;
				return ToJsObject(jintEngine.Global, loadedDoc);
			})));

			jintEngine.Run(wrapperScript);

			return jintEngine;
		}
开发者ID:thoemmi,项目名称:ravendb,代码行数:36,代码来源:ScriptedJsonPatcher.cs

示例15: ApplySingleScript

		private RavenJObject ApplySingleScript(RavenJObject doc, ScriptedPatchRequest patch, int size)
		{
			JintEngine jintEngine;
			try
			{
				jintEngine = scriptsCache.CheckoutScript(CreateEngine, patch);
			}
			catch (NotSupportedException)
			{
				throw;
			}
			catch (JintException)
			{
				throw;
			}
			catch (Exception e)
			{
				throw new InvalidOperationException("Could not parse: " + Environment.NewLine + patch.Script, e);
			}

			loadDocumentStatic = loadDocument;
			try
			{
				CustomizeEngine(jintEngine);
				
				foreach (var kvp in patch.Values)
				{
					if (kvp.Value is RavenJToken)
					{
						jintEngine.SetParameter(kvp.Key, ToJsInstance(jintEngine.Global, (RavenJToken)kvp.Value));
					}
					else
					{
						var rjt = RavenJToken.FromObject(kvp.Value);
						var jsInstance = ToJsInstance(jintEngine.Global, rjt);
						jintEngine.SetParameter(kvp.Key, jsInstance);
					}
				}
				var jsObject = ToJsObject(jintEngine.Global, doc);
				jintEngine.ResetSteps();
				if (size != 0)
				{
					jintEngine.SetMaxSteps(10*1000 + (size*5));
				}
				jintEngine.CallFunction("ExecutePatchScript", jsObject);
				foreach (var kvp in patch.Values)
				{
					jintEngine.RemoveParameter(kvp.Key);
				}
				RemoveEngineCustomizations(jintEngine);
				OutputLog(jintEngine);

				scriptsCache.CheckinScript(patch, jintEngine);

				return ConvertReturnValue(jsObject);
			}
			catch (Exception errorEx)
			{
				OutputLog(jintEngine);
				var errorMsg = "Unable to execute JavaScript: " + Environment.NewLine + patch.Script;
				var error = errorEx as JsException;
				if (error != null)
					errorMsg += Environment.NewLine + "Error: " + Environment.NewLine + string.Join(Environment.NewLine, error.Value);
				if (Debug.Count != 0)
					errorMsg += Environment.NewLine + "Debug information: " + Environment.NewLine +
								string.Join(Environment.NewLine, Debug);

				throw new InvalidOperationException(errorMsg, errorEx);
			}
			finally
			{
				loadDocumentStatic = null;
			}
		}
开发者ID:jjchiw,项目名称:ravendb,代码行数:74,代码来源:ScriptedJsonPatcher.cs


注:本文中的Raven.Abstractions.Data.ScriptedPatchRequest类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。