/// <summary>
        /// Gets the hash of a JSON number value.
        /// </summary>
        /// <param name="number">The number to hash.</param>
        /// <param name="seed">The seed to use.</param>
        /// <returns>The hash of a JSON number value.</returns>
        private static UInt192 GetNumberHash(double number, UInt192 seed)
        {
            UInt192 hash = DistinctHash.GetHash(DistinctHash.NumberHashSeed, seed);

            hash = DistinctHash.GetHash((UInt192)BitConverter.DoubleToInt64Bits(number), hash);
            return(hash);
        }
        /// <summary>
        /// Gets the hash of a JSON string value.
        /// </summary>
        /// <param name="value">The value to hash.</param>
        /// <param name="seed">The seed to use.</param>
        /// <returns>The hash of a JSON string value.</returns>
        private static UInt192 GetStringHash(string value, UInt192 seed)
        {
            UInt192 hash = DistinctHash.GetHash(DistinctHash.StringHashSeed, seed);

            byte[] stringBytes = Encoding.UTF8.GetBytes(value);
            return(DistinctHash.GetHash(stringBytes, hash));
        }
        /// <summary>
        /// Gets the hash of a JSON object.
        /// </summary>
        /// <param name="cosmosObject">The object to hash.</param>
        /// <param name="seed">The seed to use.</param>
        /// <returns>The hash of a JSON object.</returns>
        private static UInt192 GetObjectHash(CosmosObject cosmosObject, UInt192 seed)
        {
            // Start the object with a distinct hash, so that empty object doesn't hash to another value.
            UInt192 hash = DistinctHash.GetHash(DistinctHash.ObjectHashSeed, seed);

            //// Intermediate hashes of all the properties, which we don't want to xor with the final hash
            //// otherwise the following will collide:
            ////{
            ////    "pet":{
            ////        "name":"alice",
            ////        "age":5
            ////    },
            ////    "pet2":{
            ////        "name":"alice",
            ////        "age":5
            ////    }
            ////}
            ////
            ////{
            ////    "pet":{
            ////        "name":"bob",
            ////        "age":5
            ////    },
            ////    "pet2":{
            ////        "name":"bob",
            ////        "age":5
            ////    }
            ////}
            //// because they only differ on the name, but it gets repeated meaning that
            //// hash({"name":"bob", "age":5}) ^ hash({"name":"bob", "age":5}) is the same as
            //// hash({"name":"alice", "age":5}) ^ hash({"name":"alice", "age":5})
            UInt192 intermediateHash = 0;

            // Property order should not result in a different hash.
            // This is consistent with equality comparison.
            foreach (KeyValuePair <string, CosmosElement> kvp in cosmosObject)
            {
                UInt192 nameHash = DistinctHash.GetHash(
                    CosmosString.Create(kvp.Key),
                    DistinctHash.PropertyNameHashSeed);
                UInt192 propertyHash = DistinctHash.GetHash(kvp.Value, nameHash);

                //// xor is symmetric meaning that a ^ b = b ^ a
                //// Which is great since now we can add the property hashes to the intermediate hash
                //// in any order and get the same result, which upholds our definition of equality.
                //// Note that we don't have to worry about a ^ a = 0 = b ^ b for duplicate property values,
                //// since the hash of property values are seeded with the hash of property names,
                //// which are unique within an object.
                intermediateHash ^= propertyHash;
            }

            // Only if the object was not empty do we want to bring in the intermediate hash.
            if (intermediateHash > 0)
            {
                hash = DistinctHash.GetHash(intermediateHash, hash);
            }

            return(hash);
        }
        /// <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);
        }
