private PartitionKey CosmosElementToPartitionKeyObject(CosmosElement cosmosElement)
        {
            // TODO: Leverage original serialization and avoid re-serialization (bug)
            switch (cosmosElement.Type)
            {
            case CosmosElementType.String:
                CosmosString cosmosString = cosmosElement as CosmosString;
                return(new PartitionKey(cosmosString.Value));

            case CosmosElementType.Number:
                CosmosNumber cosmosNumber = cosmosElement as CosmosNumber;
                double       value        = Number64.ToDouble(cosmosNumber.Value);
                return(new PartitionKey(value));

            case CosmosElementType.Boolean:
                CosmosBoolean cosmosBool = cosmosElement as CosmosBoolean;
                return(new PartitionKey(cosmosBool.Value));

            case CosmosElementType.Null:
                return(PartitionKey.Null);

            default:
                throw new ArgumentException(
                          string.Format(CultureInfo.InvariantCulture, RMResources.UnsupportedPartitionKeyComponentValue, cosmosElement));
            }
        }
예제 #2
0
        private object CosmosElementToPartitionKeyObject(CosmosElement cosmosElement)
        {
            // TODO: Leverage original serialization and avoid re-serialization (bug)
            switch (cosmosElement.Type)
            {
            case CosmosElementType.String:
                CosmosString cosmosString = cosmosElement as CosmosString;
                return(cosmosString.Value);

            case CosmosElementType.Number:
                CosmosNumber cosmosNumber = cosmosElement as CosmosNumber;
                return(cosmosNumber.AsFloatingPoint());

            case CosmosElementType.Boolean:
                CosmosBoolean cosmosBool = cosmosElement as CosmosBoolean;
                return(cosmosBool.Value);

            case CosmosElementType.Guid:
                CosmosGuid cosmosGuid = cosmosElement as CosmosGuid;
                return(cosmosGuid.Value);

            default:
                throw new ArgumentException(
                          string.Format(CultureInfo.InvariantCulture, RMResources.UnsupportedPartitionKeyComponentValue, cosmosElement));
            }
        }
예제 #3
0
        private static IEnumerable <CosmosElement> ExecuteOffsetLimitClause(
            IEnumerable <CosmosElement> dataSource,
            SqlOffsetLimitClause sqlOffsetLimitClause)
        {
            SqlOffsetSpec sqlOffsetSpec = sqlOffsetLimitClause.OffsetSpec;

            if (sqlOffsetSpec != null)
            {
                CosmosNumber cosmosOffsetValue = (CosmosNumber)sqlOffsetSpec.OffsetExpression.Accept(
                    ScalarExpressionEvaluator.Singleton,
                    input: null);
                long offsetCount = Number64.ToLong(cosmosOffsetValue.Value);
                dataSource = dataSource.Skip((int)offsetCount);
            }

            SqlLimitSpec sqlLimitSpec = sqlOffsetLimitClause.LimitSpec;

            if (sqlLimitSpec != null)
            {
                CosmosNumber cosmosLimitValue = (CosmosNumber)sqlLimitSpec.LimitExpression.Accept(
                    ScalarExpressionEvaluator.Singleton,
                    input: null);
                long limitCount = Number64.ToLong(cosmosLimitValue.Value);
                dataSource = dataSource.Take((int)limitCount);
            }

            return(dataSource);
        }
        private static PartitionKey CosmosElementToPartitionKeyObject(IReadOnlyList <CosmosElement> cosmosElementList)
        {
            PartitionKeyBuilder partitionKeyBuilder = new PartitionKeyBuilder();

            foreach (CosmosElement cosmosElement in cosmosElementList)
            {
                if (cosmosElement == null)
                {
                    partitionKeyBuilder.AddNoneType();
                }
                else
                {
                    _ = cosmosElement switch
                    {
                        CosmosString cosmosString => partitionKeyBuilder.Add(cosmosString.Value),
                        CosmosNumber cosmosNumber => partitionKeyBuilder.Add(Number64.ToDouble(cosmosNumber.Value)),
                        CosmosBoolean cosmosBoolean => partitionKeyBuilder.Add(cosmosBoolean.Value),
                        CosmosNull _ => partitionKeyBuilder.AddNullValue(),
                        _ => throw new ArgumentException(
                                  string.Format(
                                      CultureInfo.InvariantCulture,
                                      RMResources.UnsupportedPartitionKeyComponentValue,
                                      cosmosElement)),
                    };
                }
            }

            return(partitionKeyBuilder.Build());
        }
            /// <summary>
            /// Returns the average or undefined if any of the intermediate averages resulted in an undefined value.
            /// </summary>
            /// <returns>The average or undefined if any of the intermediate averages resulted in an undefined value.</returns>
            public CosmosNumber GetAverage()
            {
                if (!this.Sum.HasValue || this.Count <= 0)
                {
                    return(null);
                }

                return(CosmosNumber.Create(this.Sum.Value / this.Count));
            }
