コード例 #1
0
        public DynamicJsonValue ToTimeSeriesEntryJson(double scale = 1)
        {
            var values = new DynamicJsonArray();

            foreach (var t in Values.Span)
            {
                values.Add(scale * t);
            }

            return(new DynamicJsonValue
            {
                [nameof(TimeSeriesEntry.Tag)] = Tag?.ToString(),
                [nameof(TimeSeriesEntry.Timestamp)] = Timestamp,
                [nameof(TimeSeriesEntry.Values)] = values,
                [nameof(TimeSeriesEntry.IsRollup)] = Type == SingleResultType.RolledUp,
            });
        }
コード例 #2
0
        public DynamicJsonValue ToJson()
        {
            var json = new DynamicJsonValue
            {
                ["Id"]                 = LowerId.ToString(),
                [nameof(Etag)]         = Etag,
                [nameof(DeletedEtag)]  = DeletedEtag,
                [nameof(Type)]         = Type.ToString(),
                [nameof(ChangeVector)] = ChangeVector
            };

            if (Type != TombstoneType.Attachment)
            {
                json[nameof(Collection)] = Collection.ToString();
            }

            return(json);
        }
コード例 #3
0
        private object Clone(object value)
        {
            if (value == null || value == NullBucket)
            {
                return(NullBucket);
            }

            return(value switch
            {
                LazyStringValue lsv => lsv.ToString(),
                string s => s,
                DateTime time => time.ToString("O"),
                DateTimeOffset offset => offset.ToString("O"),
                TimeSpan span => span.ToString("c"),
                ValueType _ => value,
                LazyCompressedStringValue lcsv => lcsv.ToString(),
                LazyNumberValue lnv => lnv.ToDouble(CultureInfo.InvariantCulture),
                BlittableJsonReaderObject json => json.CloneOnTheSameContext(),
                BlittableJsonReaderArray arr => arr.Clone(_context),
                Document doc => doc,
                _ => throw new NotSupportedException($"Unable to group by type: {value.GetType()}")
            });
コード例 #4
0
        private JsValue ConstructGrouping(List <BlittableJsonReaderObject> values)
        {
            var jsValues = ConstructValues();
            var jsKey    = ConstructKey();

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

            result.Put("values", jsValues, false);
            result.Put("key", jsKey, false);

            return(result);

            JsValue ConstructKey()
            {
                if (_singleField)
                {
                    var index = values[0].GetPropertyIndex(_groupByFields[0].Name);
                    if (index != -1)
                    {
                        BlittableJsonReaderObject.PropertyDetails prop = default;
                        values[0].GetPropertyByIndex(index, ref prop);

                        return(JsValue.FromObject(Engine, prop.Value));
                    }

                    return(JsValue.Null);
                }

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

                foreach (var groupByField in _groupByFields)
                {
                    var index = values[0].GetPropertyIndex(groupByField.Name);
                    if (index != -1)
                    {
                        BlittableJsonReaderObject.PropertyDetails prop = default;
                        values[0].GetPropertyByIndex(index, ref prop);

                        var propertyName = groupByField.Name;
                        if (groupByField is JsNestedField jsnf)
                        {
                            propertyName = jsnf.PropertyName;
                        }

                        var value = groupByField.GetValue(null, prop.Value);

                        JsValue jsValue = value switch
                        {
                            BlittableJsonReaderObject bjro => new BlittableObjectInstance(Engine, null, bjro, null, null, null),
                            Document doc => new BlittableObjectInstance(Engine, null, doc.Data, doc),
                            LazyStringValue lsv => new JsString(lsv.ToString()),
                            LazyCompressedStringValue lcsv => new JsString(lcsv.ToString()),
                            LazyNumberValue lnv => new JsNumber(lnv.ToDouble(CultureInfo.InvariantCulture)),
                            _ => JsValue.FromObject(Engine, value)
                        };

                        key.Put(propertyName, jsValue, throwOnError: false);
                    }
                }

                return(key);
            }

            ArrayInstance ConstructValues()
            {
                var items = new PropertyDescriptor[values.Count];

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

                    if (JavaScriptIndexUtils.GetValue(Engine, val, out var jsValue, isMapReduce: true) == false)
                    {
                        continue;
                    }

                    items[i] = new PropertyDescriptor(jsValue, true, true, true);
                }

                var jsArray = new ArrayInstance(Engine, items)
                {
                    Prototype  = Engine.Array.PrototypeObject,
                    Extensible = false
                };

                return(jsArray);
            }
        }