Esempio n. 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);
            }
        }
Esempio n. 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);
            }
        }
        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);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Deserializes a tags index from the given stream.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static TagsIndex Deserialize(System.IO.Stream stream)
        {
            long size;
            var  tagsIndex = MemoryMappedIndex <int[]> .Deserialize(stream, MemoryMappedDelegates.ReadFromIntArray, MemoryMappedDelegates.WriteToIntArray, false, out size);

            stream.Seek(size, System.IO.SeekOrigin.Begin);
            var limitedStream = new LimitedStream(stream);
            var stringIndex   = MemoryMappedIndex <string> .Deserialize(limitedStream, MemoryMappedDelegates.ReadFromString, MemoryMappedDelegates.WriteToString, false);

            return(new TagsIndex(stringIndex, tagsIndex));
        }
        /// <summary>
        /// Creates a new dictionary.
        /// </summary>
        /// <param name="file">A memory mapped file.</param>
        /// <param name="readKeyFrom"></param>
        /// <param name="writeToKey"></param>
        /// <param name="readValueFrom"></param>
        /// <param name="writeValueTo"></param>
        /// <param name="hashKey"></param>
        /// <param name="compareKey"></param>
        public MemoryMappedHugeDictionary(MemoryMappedFile file,
                                          MemoryMappedFile.ReadFromDelegate <TKey> readKeyFrom, MemoryMappedFile.WriteToDelegate <TKey> writeToKey,
                                          MemoryMappedFile.ReadFromDelegate <TValue> readValueFrom, MemoryMappedFile.WriteToDelegate <TValue> writeValueTo,
                                          Func <TKey, int> hashKey, Func <TKey, TKey, int> compareKey)
        {
            _file       = file;
            _size       = 0;
            _nextPairId = 0;
            _hashKey    = hashKey;
            _compareKey = compareKey;

            _hashes = new MemoryMappedHugeArrayInt64(_file, 1024 * 1024,
                                                     MemoryMappedHugeArrayInt64.DefaultFileElementSize,
                                                     (int)MemoryMappedHugeArrayInt64.DefaultFileElementSize / MemoryMappedHugeArrayInt64.DefaultBufferSize,
                                                     MemoryMappedHugeArrayInt64.DefaultCacheSize);
            for (int idx = 0; idx < _hashes.Length; idx++)
            {
                _hashes[idx] = -1;
            }
            _pairs = new MemoryMappedHugeArrayInt64(_file, 16 * 4);
            _keys  = new MemoryMappedIndex <KeyStruct>(_file,
                                                       (stream, position) =>
            {
                return(new KeyStruct()
                {
                    Key = readKeyFrom.Invoke(stream, position)
                });
            },
                                                       (stream, position, structure) =>
            {
                return(writeToKey.Invoke(stream, position, structure.Key));
            });
            _values = new MemoryMappedIndex <ValueStruct>(_file,
                                                          (stream, position) =>
            {
                return(new ValueStruct()
                {
                    Value = readValueFrom.Invoke(stream, position)
                });
            },
                                                          (stream, position, structure) =>
            {
                return(writeValueTo.Invoke(stream, position, structure.Value));
            });
        }
        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);
                }
            }
        }