예제 #6
0
        /// <summary>
        /// Gets the current sum.
        /// </summary>
        /// <returns>The current sum.</returns>
        public CosmosElement GetResult()
        {
            if (double.IsNaN(this.globalSum))
            {
                return(null);
            }

            return(CosmosNumber.Create(this.globalSum));
        }
        /// <summary>
        /// Gets the hash of a JToken given a seed.
        /// </summary>
        /// <param name="cosmosElement">The cosmos element to hash.</param>
        /// <param name="seed">The seed to use.</param>
        /// <returns>The hash of the JToken.</returns>
        private static UInt192 GetHash(CosmosElement cosmosElement, UInt192 seed)
        {
            if (cosmosElement == null)
            {
                return(DistinctHash.GetUndefinedHash(seed));
            }

            CosmosElementType cosmosElementType = cosmosElement.Type;
            UInt192           hash;

            switch (cosmosElementType)
            {
            case CosmosElementType.Array:
                hash = DistinctHash.GetArrayHash(cosmosElement as CosmosArray, seed);
                break;

            case CosmosElementType.Boolean:
                hash = DistinctHash.GetBooleanHash((cosmosElement as CosmosBoolean).Value, seed);
                break;

            case CosmosElementType.Null:
                hash = DistinctHash.GetNullHash(seed);
                break;

            case CosmosElementType.Number:
                // TODO: we need to differentiate between the different number types.
                CosmosNumber cosmosNumber = cosmosElement as CosmosNumber;
                double       number;
                if (cosmosNumber.IsFloatingPoint)
                {
                    number = cosmosNumber.AsFloatingPoint().Value;
                }
                else
                {
                    number = cosmosNumber.AsInteger().Value;
                }

                hash = DistinctHash.GetNumberHash(number, seed);
                break;

            case CosmosElementType.Object:
                hash = DistinctHash.GetObjectHash(cosmosElement as CosmosObject, seed);
                break;

            case CosmosElementType.String:
                hash = DistinctHash.GetStringHash((cosmosElement as CosmosString).Value, seed);
                break;

            default:
                throw new ArgumentException($"Unexpected {nameof(CosmosElementType)} : {cosmosElementType}");
            }

            return(hash);
        }
예제 #8
0
 private static void VisitCosmosNumber(CosmosNumber cosmosNumber, IJsonWriter jsonWriter)
 {
     if (cosmosNumber.IsFloatingPoint)
     {
         jsonWriter.WriteNumberValue(cosmosNumber.AsFloatingPoint().Value);
     }
     else
     {
         jsonWriter.WriteNumberValue(cosmosNumber.AsInteger().Value);
     }
 }
예제 #9
0
            /// <summary>
            /// Gets the hash of a JToken given a seed.
            /// </summary>
            /// <param name="cosmosElement">The cosmos element to hash.</param>
            /// <param name="seed">The seed to use.</param>
            /// <returns>The hash of the JToken.</returns>
            private UInt192 GetHashToken(CosmosElement cosmosElement, UInt192 seed)
            {
                if (cosmosElement == null)
                {
                    return(this.GetUndefinedHash(seed));
                }

                CosmosElementType cosmosElementType = cosmosElement.Type;
                UInt192           hash;

                switch (cosmosElementType)
                {
                case CosmosElementType.Array:
                    hash = this.GetArrayHash(cosmosElement as CosmosArray, seed);
                    break;

                case CosmosElementType.Boolean:
                    hash = this.GetBooleanHash((cosmosElement as CosmosBoolean).Value, seed);
                    break;

                case CosmosElementType.Null:
                    hash = this.GetNullHash(seed);
                    break;

                case CosmosElementType.Number:
                    CosmosNumber cosmosNumber = (cosmosElement as CosmosNumber);
                    double       number;
                    if (cosmosNumber.IsFloatingPoint)
                    {
                        number = cosmosNumber.AsFloatingPoint().Value;
                    }
                    else
                    {
                        number = cosmosNumber.AsInteger().Value;
                    }

                    hash = this.GetNumberHash(number, seed);
                    break;

                case CosmosElementType.Object:
                    hash = this.GetObjectHash(cosmosElement as CosmosObject, seed);
                    break;

                case CosmosElementType.String:
                    hash = this.GetStringHash((cosmosElement as CosmosString).Value, seed);
                    break;

                default:
                    throw new ArgumentException($"Unexpected {nameof(CosmosElementType)} : {cosmosElementType}");
                }

                return(hash);
            }
