Пример #1
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);
        }
Пример #2
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));
        }
Пример #3
0
            public TV Get(TK key)
            {
                TV result;

                if (!_map.TryGetValue(key, out result))
                {
                    return(default(TV));
                }

                return(result);
            }
Пример #4
0
        public override FacetLabel GetPath(int ordinal)
        {
            EnsureOpen();

            // Since the cache is shared with DTR instances allocated from
            // doOpenIfChanged, we need to ensure that the ordinal is one that this DTR
            // instance recognizes. Therefore we do this check up front, before we hit
            // the cache.
            if (ordinal < 0 || ordinal >= indexReader.MaxDoc)
            {
                return(null);
            }

            // TODO: can we use an int-based hash impl, such as IntToObjectMap,
            // wrapped as LRU?

            // LUCENENET NOTE: We don't need to convert ordinal from int to int here as was done in Java.
            // LUCENENET: Despite LRUHashMap being thread-safe, we get much better performance
            // if reads are separated from writes.
            categoryCacheLock.EnterReadLock();
            try
            {
                if (categoryCache.TryGetValue(ordinal, out FacetLabel res))
                {
                    return(res);
                }
            }
            finally
            {
                categoryCacheLock.ExitReadLock();
            }

            Document doc    = indexReader.Document(ordinal);
            var      result = new FacetLabel(FacetsConfig.StringToPath(doc.Get(Consts.FULL)));

            categoryCacheLock.EnterWriteLock();
            try
            {
                categoryCache[ordinal] = result;
            }
            finally
            {
                categoryCacheLock.ExitWriteLock();
            }

            return(result);
        }
Пример #5
0
        public override int GetOrdinal(FacetLabel cp)
        {
            EnsureOpen();
            if (cp.Length == 0)
            {
                return(ROOT_ORDINAL);
            }

            // First try to find the answer in the LRU cache:

            // LUCENENET: Despite LRUHashMap being thread-safe, we get much better performance
            // if reads are separated from writes.
            ordinalCacheLock.EnterReadLock();
            try
            {
                if (ordinalCache.TryGetValue(cp, out Int32Class res))
                {
                    if (res < indexReader.MaxDoc)
                    {
                        // Since the cache is shared with DTR instances allocated from
                        // doOpenIfChanged, we need to ensure that the ordinal is one that
                        // this DTR instance recognizes.
                        return(res);
                    }
                    else
                    {
                        // if we get here, it means that the category was found in the cache,
                        // but is not recognized by this TR instance. Therefore there's no
                        // need to continue search for the path on disk, because we won't find
                        // it there too.
                        return(TaxonomyReader.INVALID_ORDINAL);
                    }
                }
            }
            finally
            {
                ordinalCacheLock.ExitReadLock();
            }

            // If we're still here, we have a cache miss. We need to fetch the
            // value from disk, and then also put it in the cache:
            int      ret  = TaxonomyReader.INVALID_ORDINAL;
            DocsEnum docs = MultiFields.GetTermDocsEnum(indexReader, null, Consts.FULL, new BytesRef(FacetsConfig.PathToString(cp.Components, cp.Length)), 0);

            if (docs != null && docs.NextDoc() != DocIdSetIterator.NO_MORE_DOCS)
            {
                ret = docs.DocID;

                // we only store the fact that a category exists, not its inexistence.
                // This is required because the caches are shared with new DTR instances
                // that are allocated from doOpenIfChanged. Therefore, if we only store
                // information about found categories, we cannot accidently tell a new
                // generation of DTR that a category does not exist.

                ordinalCacheLock.EnterWriteLock();
                try
                {
                    ordinalCache[cp] = ret;
                }
                finally
                {
                    ordinalCacheLock.ExitWriteLock();
                }
            }

            return(ret);
        }