Ejemplo n.º 1
0
        static ICollection <CacheValue> Resolve(ICollection <byte[]> keys)
        {
            if (keys.Count == 0)
            {
                return(Array.Empty <CacheValue>());
            }
            var result   = new List <CacheValue>();
            var comparer = new ByteArrayComparer();

            using (var ms = new MemoryStream())
                foreach (var key in keys)
                {
                    ms.SetLength(0);
                    ms.Write(key, 0, key.Length);
                    ms.Position = 0;
                    var k      = ProtoBuf.Serializer.Deserialize <byte[]>(ms);
                    var hash   = comparer.GetHashCode(k);
                    var random = new Random(hash);
                    var length = Math.Max((hash & 0xff) << 8, 256);
                    var data   = new byte[length];
                    random.NextBytes(data);
                    ms.SetLength(0);
                    ProtoBuf.Serializer.Serialize(ms, data);

                    result.Add(new CacheValue(key, ms.ToArray(), TimeSpan.FromSeconds(60)));
                }

            return(result);
        }
Ejemplo n.º 2
0
        public void ByteArrayComparer_GetHashCode_Test()
        {
            var comparer = new ByteArrayComparer();
            var items    = new[]
            {
                Tuple.Create(new byte[] { 1, 2, 3 }, new byte[] { 1, 2, 3 }, true),
                Tuple.Create(new byte[] { 1, 2, 3 }, new byte[] { 1, 2 }, false),
                Tuple.Create(new byte[] { 1, 2, 3, 4 }, new byte[] { 1, 2, 3, 4 }, true),
                Tuple.Create(new byte[] { 1, 2, 3, 4 }, new byte[] { 0, 2, 3, 41 }, false),
                Tuple.Create(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, true),
                Tuple.Create(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, new byte[] { 1, 2, 3, 4, 5, 6, 7, 9 }, false),
                Tuple.Create(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, new byte[] { 1, 2, 3, 4, 4, 6, 7, 8 }, false),
                Tuple.Create(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, true),
                Tuple.Create(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, new byte[] { 1, 2, 3, 4, 5, 6, 7, 9, 9, 10 }, false),
                Tuple.Create(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11 }, false),
                Tuple.Create(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 },
                             new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, true),
                Tuple.Create(new byte[] { 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 160 },
                             new byte[] { 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 160 }, true),
                Tuple.Create(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 },
                             new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17 }, false),
                Tuple.Create(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 },
                             new byte[] { 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, false),
                Tuple.Create(new byte[] { 1, 2, 3, 4, 5, 10 }, new byte[] { 1, 2, 3, 4, 5, 10 }, true),
                Tuple.Create(new byte[] { 1, 2, 3, 4, 5, 10 }, new byte[] { 2, 2, 3, 4, 5, 11 }, false)
            };

            foreach (var item in items)
            {
                if (item.Item3)
                {
                    Assert.AreEqual(comparer.GetHashCode(item.Item1), comparer.GetHashCode(item.Item2));
                }
                else
                {
                    Assert.AreNotEqual(comparer.GetHashCode(item.Item1), comparer.GetHashCode(item.Item2),
                                       "For value: " + string.Join(", ", item.Item1));
                }
            }
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public override int GetHashCode()
        {
            var hashCode = ByteComparer.GetHashCode(_payload);

            foreach (var contentHash in _contentHashes)
            {
                unchecked {
                    hashCode = (hashCode * 17) ^ contentHash.GetHashCode();
                }
            }

            return(hashCode);
        }
Ejemplo n.º 4
0
 /// <inheritdoc />
 public override int GetHashCode()
 {
     return(ContentHash.GetHashCode() ^ ByteComparer.GetHashCode(Output));
 }
Ejemplo n.º 5
0
        public void NullsLessThanNonNulls()
        {
            var b = new byte[0];

            Assert.True(_comparer.Compare(null, b) < 0);
            Assert.True(_comparer.Compare(b, null) > 0);
            Assert.NotEqual(_comparer.GetHashCode(null), _comparer.GetHashCode(b));
        }