Beispiel #1
0
        /// <summary>
        /// Read the next block of data (<c>For</c> format).
        /// </summary>
        /// <param name="in">        The input to use to read data. </param>
        /// <param name="encoded">   A buffer that can be used to store encoded data. </param>
        /// <param name="decoded">   Where to write decoded data. </param>
        /// <exception cref="IOException"> If there is a low-level I/O error. </exception>
        internal void ReadBlock(IndexInput @in, byte[] encoded, int[] decoded)
        {
            int numBits = @in.ReadByte();

            if (Debugging.AssertsEnabled)
            {
                Debugging.Assert(numBits <= 32, numBits.ToString);
            }

            if (numBits == ALL_VALUES_EQUAL)
            {
                int value = @in.ReadVInt32();
                Arrays.Fill(decoded, 0, Lucene41PostingsFormat.BLOCK_SIZE, value);
                return;
            }

            int encodedSize = encodedSizes[numBits];

            @in.ReadBytes(encoded, 0, encodedSize);

            PackedInt32s.IDecoder decoder = decoders[numBits];
            int iters = iterations[numBits];

            if (Debugging.AssertsEnabled)
            {
                Debugging.Assert(iters * decoder.ByteValueCount >= Lucene41PostingsFormat.BLOCK_SIZE);
            }

            decoder.Decode(encoded, 0, decoded, 0, iters);
        }
Beispiel #2
0
        public override int Get(int index, long[] arr, int off, int len)
        {
            Debug.Assert(len > 0, "len must be > 0 (got " + len + ")");
            Debug.Assert(index >= 0 && index < m_valueCount);
            len = Math.Min(len, m_valueCount - index);
            Debug.Assert(off + len <= arr.Length);

            int originalIndex = index;

            // go to the next block boundary
            int valuesPerBlock = 64 / m_bitsPerValue;
            int offsetInBlock  = index % valuesPerBlock;

            if (offsetInBlock != 0)
            {
                for (int i = offsetInBlock; i < valuesPerBlock && len > 0; ++i)
                {
                    arr[off++] = Get(index++);
                    --len;
                }
                if (len == 0)
                {
                    return(index - originalIndex);
                }
            }

            // bulk get
            Debug.Assert(index % valuesPerBlock == 0);
            PackedInt32s.IDecoder decoder = BulkOperation.Of(PackedInt32s.Format.PACKED_SINGLE_BLOCK, m_bitsPerValue);
            Debug.Assert(decoder.Int64BlockCount == 1);
            Debug.Assert(decoder.Int64ValueCount == valuesPerBlock);
            int blockIndex = index / valuesPerBlock;
            int nblocks    = (index + len) / valuesPerBlock - blockIndex;

            decoder.Decode(blocks, blockIndex, arr, off, nblocks);
            int diff = nblocks * valuesPerBlock;

            index += diff;
            len   -= diff;

            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.Get(index, arr, off, len));
            }
        }
