Abstract base class for performing write operations of Lucene's low-level data types.

{@code DataOutput} may only be used from one thread, because it is not thread safe (it keeps internal state like file position).

Example #1
0
 /// <summary>
 /// Writes a codec header, which records both a string to
 /// identify the file and a version number. this header can
 /// be parsed and validated with
 /// <seealso cref="#checkHeader(DataInput, String, int, int) checkHeader()"/>.
 /// <p>
 /// CodecHeader --&gt; Magic,CodecName,Version
 /// <ul>
 ///    <li>Magic --&gt; <seealso cref="DataOutput#writeInt Uint32"/>. this
 ///        identifies the start of the header. It is always {@value #CODEC_MAGIC}.
 ///    <li>CodecName --&gt; <seealso cref="DataOutput#writeString String"/>. this
 ///        is a string to identify this file.
 ///    <li>Version --&gt; <seealso cref="DataOutput#writeInt Uint32"/>. Records
 ///        the version of the file.
 /// </ul>
 /// <p>
 /// Note that the length of a codec header depends only upon the
 /// name of the codec, so this length can be computed at any time
 /// with <seealso cref="#headerLength(String)"/>.
 /// </summary>
 /// <param name="out"> Output stream </param>
 /// <param name="codec"> String to identify this file. It should be simple ASCII,
 ///              less than 128 characters in length. </param>
 /// <param name="version"> Version number </param>
 /// <exception cref="IOException"> If there is an I/O error writing to the underlying medium. </exception>
 public static void WriteHeader(DataOutput @out, string codec, int version)
 {
     BytesRef bytes = new BytesRef(codec);
     if (bytes.Length != codec.Length || bytes.Length >= 128)
     {
         throw new System.ArgumentException("codec must be simple ASCII, less than 128 characters in length [got " + codec + "]");
     }
     @out.WriteInt(CODEC_MAGIC);
     @out.WriteString(codec);
     @out.WriteInt(version);
 }
 public override void Write(DataOutput indexOut, bool absolute)
 {
     if (absolute)
     {
         indexOut.WriteVLong(fp);
     }
     else
     {
         indexOut.WriteVLong(fp - lastFP);
     }
     lastFP = fp;
 }
Example #3
0
        /// <summary>
        /// Copy the current contents of this buffer to the named output. </summary>
        public virtual void WriteTo(DataOutput @out)
        {
            Flush();
            long end    = file.length;
            long pos    = 0;
            int  buffer = 0;

            while (pos < end)
            {
                int  length  = BUFFER_SIZE;
                long nextPos = pos + length;
                if (nextPos > end) // at the last buffer
                {
                    length = (int)(end - pos);
                }
                @out.WriteBytes(file.GetBuffer(buffer++), length);
                pos = nextPos;
            }
        }
Example #4
0
        /// <summary>
        /// Create a new <seealso cref="ForUtil"/> instance and save state into <code>out</code>.
        /// </summary>
        public ForUtil(float acceptableOverheadRatio, DataOutput @out)
        {
            @out.WriteVInt(PackedInts.VERSION_CURRENT);
            EncodedSizes = new int[33];
            Encoders = new PackedInts.Encoder[33];
            Decoders = new PackedInts.Decoder[33];
            Iterations = new int[33];

            for (int bpv = 1; bpv <= 32; ++bpv)
            {
                PackedInts.FormatAndBits formatAndBits = PackedInts.FastestFormatAndBits(Lucene41PostingsFormat.BLOCK_SIZE, bpv, acceptableOverheadRatio);
                Debug.Assert(formatAndBits.format.IsSupported(formatAndBits.bitsPerValue));
                Debug.Assert(formatAndBits.bitsPerValue <= 32);
                EncodedSizes[bpv] = EncodedSize(formatAndBits.format, PackedInts.VERSION_CURRENT, formatAndBits.bitsPerValue);
                Encoders[bpv] = PackedInts.GetEncoder(formatAndBits.format, PackedInts.VERSION_CURRENT, formatAndBits.bitsPerValue);
                Decoders[bpv] = PackedInts.GetDecoder(formatAndBits.format, PackedInts.VERSION_CURRENT, formatAndBits.bitsPerValue);
                Iterations[bpv] = ComputeIterations(Decoders[bpv]);

                @out.WriteVInt(formatAndBits.format.id << 5 | (formatAndBits.bitsPerValue - 1));
            }
        }
Example #5
0
 /// <summary>
 /// Constructs a ByteSequencesWriter to the provided DataOutput </summary>
 public ByteSequencesWriter(DataOutput os)
 {
     this.Os = os;
 }
 public override bool Store(DataOutput output)
 {
     lock (this)
     {
         output.WriteVLong(count);
         if (this.normalCompletion == null || normalCompletion.FST == null)
         {
             return false;
         }
         normalCompletion.FST.Save(output);
         return true;
     }
 }
 public override void Write(DataOutput indexOut, bool absolute)
 {
     if (absolute)
     {
         indexOut.WriteVInt(upto);
         indexOut.WriteVLong(fp);
     }
     else if (fp == lastFP)
     {
         // same block
         Debug.Assert(upto >= lastUpto);
         int uptoDelta = upto - lastUpto;
         indexOut.WriteVInt(uptoDelta << 1 | 1);
     }
     else
     {
         // new block
         indexOut.WriteVInt(upto << 1);
         indexOut.WriteVLong(fp - lastFP);
     }
     lastUpto = upto;
     lastFP = fp;
 }
 private static void SaveInts(int[] values, int length, DataOutput @out)
 {
     Debug.Assert(length > 0);
     if (length == 1)
     {
         @out.WriteVInt(values[0]);
     }
     else
     {
         bool allEqual = true;
         for (int i = 1; i < length; ++i)
         {
             if (values[i] != values[0])
             {
                 allEqual = false;
                 break;
             }
         }
         if (allEqual)
         {
             @out.WriteVInt(0);
             @out.WriteVInt(values[0]);
         }
         else
         {
             long max = 0;
             for (int i = 0; i < length; ++i)
             {
                 max |= (uint)values[i];
             }
             int bitsRequired = PackedInts.BitsRequired(max);
             @out.WriteVInt(bitsRequired);
             PackedInts.Writer w = PackedInts.GetWriterNoHeader(@out, PackedInts.Format.PACKED, length, bitsRequired, 1);
             for (int i = 0; i < length; ++i)
             {
                 w.Add(values[i]);
             }
             w.Finish();
         }
     }
 }
