Пример #1
0
        public void Add_Size0()
        {
            var a = new LruDictionary <int, string>(10);

            a.Add(1, "one", 0, onRemove: default);
            Assert.IsTrue(a.CurrentSize == 0);
        }
Пример #2
0
        public void RandomInserts_1M_MultiThreaded()
        {
            var a = new LruDictionary <int, string>(1024 * 1024 * 1024);

            var start = new ManualResetEventSlim(false);
            var ts    = new Task[4];

            for (var i = 0; i < ts.Length; i++)
            {
                ts[i] = new Task(Do, TaskCreationOptions.LongRunning);
                ts[i].Start();
            }
            var sw = new Stopwatch(); sw.Start();

            start.Set();
            Task.WhenAll(ts).Wait();
            sw.Stop();
            Console.WriteLine(sw.Elapsed);

            void Do()
            {
                start.Wait();
                var r = new Random();

                for (var i = 0; i < 1_000_000; i++)
                {
                    a.Add(r.Next(), "foo", r.Next(1, 10 * 1024 * 1024), onRemove: default);
                }
            }
        }
Пример #3
0
        public void Remove0()
        {
            var a = new LruDictionary <int, string>(15);

            Assert.IsTrue(a.Remove(1) == false);
            Assert.IsTrue(a.Count == 0);
        }
Пример #4
0
        public void SimpleTests()
        {
            int dumpCount  = 0;
            int foundCount = 0;
            int capacity   = (int)Math.Pow(10, 2);
            var dict       = new LruDictionary <int, Data>(capacity, x => dumpCount++);

            for (int i = 0; i < _inputList.Count; i++)
            {
                dict.Set(_inputList[i], new Data(_inputList[i]));
            }

            for (int i = _inputList.Count - 1; i >= 0; i--)
            {
                bool found = dict.TryGetValue(_inputList[i], out Data value);
                if (found)
                {
                    foundCount++;
                }
            }

            dict.Count.Should().Be(capacity);
            dumpCount.Should().BeGreaterThan(0);
            foundCount.Should().BeGreaterThan(0);
        }
Пример #5
0
        public void Create()
        {
            var a = new LruDictionary <int, string>(10);

            Assert.IsTrue(a.MaxSize == 10);
            Assert.IsTrue(a.CurrentSize == 0);
            Assert.IsTrue(a.Count == 0);
        }
Пример #6
0
        public void Remove1()
        {
            var a = new LruDictionary <int, string>(15);

            a.Add(1, "one", 3, onRemove: default);
            Assert.IsTrue(a.Remove(1) == true);
            Assert.IsTrue(a.Count == 0);
        }
Пример #7
0
        public void Add_NegativeSize()
        {
            var a = new LruDictionary <int, string>(10);

            Assert.Catch(() =>
            {
                a.Add(1, "one", -1, onRemove: default);
            });
        }
Пример #8
0
        public void Add_SizeTooBig()
        {
            var a = new LruDictionary <int, string>(10);

            Assert.Catch(() =>
            {
                a.Add(1, "one", 11, onRemove: default);
            });
        }
Пример #9
0
        public void Remove1_Callback_Without()
        {
            var a       = new LruDictionary <int, string>(15);
            var removed = new HashSet <int>();

            a.Add(1, "one", 3, onRemove: (k, v, s) => removed.Add(1));
            Assert.IsTrue(a.Remove(1, false) == true);
            Assert.IsTrue(a.Count == 0);
            Assert.IsTrue(!removed.Contains(1));
        }
Пример #10
0
        public void Add1()
        {
            var a = new LruDictionary <int, string>(10);

            a.Add(1, "one", 3, onRemove: default);

            Assert.IsTrue(a.MaxSize == 10);
            Assert.IsTrue(a.CurrentSize == 3);
            Assert.IsTrue(a.Count == 1);
        }
Пример #11
0
        public void Clear0()
        {
            var a = new LruDictionary <int, string>(10);

            Assert.IsTrue(a.CurrentSize == 0);
            Assert.IsTrue(a.Count == 0);

            a.Clear();
            Assert.IsTrue(a.CurrentSize == 0);
            Assert.IsTrue(a.Count == 0);
        }
