Beispiel #1
0
        /// <summary>
        /// Write a block of data (<code>For</code> format).
        /// </summary>
        /// <param name="data">     the data to write </param>
        /// <param name="encoded">  a buffer to use to encode data </param>
        /// <param name="out">      the destination output </param>
        /// <exception cref="IOException"> If there is a low-level I/O error </exception>
        public void WriteBlock(int[] data, byte[] encoded, IndexOutput @out)
        {
            if (IsAllEqual(data))
            {
                @out.WriteByte((byte)(sbyte)ALL_VALUES_EQUAL);
                @out.WriteVInt(data[0]);
                return;
            }

            int numBits = BitsRequired(data);

            Debug.Assert(numBits > 0 && numBits <= 32, numBits.ToString());
            PackedInts.Encoder encoder = Encoders[numBits];
            int iters = Iterations[numBits];

            Debug.Assert(iters * encoder.ByteValueCount() >= Lucene41PostingsFormat.BLOCK_SIZE);
            int encodedSize = EncodedSizes[numBits];

            Debug.Assert(iters * encoder.ByteBlockCount() >= encodedSize);

            @out.WriteByte((byte)numBits);

            encoder.Encode(data, 0, encoded, 0, iters);
            @out.WriteBytes(encoded, encodedSize);
        }
Beispiel #2
0
        public override int Set(int index, long[] arr, int off, int len)
        {
            Debug.Assert(len > 0, "len must be > 0 (got " + len + ")");
            Debug.Assert(index >= 0 && index < valueCount);
            len = Math.Min(len, valueCount - index);
            Debug.Assert(off + len <= arr.Length);

            int originalIndex = index;

            PackedInts.Encoder encoder = BulkOperation.Of(PackedInts.Format.PACKED, bitsPerValue);

            // go to the next block where the value does not span across two blocks
            int offsetInBlocks = index % encoder.LongValueCount();

            if (offsetInBlocks != 0)
            {
                for (int i = offsetInBlocks; i < encoder.LongValueCount() && len > 0; ++i)
                {
                    Set(index++, arr[off++]);
                    --len;
                }
                if (len == 0)
                {
                    return(index - originalIndex);
                }
            }

            // bulk set
            Debug.Assert(index % encoder.LongValueCount() == 0);
            int blockIndex = (int)((ulong)((long)index * bitsPerValue) >> BLOCK_BITS);

            Debug.Assert((((long)index * bitsPerValue) & MOD_MASK) == 0);
            int iterations = len / encoder.LongValueCount();

            encoder.Encode(arr, off, Blocks, blockIndex, iterations);
            int setValues = iterations * encoder.LongValueCount();

            index += setValues;
            len   -= setValues;
            Debug.Assert(len >= 0);

            if (index > originalIndex)
            {
                // stay at the block boundary
                return(index - originalIndex);
            }
            else
            {
                // no progress so far => already at a block boundary but no full block to get
                Debug.Assert(index == originalIndex);
                return(base.Set(index, arr, off, len));
            }
        }
        protected internal void WriteValues(int bitsRequired)
        {
            PackedInts.Encoder encoder = PackedInts.GetEncoder(PackedInts.Format.PACKED, PackedInts.VERSION_CURRENT, bitsRequired);
            int iterations             = Values.Length / encoder.ByteValueCount();
            int blockSize = encoder.ByteBlockCount() * iterations;

            if (Blocks == null || Blocks.Length < blockSize)
            {
                Blocks = new byte[blockSize];
            }
            if (Off < Values.Length)
            {
                Arrays.Fill(Values, Off, Values.Length, 0L);
            }
            encoder.Encode(Values, 0, Blocks, 0, iterations);
            int blockCount = (int)PackedInts.Format.PACKED.ByteCount(PackedInts.VERSION_CURRENT, Off, bitsRequired);

            @out.WriteBytes(Blocks, blockCount);
        }
            internal virtual void PforEncode()
            {
                if (NumExceptions > 0)
                {
                    int mask = (1 << BitsPerValue) - 1;
                    int ex   = 0;
                    for (int i = 0; i < BufferSize; ++i)
                    {
                        if (Buffer[i] > mask)
                        {
                            ExceptionIndices[ex] = i;
                            Exceptions[ex++]     = (int)((uint)Buffer[i] >> BitsPerValue);
                            Buffer[i]           &= mask;
                        }
                    }
                    Debug.Assert(ex == NumExceptions);
                    Arrays.Fill(Exceptions, NumExceptions, BLOCK_SIZE, 0);
                }

                if (BitsPerValue > 0)
                {
                    PackedInts.Encoder encoder = PackedInts.GetEncoder(PackedInts.Format.PACKED, PackedInts.VERSION_CURRENT, BitsPerValue);
                    int numIterations          = ITERATIONS[BitsPerValue];
                    encoder.Encode(Buffer, 0, Data.Bytes, Data.Length, numIterations);
                    Data.Length += encoder.ByteBlockCount() * numIterations;
                }

                if (NumExceptions > 0)
                {
                    Debug.Assert(BitsPerException > 0);
                    Data.WriteByte((sbyte)NumExceptions);
                    Data.WriteByte((sbyte)BitsPerException);
                    PackedInts.Encoder encoder = PackedInts.GetEncoder(PackedInts.Format.PACKED, PackedInts.VERSION_CURRENT, BitsPerException);
                    int numIterations          = (NumExceptions + encoder.ByteValueCount() - 1) / encoder.ByteValueCount();
                    encoder.Encode(Exceptions, 0, Data.Bytes, Data.Length, numIterations);
                    Data.Length += (int)PackedInts.Format.PACKED.ByteCount(PackedInts.VERSION_CURRENT, NumExceptions, BitsPerException);
                    for (int i = 0; i < NumExceptions; ++i)
                    {
                        Data.WriteByte((sbyte)ExceptionIndices[i]);
                    }
                }
            }