Пример #1
0
        public static BlittableJsonReaderObject Translate(JsonOperationContext context, Engine scriptEngine, ObjectInstance objectInstance, IResultModifier modifier = null, BlittableJsonDocumentBuilder.UsageMode usageMode = BlittableJsonDocumentBuilder.UsageMode.None)
        {
            if (objectInstance == null)
            {
                return(null);
            }

            if (objectInstance is BlittableObjectInstance boi && boi.Changed == false)
            {
                return(boi.Blittable.Clone(context));
            }

            using (var writer = new ManualBlittableJsonDocumentBuilder <UnmanagedWriteBuffer>(context))
            {
                writer.Reset(usageMode);
                writer.StartWriteObjectDocument();

                var blittableBridge = new JsBlittableBridge(writer, usageMode, scriptEngine);
                blittableBridge.WriteInstance(objectInstance, modifier);

                writer.FinalizeDocument();

                return(writer.CreateReader());
            }
        }
Пример #2
0
        public BlittableJsonReaderObject TranslateToObject(JsonOperationContext context, JsBlittableBridge.IResultModifier modifier = null, BlittableJsonDocumentBuilder.UsageMode usageMode = BlittableJsonDocumentBuilder.UsageMode.None)
        {
            if (IsNull)
            {
                return(null);
            }
            var obj = _instance.AsObject();

            return(JsBlittableBridge.Translate(context, _parent.ScriptEngine, obj, modifier, usageMode));
        }
Пример #3
0
            public JsValue PutDocument(JsValue self, JsValue[] args)
            {
                string changeVector = null;

                if (args.Length != 2 && args.Length != 3)
                {
                    throw new InvalidOperationException("put(id, doc, changeVector) must be called with called with 2 or 3 arguments only");
                }
                AssertValidDatabaseContext();
                AssertNotReadOnly();
                if (args[0].IsString() == false && args[0].IsNull() == false && args[0].IsUndefined() == false)
                {
                    AssertValidId();
                }

                var id = args[0].IsNull() || args[0].IsUndefined() ? null : args[0].AsString();

                if (args[1].IsObject() == false)
                {
                    throw new InvalidOperationException(
                              $"Created document must be a valid object which is not null or empty. Document ID: '{id}'.");
                }

                PutOrDeleteCalled = true;

                if (args.Length == 3)
                {
                    if (args[2].IsString())
                    {
                        changeVector = args[2].AsString();
                    }
                    else if (args[2].IsNull() == false && args[0].IsUndefined() == false)
                    {
                        throw new InvalidOperationException(
                                  $"The change vector must be a string or null. Document ID: '{id}'.");
                    }
                }

                if (DebugMode)
                {
                    DebugActions.PutDocument.Add(id);
                }

                using (var reader = JsBlittableBridge.Translate(_context, ScriptEngine, args[1].AsObject(), usageMode: BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    var put = _database.DocumentsStorage.Put(_context, id, _context.GetLazyString(changeVector), reader);
                    return(put.Id);
                }
            }
Пример #4
0
            private Client.Exceptions.Documents.Patching.JavaScriptException CreateFullError(JavaScriptException e)
            {
                string msg;

                if (e.Error.IsString())
                {
                    msg = e.Error.AsString();
                }
                else if (e.Error.IsObject())
                {
                    msg = JsBlittableBridge.Translate(_jsonCtx, ScriptEngine, e.Error.AsObject()).ToString();
                }
                else
                {
                    msg = e.Error.ToString();
                }

                msg = "At " + e.Column + ":" + e.LineNumber + " " + msg;
                var javaScriptException = new Client.Exceptions.Documents.Patching.JavaScriptException(msg, e);

                return(javaScriptException);
            }
Пример #5
0
            private static Client.Exceptions.Documents.Patching.JavaScriptException CreateFullError(DocumentsOperationContext ctx, JavaScriptException e)
            {
                string msg;

                if (e.Error.IsString())
                {
                    msg = e.Error.AsString();
                }
                else if (e.Error.IsObject())
                {
                    msg = JsBlittableBridge.Translate(ctx, e.Error.AsObject()).ToString();
                }
                else
                {
                    msg = e.Error.ToString();
                }

                msg = "At " + e.Column + ":" + e.LineNumber + Environment.NewLine + msg;
                var javaScriptException = new Client.Exceptions.Documents.Patching.JavaScriptException(msg, e);

                return(javaScriptException);
            }
Пример #6
0
            public JsValue PutDocument(JsValue self, JsValue[] args)
            {
                string changeVector = null;

                if (args.Length != 2 && args.Length != 3)
                {
                    throw new InvalidOperationException("put(id, doc, changeVector) must be called with called with 2 or 3 arguments only");
                }
                AssertValidDatabaseContext();
                AssertNotReadOnly();
                if (args[0].IsString() == false && args[0].IsNull() == false && args[0].IsUndefined() == false)
                {
                    AssertValidId();
                }

                var id = args[0].IsNull() || args[0].IsUndefined() ? null : args[0].AsString();

                if (args[1].IsObject() == false)
                {
                    throw new InvalidOperationException(
                              $"Created document must be a valid object which is not null or empty. Document ID: '{id}'.");
                }

                PutOrDeleteCalled = true;

                if (args.Length == 3)
                {
                    if (args[2].IsString())
                    {
                        changeVector = args[2].AsString();
                    }
                    else if (args[2].IsNull() == false && args[0].IsUndefined() == false)
                    {
                        throw new InvalidOperationException(
                                  $"The change vector must be a string or null. Document ID: '{id}'.");
                    }
                }

                BlittableJsonReaderObject reader = null;

                try
                {
                    reader = JsBlittableBridge.Translate(_jsonCtx, ScriptEngine, args[1].AsObject(), usageMode: BlittableJsonDocumentBuilder.UsageMode.ToDisk);

                    var put = _database.DocumentsStorage.Put(
                        _docsCtx,
                        id,
                        _docsCtx.GetLazyString(changeVector),
                        reader,
                        nonPersistentFlags: NonPersistentDocumentFlags.ResolveAttachmentsConflict
                        );

                    if (DebugMode)
                    {
                        DebugActions.PutDocument.Add(new DynamicJsonValue
                        {
                            ["Id"]   = put.Id,
                            ["Data"] = reader
                        });
                    }

                    return(put.Id);
                }
                finally
                {
                    if (DebugMode == false)
                    {
                        reader?.Dispose();
                    }
                }
            }