private static ShortHash ComputePathHash(string path)
        {
            var bytes    = MurmurHash3.Create(Encoding.UTF8.GetBytes(path));
            var pathHash = new ShortHash(new ReadOnlyFixedBytes(bytes.ToByteArray()));

            return(pathHash);
        }
Esempio n. 2
0
        private static async Task SpeedTestSingleThreadedSearchFile()
        {
            var tempPath    = System.IO.Path.GetTempPath();
            var fileName    = System.IO.Path.Combine(tempPath, "Level1_1.trim");
            var loadedWords = await System.IO.File.ReadAllLinesAsync("words.txt");


            using (var blockCache = new ProtoBlockCache(10000))
            {
                var loadedTable = new TableFile(fileName, blockCache);
                await loadedTable.LoadAsync();

                // Check we can get the values back out

                var hash = new MurmurHash3();

                var sw = Stopwatch.StartNew();
                foreach (var word in loadedWords)
                {
                    var utf8 = Encoding.UTF8.GetBytes(word);
                    var h    = hash.ComputeHash64(utf8);

                    var result = await loadedTable.GetAsync(utf8, h);

                    var resultAsString = Encoding.UTF8.GetString(result.Value.Span);
                }
                sw.Stop();
                Console.WriteLine($"Total time taken {sw.ElapsedMilliseconds} time per key {(double)sw.ElapsedMilliseconds / loadedWords.Length}");
            }
        }
Esempio n. 3
0
            public UInt128 Visit(CosmosGuid cosmosGuid, UInt128 seed)
            {
                UInt128 hash = MurmurHash3.Hash128(CosmosElementHasher.GuidHashSeed, seed);

                hash = MurmurHash3.Hash128(cosmosGuid.Value.ToByteArray(), seed);
                return(hash);
            }
Esempio n. 4
0
        public async Task Hash32_StreamAsync_BinaryTestsAsync(MurmurTestVector vector)
        {
            using var stream = new MemoryStream(vector.Buffer);
            uint result = await MurmurHash3.Hash32Async(stream, vector.Seed);

            Assert.That(result, Is.EqualTo(vector.ExpectedResult));
        }
Esempio n. 5
0
        public void Hash32_Stream_StringTests(string text, uint seed, uint expectedResult)
        {
            using var stream = new MemoryStream(Encoding.UTF8.GetBytes(text));
            uint result = MurmurHash3.Hash32(stream, seed);

            Assert.That(result, Is.EqualTo(expectedResult));
        }
Esempio n. 6
0
        public void Hash32_Stream_BinaryTests(MurmurTestVector vector)
        {
            using var stream = new MemoryStream(vector.Buffer);
            uint result = MurmurHash3.Hash32(stream, vector.Seed);

            Assert.That(result, Is.EqualTo(vector.ExpectedResult));
        }
Esempio n. 7
0
            /// <summary>
            /// Gets the hash of a byte array.
            /// </summary>
            /// <param name="bytes">The bytes.</param>
            /// <param name="seed">The seed.</param>
            /// <returns>The hash.</returns>
            public UInt192 GetHash(byte[] bytes, UInt192 seed)
            {
                UInt128 hash128 = MurmurHash3.Hash128(bytes, bytes.Length, UInt128.Create(seed.GetLow(), seed.GetMid()));
                ulong   hash64  = MurmurHash3.Hash64(bytes, bytes.Length, seed.GetHigh());

                return(UInt192.Create(hash128.GetLow(), hash128.GetHigh(), hash64));
            }
Esempio n. 8
0
        public Guid Deserialize(ReadOnlySpan <byte> data, bool isNull, SerializationContext context)
        {
            if (isNull || data.Length == 0)
            {
                return(Guid.Empty);
            }

            if (data.Length == 16)
            {
                return(new Guid(data));
            }

            if (data.Length > 16)
            {
                var hash = MurmurHash3.Hash128(data, 1);

                return(new Guid(hash));
            }

            Span <byte> key16Bytes = stackalloc byte[16];

            data.CopyTo(key16Bytes);

            return(new Guid(key16Bytes));
        }
