private void AddSingleCounterGroup(LazyStringValue docId, BlittableJsonReaderObject counerGroupDocument, string function = null)
        {
            if (counerGroupDocument.TryGet(CountersStorage.Values, out BlittableJsonReaderObject counters) == false)
            {
                return;
            }

            var prop = new BlittableJsonReaderObject.PropertyDetails();

            for (var i = 0; i < counters.Count; i++)
            {
                counters.GetPropertyByIndex(i, ref prop);

                if (GetCounterValueAndCheckIfShouldSkip(docId, function, prop, out long value, out bool delete))
                {
                    continue;
                }

                if (delete)
                {
                    _currentRun.DeleteCounter(docId, prop.Name);
                }
                else
                {
                    _currentRun.AddCounter(docId, prop.Name, value);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task ConfigRevisions()
        {
            await DatabaseConfigurations(
                ServerStore.ModifyDatabaseRevisions,
                "read-revisions-config",
                GetRaftRequestIdFromQuery(),
                beforeSetupConfiguration : (string name, ref BlittableJsonReaderObject configuration, JsonOperationContext context) =>
            {
                if (configuration == null ||
                    configuration.TryGet(nameof(RevisionsConfiguration.Collections), out BlittableJsonReaderObject collections) == false ||
                    collections?.Count > 0 == false)
                {
                    return;
                }

                var uniqueKeys = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                var prop       = new BlittableJsonReaderObject.PropertyDetails();

                for (var i = 0; i < collections.Count; i++)
                {
                    collections.GetPropertyByIndex(i, ref prop);

                    if (uniqueKeys.Add(prop.Name) == false)
                    {
                        throw new InvalidOperationException("Cannot have two different revision configurations on the same collection. " +
                                                            $"Collection name : '{prop.Name}'");
                    }
                }
            });
        }
Ejemplo n.º 3
0
        private void WriteBlittableInstance(BlittableObjectInstance obj, bool isRoot, bool filterProperties)
        {
            if (obj.DocumentId != null &&
                _usageMode == BlittableJsonDocumentBuilder.UsageMode.None)
            {
                var metadata = obj.GetOrCreate(Constants.Documents.Metadata.Key);
                metadata.Put(Constants.Documents.Metadata.Id, obj.DocumentId, false);
            }
            if (obj.Blittable != null)
            {
                foreach (var propertyIndex in obj.Blittable.GetPropertiesByInsertionOrder())
                {
                    var prop = new BlittableJsonReaderObject.PropertyDetails();

                    obj.Blittable.GetPropertyByIndex(propertyIndex, ref prop);

                    var existInObject = obj.OwnValues.Remove(prop.Name, out var modifiedValue);

                    if (existInObject == false && obj.Deletes?.Contains(prop.Name) == true)
                    {
                        continue;
                    }

                    if (ShouldFilterProperty(filterProperties, prop.Name))
                    {
                        continue;
                    }

                    _writer.WritePropertyName(prop.Name);

                    if (existInObject && modifiedValue.Changed)
                    {
                        WriteJsonValue(obj, isRoot, prop.Name, modifiedValue.Value);
                    }
                    else
                    {
                        _writer.WriteValue(prop.Token & BlittableJsonReaderBase.TypesMask, prop.Value);
                    }
                }
            }

            foreach (var modificationKvp in obj.OwnValues)
            {
                var propertyName = modificationKvp.Key;
                if (ShouldFilterProperty(filterProperties, propertyName))
                {
                    continue;
                }

                if (modificationKvp.Value.Changed == false)
                {
                    continue;
                }

                _writer.WritePropertyName(propertyName);
                var blittableObjectProperty = modificationKvp.Value;
                WriteJsonValue(obj, isRoot, propertyName, blittableObjectProperty.Value);
            }
        }
        public static void WriteMetadata(this BlittableJsonTextWriter writer, Document document, BlittableJsonReaderObject metadata)
        {
            writer.WritePropertyName(Constants.Documents.Metadata.Key);
            writer.WriteStartObject();
            bool first = true;

            if (metadata != null)
            {
                var size = metadata.Count;
                var prop = new BlittableJsonReaderObject.PropertyDetails();

                for (int i = 0; i < size; i++)
                {
                    if (first == false)
                    {
                        writer.WriteComma();
                    }
                    first = false;
                    metadata.GetPropertyByIndex(i, ref prop);
                    writer.WritePropertyName(prop.Name);
                    writer.WriteValue(prop.Token & BlittableJsonReaderBase.TypesMask, prop.Value);
                }
            }

            if (first == false)
            {
                writer.WriteComma();
            }
            writer.WritePropertyName(Constants.Documents.Metadata.ChangeVector);
            writer.WriteString(document.ChangeVector);

            if (document.Flags != DocumentFlags.None)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.Flags);
                writer.WriteString(document.Flags.ToString());
            }
            if (document.Id != null)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.Id);
                writer.WriteString(document.Id);
            }
            if (document.IndexScore != null)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.IndexScore);
                writer.WriteDouble(document.IndexScore.Value);
            }
            if (document.LastModified != DateTime.MinValue)
            {
                writer.WriteComma();
                writer.WritePropertyName(Constants.Documents.Metadata.LastModified);
                writer.WriteDateTime(document.LastModified, isUtc: true);
            }
            writer.WriteEndObject();
        }
