Exemplo n.º 1
0
        public TableValueReader(long id, byte *ptr, int size)
        {
            Id      = id;
            Pointer = ptr;
            Size    = size;

            if (size > ushort.MaxValue)
            {
                _elementSize = 4;
            }
            else if (size > byte.MaxValue)
            {
                _elementSize = 2;
            }
            else
            {
                _elementSize = 1;
            }

            byte offset;

            Count     = BlittableJsonReaderBase.ReadVariableSizeInt(ptr, 0, out offset);
            _dataPtr  = Pointer + offset;
            _dataSize = Size - offset;
        }
Exemplo n.º 2
0
        private static unsafe bool CompareStringsWithEscapePositions(JsonOperationContext context, BlittableJsonReaderObject.PropertyDetails oldProp,
                                                                     BlittableJsonReaderObject.PropertyDetails newProp)
        {
            // this is called if the values are NOT equal, but we need to check if the oldProp was read from network and already resolved
            // the escape characters

            if (oldProp.Value is LazyStringValue lsv)
            {
                int pos = lsv.Size;
                int numOfEscapePositions = BlittableJsonReaderBase.ReadVariableSizeInt(lsv.Buffer, ref pos);
                if (numOfEscapePositions == 0)
                {
                    return(false);
                }

                using (var memoryStream = new MemoryStream())
                {
                    using (var textWriter = new BlittableJsonTextWriter(context, memoryStream))
                    {
                        textWriter.WriteString(lsv);
                        textWriter.Flush();
                    }
                    memoryStream.TryGetBuffer(out var bytes);
                    fixed(byte *pBuff = bytes.Array)
                    {
                        // need to ignore the quote marks
                        using var str = context.AllocateStringValue(null, pBuff + bytes.Offset + 1, (int)memoryStream.Length - 2);

                        return(newProp.Value.Equals(str));
                    }
                }
            }
            return(false);
        }
Exemplo n.º 3
0
        private void ReadEscapePositions(byte *buffer, int escapeSequencePos)
        {
            _state.EscapePositions.Clear();
            var numberOfEscapeSequences = BlittableJsonReaderBase.ReadVariableSizeInt(buffer, ref escapeSequencePos);

            while (numberOfEscapeSequences > 0)
            {
                numberOfEscapeSequences--;
                var bytesToSkip = BlittableJsonReaderBase.ReadVariableSizeInt(buffer, ref escapeSequencePos);
                _state.EscapePositions.Add(bytesToSkip);
            }
        }
Exemplo n.º 4
0
        private LazyStringValue SetTimestampTag()
        {
            if (_tagPointer.Pointer == null)
            {
                return(null);
            }

            var lazyStringLen = BlittableJsonReaderBase.ReadVariableSizeInt(_tagPointer.Pointer, 0, out var offset);

            _tag.Renew(null, _tagPointer.Pointer + offset, lazyStringLen);
            return(_tag);
        }
Exemplo n.º 5
0
        private static Document TableValueToDocument(JsonOperationContext context, TableValueReader tvr)
        {
            var result = new Document
            {
                StorageId = tvr.Id
            };
            int size;
            // See format of the lazy string key in the GetLowerKeySliceAndStorageKey method
            var  ptr = tvr.Read(3, out size);
            byte offset;

            size        = BlittableJsonReaderBase.ReadVariableSizeInt(ptr, 0, out offset);
            result.Key  = new LazyStringValue(null, ptr + offset, size, context);
            ptr         = tvr.Read(2, out size);
            result.Etag = IPAddress.NetworkToHostOrder(*(long *)ptr);
            result.Data = new BlittableJsonReaderObject(tvr.Read(4, out size), size, context);

            return(result);
        }
Exemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="self"></param>
        /// <param name="path"></param>
        /// <param name="createSnapshots">Set to true if you want to modify selected objects</param>
        /// <returns></returns>
        public static IEnumerable <Tuple <object, object> > SelectTokenWithRavenSyntaxReturningFlatStructure(this BlittableJsonReaderBase self, string path, bool createSnapshots = false)
        {
            var pathParts = path.Split(new[] { "[]." }, StringSplitOptions.RemoveEmptyEntries);
            var result    = new BlittablePath(pathParts[0]).Evaluate(self, false);

            if (pathParts.Length == 1)
            {
                yield return(Tuple.Create(result, (object)self));

                yield break;
            }

            if (result is BlittableJsonReaderObject)
            {
                var blitResult = result as BlittableJsonReaderObject;
                blitResult.TryGetMember(Constants.Json.Fields.Values, out result);

                var prop = new BlittableJsonReaderObject.PropertyDetails();

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

                    if (prop.Value is BlittableJsonReaderBase)
                    {
                        var itemAsBlittable = (BlittableJsonReaderBase)prop.Value;
                        foreach (var subItem in itemAsBlittable.SelectTokenWithRavenSyntaxReturningFlatStructure(string.Join("[].", pathParts.Skip(1).ToArray())))
                        {
                            yield return(subItem);
                        }
                    }
                    else
                    {
                        yield return(Tuple.Create(prop.Value, result));
                    }
                }
            }
            else if (result is BlittableJsonReaderArray)
            {
                var blitResult = result as BlittableJsonReaderArray;
                for (var i = 0; i < blitResult.Length; i++)
                {
                    var item = blitResult[i];

                    if (item is BlittableJsonReaderBase)
                    {
                        var itemAsBlittable = item as BlittableJsonReaderBase;
                        foreach (var subItem in itemAsBlittable.SelectTokenWithRavenSyntaxReturningFlatStructure(string.Join("[].", pathParts.Skip(1).ToArray())))
                        {
                            yield return(subItem);
                        }
                    }
                    else
                    {
                        yield return(Tuple.Create(item, result));
                    }
                }
            }
            else if (result == null)
            {
                yield break;
            }
            else
            {
                throw new ArgumentException($"Illegal path ('{path}'), cannot understand how to get tokens from: {result} <{result.GetType().FullName}>");
            }
        }
