Ejemplo n.º 1
0
            public UInt128 Visit(CosmosInt64 cosmosInt64, UInt128 seed)
            {
                UInt128 hash  = seed == RootHashSeed ? RootCache.Int64 : MurmurHash3.Hash128(HashSeeds.Int64, seed);
                long    value = cosmosInt64.GetValue();

                hash = MurmurHash3.Hash128(value, hash);
                return(hash);
            }
Ejemplo n.º 2
0
            public UInt128 Visit(CosmosInt64 cosmosInt64, UInt128 seed)
            {
                UInt128 hash  = MurmurHash3.Hash128(CosmosNumberHasher.Int64HashSeed, seed);
                long    value = cosmosInt64.GetValue();

                hash = MurmurHash3.Hash128(value, hash);
                return(hash);
            }
Ejemplo n.º 3
0
        public void TestNumber()
        {
            {
                Number64 value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosNumber64        cosmosNumber = (CosmosNumber64)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                sbyte value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosInt8            cosmosNumber = (CosmosInt8)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                short value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosInt16           cosmosNumber = (CosmosInt16)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                int value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosInt32           cosmosNumber = (CosmosInt32)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                long value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosInt64           cosmosNumber = (CosmosInt64)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                uint value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosUInt32          cosmosNumber = (CosmosUInt32)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                float value = 32.1337f;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosFloat32         cosmosNumber = (CosmosFloat32)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                double value = 32.1337;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosFloat64         cosmosNumber = (CosmosFloat64)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }
        }
        public void TestRoundTripAsCosmosElement()
        {
            CompositeContinuationToken compositeContinuationToken = new CompositeContinuationToken()
            {
                Token = "someToken",
                Range = new Documents.Routing.Range <string>("asdf", "asdf", false, false),
            };

            List <OrderByItem> orderByItems = new List <OrderByItem>()
            {
                new OrderByItem(CosmosObject.Create(new Dictionary <string, CosmosElement>()
                {
                    { "item", CosmosString.Create("asdf") }
                })),
                new OrderByItem(CosmosObject.Create(new Dictionary <string, CosmosElement>()
                {
                    { "item", CosmosInt64.Create(1337) }
                })),
                new OrderByItem(CosmosObject.Create(new Dictionary <string, CosmosElement>()
                {
                    { "item", CosmosBinary.Create(new byte[] { 1, 2, 3 }) }
                })),
            };

            string rid       = "someRid";
            int    skipCount = 42;
            string filter    = "someFilter";
            OrderByContinuationToken orderByContinuationToken = new OrderByContinuationToken(
                compositeContinuationToken,
                orderByItems,
                rid,
                skipCount,
                filter);

            CosmosElement cosmosElementToken = OrderByContinuationToken.ToCosmosElement(orderByContinuationToken);

            Assert.AreEqual(
                @"{""compositeToken"":{""token"":""someToken"",""range"":{""min"":""asdf"",""max"":""asdf""}},""orderByItems"":[{""item"":""asdf""},{""item"":LL1337},{""item"":BAQID}],""rid"":""someRid"",""skipCount"":42,""filter"":""someFilter""}",
                cosmosElementToken.ToString());
            TryCatch <OrderByContinuationToken> tryOrderByContinuationTokenFromCosmosElement = OrderByContinuationToken.TryCreateFromCosmosElement(cosmosElementToken);

            Assert.IsTrue(tryOrderByContinuationTokenFromCosmosElement.Succeeded);
            OrderByContinuationToken orderByContinuationTokenFromCosmosElement = tryOrderByContinuationTokenFromCosmosElement.Result;

            Assert.IsNotNull(orderByContinuationTokenFromCosmosElement);
            Assert.AreEqual(cosmosElementToken.ToString(), OrderByContinuationToken.ToCosmosElement(orderByContinuationTokenFromCosmosElement).ToString());
        }