예제 #10
0
            /// <summary>
            /// Adds a JToken to this map if it hasn't already been added.
            /// </summary>
            /// <param name="cosmosElement">The element to add.</param>
            /// <param name="hash">The hash of the token.</param>
            /// <returns>Whether or not the item was added to this Distinct Map.</returns>
            public override bool Add(CosmosElement cosmosElement, out UInt192?hash)
            {
                // Unordered distinct does not need to return a valid hash.
                // Since it doesn't need the last hash for a continuation.
                hash = null;
                bool added = false;
                CosmosElementType cosmosElementType = cosmosElement.Type;

                switch (cosmosElementType)
                {
                case CosmosElementType.Array:
                    added = this.AddArrayValue(cosmosElement as CosmosArray);
                    break;

                case CosmosElementType.Boolean:
                    added = this.AddSimpleValue((cosmosElement as CosmosBoolean).Value ? SimpleValues.True : SimpleValues.False);
                    break;

                case CosmosElementType.Null:
                    added = this.AddSimpleValue(SimpleValues.Null);
                    break;

                case CosmosElementType.Number:
                    CosmosNumber cosmosNumber = cosmosElement as CosmosNumber;
                    double       number;
                    if (cosmosNumber.IsFloatingPoint)
                    {
                        number = cosmosNumber.AsFloatingPoint().Value;
                    }
                    else
                    {
                        number = cosmosNumber.AsInteger().Value;
                    }

                    added = this.AddNumberValue(number);
                    break;

                case CosmosElementType.Object:
                    added = this.AddObjectValue(cosmosElement as CosmosObject);
                    break;

                case CosmosElementType.String:
                    added = this.AddStringValue((cosmosElement as CosmosString).Value);
                    break;

                default:
                    throw new ArgumentException($"Unexpected {nameof(CosmosElementType)}: {cosmosElementType}");
                }

                return(added);
            }
        public static MockQueryPipelineStage Create(
            IReadOnlyList <IReadOnlyList <CosmosElement> > pages,
            CosmosElement continuationToken)
        {
            MockQueryPipelineStage stage = new MockQueryPipelineStage(pages);

            if (continuationToken != null)
            {
                CosmosNumber index = continuationToken as CosmosNumber;
                stage.PageIndex = Number64.ToLong(index.Value);
            }

            return(stage);
        }
            public TryCatch <object> Visit(CosmosArray cosmosArray, Type type)
            {
                bool isReadOnlyList = type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IReadOnlyList <>));

                if (!isReadOnlyList)
                {
                    return(TryCatch <object> .FromException(Visitor.Exceptions.ExpectedArray));
                }

                Type genericArgumentType = type.GenericTypeArguments.First();

                Type  listType = typeof(List <>).MakeGenericType(genericArgumentType);
                IList list     = (IList)Activator.CreateInstance(listType);

                foreach (CosmosElement arrayItem in cosmosArray)
                {
                    TryCatch <object> tryGetMaterializedArrayItem;
                    if (genericArgumentType == typeof(object))
                    {
                        Type dotNetType = arrayItem switch
                        {
                            CosmosArray _ => typeof(IReadOnlyList <object>),
                            CosmosBoolean _ => typeof(bool),
                            CosmosNull _ => typeof(object),
                            CosmosNumber _ => typeof(Number64),
                            CosmosObject _ => typeof(object),
                            CosmosString _ => typeof(string),
                            CosmosGuid _ => typeof(Guid),
                            CosmosBinary _ => typeof(ReadOnlyMemory <byte>),
                            _ => throw new ArgumentOutOfRangeException($"Unknown cosmos element type."),
                        };

                        tryGetMaterializedArrayItem = arrayItem.Accept(this, dotNetType);
                    }
                    else
                    {
                        tryGetMaterializedArrayItem = arrayItem.Accept(this, genericArgumentType);
                    }

                    if (tryGetMaterializedArrayItem.Failed)
                    {
                        return(tryGetMaterializedArrayItem);
                    }

                    list.Add(tryGetMaterializedArrayItem.Result);
                }

                return(TryCatch <object> .FromResult(list));
            }
예제 #13
0
        public bool Visit(CosmosNumber cosmosNumber)
        {
            if (cosmosNumber.Value.IsInteger)
            {
                return(false);
            }

            double value = Number64.ToDouble(cosmosNumber.Value);

            return
                (double.IsInfinity(value) ||
                 double.IsNaN(value) ||
                 double.IsNegativeInfinity(value) ||
                 double.IsPositiveInfinity(value));
        }
