public PreFlexRWTermVectorsWriter(Directory directory, string segment, IOContext context)
        {
            this.Directory = directory;
            this.Segment   = segment;
            bool success = false;

            try
            {
                // Open files for TermVector storage
                Tvx = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", Lucene3xTermVectorsReader.VECTORS_INDEX_EXTENSION), context);
                Tvx.WriteInt(Lucene3xTermVectorsReader.FORMAT_CURRENT);
                Tvd = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", Lucene3xTermVectorsReader.VECTORS_DOCUMENTS_EXTENSION), context);
                Tvd.WriteInt(Lucene3xTermVectorsReader.FORMAT_CURRENT);
                Tvf = directory.CreateOutput(IndexFileNames.SegmentFileName(segment, "", Lucene3xTermVectorsReader.VECTORS_FIELDS_EXTENSION), context);
                Tvf.WriteInt(Lucene3xTermVectorsReader.FORMAT_CURRENT);
                success = true;
            }
            finally
            {
                if (!success)
                {
                    Abort();
                }
            }
        }
        public override void StartField(FieldInfo info, int numTerms, bool positions, bool offsets, bool payloads)
        {
            Debug.Assert(LastFieldName == null || info.Name.CompareTo(LastFieldName) > 0, "fieldName=" + info.Name + " lastFieldName=" + LastFieldName);
            LastFieldName     = info.Name;
            this.Positions    = positions;
            this.Offsets      = offsets;
            this.Payloads     = payloads;
            LastTerm.Length   = 0;
            LastPayloadLength = -1; // force first payload to write its length
            Fps[FieldCount++] = Tvf.FilePointer;
            Tvd.WriteVInt(info.Number);
            Tvf.WriteVInt(numTerms);
            sbyte bits = 0x0;

            if (positions)
            {
                bits |= Lucene40TermVectorsReader.STORE_POSITIONS_WITH_TERMVECTOR;
            }
            if (offsets)
            {
                bits |= Lucene40TermVectorsReader.STORE_OFFSET_WITH_TERMVECTOR;
            }
            if (payloads)
            {
                bits |= Lucene40TermVectorsReader.STORE_PAYLOAD_WITH_TERMVECTOR;
            }
            Tvf.WriteByte(bits);
        }
 public override void FinishDocument()
 {
     Debug.Assert(FieldCount == NumVectorFields);
     for (int i = 1; i < FieldCount; i++)
     {
         Tvd.WriteVLong(Fps[i] - Fps[i - 1]);
     }
 }
 public override void StartDocument(int numVectorFields)
 {
     LastFieldName        = null;
     this.NumVectorFields = numVectorFields;
     Tvx.WriteLong(Tvd.FilePointer);
     Tvx.WriteLong(Tvf.FilePointer);
     Tvd.WriteVInt(numVectorFields);
     FieldCount = 0;
     Fps        = ArrayUtil.Grow(Fps, numVectorFields);
 }
        /// <summary>
        /// Do a bulk copy of numDocs documents from reader to our
        /// streams.  this is used to expedite merging, if the
        /// field numbers are congruent.
        /// </summary>
        private void AddRawDocuments(Lucene40TermVectorsReader reader, int[] tvdLengths, int[] tvfLengths, int numDocs)
        {
            long tvdPosition = Tvd.FilePointer;
            long tvfPosition = Tvf.FilePointer;
            long tvdStart    = tvdPosition;
            long tvfStart    = tvfPosition;

            for (int i = 0; i < numDocs; i++)
            {
                Tvx.WriteLong(tvdPosition);
                tvdPosition += tvdLengths[i];
                Tvx.WriteLong(tvfPosition);
                tvfPosition += tvfLengths[i];
            }
            Tvd.CopyBytes(reader.TvdStream, tvdPosition - tvdStart);
            Tvf.CopyBytes(reader.TvfStream, tvfPosition - tvfStart);
            Debug.Assert(Tvd.FilePointer == tvdPosition);
            Debug.Assert(Tvf.FilePointer == tvfPosition);
        }
        public override void StartField(FieldInfo info, int numTerms, bool positions, bool offsets, bool payloads)
        {
            Debug.Assert(LastFieldName == null || info.Name.CompareTo(LastFieldName) > 0, "fieldName=" + info.Name + " lastFieldName=" + LastFieldName);
            LastFieldName = info.Name;
            if (payloads)
            {
                throw new System.NotSupportedException("3.x codec does not support payloads on vectors!");
            }
            this.Positions    = positions;
            this.Offsets      = offsets;
            LastTerm.Length   = 0;
            Fps[FieldCount++] = Tvf.FilePointer;
            Tvd.WriteVInt(info.Number);
            Tvf.WriteVInt(numTerms);
            sbyte bits = 0x0;

            if (positions)
            {
                bits |= Lucene3xTermVectorsReader.STORE_POSITIONS_WITH_TERMVECTOR;
            }
            if (offsets)
            {
                bits |= Lucene3xTermVectorsReader.STORE_OFFSET_WITH_TERMVECTOR;
            }
            Tvf.WriteByte(bits);

            Debug.Assert(FieldCount <= NumVectorFields);
            if (FieldCount == NumVectorFields)
            {
                // last field of the document
                // this is crazy because the file format is crazy!
                for (int i = 1; i < FieldCount; i++)
                {
                    Tvd.WriteVLong(Fps[i] - Fps[i - 1]);
                }
            }
        }