Exemplo n.º 1
0
        public virtual void  TestLongStream()
        {
            NumericTokenStream stream = new NumericTokenStream().SetLongValue(lvalue);
            // use getAttribute to test if attributes really exist, if not an IAE will be throwed
            TermAttribute termAtt = (TermAttribute)stream.GetAttribute(typeof(TermAttribute));
            TypeAttribute typeAtt = (TypeAttribute)stream.GetAttribute(typeof(TypeAttribute));

            for (int shift = 0; shift < 64; shift += NumericUtils.PRECISION_STEP_DEFAULT)
            {
                Assert.IsTrue(stream.IncrementToken(), "New token is available");
                Assert.AreEqual(NumericUtils.LongToPrefixCoded(lvalue, shift), termAtt.Term(), "Term is correctly encoded");
                Assert.AreEqual((shift == 0)?NumericTokenStream.TOKEN_TYPE_FULL_PREC:NumericTokenStream.TOKEN_TYPE_LOWER_PREC, typeAtt.Type(), "Type correct");
            }
            Assert.IsFalse(stream.IncrementToken(), "No more tokens available");
        }
Exemplo n.º 2
0
        private bool IncrementTokenUnlikely()
        {
            char[] buffer;
            if (valSize == 64)
            {
                buffer = termAtt.ResizeTermBuffer(NumericUtils.BUF_SIZE_LONG);
                termAtt.SetTermLength(NumericUtils.LongToPrefixCoded(value_Renamed, shift, buffer));
            }
            else if (valSize == 32)
            {
                buffer = termAtt.ResizeTermBuffer(NumericUtils.BUF_SIZE_INT);
                termAtt.SetTermLength(NumericUtils.IntToPrefixCoded((int)value_Renamed, shift, buffer));
            }
            else
            {
                return(ThrowValueSizeIsNotValid <bool> ());
            }

            typeAtt.Type = (shift == 0) ? TOKEN_TYPE_FULL_PREC : TOKEN_TYPE_LOWER_PREC;
            posIncrAtt.PositionIncrement = (shift == 0) ? 1 : 0;
            shift += precisionStep;
            return(true);
        }
Exemplo n.º 3
0
        // @Override
        public override bool IncrementToken()
        {
            if (valSize == 0)
            {
                throw new System.SystemException("call set???Value() before usage");
            }
            if (shift >= valSize)
            {
                return(false);
            }

            ClearAttributes();
            char[] buffer;
            switch (valSize)
            {
            case 64:
                buffer = termAtt.ResizeTermBuffer(NumericUtils.BUF_SIZE_LONG);
                termAtt.SetTermLength(NumericUtils.LongToPrefixCoded(value_Renamed, shift, buffer));
                break;


            case 32:
                buffer = termAtt.ResizeTermBuffer(NumericUtils.BUF_SIZE_INT);
                termAtt.SetTermLength(NumericUtils.IntToPrefixCoded((int)value_Renamed, shift, buffer));
                break;


            default:
                // should not happen
                throw new System.ArgumentException("valSize must be 32 or 64");
            }

            typeAtt.Type = (shift == 0)?TOKEN_TYPE_FULL_PREC:TOKEN_TYPE_LOWER_PREC;
            posIncrAtt.PositionIncrement = (shift == 0)?1:0;
            shift += precisionStep;
            return(true);
        }