Пример #5
0
            /// <summary>
            /// Adds a string to the distinct map.
            /// </summary>
            /// <param name="value">The string to add.</param>
            /// <returns>Whether or not the value was successfully added.</returns>
            private bool AddStringValue(string value)
            {
                bool added      = false;
                int  utf8Length = Encoding.UTF8.GetByteCount(value);

                // If you fit the string with full fidelity in 24 bytes, then you might as well just hash the string.
                if (utf8Length <= UnorderdDistinctMap.UInt192Length)
                {
                    // Zero out the array since you want all trailing bytes to be 0 for the conversions that happen next.
                    Array.Clear(this.utf8Buffer, 0, this.utf8Buffer.Length);
                    Encoding.UTF8.GetBytes(value, 0, utf8Length, this.utf8Buffer, 0);

                    if (utf8Length == 0)
                    {
                        added = this.AddSimpleValue(SimpleValues.EmptyString);
                    }
                    else if (utf8Length <= UnorderdDistinctMap.UIntLength)
                    {
                        uint uintValue = BitConverter.ToUInt32(this.utf8Buffer, 0);
                        added = this.stringsLength4.Add(uintValue);
                    }
                    else if (utf8Length <= UnorderdDistinctMap.ULongLength)
                    {
                        ulong uLongValue = BitConverter.ToUInt64(this.utf8Buffer, 0);
                        added = this.stringLength8.Add(uLongValue);
                    }
                    else if (utf8Length <= UnorderdDistinctMap.UInt128Length)
                    {
                        UInt128 uInt128Value = UInt128.FromByteArray(this.utf8Buffer, 0);
                        added = this.stringLength16.Add(uInt128Value);
                    }
                    else
                    {
                        UInt192 uInt192Value = UInt192.FromByteArray(this.utf8Buffer, 0);
                        added = this.stringLength24.Add(uInt192Value);
                    }
                }
                else
                {
                    // Else the string is too large and we will just store the hash.
                    UInt192 uint192Value = DistinctHash.GetHash(CosmosString.Create(value));
                    added = this.stringLength24Plus.Add(uint192Value);
                }

                return(added);
            }
            /// <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>
            /// <remarks>This function assumes data is added in sorted order.</remarks>
            public override bool Add(CosmosElement cosmosElement, out UInt128 hash)
            {
                hash = DistinctHash.GetHash(cosmosElement);

                bool added;

                if (this.lastHash != hash)
                {
                    this.lastHash = hash;
                    added         = true;
                }
                else
                {
                    added = false;
                }

                return(added);
            }
        /// <summary>
        /// Gets the hash of a JSON array.
        /// </summary>
        /// <param name="cosmosArray">The array to hash.</param>
        /// <param name="seed">The seed to use.</param>
        /// <returns>The hash of a JSON array.</returns>
        private static UInt192 GetArrayHash(CosmosArray cosmosArray, UInt192 seed)
        {
            // Start the array with a distinct hash, so that empty array doesn't hash to another value.
            UInt192 hash = DistinctHash.GetHash(DistinctHash.ArrayHashSeed, seed);

            // Incorporate all the array items into the hash.
            for (int index = 0; index < cosmosArray.Count; index++)
            {
                CosmosElement arrayItem = cosmosArray[index];

                // Order of array items matter in equality check, so we add the index just to be safe.
                // For now we know that murmurhash will correctly give a different hash for
                // [true, false, true] and [true, true, false]
                // due to the way the seed works.
                // But we add the index just incase that property does not hold in the future.
                UInt192 arrayItemSeed = DistinctHash.ArrayIndexHashSeed + index;
                hash = DistinctHash.GetHash(hash, DistinctHash.GetHash(arrayItem, arrayItemSeed));
            }

            return(hash);
        }
 /// <summary>
 /// Gets the hash given a value and a seed.
 /// </summary>
 /// <param name="value">The value to hash.</param>
 /// <param name="seed">The seed.</param>
 /// <returns>The hash.</returns>
 public static UInt192 GetHash(UInt192 value, UInt192 seed)
 {
     return(DistinctHash.GetHash(UInt192.ToByteArray(value), seed));
 }
 /// <summary>
 /// Gets the hash of a boolean JSON value.
 /// </summary>
 /// <param name="boolean">The boolean to hash.</param>
 /// <param name="seed">The seed.</param>
 /// <returns>The hash of a boolean JSON value.</returns>
 private static UInt192 GetBooleanHash(bool boolean, UInt192 seed)
 {
     return(DistinctHash.GetHash(boolean ? DistinctHash.TrueHashSeed : DistinctHash.FalseHashSeed, seed));
 }
 /// <summary>
 /// Gets the hash of a null JSON value.
 /// </summary>
 /// <param name="seed">The seed to use.</param>
 /// <returns>The hash of a null JSON value given a seed.</returns>
 private static UInt192 GetNullHash(UInt192 seed)
 {
     return(DistinctHash.GetHash(DistinctHash.NullHashSeed, seed));
 }
 /// <summary>
 /// Gets the hash of a JToken value.
 /// </summary>
 /// <param name="cosmosElement">The element to load.</param>
 /// <returns>The hash of the JToken.</returns>
 public static UInt192 GetHash(CosmosElement cosmosElement)
 {
     return(DistinctHash.GetHash(cosmosElement, DistinctHash.RootHashSeed));
 }
Пример #12
0
            /// <summary>
            /// Adds an object value to the distinct map.
            /// </summary>
            /// <param name="cosmosObject">The object to add.</param>
            /// <returns>Whether or not the value was successfully added.</returns>
            private bool AddObjectValue(CosmosObject cosmosObject)
            {
                UInt128 hash = DistinctHash.GetHash(cosmosObject);

                return(this.objects.Add(hash));
            }
Пример #13
0
            /// <summary>
            /// Adds an array value to the distinct map.
            /// </summary>
            /// <param name="array">The array to add.</param>
            /// <returns>Whether or not the value was successfully added.</returns>
            private bool AddArrayValue(CosmosArray array)
            {
                UInt128 hash = DistinctHash.GetHash(array);

                return(this.arrays.Add(hash));
            }
Пример #14
0
        public override Output ExecuteTest(Input input)
        {
            UInt128 hash = DistinctHash.GetHash(input.CosmosElement);

            return(new Output(hash));
        }
 /// <summary>
 /// Gets the hash given a value and a seed.
 /// </summary>
 /// <param name="value">The value to hash.</param>
 /// <param name="seed">The seed.</param>
 /// <returns>The hash.</returns>
 public static UInt128 GetHash(UInt128 value, UInt128 seed)
 {
     return(DistinctHash.GetHash(UInt128.ToByteArray(value), seed));
 }
        /// <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;

                case CosmosElementType.Array:
                case CosmosElementType.Object:
                {
                    UInt192 hash1 = DistinctHash.GetHash(element1);
                    UInt192 hash2 = DistinctHash.GetHash(element2);
                    return(hash1.CompareTo(hash2));
                }

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

            return(cmp);
        }