Example #9
0
 /// <summary>
 /// Copy the current contents of this buffer to the named output. </summary>
 public virtual void WriteTo(DataOutput @out)
 {
     Flush();
     long end = File.Length_Renamed;
     long pos = 0;
     int buffer = 0;
     while (pos < end)
     {
         int length = BUFFER_SIZE;
         long nextPos = pos + length;
         if (nextPos > end) // at the last buffer
         {
             length = (int)(end - pos);
         }
         @out.WriteBytes(File.GetBuffer(buffer++), length);
         pos = nextPos;
     }
 }
Example #10
0
 // pre-order traversal
 private void WriteRecursively(DataOutput @out, TernaryTreeNode node)
 {
     // write out the current node
     @out.WriteString(new string(new char[] { node.splitchar }, 0, 1));
     // prepare a mask of kids
     sbyte mask = 0;
     if (node.eqKid != null)
     {
         mask |= EQ_KID;
     }
     if (node.loKid != null)
     {
         mask |= LO_KID;
     }
     if (node.hiKid != null)
     {
         mask |= HI_KID;
     }
     if (node.token != null)
     {
         mask |= HAS_TOKEN;
     }
     if (node.val != null)
     {
         mask |= HAS_VALUE;
     }
     @out.WriteByte((byte)mask);
     if (node.token != null)
     {
         @out.WriteString(node.token);
     }
     if (node.val != null)
     {
         @out.WriteLong((long)node.val);
     }
     // recurse and write kids
     if (node.loKid != null)
     {
         WriteRecursively(@out, node.loKid);
     }
     if (node.eqKid != null)
     {
         WriteRecursively(@out, node.eqKid);
     }
     if (node.hiKid != null)
     {
         WriteRecursively(@out, node.hiKid);
     }
 }
Example #11
0
 public override bool Store(DataOutput output)
 {
     lock (this)
     {
         output.WriteVLong(count);
         WriteRecursively(output, root);
         return true;
     }
 }
 private void WriteRecursively(DataOutput @out, JaspellTernarySearchTrie.TSTNode node)
 {
     if (node == null)
     {
         return;
     }
     @out.WriteString(new string(new char[] { node.splitchar }, 0, 1));
     sbyte mask = 0;
     if (node.relatives[JaspellTernarySearchTrie.TSTNode.LOKID] != null)
     {
         mask |= LO_KID;
     }
     if (node.relatives[JaspellTernarySearchTrie.TSTNode.EQKID] != null)
     {
         mask |= EQ_KID;
     }
     if (node.relatives[JaspellTernarySearchTrie.TSTNode.HIKID] != null)
     {
         mask |= HI_KID;
     }
     if (node.data != null)
     {
         mask |= HAS_VALUE;
     }
     @out.WriteByte((byte)mask);
     if (node.data != null)
     {
         @out.WriteLong((long)(node.data));
     }
     WriteRecursively(@out, node.relatives[JaspellTernarySearchTrie.TSTNode.LOKID]);
     WriteRecursively(@out, node.relatives[JaspellTernarySearchTrie.TSTNode.EQKID]);
     WriteRecursively(@out, node.relatives[JaspellTernarySearchTrie.TSTNode.HIKID]);
 }
 public override bool Store(DataOutput output)
 {
     output.WriteVLong(count);
     JaspellTernarySearchTrie.TSTNode root = trie.Root;
     if (root == null) // empty tree
     {
         return false;
     }
     WriteRecursively(output, root);
     return true;
 }
 public override bool Store(DataOutput @in)
 {
     return false;
 }
 public override void EncodeTerm(long[] empty, DataOutput output, FieldInfo fieldInfo, BlockTermState _state,
     bool absolute)
 {
     PulsingTermState state = (PulsingTermState) _state;
     Debug.Debug.Assert((empty.Length == 0);
     this.absolute = this.absolute || absolute;
     if (state.bytes == null)
     {
         _wrappedPostingsWriter.EncodeTerm(longs, buffer, fieldInfo, state.wrappedState, this.absolute);
         for (int i = 0; i < longsSize; i++)
         {
             output.WriteVLong(longs[i]);
         }
         buffer.WriteTo(output);
         buffer.Reset();
         this.absolute = false;
     }
     else
     {
         output.WriteVInt(state.bytes.Length);
         output.WriteBytes(state.bytes, 0, state.bytes.Length);
         this.absolute = this.absolute || absolute;
     }
 }