Пример #12
0
        /// <summary>
        /// Opens a <see cref="DirectoryTaxonomyReader"/> over the given
        /// <see cref="DirectoryTaxonomyWriter"/> (for NRT).
        /// </summary>
        /// <param name="taxoWriter">
        ///          The <see cref="DirectoryTaxonomyWriter"/> from which to obtain newly
        ///          added categories, in real-time. </param>
        public DirectoryTaxonomyReader(DirectoryTaxonomyWriter taxoWriter)
        {
            this.taxoWriter = taxoWriter;
            taxoEpoch       = taxoWriter.TaxonomyEpoch;
            indexReader     = OpenIndexReader(taxoWriter.InternalIndexWriter);

            // These are the default cache sizes; they can be configured after
            // construction with the cache's setMaxSize() method

            ordinalCache  = new LruDictionary <FacetLabel, Int32Class>(DEFAULT_CACHE_VALUE);
            categoryCache = new LruDictionary <int, FacetLabel>(DEFAULT_CACHE_VALUE);
        }
Пример #13
0
        /// <summary>
        /// Open for reading a taxonomy stored in a given <see cref="Directory"/>.
        /// </summary>
        /// <param name="directory"> The <see cref="Directory"/> in which the taxonomy resides. </param>
        /// <exception cref="Index.CorruptIndexException"> if the Taxonomy is corrupt. </exception>
        /// <exception cref="IOException"> if another error occurred. </exception>
        public DirectoryTaxonomyReader(Directory directory)
        {
            indexReader = OpenIndexReader(directory);
            taxoWriter  = null;
            taxoEpoch   = -1;

            // These are the default cache sizes; they can be configured after
            // construction with the cache's setMaxSize() method

            ordinalCache  = new LruDictionary <FacetLabel, Int32Class>(DEFAULT_CACHE_VALUE);
            categoryCache = new LruDictionary <int, FacetLabel>(DEFAULT_CACHE_VALUE);
        }
Пример #14
0
        public void Clear2()
        {
            var a = new LruDictionary <int, string>(10);

            a.Add(1, "one", 3, onRemove: default);
            a.Add(2, "two", 4, onRemove: default);
            Assert.IsTrue(a.CurrentSize == 7);
            Assert.IsTrue(a.Count == 2);

            a.Clear();
            Assert.IsTrue(a.CurrentSize == 0);
            Assert.IsTrue(a.Count == 0);
        }
Пример #15
0
        public void Add2_ReplaceLast()
        {
            var a = new LruDictionary <int, string>(10);

            a.Add(1, "one", 3, onRemove: default);
            a.Add(2, "two", 4, onRemove: default);
            Assert.IsTrue(a.CurrentSize == 7);
            Assert.IsTrue(a.Count == 2);

            a.Add(2, "two", 5, onRemove: default);
            Assert.IsTrue(a.CurrentSize == 8);
            Assert.IsTrue(a.Count == 2);
        }
Пример #16
0
        public void Add3_Replace_Middle()
        {
            var a = new LruDictionary <int, string>(20);

            a.Add(1, "one", 3, onRemove: default);
            a.Add(2, "two", 4, onRemove: default);
            a.Add(3, "three", 5, onRemove: default);
            Assert.IsTrue(a.CurrentSize == 12);
            Assert.IsTrue(a.Count == 3);

            a.Add(2, "two", 6, onRemove: default);
            Assert.IsTrue(a.CurrentSize == 14);
            Assert.IsTrue(a.Count == 3);
        }
Пример #17
0
        public void RandomInserts_1M_SingleThreaded()
        {
            var a = new LruDictionary <int, string>(1024 * 1024 * 1024);

            var r  = new Random();
            var sw = new Stopwatch(); sw.Start();

            for (var i = 0; i < 1_000_000; i++)
            {
                a.Add(r.Next(), "foo", r.Next(1, 10 * 1024 * 1024), onRemove: default);
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed);
        }
Пример #18
0
        /// <summary>
        /// Called only from <see cref="DoOpenIfChanged()"/>. If the taxonomy has been
        /// recreated, you should pass <c>null</c> as the caches and parent/children
        /// arrays.
        /// </summary>
        private DirectoryTaxonomyReader(DirectoryReader indexReader, DirectoryTaxonomyWriter taxoWriter,
                                        LruDictionary <FacetLabel, Int32Class> ordinalCache, LruDictionary <int, FacetLabel> categoryCache,
                                        TaxonomyIndexArrays taxoArrays)
        {
            this.indexReader = indexReader;
            this.taxoWriter  = taxoWriter;
            this.taxoEpoch   = taxoWriter == null ? -1 : taxoWriter.TaxonomyEpoch;

            // use the same instance of the cache, note the protective code in getOrdinal and getPath
            this.ordinalCache  = ordinalCache ?? new LruDictionary <FacetLabel, Int32Class>(DEFAULT_CACHE_VALUE);
            this.categoryCache = categoryCache ?? new LruDictionary <int, FacetLabel>(DEFAULT_CACHE_VALUE);

            this.taxoArrays = taxoArrays != null ? new TaxonomyIndexArrays(indexReader, taxoArrays) : null;
        }