Esempio n. 9
0
 public void Hash128_ReturnsExpectedVersions(string text, string expectedHash, uint seed)
 {
     byte[] buffer         = Encoding.UTF8.GetBytes(text);
     byte[] expectedResult = Base16.Decode(expectedHash);
     byte[] result         = MurmurHash3.Hash128(buffer, seed);
     CollectionAssert.AreEqual(expectedResult, result);
 }
            public UInt128 Visit(CosmosGuid cosmosGuid, UInt128 seed)
            {
                UInt128 hash = seed == RootHashSeed ? RootCache.Guid : MurmurHash3.Hash128(HashSeeds.Guid, seed);

                hash = MurmurHash3.Hash128(cosmosGuid.Value.ToByteArray(), hash);
                return(hash);
            }
Esempio n. 11
0
        public void CreateFingerprintFromWellDistributedHash()
        {
            var hash        = new MurmurHash3(0x0102030405060608, 0x0910111213141516);
            var fingerprint = ContentHashingUtilities.CreateFrom(hash).ToHex();

            XAssert.AreEqual("080606050403020116151413121110090000000000000000000000000000000000", fingerprint);
        }
Esempio n. 12
0
        public override int GetHashCode()
        {
            uint hash = HashSeed;

            hash = MurmurHash3.Hash32(this.Value, hash);
            return((int)hash);
        }
            public UInt128 Visit(CosmosUInt32 cosmosUInt32, UInt128 seed)
            {
                UInt128 hash  = seed == RootHashSeed ? RootCache.UInt32 : MurmurHash3.Hash128(HashSeeds.UInt32, seed);
                uint    value = cosmosUInt32.GetValue();

                hash = MurmurHash3.Hash128(value, hash);
                return(hash);
            }
            public UInt128 Visit(CosmosBinary cosmosBinary, UInt128 seed)
            {
                // Hash with binary seed to differntiate between empty binary and no binary.
                UInt128 hash = seed == RootHashSeed ? RootCache.Binary : MurmurHash3.Hash128(HashSeeds.Binary, seed);

                hash = MurmurHash3.Hash128(cosmosBinary.Value.Span, hash);
                return(hash);
            }
Esempio n. 15
0
 public void Hash32_Stream_StringTests(string text, uint seed, uint expectedResult)
 {
     using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)))
     {
         uint result = MurmurHash3.Hash32(stream, seed);
         Assert.AreEqual(expectedResult, result);
     }
 }
            public UInt128 Visit(CosmosNumber64 cosmosNumber64, UInt128 seed)
            {
                UInt128  hash  = seed == RootHashSeed ? RootCache.Number64 : MurmurHash3.Hash128(HashSeeds.Number64, seed);
                Number64 value = cosmosNumber64.GetValue();

                Number64.DoubleEx doubleExValue = Number64.ToDoubleEx(value);
                return(MurmurHash3.Hash128(doubleExValue, hash));
            }
            public UInt128 Visit(CosmosInt8 cosmosInt8, UInt128 seed)
            {
                UInt128 hash  = seed == RootHashSeed ? RootCache.Int8 : MurmurHash3.Hash128(HashSeeds.Int8, seed);
                sbyte   value = cosmosInt8.GetValue();

                hash = MurmurHash3.Hash128(value, hash);
                return(hash);
            }
            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);
            }
            public UInt128 Visit(CosmosInt16 cosmosInt16, UInt128 seed)
            {
                UInt128 hash  = seed == RootHashSeed ? RootCache.Int16 : MurmurHash3.Hash128(HashSeeds.Int16, seed);
                short   value = cosmosInt16.GetValue();

                hash = MurmurHash3.Hash128(value, hash);
                return(hash);
            }
Esempio n. 20
0
 public void Hash32_Stream_BinaryTests(MurmurTestVector vector)
 {
     using (var stream = new MemoryStream(vector.Buffer))
     {
         uint result = MurmurHash3.Hash32(stream, vector.Seed);
         Assert.AreEqual(vector.ExpectedResult, result);
     }
 }