예제 #14
0
 /// <summary>
 /// Adds a JToken to this map if it hasn't already been added.
 /// </summary>
 /// <param name="cosmosElement">The element to add.</param>
 /// <param name="hash">The hash of the token.</param>
 /// <returns>Whether or not the item was added to this Distinct Map.</returns>
 public override bool Add(CosmosElement cosmosElement, out UInt128 hash)
 {
     // Unordered distinct does not need to return a valid hash.
     // Since it doesn't need the last hash for a continuation.
     hash = default;
     return(cosmosElement switch
     {
         CosmosArray cosmosArray => this.AddArrayValue(cosmosArray),
         CosmosBoolean cosmosBoolean => this.AddSimpleValue(cosmosBoolean.Value ? SimpleValues.True : SimpleValues.False),
         CosmosNull _ => this.AddSimpleValue(SimpleValues.Null),
         CosmosNumber cosmosNumber => this.AddNumberValue(cosmosNumber.Value),
         CosmosObject cosmosObject => this.AddObjectValue(cosmosObject),
         CosmosString cosmosString => this.AddStringValue(cosmosString.Value),
         _ => throw new ArgumentOutOfRangeException($"Unexpected {nameof(CosmosElement)}: {cosmosElement}"),
     });
예제 #15
0
        public async Task DCount()
        {
            List <CosmosObject> documents = new List <CosmosObject>();

            for (int i = 0; i < 250; i++)
            {
                documents.Add(CosmosObject.Parse($"{{\"pk\" : {i}, \"val\": {i % 50} }}"));
            }

            List <CosmosElement> documentsQueried = await ExecuteQueryAsync(
                query : "SELECT VALUE COUNT(1) FROM (SELECT DISTINCT VALUE c.val FROM c)",
                documents : documents);

            Assert.AreEqual(expected: 1, actual: documentsQueried.Count);
            Assert.IsTrue(documentsQueried[0] is CosmosNumber);
            CosmosNumber result = documentsQueried[0] as CosmosNumber;

            Assert.AreEqual(expected: 50, actual: result);
        }
예제 #16
0
        private static IEnumerable <CosmosElement> ExecuteSelectClause(
            IEnumerable <IGrouping <GroupByKey, CosmosElement> > groupings,
            SqlSelectClause sqlSelectClause)
        {
            IEnumerable <CosmosElement> dataSource = ProjectOnGroupings(
                groupings,
                sqlSelectClause);

            if (sqlSelectClause.HasDistinct)
            {
                dataSource = dataSource.Distinct();
            }

            if (sqlSelectClause.TopSpec != null)
            {
                CosmosNumber cosmosTopValue = (CosmosNumber)sqlSelectClause.TopSpec.TopExpresion.Accept(
                    ScalarExpressionEvaluator.Singleton,
                    input: null);
                long topCount = Number64.ToLong(cosmosTopValue.Value);
                dataSource = dataSource.Take((int)topCount);
            }

            return(dataSource);
        }
예제 #17
0
 public int Visit(CosmosNumber cosmosNumber)
 {
     return(2);
 }
 private static void VisitCosmosNumber(CosmosNumber cosmosNumber, IJsonWriter jsonWriter)
 {
     jsonWriter.WriteNumber64Value(cosmosNumber.Value);
 }
 public int Visit(CosmosNumber cosmosNumber) => 2;
예제 #20
0
        internal void SerializeAndDeserializeEdgeDocumentTest(JsonSerializationFormat jsonSerializationFormat)
        {
            // Constants to use for vertex document property key/values
            const string idName                = "id";
            const string idValue               = "e_0";
            const string pkValue               = "pk_0";
            const string labelName             = "label";
            const string labelValue            = "l_0";
            const string vertexIdValue         = "v_0";
            const string vertexLabelValue      = "l_1";
            const string sinkIdValue           = "v_1";
            const string sinkLabelValue        = "l_2";
            const string sinkPartitionValue    = "pk_1";
            const bool   isEdgeValue           = true;
            const bool   isPkEdgePropertyValue = true;
            const string boolName              = "myBool";
            const bool   boolValue             = true;
            const string intName               = "myInteger";
            const int    intValue              = 12345;
            const string longName              = "myLong";
            const long   longValue             = 67890L;
            const string floatName             = "myFloatingPoint";
            const float  floatValue            = 123.4f;
            const string doubleName            = "myDouble";
            const double doubleValue           = 56.78;
            const string stringName            = "myString";
            const string stringValue           = "str_0";

            Dictionary <string, CosmosElement> edgeDocumentProperties = new Dictionary <string, CosmosElement>()
            {
                { idName, CosmosString.Create(idValue) },
                { GremlinScenarioTests.PartitionKeyPropertyName, CosmosString.Create(pkValue) },
                { labelName, CosmosString.Create(labelValue) },
                { GremlinKeywords.KW_EDGEDOC_VERTEXID, CosmosString.Create(vertexIdValue) },
                { GremlinKeywords.KW_EDGEDOC_VERTEXLABEL, CosmosString.Create(vertexLabelValue) },
                { GremlinKeywords.KW_EDGE_SINKV, CosmosString.Create(sinkIdValue) },
                { GremlinKeywords.KW_EDGE_SINKV_LABEL, CosmosString.Create(sinkLabelValue) },
                { GremlinKeywords.KW_EDGE_SINKV_PARTITION, CosmosString.Create(sinkPartitionValue) },
                { GremlinKeywords.KW_EDGEDOC_IDENTIFIER, CosmosBoolean.Create(isEdgeValue) },
                { GremlinKeywords.KW_EDGEDOC_ISPKPROPERTY, CosmosBoolean.Create(isPkEdgePropertyValue) },
                { boolName, CosmosBoolean.Create(boolValue) },
                { intName, CosmosNumber64.Create(intValue) },
                { longName, CosmosNumber64.Create(longValue) },
                { floatName, CosmosNumber64.Create(floatValue) },
                { doubleName, CosmosNumber64.Create(doubleValue) },
                { stringName, CosmosString.Create(stringValue) },
            };

            CosmosObject edgeEagerObject = CosmosObject.Create(edgeDocumentProperties);

            // Serialize the edge object into a document using the specified serialization format
            IJsonWriter jsonWriter = JsonWriter.Create(jsonSerializationFormat);

            edgeEagerObject.WriteTo(jsonWriter);
            ReadOnlyMemory <byte> jsonResult = jsonWriter.GetResult();

            Assert.IsTrue(jsonResult.Length > 0, "IJsonWriter result data is empty.");

            // Navigate into the serialized edge document using lazy CosmosElements
            CosmosElement rootLazyElement = CosmosElement.CreateFromBuffer(jsonResult);

            // Validate the expected edge document structure/values

            // Root edge document object
            CosmosObject edgeLazyObject = rootLazyElement as CosmosObject;

            Assert.IsNotNull(edgeLazyObject, $"Edge document root is not {nameof(CosmosObject)}.");
            Assert.AreEqual(edgeDocumentProperties.Count, edgeLazyObject.Count);

            // Edge system document properties
            CosmosString idLazyString = this.GetAndAssertObjectProperty <CosmosString>(edgeLazyObject, idName);

            Assert.AreEqual(idValue, idLazyString.Value);

            CosmosString pkLazyString = this.GetAndAssertObjectProperty <CosmosString>(edgeLazyObject, GremlinScenarioTests.PartitionKeyPropertyName);

            Assert.AreEqual(pkValue, pkLazyString.Value);

            CosmosString labelLazyString = this.GetAndAssertObjectProperty <CosmosString>(edgeLazyObject, labelName);

            Assert.AreEqual(labelValue, labelLazyString.Value);

            CosmosString vertexIdLazyString = this.GetAndAssertObjectProperty <CosmosString>(edgeLazyObject, GremlinKeywords.KW_EDGEDOC_VERTEXID);

            Assert.AreEqual(vertexIdValue, vertexIdLazyString.Value);

            CosmosString vertexLabelLazyString = this.GetAndAssertObjectProperty <CosmosString>(edgeLazyObject, GremlinKeywords.KW_EDGEDOC_VERTEXLABEL);

            Assert.AreEqual(vertexLabelValue, vertexLabelLazyString.Value);

            CosmosString sinkIdLazyString = this.GetAndAssertObjectProperty <CosmosString>(edgeLazyObject, GremlinKeywords.KW_EDGE_SINKV);

            Assert.AreEqual(sinkIdValue, sinkIdLazyString.Value);

            CosmosString sinkLabelLazyString = this.GetAndAssertObjectProperty <CosmosString>(edgeLazyObject, GremlinKeywords.KW_EDGE_SINKV_LABEL);

            Assert.AreEqual(sinkLabelValue, sinkLabelLazyString.Value);

            CosmosString sinkPartitionLazyString = this.GetAndAssertObjectProperty <CosmosString>(edgeLazyObject, GremlinKeywords.KW_EDGE_SINKV_PARTITION);

            Assert.AreEqual(sinkPartitionValue, sinkPartitionLazyString.Value);

            CosmosBoolean isEdgeLazyBool = this.GetAndAssertObjectProperty <CosmosBoolean>(edgeLazyObject, GremlinKeywords.KW_EDGEDOC_IDENTIFIER);

            Assert.AreEqual(isEdgeValue, isEdgeLazyBool.Value);

            CosmosBoolean isPkEdgePropertyLazyBool = this.GetAndAssertObjectProperty <CosmosBoolean>(edgeLazyObject, GremlinKeywords.KW_EDGEDOC_ISPKPROPERTY);

            Assert.AreEqual(isPkEdgePropertyValue, isPkEdgePropertyLazyBool.Value);

            // Edge user properties

            CosmosBoolean boolValueLazyBool = this.GetAndAssertObjectProperty <CosmosBoolean>(edgeLazyObject, boolName);

            Assert.AreEqual(boolValue, boolValueLazyBool.Value);

            CosmosNumber intValueLazyNumber = this.GetAndAssertObjectProperty <CosmosNumber>(edgeLazyObject, intName);

            Assert.AreEqual(CosmosNumberType.Number64, intValueLazyNumber.NumberType);
            Assert.IsTrue(intValueLazyNumber.IsInteger);
            Assert.AreEqual((long)intValue, intValueLazyNumber.AsInteger().Value);

            CosmosNumber longValueLazyNumber = this.GetAndAssertObjectProperty <CosmosNumber>(edgeLazyObject, longName);

            Assert.AreEqual(CosmosNumberType.Number64, intValueLazyNumber.NumberType);
            Assert.IsTrue(intValueLazyNumber.IsInteger);
            Assert.AreEqual(longValue, longValueLazyNumber.AsInteger().Value);

            CosmosNumber floatValueLazyNumber = this.GetAndAssertObjectProperty <CosmosNumber>(edgeLazyObject, floatName);

            Assert.AreEqual(CosmosNumberType.Number64, floatValueLazyNumber.NumberType);
            Assert.IsTrue(floatValueLazyNumber.IsFloatingPoint);
            Assert.AreEqual((double)floatValue, floatValueLazyNumber.AsFloatingPoint().Value);

            CosmosNumber doubleValueLazyNumber = this.GetAndAssertObjectProperty <CosmosNumber>(edgeLazyObject, doubleName);

            Assert.AreEqual(CosmosNumberType.Number64, doubleValueLazyNumber.NumberType);
            Assert.IsTrue(doubleValueLazyNumber.IsFloatingPoint);
            Assert.AreEqual((double)doubleValue, doubleValueLazyNumber.AsFloatingPoint().Value);

            CosmosString stringValueLazyString = this.GetAndAssertObjectProperty <CosmosString>(edgeLazyObject, stringName);

            Assert.AreEqual(stringValue, stringValueLazyString.Value);
        }
 public int Visit(CosmosNumber cosmosNumber, CosmosElement input) => cosmosNumber.CompareTo((CosmosNumber)input);
예제 #22
0
        /// <summary>
        /// Compares to objects and returns their partial sort relationship.
        /// </summary>
        /// <param name="element1">The first element to compare.</param>
        /// <param name="element2">The second element to compare.</param>
        /// <returns>
        /// Less than zero if obj1 comes before obj2 in the sort order.
        /// Zero if obj1 and obj2 are interchangeable in the sort order.
        /// Greater than zero if obj2 comes before obj1 in the sort order.
        /// </returns>
        public int Compare(CosmosElement element1, CosmosElement element2)
        {
            if (object.ReferenceEquals(element1, element2))
            {
                return(0);
            }

            if (object.ReferenceEquals(element1, MinValueItem.Singleton))
            {
                return(-1);
            }

            if (object.ReferenceEquals(element2, MinValueItem.Singleton))
            {
                return(1);
            }

            if (object.ReferenceEquals(element1, MaxValueItem.Singleton))
            {
                return(1);
            }

            if (object.ReferenceEquals(element2, MaxValueItem.Singleton))
            {
                return(-1);
            }

            if (element1 == Undefined)
            {
                return(-1);
            }

            if (element2 == Undefined)
            {
                return(1);
            }

            CosmosElementType type1 = element1.Type;

            int cmp = CompareTypes(element1, element2);

            if (cmp == 0)
            {
                // If they are the same type then you need to break the tie.
                switch (type1)
                {
                case CosmosElementType.Boolean:
                    cmp = Comparer <bool> .Default.Compare(
                        (element1 as CosmosBoolean).Value,
                        (element2 as CosmosBoolean).Value);

                    break;

                case CosmosElementType.Null:
                    // All nulls are the same.
                    cmp = 0;
                    break;

                case CosmosElementType.Number:
                    CosmosNumber number1 = element1 as CosmosNumber;
                    CosmosNumber number2 = element2 as CosmosNumber;
                    if (number1.NumberType == CosmosNumberType.Number64)
                    {
                        double double1;
                        if (number1.IsFloatingPoint)
                        {
                            double1 = number1.AsFloatingPoint().Value;
                        }
                        else
                        {
                            double1 = number1.AsInteger().Value;
                        }

                        double double2;
                        if (number2.IsFloatingPoint)
                        {
                            double2 = number2.AsFloatingPoint().Value;
                        }
                        else
                        {
                            double2 = number2.AsInteger().Value;
                        }

                        cmp = Comparer <double> .Default.Compare(
                            double1,
                            double2);
                    }
                    else if (number1.IsFloatingPoint)
                    {
                        double double1 = number1.AsFloatingPoint().Value;
                        double double2 = number2.AsFloatingPoint().Value;
                        cmp = Comparer <double> .Default.Compare(double1, double2);
                    }
                    else
                    {
                        long integer1 = number1.AsInteger().Value;
                        long integer2 = number2.AsInteger().Value;
                        cmp = Comparer <long> .Default.Compare(integer1, integer2);
                    }

                    break;

                case CosmosElementType.String:
                    CosmosString string1 = element1 as CosmosString;
                    CosmosString string2 = element2 as CosmosString;
                    cmp = string.CompareOrdinal(
                        string1.Value,
                        string2.Value);
                    break;

                case CosmosElementType.Guid:
                    CosmosGuid guid1 = element1 as CosmosGuid;
                    CosmosGuid guid2 = element2 as CosmosGuid;
                    cmp = guid1.Value.CompareTo(guid2.Value);
                    break;

                case CosmosElementType.Binary:
                    CosmosBinary binary1 = element1 as CosmosBinary;
                    CosmosBinary binary2 = element2 as CosmosBinary;
                    cmp = ItemComparer.CompareTo(binary1, binary2);
                    break;

                default:
                    throw new ArgumentException($"Unknown: {nameof(CosmosElementType)}: {type1}");
                }
            }

            return(cmp);
        }
예제 #23
0
        internal void SerializeAndDeserializeVertexDocumentTest(JsonSerializationFormat jsonSerializationFormat)
        {
            // Constants to use for vertex document property key/values
            const string idName             = "id";
            const string idValue            = "v_0";
            const string pkValue            = "pk_0";
            const string labelName          = "label";
            const string labelValue         = "l_0";
            const string boolName           = "myBool";
            const string boolId             = "3648bdcc-5113-43f8-86dd-c19fe793a2f8";
            const bool   boolValue          = true;
            const string intName            = "myInteger";
            const string intId              = "7546f541-a003-4e69-a25c-608372ed1321";
            const int    intValue           = 12345;
            const string longId             = "b119c62a-82a2-48b2-b293-9963fa99fbe2";
            const long   longValue          = 67890L;
            const string floatName          = "myFloatingPoint";
            const string floatId            = "98d27280-70ee-4edd-8461-7633a328539a";
            const float  floatValue         = 123.4f;
            const string doubleId           = "f9bfcc22-221a-4c92-b5b9-be53cdedb092";
            const double doubleValue        = 56.78;
            const string stringName         = "myString";
            const string stringId           = "6bb8ae5b-19ca-450e-b369-922a34c02729";
            const string stringValue        = "str_0";
            const string metaProperty0Name  = "myMetaProperty0";
            const string metaProperty0Value = "m_0";
            const string metaProperty1Name  = "myMetaProperty1";
            const int    metaProperty1Value = 123;

            // Compose the vertex document using eager CosmosElements
            Dictionary <string, CosmosElement> vertexDocumentProperties = new Dictionary <string, CosmosElement>()
            {
                { idName, CosmosString.Create(idValue) },
                { GremlinScenarioTests.PartitionKeyPropertyName, CosmosString.Create(pkValue) },
                { labelName, CosmosString.Create(labelValue) },
                {
                    boolName,
                    CosmosArray.Create(
                        new CosmosElement[]
                    {
                        this.CreateVertexPropertySingleComplexValue(CosmosString.Create(boolId), CosmosBoolean.Create(boolValue)),
                    }
                        )
                },
                {
                    intName,
                    CosmosArray.Create(
                        new CosmosElement[]
                    {
                        this.CreateVertexPropertySingleComplexValue(CosmosString.Create(intId), CosmosNumber64.Create(intValue)),
                        this.CreateVertexPropertySingleComplexValue(CosmosString.Create(longId), CosmosNumber64.Create(longValue)),
                    }
                        )
                },
                {
                    floatName,
                    CosmosArray.Create(
                        new CosmosElement[]
                    {
                        this.CreateVertexPropertySingleComplexValue(CosmosString.Create(floatId), CosmosNumber64.Create(floatValue)),
                        this.CreateVertexPropertySingleComplexValue(CosmosString.Create(doubleId), CosmosNumber64.Create(doubleValue)),
                    }
                        )
                },
                {
                    stringName,
                    CosmosArray.Create(
                        new CosmosElement[]
                    {
                        this.CreateVertexPropertySingleComplexValue(
                            CosmosString.Create(stringId),
                            CosmosString.Create(stringValue),
                            Tuple.Create <string, CosmosElement>(metaProperty0Name, CosmosString.Create(metaProperty0Value)),
                            Tuple.Create <string, CosmosElement>(metaProperty1Name, CosmosNumber64.Create(metaProperty1Value))),
                    }
                        )
                },
            };

            CosmosObject vertexEagerObject = CosmosObject.Create(vertexDocumentProperties);

            // Serialize the vertex object into a document using the specified serialization format
            IJsonWriter jsonWriter = JsonWriter.Create(jsonSerializationFormat);

            vertexEagerObject.WriteTo(jsonWriter);
            ReadOnlyMemory <byte> jsonResult = jsonWriter.GetResult();

            Assert.IsTrue(jsonResult.Length > 0, "IJsonWriter result data is empty.");

            // Navigate into the serialized vertex document using lazy CosmosElements
            CosmosElement rootLazyElement = CosmosElement.CreateFromBuffer(jsonResult);

            // Validate the expected vertex document structure/values

            // Root vertex document object
            CosmosObject vertexLazyObject = rootLazyElement as CosmosObject;

            Assert.IsNotNull(vertexLazyObject, $"Vertex document root is not {nameof(CosmosObject)}.");
            Assert.AreEqual(vertexDocumentProperties.Count, vertexLazyObject.Count);

            // Vertex system document properties
            CosmosString idLazyString = this.GetAndAssertObjectProperty <CosmosString>(vertexLazyObject, idName);

            Assert.AreEqual(idValue, idLazyString.Value);

            CosmosString pkLazyString = this.GetAndAssertObjectProperty <CosmosString>(vertexLazyObject, GremlinScenarioTests.PartitionKeyPropertyName);

            Assert.AreEqual(pkValue, pkLazyString.Value);

            CosmosString labelLazyString = this.GetAndAssertObjectProperty <CosmosString>(vertexLazyObject, labelName);

            Assert.AreEqual(labelValue, labelLazyString.Value);

            // Vertex user properties
            CosmosArray boolLazyArray = this.GetAndAssertObjectProperty <CosmosArray>(vertexLazyObject, boolName);

            Assert.AreEqual(1, boolLazyArray.Count);

            // Bool value(s)
            CosmosObject boolValue0LazyObject   = this.GetAndAssertArrayValue <CosmosObject>(boolLazyArray, 0);
            CosmosString boolValue0IdLazyString = this.GetAndAssertObjectProperty <CosmosString>(boolValue0LazyObject, GremlinKeywords.KW_PROPERTY_ID);

            Assert.AreEqual(boolId, boolValue0IdLazyString.Value);
            CosmosBoolean boolValue0ValueLazyBool = this.GetAndAssertObjectProperty <CosmosBoolean>(boolValue0LazyObject, GremlinKeywords.KW_PROPERTY_VALUE);

            Assert.AreEqual(boolValue, boolValue0ValueLazyBool.Value);

            CosmosArray intLazyArray = this.GetAndAssertObjectProperty <CosmosArray>(vertexLazyObject, intName);

            Assert.AreEqual(2, intLazyArray.Count);

            // Integer value(s)
            CosmosObject intValue0LazyObject   = this.GetAndAssertArrayValue <CosmosObject>(intLazyArray, 0);
            CosmosString intValue0IdLazyString = this.GetAndAssertObjectProperty <CosmosString>(intValue0LazyObject, GremlinKeywords.KW_PROPERTY_ID);

            Assert.AreEqual(intId, intValue0IdLazyString.Value);
            CosmosNumber intValue0ValueLazyNumber = this.GetAndAssertObjectProperty <CosmosNumber>(intValue0LazyObject, GremlinKeywords.KW_PROPERTY_VALUE);

            Assert.AreEqual(CosmosNumberType.Number64, intValue0ValueLazyNumber.NumberType);
            Assert.IsTrue(intValue0ValueLazyNumber.IsInteger);
            Assert.AreEqual((long)intValue, intValue0ValueLazyNumber.AsInteger().Value);

            CosmosObject intValue1LazyObject   = this.GetAndAssertArrayValue <CosmosObject>(intLazyArray, 1);
            CosmosString intValue1IdLazyString = this.GetAndAssertObjectProperty <CosmosString>(intValue1LazyObject, GremlinKeywords.KW_PROPERTY_ID);

            Assert.AreEqual(longId, intValue1IdLazyString.Value);
            CosmosNumber intValue1ValueLazyNumber = this.GetAndAssertObjectProperty <CosmosNumber>(intValue1LazyObject, GremlinKeywords.KW_PROPERTY_VALUE);

            Assert.AreEqual(CosmosNumberType.Number64, intValue1ValueLazyNumber.NumberType);
            Assert.IsTrue(intValue1ValueLazyNumber.IsInteger);
            Assert.AreEqual(longValue, intValue1ValueLazyNumber.AsInteger().Value);

            // Floating point value(s)
            CosmosArray floatLazyArray = this.GetAndAssertObjectProperty <CosmosArray>(vertexLazyObject, floatName);

            Assert.AreEqual(2, floatLazyArray.Count);

            CosmosObject floatValue0LazyObject   = this.GetAndAssertArrayValue <CosmosObject>(floatLazyArray, 0);
            CosmosString floatValue0IdLazyString = this.GetAndAssertObjectProperty <CosmosString>(floatValue0LazyObject, GremlinKeywords.KW_PROPERTY_ID);

            Assert.AreEqual(floatId, floatValue0IdLazyString.Value);
            CosmosNumber floatValue0ValueLazyNumber = this.GetAndAssertObjectProperty <CosmosNumber>(floatValue0LazyObject, GremlinKeywords.KW_PROPERTY_VALUE);

            Assert.AreEqual(CosmosNumberType.Number64, floatValue0ValueLazyNumber.NumberType);
            Assert.IsTrue(floatValue0ValueLazyNumber.IsFloatingPoint);
            Assert.AreEqual((double)floatValue, floatValue0ValueLazyNumber.AsFloatingPoint().Value);

            CosmosObject floatValue1LazyObject   = this.GetAndAssertArrayValue <CosmosObject>(floatLazyArray, 1);
            CosmosString floatValue1IdLazyString = this.GetAndAssertObjectProperty <CosmosString>(floatValue1LazyObject, GremlinKeywords.KW_PROPERTY_ID);

            Assert.AreEqual(doubleId, floatValue1IdLazyString.Value);
            CosmosNumber floatValue1ValueLazyNumber = this.GetAndAssertObjectProperty <CosmosNumber>(floatValue1LazyObject, GremlinKeywords.KW_PROPERTY_VALUE);

            Assert.AreEqual(CosmosNumberType.Number64, floatValue1ValueLazyNumber.NumberType);
            Assert.IsTrue(floatValue1ValueLazyNumber.IsFloatingPoint);
            Assert.AreEqual(doubleValue, floatValue1ValueLazyNumber.AsFloatingPoint().Value);

            // String value(s)
            CosmosArray stringLazyArray = this.GetAndAssertObjectProperty <CosmosArray>(vertexLazyObject, stringName);

            Assert.AreEqual(1, stringLazyArray.Count);

            CosmosObject stringValue0LazyObject   = this.GetAndAssertArrayValue <CosmosObject>(stringLazyArray, 0);
            CosmosString stringValue0IdLazyString = this.GetAndAssertObjectProperty <CosmosString>(stringValue0LazyObject, GremlinKeywords.KW_PROPERTY_ID);

            Assert.AreEqual(stringId, stringValue0IdLazyString.Value);
            CosmosString stringValue0ValueLazyString = this.GetAndAssertObjectProperty <CosmosString>(stringValue0LazyObject, GremlinKeywords.KW_PROPERTY_VALUE);

            Assert.AreEqual(stringValue, stringValue0ValueLazyString.Value);

            // String value meta-properties
            CosmosObject stringValue0MetaLazyObject = this.GetAndAssertObjectProperty <CosmosObject>(stringValue0LazyObject, GremlinKeywords.KW_PROPERTY_META);

            Assert.AreEqual(2, stringValue0MetaLazyObject.Count);

            CosmosString stringValue0MetaValue0LazyString = this.GetAndAssertObjectProperty <CosmosString>(stringValue0MetaLazyObject, metaProperty0Name);

            Assert.AreEqual(metaProperty0Value, stringValue0MetaValue0LazyString.Value);

            CosmosNumber stringValue0MetaValue1LazyNumber = this.GetAndAssertObjectProperty <CosmosNumber>(stringValue0MetaLazyObject, metaProperty1Name);

            Assert.AreEqual(CosmosNumberType.Number64, stringValue0MetaValue1LazyNumber.NumberType);
            Assert.IsTrue(stringValue0MetaValue1LazyNumber.IsInteger);
            Assert.AreEqual((long)metaProperty1Value, stringValue0MetaValue1LazyNumber.AsInteger().Value);
        }
예제 #24
0
 public override bool Equals(CosmosNumber cosmosNumber)
 {
     return(cosmosNumber is CosmosInt64 cosmosInt64 && this.Equals(cosmosInt64));
 }
예제 #25
0
        private static int TypeToOrder(CosmosElement cosmosElement)
        {
            int order;

            switch (cosmosElement.Type)
            {
            case CosmosElementType.Null:
                order = 0;
                break;

            case CosmosElementType.Boolean:
                order = 1;
                break;

            case CosmosElementType.Number:
            {
                CosmosNumber number = (CosmosNumber)cosmosElement;
                switch (number.NumberType)
                {
                case CosmosNumberType.Number64:
                    order = 2;
                    break;

                case CosmosNumberType.Float32:
                    order = 6;
                    break;

                case CosmosNumberType.Float64:
                    order = 7;
                    break;

                case CosmosNumberType.Int16:
                    order = 8;
                    break;

                case CosmosNumberType.Int32:
                    order = 9;
                    break;

                case CosmosNumberType.Int64:
                    order = 10;
                    break;

                case CosmosNumberType.Int8:
                    order = 11;
                    break;

                case CosmosNumberType.UInt32:
                    order = 12;
                    break;

                default:
                    throw new ArgumentException($"Unknown number type. Number {(int)number.NumberType}; Typeof:{ typeof(CosmosNumberType)}");
                }
            }

            break;

            case CosmosElementType.String:
                order = 3;
                break;

            case CosmosElementType.Guid:
                order = 5;
                break;

            case CosmosElementType.Binary:
                order = 4;
                break;

            default:
                throw new ArgumentException($"Unknown: {nameof(CosmosElementType)}: {cosmosElement.Type}");
            }

            return(order);
        }
예제 #26
0
 public CosmosElement Visit(CosmosNumber cosmosNumber, CosmosElement indexer) => Undefined;
 public override bool Equals(CosmosNumber cosmosNumber)
 {
     return(cosmosNumber is CosmosFloat32 cosmosFloat32 && this.Equals(cosmosFloat32));
 }
예제 #28
0
 public UInt128 Visit(CosmosNumber cosmosNumber, UInt128 seed)
 {
     return(cosmosNumber.Accept(CosmosNumberHasher.Singleton, seed));
 }
예제 #29
0
 public int Visit(CosmosNumber cosmosNumber, CosmosElement input)
 {
     return(cosmosNumber.CompareTo((CosmosNumber)input));
 }
 /// <summary>
 /// Gets the global count.
 /// </summary>
 /// <returns>The global count.</returns>
 public CosmosElement GetResult()
 {
     return(CosmosNumber.Create(this.globalCount));
 }