Exemplo n.º 7
0
        internal object Evaluate(BlittableJsonReaderBase root, bool errorWhenNoMatch)
        {
            object current = root;

            foreach (var part in Parts)
            {
                var propertyName = part as string;
                if (propertyName != null)
                {
                    var o = current as BlittableJsonReaderObject;
                    if (o != null)
                    {
                        if (o.TryGet(propertyName, out current) == false)
                        {
                            current = null;
                        }

                        if (current == null && errorWhenNoMatch)
                        {
                            string.Format(CultureInfo.InvariantCulture, "Property '{0}' does not exist on JSON.", propertyName);
                        }
                    }
                    else
                    {
                        var array = current as BlittableJsonReaderArray;
                        if (array != null)
                        {
                            switch (propertyName)
                            {
                            case "Count":
                            case "count":
                            case "Length":
                            case "length":
                                current = array.Length;
                                break;

                            default:
                                if (errorWhenNoMatch)
                                {
                                    string.Format(CultureInfo.InvariantCulture, "Property '{0}' not valid on {1}.", current.GetType().Name);
                                }

                                break;
                            }
                            continue;
                        }

                        if (errorWhenNoMatch)
                        {
                            string.Format(CultureInfo.InvariantCulture, "Property '{0}' not valid on {1}.", current.GetType().Name);
                        }

                        return(null);
                    }
                }
                else
                {
                    var index = (int)part;


                    var a = current as BlittableJsonReaderArray;

                    if (a != null)
                    {
                        if (a.Length <= index)
                        {
                            if (errorWhenNoMatch)
                            {
                                string.Format(CultureInfo.InvariantCulture, "Index {0} outside the bounds of JSON.", index);
                            }

                            return(null);
                        }

                        current = a[index];
                    }
                    else
                    {
                        var b = current as BlittableJsonReaderObject;

                        if (a.Length <= index)
                        {
                            if (errorWhenNoMatch)
                            {
                                string.Format(CultureInfo.InvariantCulture, "Index {0} outside the bounds of JSON.", index);
                            }

                            return(null);
                        }
                        var trueIndex = b.GetPropertiesByInsertionOrder()[index];
                        var prop      = new BlittableJsonReaderObject.PropertyDetails();

                        b.GetPropertyByIndex(trueIndex, ref prop);

                        current = prop.Value;
                    }
                }
            }

            return(current);
        }
Exemplo n.º 8
0
        internal object Evaluate(BlittableJsonReaderBase root)
        {
            object current = root;

            foreach (var part in Parts)
            {
                var propertyName = part as string;
                if (propertyName != null)
                {
                    var o = current as BlittableJsonReaderObject;
                    if (o != null)
                    {
                        if (o.TryGet(propertyName, out current) == false)
                        {
                            current = null;
                        }

                        if (current == null)
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        var array = current as BlittableJsonReaderArray;
                        if (array != null)
                        {
                            switch (propertyName)
                            {
                            case "Count":
                            case "count":
                            case "Length":
                            case "length":
                                current = array.Length;
                                break;

                            default:
                                return(null);
                            }
                            continue;
                        }

                        return(null);
                    }
                }
                else
                {
                    var index = (int)part;


                    var a = current as BlittableJsonReaderArray;

                    if (a != null)
                    {
                        if (a.Length <= index)
                        {
                            return(null);
                        }

                        current = a[index];
                    }
                    else
                    {
                        return(null);
                    }
                }
            }

            return(current);
        }
Exemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="self"></param>
        /// <param name="path"></param>
        /// <param name="createSnapshots">Set to true if you want to modify selected objects</param>
        /// <returns></returns>
        public static IEnumerable <Tuple <object, object> > SelectTokenWithRavenSyntaxReturningFlatStructure(this BlittableJsonReaderBase self, string path, bool createSnapshots = false)
        {
            var    pathParts = path.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            object result    = null;

            result = new BlitPath(pathParts[0]).Evaluate(self, false);

            if (pathParts.Length == 1)
            {
                yield return(Tuple.Create(result, (object)self));

                yield break;
            }

            if (result is BlittableJsonReaderObject)
            {
                var blitResult = result as BlittableJsonReaderObject;
                blitResult.TryGetMember("$values", out result);

                var prop = new BlittableJsonReaderObject.PropertyDetails();

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

                    if (prop.Value is BlittableJsonReaderBase)
                    {
                        var itemAsBlittable = prop.Value as BlittableJsonReaderBase;
                        foreach (var subItem in itemAsBlittable.SelectTokenWithRavenSyntaxReturningFlatStructure(string.Join(",", pathParts.Skip(1).ToArray())))
                        {
                            yield return(subItem);
                        }
                    }
                    else
                    {
                        yield return(Tuple.Create((object)prop.Value, result));
                    }
                }
            }
            else if (result is BlittableJsonReaderArray)
            {
                var blitResult = result as BlittableJsonReaderArray;
                for (var i = 0; i < blitResult.Length; i++)
                {
                    var item = blitResult[i];

                    if (item is BlittableJsonReaderBase)
                    {
                        var itemAsBlittable = item as BlittableJsonReaderBase;
                        foreach (var subItem in itemAsBlittable.SelectTokenWithRavenSyntaxReturningFlatStructure(string.Join(",", pathParts.Skip(1).ToArray())))
                        {
                            yield return(subItem);
                        }
                    }
                    else
                    {
                        yield return(Tuple.Create((object)item, result));
                    }
                }
            }
            else
            {
                throw new ArgumentException("Illegal path");
            }
        }