Esempio n. 21
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);
            }
Esempio n. 22
0
            public UInt128 Visit(CosmosUInt32 cosmosUInt32, UInt128 seed)
            {
                UInt128 hash  = MurmurHash3.Hash128(CosmosNumberHasher.UInt32HashSeed, seed);
                uint    value = cosmosUInt32.GetValue();

                hash = MurmurHash3.Hash128(value, hash);
                return(hash);
            }
Esempio n. 23
0
            public UInt128 Visit(CosmosInt8 cosmosInt8, UInt128 seed)
            {
                UInt128 hash  = MurmurHash3.Hash128(CosmosNumberHasher.Int8HashSeed, seed);
                sbyte   value = cosmosInt8.GetValue();

                hash = MurmurHash3.Hash128(value, hash);
                return(hash);
            }
Esempio n. 24
0
            public UInt128 Visit(CosmosInt16 cosmosInt16, UInt128 seed)
            {
                UInt128 hash  = MurmurHash3.Hash128(CosmosNumberHasher.Int16HashSeed, seed);
                short   value = cosmosInt16.GetValue();

                hash = MurmurHash3.Hash128(value, hash);
                return(hash);
            }
Esempio n. 25
0
            public UInt128 Visit(CosmosBinary cosmosBinary, UInt128 seed)
            {
                // Hash with binary seed to differntiate between empty binary and no binary.
                UInt128 hash = MurmurHash3.Hash128(CosmosElementHasher.BinaryHashSeed, seed);

                // TODO: replace this with Span based hashing.
                hash = MurmurHash3.Hash128(cosmosBinary.Value.Span, seed);
                return(hash);
            }
Esempio n. 26
0
            public UInt128 Visit(CosmosNumber64 cosmosNumber64, UInt128 seed)
            {
                UInt128     hash   = MurmurHash3.Hash128(CosmosNumberHasher.Number64HashSeed, seed);
                Number64    value  = cosmosNumber64.GetValue();
                Span <byte> buffer = stackalloc byte[Number64.SizeOf];

                value.CopyTo(buffer);
                return(MurmurHash3.Hash128(buffer, hash));
            }
Esempio n. 27
0
            public UInt128 Visit(CosmosObject cosmosObject, UInt128 seed)
            {
                // Start the object with a distinct hash, so that empty object doesn't hash to another value.
                UInt128 hash = MurmurHash3.Hash128(CosmosElementHasher.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})
                UInt128 intermediateHash = 0;

                // Property order should not result in a different hash.
                // This is consistent with equality comparison.
                foreach (KeyValuePair <string, CosmosElement> kvp in cosmosObject)
                {
                    UInt128 nameHash     = CosmosString.Create(kvp.Key).Accept(this, CosmosElementHasher.PropertyNameHashSeed);
                    UInt128 propertyHash = kvp.Value.Accept(this, 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 = MurmurHash3.Hash128(intermediateHash, hash);
                }

                return(hash);
            }
        public void MurmurHash3_HashUnencodedChars()
        {
            var hashed = MurmurHash3.HashUnencodedChars("someClientId.123456.tntId123.salty");

            Assert.Equal(-1846592194, hashed);

            hashed = MurmurHash3.HashUnencodedChars("targettesting.125880.4c038b35f1b1453d80a3e7da8208c617.campaign");
            Assert.Equal(-683299703, hashed);
        }
            public UInt128 Visit(CosmosNull cosmosNull, UInt128 seed)
            {
                if (seed == RootHashSeed)
                {
                    return(RootCache.Null);
                }

                return(MurmurHash3.Hash128(HashSeeds.Null, seed));
            }
Esempio n. 30
0
 public MurmurHash3 GetHash()
 {
     unsafe
     {
         Item  i = this;
         Item *p = &i;
         return(MurmurHash3.Create((byte *)p, (uint)sizeof(Item), 0));
     }
 }