Ejemplo n.º 5
0
        private JsValue ConstructValues(List <BlittableJsonReaderObject> values)
        {
            var items = new PropertyDescriptor[values.Count];

            for (int j = 0; j < values.Count; j++)
            {
                var val = values[j];

                if (JavaScriptIndexUtils.GetValue(Engine, val, out JsValue jsValue, true) == false)
                {
                    continue;
                }
                items[j] = new PropertyDescriptor(jsValue, true, true, true);
            }
            var jsArray = new ArrayInstance(Engine, items);

            jsArray.Prototype  = Engine.Array.PrototypeObject;
            jsArray.Extensible = false;

            var result = new ObjectInstance(Engine)
            {
                Extensible = true
            };

            result.Put("values", jsArray, false);
            if (_singleField)
            {
                var index = values[0].GetPropertyIndex(_groupByFields[0]);
                if (index != -1)
                {
                    BlittableJsonReaderObject.PropertyDetails prop = default;
                    values[0].GetPropertyByIndex(index, ref prop, addObjectToCache: false);
                    result.Put("key", JsValue.FromObject(Engine, prop.Value), false);
                }
            }
            else
            {
                var key = new ObjectInstance(Engine)
                {
                    Extensible = true
                };
                result.Put("key", key, false);
                for (int i = 0; i < _groupByFields.Length; i++)
                {
                    var index = values[0].GetPropertyIndex(_groupByFields[i]);
                    if (index != -1)
                    {
                        BlittableJsonReaderObject.PropertyDetails prop = default;
                        values[0].GetPropertyByIndex(index, ref prop, addObjectToCache: false);
                        key.Put(_groupByFields[i], JsValue.FromObject(Engine, prop.Value), false);
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 6
0
            public override int Execute(DocumentsOperationContext context)
            {
                var hiLoDocumentId = RavenIdGeneratorsHilo + Key;
                var prefix         = Key + Separator;

                long oldMax = 0;
                var  newDoc = new DynamicJsonValue();
                BlittableJsonReaderObject hiloDocReader = null;

                try
                {
                    try
                    {
                        hiloDocReader = Database.DocumentsStorage.Get(context, hiLoDocumentId)?.Data;
                    }
                    catch (DocumentConflictException e)
                    {
                        throw new InvalidDataException("Failed to fetch HiLo document due to a conflict on the document. " +
                                                       "This shouldn't happen, since it this conflict should've been resolved during replication. " +
                                                       "This exception should not happen and is likely a bug.", e);
                    }

                    if (hiloDocReader != null)
                    {
                        var prop = new BlittableJsonReaderObject.PropertyDetails();
                        foreach (var propertyId in hiloDocReader.GetPropertiesByInsertionOrder())
                        {
                            hiloDocReader.GetPropertyByIndex(propertyId, ref prop);
                            if (prop.Name == "Max")
                            {
                                oldMax = (long)prop.Value;
                                continue;
                            }

                            newDoc[prop.Name] = prop.Value;
                        }
                    }

                    oldMax = Math.Max(oldMax, LastRangeMax);

                    newDoc["Max"] = oldMax + Capacity;

                    using (var freshHilo = context.ReadObject(newDoc, hiLoDocumentId, BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                        Database.DocumentsStorage.Put(context, hiLoDocumentId, null, freshHilo);

                    OldMax = oldMax;
                    Prefix = prefix;
                }
                finally
                {
                    hiloDocReader?.Dispose();
                }
                return(1);
            }
Ejemplo n.º 7
0
        private void Init()
        {
            _metadata = new Dictionary <string, object>();
            var indexes = _source.GetPropertiesByInsertionOrder();

            foreach (var index in indexes)
            {
                var propDetails = new BlittableJsonReaderObject.PropertyDetails();
                _source.GetPropertyByIndex(index, ref propDetails);
                _metadata[propDetails.Name] = ConvertValue(propDetails.Value);
            }
        }
Ejemplo n.º 8
0
        public void Init()
        {
            _metadata = new Dictionary <string, string>();
            var indexes = _source.GetPropertiesByInsertionOrder();

            foreach (var index in indexes)
            {
                var propDetails = new BlittableJsonReaderObject.PropertyDetails();
                _source.GetPropertyByIndex(index, ref propDetails);
                _metadata[propDetails.Name] = propDetails.Value.ToString();
            }
            ;
        }
Ejemplo n.º 9
0
        private void WriteObject(BlittableJsonReaderObject obj)
        {
            var propDetails = new BlittableJsonReaderObject.PropertyDetails();

            _manualBlittableJsonDocumentBuilder.StartWriteObject();
            for (int i = 0; i < obj.Count; i++)
            {
                obj.GetPropertyByIndex(i, ref propDetails);
                _manualBlittableJsonDocumentBuilder.WritePropertyName(propDetails.Name);
                WritePropertyValue(propDetails);
            }

            _manualBlittableJsonDocumentBuilder.WriteObjectEnd();
        }
Ejemplo n.º 10
0
        protected override void LoadToFunction(string tableName, ScriptRunnerResult cols)
        {
            if (tableName == null)
            {
                ThrowLoadParameterIsMandatory(nameof(tableName));
            }

            var result  = cols.TranslateToObject(Context);
            var columns = new List <SqlColumn>(result.Count);
            var prop    = new BlittableJsonReaderObject.PropertyDetails();

            for (var i = 0; i < result.Count; i++)
            {
                result.GetPropertyByIndex(i, ref prop);

                var sqlColumn = new SqlColumn
                {
                    Id    = prop.Name,
                    Value = prop.Value,
                    Type  = prop.Token
                };


                if (_transformation.HasLoadAttachment &&
                    prop.Token == BlittableJsonToken.String && IsLoadAttachment(prop.Value as LazyStringValue, out var attachmentName))
                {
                    Stream attachmentStream;
                    using (Slice.From(Context.Allocator, Current.Document.ChangeVector, out var cv))
                    {
                        attachmentStream = Database.DocumentsStorage.AttachmentsStorage.GetAttachment(
                            Context,
                            Current.DocumentId,
                            attachmentName,
                            AttachmentType.Document,
                            cv)
                                           ?.Stream ?? Stream.Null;
                    }

                    sqlColumn.Type  = 0;
                    sqlColumn.Value = attachmentStream;
                }

                columns.Add(sqlColumn);
            }

            GetOrAdd(tableName).Inserts.Add(new ToSqlItem(Current)
            {
                Columns = columns
            });
        }
Ejemplo n.º 11
0
        public MergeResult Resolve(int indent = 1)
        {
            var result = new Dictionary <string, object>();

            for (var index = 0; index < _docs.Length; index++)
            {
                var doc = _docs[index];
                if (doc == null)
                {
                    continue; // we never suggest to delete a document
                }
                for (var indexProp = 0; indexProp < doc.Count; indexProp++)
                {
                    BlittableJsonReaderObject.PropertyDetails prop = new BlittableJsonReaderObject.PropertyDetails();
                    doc.GetPropertyByIndex(indexProp, ref prop);

                    if (result.ContainsKey(prop.Name)) // already dealt with
                    {
                        continue;
                    }

                    prop.Token = doc.ProcessTokenTypeFlags(prop.Token);
                    switch (prop.Token)
                    {
                    case BlittableJsonToken.StartObject:
                    case BlittableJsonToken.EmbeddedBlittable:
                        var objTuple = new KeyValuePair <string, BlittableJsonReaderObject>(prop.Name, (BlittableJsonReaderObject)prop.Value);
                        if (TryHandleObjectValue(index, result, objTuple) == false)
                        {
                            goto default;
                        }
                        break;

                    case BlittableJsonToken.StartArray:
                        var arrTuple = new KeyValuePair <string, BlittableJsonReaderArray>(prop.Name, (BlittableJsonReaderArray)prop.Value);
                        if (TryHandleArrayValue(index, result, arrTuple) == false)
                        {
                            goto default;
                        }
                        break;

                    default:
                        HandleSimpleValues(result, prop, index);
                        break;
                    }
                }
            }
            return(GenerateOutput(result, indent));
        }
Ejemplo n.º 12
0
        private void Init()
        {
            _metadata = new Dictionary <string, object>();
            for (int i = 0; i < _source.Count; i++)
            {
                var propDetails = new BlittableJsonReaderObject.PropertyDetails();
                _source.GetPropertyByIndex(i, ref propDetails);
                _metadata[propDetails.Name] = ConvertValue(propDetails.Name, propDetails.Value);
            }

            if (_parent != null) // mark parent as dirty
            {
                _parent[_parentKey] = this;
            }
        }
Ejemplo n.º 13
0
        public static void FetchColumnNames(BlittableJsonReaderObject data, HashSet <LazyStringValue> columns, BlittableJsonReaderObject.PropertiesInsertionBuffer buffers)
        {
            var size = data.GetPropertiesByInsertionOrder(buffers);
            var prop = new BlittableJsonReaderObject.PropertyDetails();

            for (var i = 0; i < size; i++)
            {
                data.GetPropertyByIndex(buffers.Properties[i], ref prop);
                var propName = prop.Name;
                if (columns.Contains(propName) == false)
                {
                    columns.Add(prop.Name);
                }
            }
        }
Ejemplo n.º 14
0
        private List <CounterOperation> GetCounterOperationsFor(RavenEtlItem item)
        {
            var counterOperations = new List <CounterOperation>();

            foreach (var cgd in Database.DocumentsStorage.CountersStorage.GetCounterValuesForDocument(Context, item.DocumentId))
            {
                if (cgd.Values.TryGet(CountersStorage.Values, out BlittableJsonReaderObject counters) == false)
                {
                    return(null);
                }

                var prop = new BlittableJsonReaderObject.PropertyDetails();
                for (var i = 0; i < counters.Count; i++)
                {
                    counters.GetPropertyByIndex(i, ref prop);

                    if (GetCounterValueAndCheckIfShouldSkip(item.DocumentId, null, prop, out long value, out bool delete))
                    {
                        continue;
                    }

                    if (delete == false)
                    {
                        counterOperations.Add(new CounterOperation
                        {
                            Type        = CounterOperationType.Put,
                            CounterName = prop.Name,
                            Delta       = value
                        });
                    }
                    else
                    {
                        if (ShouldFilterOutDeletion(item))
                        {
                            continue;
                        }

                        counterOperations.Add(new CounterOperation
                        {
                            Type        = CounterOperationType.Delete,
                            CounterName = prop.Name,
                        });
                    }
                }
            }

            return(counterOperations);
        }
Ejemplo n.º 15
0
        private void WriteObject(BlittableJsonReaderObject obj)
        {
            var propDetails = new BlittableJsonReaderObject.PropertyDetails();

            _manualBlittableJsonDocumentBuilder.StartWriteObject();
            var propsIndexes = obj.GetPropertiesByInsertionOrder();

            foreach (var index in propsIndexes)
            {
                obj.GetPropertyByIndex(index, ref propDetails);
                _manualBlittableJsonDocumentBuilder.WritePropertyName(propDetails.Name);
                WritePropertyValue(propDetails);
            }

            _manualBlittableJsonDocumentBuilder.WriteObjectEnd();
        }
Ejemplo n.º 16
0
        public static void FetchColumnNames(BlittableJsonReaderObject data, HashSet <LazyStringValue> columns)
        {
            using (var buffers = data.GetPropertiesByInsertionOrder())
            {
                var prop = new BlittableJsonReaderObject.PropertyDetails();

                for (var i = 0; i < buffers.Properties.Count; i++)
                {
                    data.GetPropertyByIndex(buffers.Properties.Array[i + buffers.Properties.Offset], ref prop);
                    var propName = prop.Name;
                    if (columns.Contains(propName) == false)
                    {
                        columns.Add(prop.Name);
                    }
                }
            }
        }
Ejemplo n.º 17
0
        public ObjectInstance ToJsObject(Engine engine, BlittableJsonReaderObject json, string propertyName = null)
        {
            var jsObject = engine.Object.Construct(Arguments.Empty);
            var prop     = new BlittableJsonReaderObject.PropertyDetails();

            for (int i = 0; i < json.Count; i++)
            {
                json.GetPropertyByIndex(i, ref prop);
                var     name        = prop.Name.ToString();
                var     propertyKey = CreatePropertyKey(name, propertyName);
                var     value       = prop.Value;
                JsValue jsValue     = ToJsValue(engine, value, prop.Token, propertyKey);
                _propertiesByValue[propertyKey] = new KeyValuePair <object, JsValue>(value, jsValue);
                jsObject.FastAddProperty(name, jsValue, true, true, true);
            }
            return(jsObject);
        }
Ejemplo n.º 18
0
        protected override void HandleSpecialColumnsIfNeeded(string columnName, BlittableJsonReaderObject.PropertyDetails property, object value, ref ReadOnlyMemory <byte>?[] row)
        {
            if (_replaces == null)
            {
                return;
            }

            if (_replaces.TryGetValue(columnName, out var replace))
            {
                var replaceColumn = Columns[replace.DstColumnName];

                object replacedValue = null;

                switch (property.Token & BlittableJsonReaderBase.TypesMask)
                {
                case BlittableJsonToken.String:
                case BlittableJsonToken.StartArray:
                case BlittableJsonToken.StartObject:
                case BlittableJsonToken.CompressedString:
                case BlittableJsonToken.EmbeddedBlittable:
                    if (value != null)
                    {
                        var columnValue = value.ToString();

                        replacedValue = columnValue?.Replace(replace.OldValue?.ToString() ?? string.Empty, replace.NewValue.ToString());
                    }
                    break;
                }

                ReadOnlyMemory <byte>?replaceValueBytes;

                if (replacedValue != null)
                {
                    replaceValueBytes = GetValueByType(property, replacedValue, replaceColumn);
                }
                else
                {
                    replaceValueBytes = Array.Empty <byte>();
                }

                row[replaceColumn.ColumnIndex] = replaceValueBytes;

                HandleSpecialColumnsIfNeeded(replace.DstColumnName, property, replacedValue, ref row);
            }
        }
Ejemplo n.º 19
0
        private unsafe static void ValidateProperties(List <string> properties, BlittableJsonReaderObject json)
        {
            var propDetails = new BlittableJsonReaderObject.PropertyDetails();
            var propertiesByInsertionOrder = json.GetPropertiesByInsertionOrder();

            for (var i = 0; i < properties.Count; i++)
            {
                json.GetPropertyByIndex(propertiesByInsertionOrder.Properties[i], ref propDetails);

                if (propDetails.Name == Constants.Documents.Metadata.Key)
                {
                    continue;
                }

                var expected = properties[i];
                Assert.Equal(expected, propDetails.Name);
            }
        }
Ejemplo n.º 20
0
            private bool TryGetValueFromDocument(BlittableObjectInstance parent, string property, out JsValue value)
            {
                value = null;

                var index = parent.Blittable?.GetPropertyIndex(property);

                if (index == null || index == -1)
                {
                    return(false);
                }

                var propertyDetails = new BlittableJsonReaderObject.PropertyDetails();

                parent.Blittable.GetPropertyByIndex(index.Value, ref propertyDetails, true);

                value = TranslateToJs(parent, property, propertyDetails.Token, propertyDetails.Value);
                return(true);
            }
Ejemplo n.º 21
0
        private void HandleSimpleValues(Dictionary <string, object> result, BlittableJsonReaderObject.PropertyDetails prop, int index)
        {
            var conflicted = new Conflicted
            {
                Values = { prop }
            };

            for (var i = 0; i < _docs.Length; i++)
            {
                if (i == index)
                {
                    continue;
                }
                var other = _docs[i];
                if (other == null)
                {
                    continue;
                }

                BlittableJsonReaderObject.PropertyDetails otherProp = new BlittableJsonReaderObject.PropertyDetails();
                var propIndex = other.GetPropertyIndex(prop.Name);
                if (propIndex == -1)
                {
                    continue;
                }
                other.GetPropertyByIndex(propIndex, ref otherProp);

                if (otherProp.Token != prop.Token ||// if type is null there could not be a conflict
                    (prop.Value?.Equals(otherProp.Value) == false)
                    )
                {
                    conflicted.Values.Add(otherProp);
                }
            }

            if (conflicted.Values.Count == 1)
            {
                result.Add(prop.Name, prop);
            }
            else
            {
                result.Add(prop.Name, conflicted);
            }
        }
Ejemplo n.º 22
0
        internal ClassType GenerateClassTypesFromObject(string name, BlittableJsonReaderObject blittableObject)
        {
            var fields = new Dictionary <string, FieldType>();

            for (var i = 0; i < blittableObject.Count; i++)
            {
                // this call ensures properties to be returned in the same order, regardless their storing order
                var prop = new BlittableJsonReaderObject.PropertyDetails();
                blittableObject.GetPropertyByIndex(i, ref prop);

                if (prop.Name.ToString().Equals("@metadata", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                switch (prop.Token)
                {
                case BlittableJsonToken.EmbeddedBlittable:
                case BlittableJsonToken.StartObject:
                case BlittableJsonToken.StartObject | BlittableJsonToken.OffsetSizeByte | BlittableJsonToken.PropertyIdSizeByte:
                    var type = GenerateClassTypesFromObject(prop.Name, (BlittableJsonReaderObject)prop.Value);
                    fields[prop.Name] = new FieldType(type.Name, type.IsArray);
                    break;

                case BlittableJsonToken.StartArray:
                case BlittableJsonToken.StartArray | BlittableJsonToken.OffsetSizeByte:
                case BlittableJsonToken.StartArray | BlittableJsonToken.OffsetSizeShort:
                    var array = (BlittableJsonReaderArray)prop.Value;
                    fields[prop.Name] = GetArrayField(array, prop.Name);
                    break;

                default:
                    fields[prop.Name] = GetTokenTypeFromPrimitiveType(prop.Token, prop.Value);
                    break;
                }
            }

            // check if we can get the name from the metadata.
            var clazz = new ClassType(name, fields);

            clazz = IncludeGeneratedClass(clazz);
            return(clazz);
        }
Ejemplo n.º 23
0
        protected override void LoadToFunction(string tableName, ScriptRunnerResult cols)
        {
            if (tableName == null)
            {
                ThrowLoadParameterIsMandatory(nameof(tableName));
            }

            var result  = cols.TranslateToObject(Context);
            var columns = new List <SqlColumn>(result.Count);
            var prop    = new BlittableJsonReaderObject.PropertyDetails();

            for (var i = 0; i < result.Count; i++)
            {
                result.GetPropertyByIndex(i, ref prop);

                var sqlColumn = new SqlColumn
                {
                    Id    = prop.Name,
                    Value = prop.Value,
                    Type  = prop.Token
                };

                if (_transformation.IsLoadingAttachments &&
                    prop.Token == BlittableJsonToken.String && IsLoadAttachment(prop.Value as LazyStringValue, out var attachmentName))
                {
                    var attachment = _loadedAttachments[attachmentName].Dequeue();

                    sqlColumn.Type  = 0;
                    sqlColumn.Value = attachment.Stream;

                    _stats.IncrementBatchSize(attachment.Stream.Length);
                }

                columns.Add(sqlColumn);
            }

            GetOrAdd(tableName).Inserts.Add(new ToSqlItem(Current)
            {
                Columns = columns
            });

            _stats.IncrementBatchSize(result.Size);
        }
Ejemplo n.º 24
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();
        }
Ejemplo n.º 25
0
        private static Dictionary <string, string> ConvertToAdditionalSources(BlittableJsonReaderObject json)
        {
            if (json == null || json.Count == 0)
            {
                return(null);
            }

            var result = new Dictionary <string, string>();

            BlittableJsonReaderObject.PropertyDetails propertyDetails = new BlittableJsonReaderObject.PropertyDetails();
            for (int i = 0; i < json.Count; i++)
            {
                json.GetPropertyByIndex(i, ref propertyDetails);

                result[propertyDetails.Name] = propertyDetails.Value?.ToString();
            }

            return(result);
        }
Ejemplo n.º 26
0
        private static bool CompareValues(BlittableJsonReaderObject.PropertyDetails oldProp, BlittableJsonReaderObject.PropertyDetails newProp)
        {
            if (newProp.Token == BlittableJsonToken.Integer && oldProp.Token == BlittableJsonToken.LazyNumber)
            {
                var @long   = (long)newProp.Value;
                var @double = ((LazyNumberValue)oldProp.Value).ToDouble(CultureInfo.InvariantCulture);

                return(@double % 1 == 0 && @long.Equals((long)@double));
            }

            if (oldProp.Token == BlittableJsonToken.Integer && newProp.Token == BlittableJsonToken.LazyNumber)
            {
                var @long   = (long)oldProp.Value;
                var @double = ((LazyNumberValue)newProp.Value).ToDouble(CultureInfo.InvariantCulture);

                return(@double % 1 == 0 && @long.Equals((long)@double));
            }

            return(false);
        }
Ejemplo n.º 27
0
        public async Task PutSettings()
        {
            await ServerStore.EnsureNotPassiveAsync();

            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
            {
                var databaseSettingsJson = await context.ReadForDiskAsync(RequestBodyStream(), Constants.DatabaseSettings.StudioId);

                Dictionary <string, string> settings = new Dictionary <string, string>();
                var prop = new BlittableJsonReaderObject.PropertyDetails();

                for (int i = 0; i < databaseSettingsJson.Count; i++)
                {
                    databaseSettingsJson.GetPropertyByIndex(i, ref prop);
                    settings.Add(prop.Name, prop.Value?.ToString());
                }

                await UpdateDatabaseRecord(context, (record, _) => record.Settings = settings, GetRaftRequestIdFromQuery());
            }

            NoContentStatus(HttpStatusCode.Created);
        }
        public void FetchFields(BlittableJsonReaderObject data, Dictionary <LazyStringValue, FieldType> fields, BlittableJsonReaderObject.PropertiesInsertionBuffer buffers)
        {
            var size = data.GetPropertiesByInsertionOrder(buffers);
            var prop = new BlittableJsonReaderObject.PropertyDetails();

            for (var i = 0; i < size; i++)
            {
                data.GetPropertyByIndex(buffers.Properties[i], ref prop);
                var type = GetFieldType(prop.Token & BlittableJsonReaderBase.TypesMask, prop.Value);
                if (fields.TryGetValue(prop.Name, out var token))
                {
                    if (token != type)
                    {
                        fields[prop.Name] = token | type;
                    }
                }
                else
                {
                    fields[prop.Name] = type;
                }
            }
        }
Ejemplo n.º 29
0
        internal static Dictionary <string, long> GetMapping(ServerStore serverStore, TransactionOperationContext context)
        {
            var json = serverStore.Cluster.Read(context, Constants.Monitoring.Snmp.DatabasesMappingKey);

            var result = new Dictionary <string, long>(StringComparer.OrdinalIgnoreCase);

            if (json == null)
            {
                return(result);
            }

            var propertyDetails = new BlittableJsonReaderObject.PropertyDetails();

            for (int i = 0; i < json.Count; i++)
            {
                json.GetPropertyByIndex(i, ref propertyDetails);

                result[propertyDetails.Name] = (long)propertyDetails.Value;
            }

            return(result);
        }
Ejemplo n.º 30
0
        internal static Dictionary <string, long> GetIndexMapping(TransactionOperationContext context, ServerStore serverStore, string databaseName)
        {
            var json = serverStore.Cluster.Read(context, UpdateSnmpDatabaseIndexesMappingCommand.GetStorageKey(databaseName));

            var result = new Dictionary <string, long>(StringComparer.OrdinalIgnoreCase);

            if (json == null)
            {
                return(result);
            }

            var propertyDetails = new BlittableJsonReaderObject.PropertyDetails();

            for (int i = 0; i < json.Count; i++)
            {
                json.GetPropertyByIndex(i, ref propertyDetails);

                result[propertyDetails.Name] = (long)propertyDetails.Value;
            }

            return(result);
        }