Пример #19
0
        // LUCENENET specific - eliminated the InitTaxoArrays() method in favor of LazyInitializer

        protected override void Dispose(bool disposing) // LUCENENET specific - changed from DoClose()
        {
            if (disposing && !isDisposed)
            {
                indexReader.Dispose();
                taxoArrays = null;
                // do not clear() the caches, as they may be used by other DTR instances.
                ordinalCache  = null;
                categoryCache = null;
                ordinalCacheLock.Dispose(); // LUCENENET specific - cleanup ReaderWriterLockSlim instances
                categoryCacheLock.Dispose();
                isDisposed = true;
            }
        }
Пример #20
0
        public void Add3_Overshoot()
        {
            var a = new LruDictionary <int, string>(15);

            a.Add(1, "one", 3, onRemove: default);
            a.Add(2, "two", 4, onRemove: default);
            a.Add(3, "three", 5, onRemove: default);
            Assert.IsTrue(a.CurrentSize == 12);
            Assert.IsTrue(a.Count == 3);

            a.Add(4, "four", 10, onRemove: default);
            Assert.IsTrue(a.CurrentSize == 15);
            Assert.IsTrue(a.Count == 2);
        }
Пример #21
0
        public void TestLruDictionary()
        {
            var dic = new LruDictionary <int, string>(10);

            for (var i = 0; i < 12; ++i)
            {
                dic[i] = i.ToString();
            }
            //the dic[1] is null because the item exceeded the capacity of the dictionary and is not latest visited
            Assert.AreEqual(null, dic[1]);
            Assert.AreEqual("3", dic[3]);
            Assert.AreEqual("4", dic[4]);
            Assert.AreEqual("3", dic[3]);
        }
Пример #22
0
        /// <summary>
        /// Loads point cloud from store.
        /// </summary>
        public static PointSet Load(string key, string storePath, LruDictionary <string, object> cache)
        {
            if (!Directory.Exists(storePath))
            {
                throw new InvalidOperationException($"Not a store ({storePath}).");
            }

            var store  = OpenStore(storePath, cache);
            var result = store.GetPointSet(key);

            if (result == null)
            {
                throw new InvalidOperationException($"Key {key} not found in {storePath}.");
            }
            return(result);
        }
Пример #23
0
        /// <summary>
        /// Opens or creates a store at the specified location.
        /// </summary>
        public static Storage OpenStore(string storePath, LruDictionary <string, object> cache)
        {
            lock (s_stores)
            {
                if (s_stores.TryGetValue(storePath, out WeakReference <Storage> x) && x.TryGetTarget(out Storage cached))
                {
                    if (!cached.IsDisposed)
                    {
                        return(cached);
                    }
                }

                var store = new SimpleDiskStore(storePath).ToPointCloudStore(cache);
                s_stores[storePath] = new WeakReference <Storage>(store);
                return(store);
            }
        }
Пример #24
0
        public void ContainsKey()
        {
            var a = new LruDictionary <int, string>(15);

            a.Add(1, "one", 3, onRemove: default);
            a.Add(2, "two", 4, onRemove: default);

            Assert.IsTrue(a.ContainsKey(1));
            Assert.IsTrue(a.ContainsKey(2));
            Assert.IsTrue(!a.ContainsKey(3));

            a.Remove(1);
            Assert.IsTrue(!a.ContainsKey(1));

            a.Remove(2);
            Assert.IsTrue(!a.ContainsKey(2));
        }
Пример #25
0
        public void Remove3_Middle()
        {
            var a = new LruDictionary <int, string>(15);

            a.Add(1, "one", 3, onRemove: default);
            a.Add(2, "two", 4, onRemove: default);
            a.Add(3, "three", 5, onRemove: default);

            Assert.IsTrue(a.Remove(2) == true);
            Assert.IsTrue(a.Count == 2);

            Assert.IsTrue(a.Remove(1) == true);
            Assert.IsTrue(a.Count == 1);

            Assert.IsTrue(a.Remove(3) == true);
            Assert.IsTrue(a.Count == 0);
        }