Ejemplo n.º 5
0
        private static IReadOnlyList <(string serializedToken, CosmosElement element)> TokenTestData()
        {
            Guid guid = Guid.Parse("69D5AB17-C94A-4173-A278-B59D0D9C7C37");

            byte[] randomBytes = guid.ToByteArray();
            string hexString   = PartitionKeyInternal.HexConvert.ToHex(randomBytes, 0, randomBytes.Length);

            return(new List <(string, CosmosElement)>
            {
                ("[42, 37]", CosmosArray.Parse("[42, 37]")),
                ($@"{{C_Binary(""0x{hexString}"")}}", CosmosBinary.Create(new ReadOnlyMemory <byte>(randomBytes))),
                ("false", CosmosBoolean.Create(false)),
                ($@"{{C_Guid(""{guid}"")}}", CosmosGuid.Create(guid)),
                ("null", CosmosNull.Create()),
                ("1", CosmosInt64.Create(1)),
                ("{\"foo\": false}", CosmosObject.Parse("{\"foo\": false}")),
                ("asdf", CosmosString.Create("asdf"))
            });
Ejemplo n.º 6
0
            public override CosmosElement GetCosmosElementContinuationToken()
            {
                Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>()
                {
                    {
                        UnorderdDistinctMap.PropertyNames.Numbers,
                        CosmosArray.Create(this.numbers.Select(x => CosmosNumber64.Create(x)))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.StringsLength4,
                        CosmosArray.Create(this.stringsLength4.Select(x => CosmosUInt32.Create(x)))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.StringsLength8,
                        CosmosArray.Create(this.stringsLength8.Select(x => CosmosInt64.Create((long)x)))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.StringsLength16,
                        CosmosArray.Create(this.stringsLength16.Select(x => CosmosBinary.Create(UInt128.ToByteArray(x))))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.StringsLength16Plus,
                        CosmosArray.Create(this.stringsLength16Plus.Select(x => CosmosBinary.Create(UInt128.ToByteArray(x))))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.Arrays,
                        CosmosArray.Create(this.arrays.Select(x => CosmosBinary.Create(UInt128.ToByteArray(x))))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.Object,
                        CosmosArray.Create(this.objects.Select(x => CosmosBinary.Create(UInt128.ToByteArray(x))))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.SimpleValues,
                        CosmosString.Create(this.simpleValues.ToString())
                    }
                };

                return(CosmosObject.Create(dictionary));
            }
        public void VerifyNumberSort()
        {
            List <CosmosElement> manuallySortedList = new List <CosmosElement>()
            {
                CosmosNumber64.Create(-1),
                CosmosNumber64.Create(0),
                CosmosNumber64.Create(1),

                CosmosInt8.Create(-1),
                CosmosInt8.Create(0),
                CosmosInt8.Create(1),

                CosmosInt16.Create(-1),
                CosmosInt16.Create(0),
                CosmosInt16.Create(1),

                CosmosInt32.Create(-1),
                CosmosInt32.Create(0),
                CosmosInt32.Create(1),


                CosmosInt64.Create(-1),
                CosmosInt64.Create(0),
                CosmosInt64.Create(1),

                CosmosUInt32.Create(0),
                CosmosUInt32.Create(1),

                CosmosFloat32.Create(-1),
                CosmosFloat32.Create(0),
                CosmosFloat32.Create(1),

                CosmosFloat64.Create(-1),
                CosmosFloat64.Create(0),
                CosmosFloat64.Create(1),
            };

            VerifySort(manuallySortedList);
        }
Ejemplo n.º 8
0
 public void Visit(CosmosInt64 cosmosInt64)
 {
     this.stringBuilder.AppendFormat("C_Int64({0})", cosmosInt64.GetValue());
 }
        public void TestOrderByQueryLiterals()
        {
            StringBuilder sb      = new StringBuilder();
            CosmosElement element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosString.Create("asdf") }
            });

            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":""asdf""}",
                sb.ToString());

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosBoolean.Create(true) }
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":true}",
                sb.ToString());

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosBoolean.Create(false) }
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":false}",
                sb.ToString());

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosNull.Create() }
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":null}",
                sb.ToString());

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosNumber64.Create(1.0) }
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":1}",
                sb.ToString());

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosNumber64.Create(1L) }
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":1}",
                sb.ToString());

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosInt8.Create(3) },
                { "item2", CosmosInt16.Create(4) },
                { "item3", CosmosInt32.Create(5) },
                { "item5", CosmosUInt32.Create(7) },
                { "item6", CosmosInt64.Create(8) },
                { "item7", CosmosFloat32.Create(9.1f) },
                { "item8", CosmosFloat64.Create(10.2) },
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                @"{""item"":C_Int8(3),""item2"":C_Int16(4),""item3"":C_Int32(5),""item5"":C_UInt32(7),""item6"":C_Int64(8)," +
                @"""item7"":C_Float32(9.1),""item8"":C_Float64(10.2)}",
                sb.ToString());

            Guid guid = Guid.NewGuid();

            byte[] randomBytes = Guid.NewGuid().ToByteArray();
            string hexString   = PartitionKeyInternal.HexConvert.ToHex(randomBytes, 0, randomBytes.Length);

            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosGuid.Create(guid) },
                { "item2", CosmosBinary.Create(new ReadOnlyMemory <byte>(randomBytes)) },
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                $@"{{""item"":C_Guid(""{guid.ToString()}""),""item2"":C_Binary(""0x{hexString}"")}}",
                sb.ToString());

            // deeply nested arrays and objects
            element = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { "item", CosmosGuid.Create(guid) },

                // empty array
                { "item2", CosmosArray.Create(new CosmosElement[] { }) },

                // empty object
                { "item3", CosmosObject.Create(new Dictionary <string, CosmosElement>()) },

                // array of objects with numbers
                { "item4", CosmosArray.Create(new CosmosElement[]
                    {
                        CosmosObject.Create(new Dictionary <string, CosmosElement>()
                        {
                            { "a", CosmosInt8.Create(3) },
                            { "b", CosmosString.Create("adf") },
                        }),
                        CosmosInt16.Create(25)
                    }) },
            });
            sb.Clear();
            element.Accept(new CosmosElementToQueryLiteral(sb));
            Assert.AreEqual(
                $@"{{""item"":C_Guid(""{guid.ToString()}""),""item2"":[],""item3"":{{}},""item4"":[{{""a"":C_Int8(3),""b"":""adf""}},C_Int16(25)]}}",
                sb.ToString());
        }