Exemplo n.º 4
0
        // @Override
        public override bool IncrementToken()
        {
            if (valSize == 0)
            {
                goto Error;
            }

            if (shift >= valSize)
            {
                return(false);
            }

            ClearAttributes();

            var termAttLikely = termAtt as TermAttribute;

            if (termAttLikely == null)
            {
                goto Unlikely;
            }

            char[] buffer;
            if (valSize == 64)
            {
                buffer = termAttLikely.ResizeTermBuffer(NumericUtils.BUF_SIZE_LONG);
                termAttLikely.SetTermLength(NumericUtils.LongToPrefixCoded(value_Renamed, shift, buffer));
            }
            else if (valSize == 32)
            {
                buffer = termAttLikely.ResizeTermBuffer(NumericUtils.BUF_SIZE_INT);
                termAttLikely.SetTermLength(NumericUtils.IntToPrefixCoded((int)value_Renamed, shift, buffer));
            }
            else
            {
                goto Error;
            }

            string type      = (shift == 0) ? TOKEN_TYPE_FULL_PREC : TOKEN_TYPE_LOWER_PREC;
            int    increment = (shift == 0) ? 1 : 0;

            // PERF: Try to avoid as much as possible the virtual calls here.
            var typeAttConcrete = typeAtt as TypeAttribute;

            if (typeAttConcrete != null)
            {
                typeAttConcrete.Type = type;
            }
            else
            {
                typeAtt.Type = type;
            }

            // PERF: Try to avoid as much as possible the virtual calls here.
            var posIncrAttConcrete = posIncrAtt as PositionIncrementAttribute;

            if (posIncrAttConcrete != null)
            {
                posIncrAttConcrete.PositionIncrement = increment;
            }
            else
            {
                posIncrAtt.PositionIncrement = increment;
            }

            shift += precisionStep;
            return(true);

Unlikely:
            return(IncrementTokenUnlikely());

Error:
            return(ThrowValueSizeIsNotValid <bool>());
        }
        private void  TestRandomTrieAndClassicRangeQuery(int precisionStep)
        {
            System.Random rnd = NewRandom();
            System.String field = "field" + precisionStep;
            int           termCountT = 0, termCountC = 0;

            for (int i = 0; i < 50; i++)
            {
                long lower = (long)(rnd.NextDouble() * noDocs * distance) + startOffset;
                long upper = (long)(rnd.NextDouble() * noDocs * distance) + startOffset;
                if (lower > upper)
                {
                    long a = lower; lower = upper; upper = a;
                }
                // test inclusive range
                System.Int64      tempAux  = (long)lower;
                System.Int64      tempAux2 = (long)upper;
                NumericRangeQuery tq       = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux, tempAux2, true, true);
                TermRangeQuery    cq       = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), true, true);
                TopDocs           tTopDocs = searcher.Search(tq, 1);
                TopDocs           cTopDocs = searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.totalHits, tTopDocs.totalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                termCountT += tq.GetTotalNumberOfTerms();
                termCountC += cq.GetTotalNumberOfTerms();
                // test exclusive range
                System.Int64 tempAux3 = (long)lower;
                System.Int64 tempAux4 = (long)upper;
                tq       = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux3, tempAux4, false, false);
                cq       = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), false, false);
                tTopDocs = searcher.Search(tq, 1);
                cTopDocs = searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.totalHits, tTopDocs.totalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                termCountT += tq.GetTotalNumberOfTerms();
                termCountC += cq.GetTotalNumberOfTerms();
                // test left exclusive range
                System.Int64 tempAux5 = (long)lower;
                System.Int64 tempAux6 = (long)upper;
                tq       = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux5, tempAux6, false, true);
                cq       = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), false, true);
                tTopDocs = searcher.Search(tq, 1);
                cTopDocs = searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.totalHits, tTopDocs.totalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                termCountT += tq.GetTotalNumberOfTerms();
                termCountC += cq.GetTotalNumberOfTerms();
                // test right exclusive range
                System.Int64 tempAux7 = (long)lower;
                System.Int64 tempAux8 = (long)upper;
                tq       = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux7, tempAux8, true, false);
                cq       = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), true, false);
                tTopDocs = searcher.Search(tq, 1);
                cTopDocs = searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.totalHits, tTopDocs.totalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                termCountT += tq.GetTotalNumberOfTerms();
                termCountC += cq.GetTotalNumberOfTerms();
            }
            if (precisionStep == System.Int32.MaxValue)
            {
                Assert.AreEqual(termCountT, termCountC, "Total number of terms should be equal for unlimited precStep");
            }
            else
            {
                System.Console.Out.WriteLine("Average number of terms during random search on '" + field + "':");
                System.Console.Out.WriteLine(" Trie query: " + (((double)termCountT) / (50 * 4)));
                System.Console.Out.WriteLine(" Classical query: " + (((double)termCountC) / (50 * 4)));
            }
        }