예제 #1
0
        public void TestStructuresOfArrays()
        {
            var randomGenerator = new RandomGenerator(66707770); // make this deterministic.

            var index = new MemoryMappedIndex <int[]>(new MemoryMappedStream(new MemoryStream()),
                                                      MemoryMappedDelegates.ReadFromIntArray, MemoryMappedDelegates.WriteToIntArray);
            var indexRef = new Dictionary <long, int[]>();

            // add the data.
            var testCount = 10;

            while (testCount > 0)
            {
                var data = randomGenerator.GenerateArray(512, 512);
                indexRef.Add(index.Add(data), data);
                testCount--;
            }

            // get the data and check.
            foreach (var entry in indexRef)
            {
                var data = index.Get(entry.Key);
                Assert.AreEqual(indexRef[entry.Key], data);
            }
        }
예제 #2
0
        public void TestStringHuge()
        {
            var randomGenerator = new RandomGenerator(66707770); // make this deterministic.

            var index = new MemoryMappedIndex <string>(new MemoryMappedStream(new MemoryStream()),
                                                       MemoryMappedDelegates.ReadFromString, MemoryMappedDelegates.WriteToString, 1024, false);
            var indexRef = new Dictionary <long, string>();

            // add the data.
            var testCount = 1000;

            while (testCount > 0)
            {
                var data = randomGenerator.GenerateString(
                    randomGenerator.Generate(256) + 32);
                indexRef.Add(index.Add(data), data);
                testCount--;
            }

            // get the data and check.
            foreach (var entry in indexRef)
            {
                var data = index.Get(entry.Key);
                Assert.AreEqual(indexRef[entry.Key], data);
            }
        }
예제 #3
0
        public void TestString()
        {
            var randomGenerator = new RandomGenerator(66707770); // make this deterministic.

            var index = new MemoryMappedIndex<string>(new MemoryMappedStream(new MemoryStream()),
                MemoryMappedDelegates.ReadFromString, MemoryMappedDelegates.WriteToString);
            var indexRef = new Dictionary<long, string>();

            // add the data.
            var testCount = 10;
            while(testCount > 0)
            {
                var data = randomGenerator.GenerateString(
                    randomGenerator.Generate(256) + 32);
                indexRef.Add(index.Add(data), data);
                testCount--;
            }

            // get the data and check.
            foreach(var entry in indexRef)
            {
                var data = index.Get(entry.Key);
                Assert.AreEqual(indexRef[entry.Key], data);
            }
        }
        /// <summary>
        /// Gets the value associated with the specified key.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGetValue(TKey key, out TValue value)
        {
            long keyId, valueId, pairId;
            var  hash = this.Hash(key);

            if (this.Get(hash, key, out keyId, out valueId, out pairId))
            { // key was found.
                value = _values.Get(valueId).Value;
                return(true);
            }
            value = default(TValue);
            return(false);
        }
        /// <summary>
        /// Sets to
        /// </summary>
        /// <param name="hash"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        private void Set(long hash, TKey key, TValue value)
        {
            // try an find the key.
            var pairId = _hashes[hash];

            while (pairId >= 0)
            {
                var keyId      = _pairs[pairId];
                var currentKey = _keys.Get(keyId).Key;
                if ((_compareKey == null && currentKey.Equals(key)) ||
                    (_compareKey != null && _compareKey(currentKey, key) == 0))
                { // key was found.
                    _pairs[pairId + 1] = _values.Add(new ValueStruct()
                    {
                        Value = value
                    });
                    return;
                }
                pairId = _pairs[pairId + 2];
            }
            this.Add(hash, key, value);
        }
예제 #6
0
        public void TestStructuresOfArraysSerialize()
        {
            var randomGenerator = new RandomGenerator(66707770); // make this deterministic.

            var index = new MemoryMappedIndex<int[]>(new MemoryMappedStream(new MemoryStream()),
                MemoryMappedDelegates.ReadFromIntArray, MemoryMappedDelegates.WriteToIntArray);
            var indexRef = new Dictionary<long, int[]>();

            // add the data.
            var testCount = 10;
            while (testCount > 0)
            {
                var data = randomGenerator.GenerateArray(512, 512);
                indexRef.Add(index.Add(data), data);
                testCount--;
            }

            MemoryMappedIndex<int[]> deserializedIndex;
            using (var stream = new MemoryStream())
            {
                var size = index.Serialize(stream);
                deserializedIndex = MemoryMappedIndex<int[]>.Deserialize(stream,
                    MemoryMappedDelegates.ReadFromIntArray, MemoryMappedDelegates.WriteToIntArray, false);

                // get the data and check.
                foreach (var entry in indexRef)
                {
                    var data = index.Get(entry.Key);
                    Assert.AreEqual(indexRef[entry.Key], data);
                }
            }
        }