Esempio n. 1
0
        private bool TryParse(DocumentsOperationContext context, PatcherOperationScope scope, out BlittableJsonReaderObject val)
        {
            if (scope.ActualPatchResult == JsValue.Undefined || scope.ActualPatchResult == JsValue.Undefined)
            {
                val = null;
                return(false);
            }

            if (scope.ActualPatchResult == TombstoneResolverValue)
            {
                val = null;
                return(true);
            }
            var obj = scope.ActualPatchResult.AsObject();

            using (var writer = new ManualBlittalbeJsonDocumentBuilder <UnmanagedWriteBuffer>(context))
            {
                writer.Reset(BlittableJsonDocumentBuilder.UsageMode.None);
                writer.StartWriteObjectDocument();
                scope.ToBlittableJsonReaderObject(writer, obj);
                writer.FinalizeDocument();
                val = writer.CreateReader();
                return(true);
            }
        }
Esempio n. 2
0
 protected virtual void CustomizeEngine(Engine engine, PatcherOperationScope scope)
 {
     engine.Global.Delete("PutDocument", false);
     engine.Global.Delete("DeleteDocument", false);
     engine.SetValue("PutDocument", (Func <string, JsValue, JsValue, JsValue, string>)((key, data, metadata, etag) => scope.PutDocument(key, data, metadata, etag, engine)));
     engine.SetValue("DeleteDocument", (Action <string>)scope.DeleteDocument);
 }
Esempio n. 3
0
            public SingleScriptRun(PatchDocument parent, DocumentsOperationContext context, PatchRequest patch, bool isTestOnly)
            {
                _parent = parent;
                _patch  = patch;
                Scope   = new PatcherOperationScope(parent._database, context, isTestOnly)
                {
                    AdditionalStepsPerSize = parent.additionalStepsPerSize,
                    MaxSteps = parent.maxSteps,
                };

                try
                {
                    JintEngine = ScriptsCache.GetEngine(parent.CreateEngine, patch, Scope.CustomFunctions);
                }
                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);
                }
            }
Esempio n. 4
0
        protected override void CustomizeEngine(Engine engine, PatcherOperationScope scope)
        {
            base.CustomizeEngine(engine, scope);

            engine.Global.Delete("ResolveToTombstone", false);
            engine.Global.Delete(TombstoneResolverValue, false);
            engine.SetValue("ResolveToTombstone", new Func <string>(() => TombstoneResolverValue));

            engine.Global.Delete("HasTombstone", false);
            engine.SetValue("HasTombstone", _hasTombstone);
        }
Esempio n. 5
0
        private void OutputLog(Engine engine, PatcherOperationScope scope)
        {
            var numberOfOutputs = (int)engine.GetValue("number_of_outputs").AsNumber();

            if (numberOfOutputs == 0)
            {
                return;
            }

            var arr = engine.GetValue("debug_outputs").AsArray();

            foreach (var property in arr.GetOwnProperties())
            {
                if (property.Key == "length")
                {
                    continue;
                }

                var jsInstance = property.Value.Value;
                if (!jsInstance.HasValue)
                {
                    continue;
                }

                var    value  = jsInstance.Value;
                string output = null;
                switch (value.Type)
                {
                case Types.Boolean:
                    output = value.AsBoolean().ToString();
                    break;

                case Types.Null:
                case Types.Undefined:
                    output = value.ToString();
                    break;

                case Types.Number:
                    output = value.AsNumber().ToString(CultureInfo.InvariantCulture);
                    break;

                case Types.String:
                    output = value.AsString();
                    break;
                }

                if (output != null)
                {
                    scope.DebugInfo.Add(output);
                }
            }

            engine.Invoke("clear_debug_outputs");
        }
Esempio n. 6
0
        private void CleanupEngine(PatchRequest patch, Engine jintEngine, PatcherOperationScope scope)
        {
            if (patch.Values != null)
            {
                foreach (var name in patch.Values.GetPropertyNames())
                {
                    jintEngine.Global.Delete(name, true);
                }
            }

            jintEngine.Global.Delete("__document_id", true);
            RemoveEngineCustomizations(jintEngine, scope);
        }
Esempio n. 7
0
        private void PrepareEngine(PatchRequest patch, PatcherOperationScope scope, Engine jintEngine, int documentSize)
        {
            int totalScriptSteps = 0;

            if (documentSize != 0)
            {
                totalScriptSteps = maxSteps + (documentSize * additionalStepsPerSize);
                jintEngine.Options.MaxStatements(totalScriptSteps);
            }


            jintEngine.Global.Delete("LoadDocument", false);
            jintEngine.Global.Delete("IncreaseNumberOfAllowedStepsBy", false);

            CustomizeEngine(jintEngine, scope);

            jintEngine.SetValue("LoadDocument", (Func <string, JsValue>)(key => scope.LoadDocument(key, jintEngine, ref totalScriptSteps)));

            jintEngine.SetValue("IncreaseNumberOfAllowedStepsBy", (Action <int>)(number =>
            {
                if (allowScriptsToAdjustNumberOfSteps == false)
                {
                    throw new InvalidOperationException("Cannot use 'IncreaseNumberOfAllowedStepsBy' method, because `Raven/AllowScriptsToAdjustNumberOfSteps` is set to false.");
                }

                scope.MaxSteps   += number;
                totalScriptSteps += number;
                jintEngine.Options.MaxStatements(totalScriptSteps);
            }));

            if (patch.Values != null)
            {
                var prop = new BlittableJsonReaderObject.PropertyDetails();

                for (int i = 0; i < patch.Values.Count; i++)
                {
                    patch.Values.GetPropertyByIndex(i, ref prop);
                    jintEngine.SetValue(prop.Name, scope.ToJsValue(jintEngine, prop.Value, prop.Token));
                }
            }

            jintEngine.ResetStatementsCount();
        }
Esempio n. 8
0
        protected void SetupInputs(PatcherOperationScope scope, Engine jintEngine)
        {
            var docsArr = jintEngine.Array.Construct(Arguments.Empty);
            int index   = 0;

            foreach (var doc in _docs)
            {
                //TODO : add unit test that has a conflict here to make sure that it is ok
                var jsVal = scope.ToJsObject(jintEngine, doc.Doc, "doc" + index);
                docsArr.FastAddProperty(index.ToString(), jsVal, true, true, true);
                index++;
            }
            docsArr.FastSetProperty("length", new PropertyDescriptor
            {
                Value        = new JsValue(index),
                Configurable = true,
                Enumerable   = true,
                Writable     = true,
            });

            scope.PatchObject = docsArr;
        }
Esempio n. 9
0
 protected virtual void RemoveEngineCustomizations(Engine engine, PatcherOperationScope scope)
 {
 }
Esempio n. 10
0
 protected void SetupInputs(Document document, PatcherOperationScope scope, Engine jintEngine)
 {
     jintEngine.SetValue("__document_id", document.Key);
     scope.PatchObject = scope.ToJsObject(jintEngine, document.Data);
 }