예제 #1
0
        public static Predicate <int> GetDocsExistenceVerificationMethodInSet(this IndexReader reader, string field, Predicate <string> termAcceptanceFunction)
        {
            ConcurrentDictionary <Tuple <string, Predicate <string> >, Predicate <int> > value;

            if (documentIdsSetBuildersCache.TryGetValue(reader, out value) == false)
            {
                value = documentIdsSetBuildersCache.GetOrCreateValue(reader);
                _keys.Add(new WeakReference <IndexReader>(reader));
                IndexReader target;
                _keys.RemoveWhere(x => x.TryGetTarget(out target) == false);
            }

            var readerFieldTuple = Tuple.Create(field, termAcceptanceFunction);

            Predicate <int> predicate;

            if (value.TryGetValue(readerFieldTuple, out predicate))
            {
                return(predicate);
            }


            var termDocs        = reader.TermDocs();
            var termEnum        = reader.Terms(new Term(field));
            int t               = 0; // current term number
            var maxDoc          = reader.MaxDoc;
            var documentsIDsSet = new DocumentsIDsSetBuilder(reader.MaxDoc);

            do
            {
                Term term = termEnum.Term;
                if (term == null || term.Field != field || t >= maxDoc)
                {
                    break;
                }

                bool termMatches = termAcceptanceFunction(term.Text);

                if (termMatches)
                {
                    termDocs.Seek(termEnum);
                    while (termDocs.Next())
                    {
                        documentsIDsSet.Set(termDocs.Doc);
                    }
                }

                t++;
            }while (termEnum.Next());
            predicate = documentsIDsSet.Build();
            value.TryAdd(readerFieldTuple, predicate);
            return(predicate);
        }
예제 #2
0
		public static Predicate<int> GetDocsExistenceVerificationMethodInSet(this IndexReader reader, string field, Predicate<string> termAcceptanceFunction)
		{
			ConcurrentDictionary<Tuple<string, Predicate<string>>, Predicate<int>> value;
			if (documentIdsSetBuildersCache.TryGetValue(reader, out value) == false)
			{
				value = documentIdsSetBuildersCache.GetOrCreateValue(reader);
				_keys.Add(new WeakReference<IndexReader>(reader));
				IndexReader target;
				_keys.RemoveWhere(x=>x.TryGetTarget(out target) == false);
			}

			var readerFieldTuple = Tuple.Create(field, termAcceptanceFunction);

			Predicate<int> predicate;
			if (value.TryGetValue(readerFieldTuple, out predicate))
				return predicate;
		

			var termDocs = reader.TermDocs();
			var termEnum = reader.Terms(new Term(field));
			int t = 0; // current term number
			var maxDoc = reader.MaxDoc;
			var documentsIDsSet = new DocumentsIDsSetBuilder(32);

			do
			{
				Term term = termEnum.Term;
				if (term == null || term.Field != field || t >= maxDoc) break;

				bool termMatches = termAcceptanceFunction(term.Text);

				if (termMatches)
				{
					termDocs.Seek(termEnum);
					while (termDocs.Next())
					{
						documentsIDsSet.Set(termDocs.Doc);
					}
				}

				t++;
			}
			while (termEnum.Next());
			predicate = documentsIDsSet.Build();
			value.TryAdd(readerFieldTuple, predicate);
			return predicate;
		}