Пример #26
0
        public void TryGetValue()
        {
            var a = new LruDictionary <int, string>(15);

            a.Add(1, "one", 3, onRemove: default);
            a.Add(2, "two", 4, onRemove: default);

            Assert.IsTrue(a.TryGetValue(1, out string s1) && s1 == "one");
            Assert.IsTrue(a.TryGetValue(2, out string s2) && s2 == "two");
            Assert.IsTrue(!a.TryGetValue(3, out string s3));

            a.Remove(1);
            Assert.IsTrue(!a.TryGetValue(1, out string s1a));

            a.Remove(2);
            Assert.IsTrue(!a.TryGetValue(2, out string s2a));
        }
Пример #27
0
        /// <summary></summary>
        public Storage(
            Action <string, object, Func <byte[]> > add,
            Func <string, byte[]> get,
            Action <string> remove,
            Action dispose,
            Action flush,
            LruDictionary <string, object> cache
            )
        {
            f_add     = add;
            f_get     = get;
            f_remove  = remove;
            f_dispose = dispose;
            f_flush   = flush;
            Cache     = cache;

            Register(this);
        }
Пример #28
0
        /// <summary>
        /// Imports file into out-of-core store.
        /// Format is guessed based on file extension.
        /// </summary>
        public static PointSet Import(string filename, string storeDirectory, LruDictionary <string, object> cache)
        {
            if (filename == null)
            {
                throw new ArgumentNullException(nameof(filename));
            }
            if (!File.Exists(filename))
            {
                throw new FileNotFoundException("File does not exit.", filename);
            }

            var config = ImportConfig.Default
                         .WithStorage(OpenStore(storeDirectory, cache))
                         .WithKey(FileHelpers.ComputeMd5Hash(filename, true))
            ;

            var result = PointCloudFileFormat.FromFileName(filename).ImportFile(filename, config);

            config.Storage.Flush();
            return(result);
        }
Пример #29
0
        public void Add3_Overshoot_WithCallback()
        {
            var a       = new LruDictionary <int, string>(15);
            var removed = new HashSet <int>();

            a.Add(1, "one", 3, onRemove: (k, v, s) => { removed.Add(1); Assert.IsTrue(k == 1 && v == "one" && s == 3); });
            a.Add(2, "two", 4, onRemove: (k, v, s) => { removed.Add(2); Assert.IsTrue(k == 2 && v == "two" && s == 4); });
            a.Add(3, "three", 5, onRemove: (k, v, s) => { removed.Add(3); Assert.IsTrue(k == 3 && v == "three" && s == 5); });
            a.Add(1, "one", 3, onRemove: (k, v, s) => { removed.Add(1); Assert.IsTrue(k == 1 && v == "one" && s == 3); });
            Assert.IsTrue(a.CurrentSize == 12);
            Assert.IsTrue(a.Count == 3);

            a.Add(4, "four", 10, onRemove: (k, v, s) => { removed.Add(4); Assert.IsTrue(k == 4 && v == "four" && s == 10); });
            Assert.IsTrue(a.CurrentSize == 13);
            Assert.IsTrue(a.Count == 2);

            Assert.IsTrue(!removed.Contains(1));
            Assert.IsTrue(removed.Contains(2));
            Assert.IsTrue(removed.Contains(3));
            Assert.IsTrue(!removed.Contains(4));
        }
Пример #30
0
        public void GetOrCreate()
        {
            var a       = new LruDictionary <int, string>(15);
            var removed = new HashSet <int>();

            var s1 = a.GetOrCreate(1, () => ("one", 3), (k, v, s) => removed.Add(1));

            Assert.IsTrue(s1 == "one");
            Assert.IsTrue(a.Count == 1);
            Assert.IsTrue(a.ContainsKey(1));

            var s1a = a.GetOrCreate(1, () => throw new Exception(), (k, v, s) => removed.Add(-1));

            Assert.IsTrue(s1 == "one");
            Assert.IsTrue(a.Count == 1);
            Assert.IsTrue(a.ContainsKey(1));

            Assert.IsTrue(a.Remove(1, true));
            Assert.IsTrue(removed.Contains(1));
            Assert.IsTrue(!removed.Contains(-1));
            Assert.IsTrue(a.Count == 0);
        }