code: func (w *Writer) ensureStage(s indexWriterStage) error {
  	if w.stage == s {
  		return nil
  	}
  	if w.stage > s {
  		return errors.Errorf("invalid stage %q, currently at %q", s, w.stage)
  	}
  
  	// Mark start of sections in table of contents.
  	switch s {
  	case idxStageSymbols:
  		w.toc.Symbols = w.pos
  	case idxStageSeries:
  		w.toc.Series = w.pos
  
  	case idxStageLabelIndex:
  		w.toc.LabelIndices = w.pos
  
  	case idxStagePostings:
  		w.toc.Postings = w.pos
  
  	case idxStageDone:
  		w.toc.LabelIndicesTable = w.pos
  		if err := w.writeOffsetTable(w.labelIndexes); err != nil {
  			return err
  		}
  		w.toc.PostingsTable = w.pos
  		if err := w.writeOffsetTable(w.postings); err != nil {
  			return err
  		}
  		if err := w.writeTOC(); err != nil {
  			return err
  		}
  	}
  
  	w.stage = s
  	return nil
  }
docs: // ensureStage handles transitions between write stages and ensures that IndexWriter
  // methods are called in an order valid for the implementation.