Exemplo n.º 1
0
 public override void Add(long v)
 {
     Debug.Assert(bitsPerValue == 64 || (v >= 0 && v <= PackedInts.MaxValue(bitsPerValue)), bitsPerValue.ToString());
     Debug.Assert(!Finished);
     if (valueCount != -1 && Written >= valueCount)
     {
         throw new System.IO.EndOfStreamException("Writing past end of stream");
     }
     NextValues[Off++] = v;
     if (Off == NextValues.Length)
     {
         Flush();
     }
     ++Written;
 }
Exemplo n.º 2
0
        protected internal override void Flush()
        {
            Debug.Assert(Off > 0);
            long min = long.MaxValue, max = long.MinValue;

            for (int i = 0; i < Off; ++i)
            {
                min = Math.Min(Values[i], min);
                max = Math.Max(Values[i], max);
            }

            long delta        = max - min;
            int  bitsRequired = delta < 0 ? 64 : delta == 0L ? 0 : PackedInts.BitsRequired(delta);

            if (bitsRequired == 64)
            {
                // no need to delta-encode
                min = 0L;
            }
            else if (min > 0L)
            {
                // make min as small as possible so that writeVLong requires fewer bytes
                min = Math.Max(0L, max - PackedInts.MaxValue(bitsRequired));
            }

            int token = (bitsRequired << BPV_SHIFT) | (min == 0 ? MIN_VALUE_EQUALS_0 : 0);

            @out.WriteByte((byte)(sbyte)token);

            if (min != 0)
            {
                WriteVLong(@out, ZigZagEncode(min) - 1);
            }

            if (bitsRequired > 0)
            {
                if (min != 0)
                {
                    for (int i = 0; i < Off; ++i)
                    {
                        Values[i] -= min;
                    }
                }
                WriteValues(bitsRequired);
            }

            Off = 0;
        }
Exemplo n.º 3
0
 /// <summary>
 /// Write a value using exactly <code>bitsPerValue</code> bits.
 /// </summary>
 public void WriteLong(long value, int bitsPerValue)
 {
     Debug.Assert(bitsPerValue == 64 || (value >= 0 && value <= PackedInts.MaxValue(bitsPerValue)));
     while (bitsPerValue > 0)
     {
         if (RemainingBits == 0)
         {
             @out.WriteByte((sbyte)Current);
             Current       = 0L;
             RemainingBits = 8;
         }
         int bits = Math.Min(RemainingBits, bitsPerValue);
         Current        = Current | ((((long)((ulong)value >> (bitsPerValue - bits))) & ((1L << bits) - 1)) << (RemainingBits - bits));
         bitsPerValue  -= bits;
         RemainingBits -= bits;
     }
 }
Exemplo n.º 4
0
 private static long Mask(int bitsPerValue)
 {
     return(bitsPerValue == 64 ? ~0L : PackedInts.MaxValue(bitsPerValue));
 }
Exemplo n.º 5
0
        private void DoTest(FieldInfo.DocValuesType_e type)
        {
            Directory         d        = NewDirectory();
            IndexWriterConfig iwConfig = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()));
            int   nDocs = AtLeast(50);
            Field id    = new NumericDocValuesField("id", 0);
            Field f;

            switch (type)
            {
            case FieldInfo.DocValuesType_e.BINARY:
                f = new BinaryDocValuesField("dv", new BytesRef());
                break;

            case FieldInfo.DocValuesType_e.SORTED:
                f = new SortedDocValuesField("dv", new BytesRef());
                break;

            case FieldInfo.DocValuesType_e.NUMERIC:
                f = new NumericDocValuesField("dv", 0);
                break;

            default:
                throw new InvalidOperationException();
            }
            Document document = new Document();

            document.Add(id);
            document.Add(f);

            object[] vals = new object[nDocs];

            RandomIndexWriter iw = new RandomIndexWriter(Random(), d, iwConfig);

            for (int i = 0; i < nDocs; ++i)
            {
                id.LongValue = i;
                switch (type)
                {
                case FieldInfo.DocValuesType_e.SORTED:
                case FieldInfo.DocValuesType_e.BINARY:
                    do
                    {
                        vals[i] = TestUtil.RandomSimpleString(Random(), 20);
                    } while (((string)vals[i]).Length == 0);
                    f.BytesValue = new BytesRef((string)vals[i]);
                    break;

                case FieldInfo.DocValuesType_e.NUMERIC:
                    int bitsPerValue = Random().NextIntBetween(1, 31);     // keep it an int
                    vals[i]     = (long)Random().Next((int)PackedInts.MaxValue(bitsPerValue));
                    f.LongValue = (long)vals[i];
                    break;
                }
                iw.AddDocument(document);
                if (Random().NextBoolean() && i % 10 == 9)
                {
                    iw.Commit();
                }
            }
            iw.Dispose();

            DirectoryReader rd = DirectoryReader.Open(d);

            foreach (AtomicReaderContext leave in rd.Leaves)
            {
                FunctionValues ids = (new LongFieldSource("id")).GetValues(null, leave);
                ValueSource    vs;
                switch (type)
                {
                case FieldInfo.DocValuesType_e.BINARY:
                case FieldInfo.DocValuesType_e.SORTED:
                    vs = new BytesRefFieldSource("dv");
                    break;

                case FieldInfo.DocValuesType_e.NUMERIC:
                    vs = new LongFieldSource("dv");
                    break;

                default:
                    throw new InvalidOperationException();
                }
                FunctionValues values = vs.GetValues(null, leave);
                BytesRef       bytes  = new BytesRef();
                for (int i = 0; i < leave.AtomicReader.MaxDoc; ++i)
                {
                    assertTrue(values.Exists(i));
                    if (vs is BytesRefFieldSource)
                    {
                        assertTrue(values.ObjectVal(i) is string);
                    }
                    else if (vs is LongFieldSource)
                    {
                        assertTrue(values.ObjectVal(i) is long?);
                        assertTrue(values.BytesVal(i, bytes));
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }

                    object expected = vals[ids.IntVal(i)];
                    switch (type)
                    {
                    case FieldInfo.DocValuesType_e.SORTED:
                        values.OrdVal(i);     // no exception
                        assertTrue(values.NumOrd() >= 1);
                        goto case FieldInfo.DocValuesType_e.BINARY;

                    case FieldInfo.DocValuesType_e.BINARY:
                        assertEquals(expected, values.ObjectVal(i));
                        assertEquals(expected, values.StrVal(i));
                        assertEquals(expected, values.ObjectVal(i));
                        assertEquals(expected, values.StrVal(i));
                        assertTrue(values.BytesVal(i, bytes));
                        assertEquals(new BytesRef((string)expected), bytes);
                        break;

                    case FieldInfo.DocValuesType_e.NUMERIC:
                        assertEquals(Number.ToInt64(expected.ToString()), values.LongVal(i));
                        break;
                    }
                }
            }
            rd.Dispose();
            d.Dispose();
        }