예제 #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: void addNumericField(index.FieldInfo field, Iterable<Number> values, boolean optimizeStorage) throws java.io.IOException
        internal virtual void addNumericField(FieldInfo field, IEnumerable <Number> values, bool optimizeStorage)
        {
            meta.writeVInt(field.number);
            meta.writeByte(NUMBER);
            meta.writeLong(data.FilePointer);
            long minValue = long.MaxValue;
            long maxValue = long.MinValue;
            long gcd      = 0;
            bool missing  = false;
            // TODO: more efficient?
            HashSet <long?> uniqueValues = null;

            if (optimizeStorage)
            {
                uniqueValues = new HashSet <>();

                long count = 0;
                foreach (Number nv in values)
                {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long v;
                    long v;
                    if (nv == null)
                    {
                        v       = 0;
                        missing = true;
                    }
                    else
                    {
                        v = (long)nv;
                    }

                    if (gcd != 1)
                    {
                        if (v < long.MinValue / 2 || v > long.MaxValue / 2)
                        {
                            // in that case v - minValue might overflow and make the GCD computation return
                            // wrong results. Since these extreme values are unlikely, we just discard
                            // GCD computation for them
                            gcd = 1;
                        }   // minValue needs to be set first
                        else if (count != 0)
                        {
                            gcd = MathUtil.gcd(gcd, v - minValue);
                        }
                    }

                    minValue = Math.Min(minValue, v);
                    maxValue = Math.Max(maxValue, v);

                    if (uniqueValues != null)
                    {
                        if (uniqueValues.Add(v))
                        {
                            if (uniqueValues.Count > 256)
                            {
                                uniqueValues = null;
                            }
                        }
                    }

                    ++count;
                }
                Debug.Assert(count == maxDoc);
            }

            if (missing)
            {
                long start = data.FilePointer;
                writeMissingBitset(values);
                meta.writeLong(start);
                meta.writeLong(data.FilePointer - start);
            }
            else
            {
                meta.writeLong(-1L);
            }

            if (uniqueValues != null)
            {
                // small number of unique values
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int bitsPerValue = util.packed.PackedInts.bitsRequired(uniqueValues.size()-1);
                int           bitsPerValue  = PackedInts.bitsRequired(uniqueValues.Count - 1);
                FormatAndBits formatAndBits = PackedInts.fastestFormatAndBits(maxDoc, bitsPerValue, acceptableOverheadRatio);
                if (formatAndBits.bitsPerValue == 8 && minValue >= sbyte.MinValue && maxValue <= sbyte.MaxValue)
                {
                    meta.writeByte(UNCOMPRESSED);     // uncompressed
                    foreach (Number nv in values)
                    {
                        data.writeByte(nv == null ? 0 : (long)(sbyte)nv);
                    }
                }
                else
                {
                    meta.writeByte(TABLE_COMPRESSED);     // table-compressed
                    long?[] decode = uniqueValues.toArray(new long?[uniqueValues.Count]);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.HashMap<Long,Integer> encode = new java.util.HashMap<>();
                    Dictionary <long?, int?> encode = new Dictionary <long?, int?>();
                    data.writeVInt(decode.Length);
                    for (int i = 0; i < decode.Length; i++)
                    {
                        data.writeLong(decode[i]);
                        encode[decode[i]] = i;
                    }

                    meta.writeVInt(PackedInts.VERSION_CURRENT);
                    data.writeVInt(formatAndBits.format.Id);
                    data.writeVInt(formatAndBits.bitsPerValue);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final util.packed.PackedInts.Writer writer = util.packed.PackedInts.getWriterNoHeader(data, formatAndBits.format, maxDoc, formatAndBits.bitsPerValue, util.packed.PackedInts.DEFAULT_BUFFER_SIZE);
                    PackedInts.Writer writer = PackedInts.getWriterNoHeader(data, formatAndBits.format, maxDoc, formatAndBits.bitsPerValue, PackedInts.DEFAULT_BUFFER_SIZE);
                    foreach (Number nv in values)
                    {
                        writer.add(encode[nv == null ? 0 : (long)nv]);
                    }
                    writer.finish();
                }
            }
            else if (gcd != 0 && gcd != 1)
            {
                meta.writeByte(GCD_COMPRESSED);
                meta.writeVInt(PackedInts.VERSION_CURRENT);
                data.writeLong(minValue);
                data.writeLong(gcd);
                data.writeVInt(BLOCK_SIZE);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final util.packed.BlockPackedWriter writer = new util.packed.BlockPackedWriter(data, BLOCK_SIZE);
                BlockPackedWriter writer = new BlockPackedWriter(data, BLOCK_SIZE);
                foreach (Number nv in values)
                {
                    long value = nv == null ? 0 : (long)nv;
                    writer.add((value - minValue) / gcd);
                }
                writer.finish();
            }
            else
            {
                meta.writeByte(DELTA_COMPRESSED);   // delta-compressed

                meta.writeVInt(PackedInts.VERSION_CURRENT);
                data.writeVInt(BLOCK_SIZE);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final util.packed.BlockPackedWriter writer = new util.packed.BlockPackedWriter(data, BLOCK_SIZE);
                BlockPackedWriter writer = new BlockPackedWriter(data, BLOCK_SIZE);
                foreach (Number nv in values)
                {
                    writer.add(nv == null ? 0 : (long)nv);
                }
                writer.finish();
            }
        }