Beispiel #3
0
        /*/// <param name="index"> the position of the value. </param>
         * /// <returns> the value at the given index. </returns>
         * public override long Get(int index)
         * {
         *  // The abstract index in a bit stream
         *  long majorBitPos = (long)index * bitsPerValue;
         *  // The index in the backing long-array
         *  int elementPos = (int)((long)((ulong)majorBitPos >> BLOCK_BITS));
         *  // The number of value-bits in the second long
         *  long endBits = (majorBitPos & MOD_MASK) + BpvMinusBlockSize;
         *
         *  if (endBits <= 0) // Single block
         *  {
         *      var mod = (long) ((ulong) (Blocks[elementPos]) >> (int) (-endBits)) & MaskRight;
         *      var og = ((long) ((ulong) Blocks[elementPos] >> (int) -endBits)) & MaskRight;
         *      Debug.Assert(mod == og);
         *
         *      //return (long)((ulong)(Blocks[elementPos]) >> (int)(-endBits)) & MaskRight;
         *      return ((long)((ulong)Blocks[elementPos] >> (int)-endBits)) & MaskRight;
         *  }
         *  // Two blocks
         *  var a = (((Blocks[elementPos] << (int)endBits) | (long)(((ulong)(Blocks[elementPos + 1])) >> (int)(BLOCK_SIZE - endBits))) & MaskRight);
         *  var b = ((Blocks[elementPos] << (int)endBits) | ((long)((ulong)Blocks[elementPos + 1] >> (int)(BLOCK_SIZE - endBits)))) & MaskRight;
         *
         *  Debug.Assert(a == b);
         *
         *  //return (((Blocks[elementPos] << (int)endBits) | (long)(((ulong)(Blocks[elementPos + 1])) >> (int)(BLOCK_SIZE - endBits))) & MaskRight);
         *  return ((Blocks[elementPos] << (int)endBits) | ((long)((ulong)Blocks[elementPos + 1] >> (int)(BLOCK_SIZE - endBits)))) & MaskRight;
         * }*/

        public override int Get(int index, long[] arr, int off, int len)
        {
            Debug.Assert(len > 0, "len must be > 0 (got " + len + ")");
            Debug.Assert(index >= 0 && index < m_valueCount);
            len = Math.Min(len, m_valueCount - index);
            Debug.Assert(off + len <= arr.Length);

            int originalIndex = index;

            PackedInt32s.IDecoder decoder = BulkOperation.Of(PackedInt32s.Format.PACKED, m_bitsPerValue);

            // go to the next block where the value does not span across two blocks
            int offsetInBlocks = index % decoder.Int64ValueCount;

            if (offsetInBlocks != 0)
            {
                for (int i = offsetInBlocks; i < decoder.Int64ValueCount && len > 0; ++i)
                {
                    arr[off++] = Get(index++);
                    --len;
                }
                if (len == 0)
                {
                    return(index - originalIndex);
                }
            }

            // bulk get
            Debug.Assert(index % decoder.Int64ValueCount == 0);
            int blockIndex = (int)((ulong)((long)index * m_bitsPerValue) >> BLOCK_BITS);

            Debug.Assert((((long)index * m_bitsPerValue) & MOD_MASK) == 0);
            int iterations = len / decoder.Int64ValueCount;

            decoder.Decode(blocks, blockIndex, arr, off, iterations);
            int gotValues = iterations * decoder.Int64ValueCount;

            index += gotValues;
            len   -= gotValues;
            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.Get(index, arr, off, len));
            }
        }
        private void Refill()
        {
            int  token        = @in.ReadByte() & 0xFF;
            bool minEquals0   = (token & AbstractBlockPackedWriter.MIN_VALUE_EQUALS_0) != 0;
            int  bitsPerValue = (int)((uint)token >> AbstractBlockPackedWriter.BPV_SHIFT);

            if (bitsPerValue > 64)
            {
                throw new IOException("Corrupted");
            }
            long minValue = minEquals0 ? 0L : ZigZagDecode(1L + ReadVInt64(@in));

            if (Debugging.AssertsEnabled)
            {
                Debugging.Assert(minEquals0 || minValue != 0);
            }

            if (bitsPerValue == 0)
            {
                Arrays.Fill(values, minValue);
            }
            else
            {
                PackedInt32s.IDecoder decoder = PackedInt32s.GetDecoder(PackedInt32s.Format.PACKED, packedIntsVersion, bitsPerValue);
                int iterations = blockSize / decoder.ByteValueCount;
                int blocksSize = iterations * decoder.ByteBlockCount;
                if (blocks == null || blocks.Length < blocksSize)
                {
                    blocks = new byte[blocksSize];
                }

                int valueCount  = (int)Math.Min(this.valueCount - ord, blockSize);
                int blocksCount = (int)PackedInt32s.Format.PACKED.ByteCount(packedIntsVersion, valueCount, bitsPerValue);
                @in.ReadBytes(blocks, 0, blocksCount);

                decoder.Decode(blocks, 0, values, 0, iterations);

                if (minValue != 0)
                {
                    for (int i = 0; i < valueCount; ++i)
                    {
                        values[i] += minValue;
                    }
                }
            }
            off = 0;
        }
Beispiel #5
0
        private static int LoadMaxDataSize() // LUCENENET: Avoid static constructors (see https://github.com/apache/lucenenet/pull/224#issuecomment-469284006)
        {
            int maxDataSize = 0;

            for (int version = PackedInt32s.VERSION_START; version <= PackedInt32s.VERSION_CURRENT; version++)
            {
                foreach (PackedInt32s.Format format in PackedInt32s.Format.Values /* Enum.GetValues(typeof(PackedInts.Format))*/)
                {
                    for (int bpv = 1; bpv <= 32; ++bpv)
                    {
                        if (!format.IsSupported(bpv))
                        {
                            continue;
                        }
                        PackedInt32s.IDecoder decoder = PackedInt32s.GetDecoder(format, version, bpv);
                        int iterations = ComputeIterations(decoder);
                        maxDataSize = Math.Max(maxDataSize, iterations * decoder.ByteValueCount);
                    }
                }
            }
            return(maxDataSize);
        }
Beispiel #6
0
        static ForUtil()
        {
            int maxDataSize = 0;

            for (int version = PackedInt32s.VERSION_START; version <= PackedInt32s.VERSION_CURRENT; version++)
            {
                foreach (PackedInt32s.Format format in PackedInt32s.Format.Values /* Enum.GetValues(typeof(PackedInts.Format))*/)
                {
                    for (int bpv = 1; bpv <= 32; ++bpv)
                    {
                        if (!format.IsSupported(bpv))
                        {
                            continue;
                        }
                        PackedInt32s.IDecoder decoder = PackedInt32s.GetDecoder(format, version, bpv);
                        int iterations = ComputeIterations(decoder);
                        maxDataSize = Math.Max(maxDataSize, iterations * decoder.ByteValueCount);
                    }
                }
            }
            MAX_DATA_SIZE = maxDataSize;
        }
Beispiel #7
0
 /// <summary>
 /// Compute the number of iterations required to decode <see cref="Lucene41PostingsFormat.BLOCK_SIZE"/>
 /// values with the provided <see cref="PackedInt32s.IDecoder"/>.
 /// </summary>
 private static int ComputeIterations(PackedInt32s.IDecoder decoder)
 {
     return((int)Math.Ceiling((float)Lucene41PostingsFormat.BLOCK_SIZE / decoder.ByteValueCount));
 }