/** Produce _X.nrm if any document had a field with norms not disabled */
  @Override
  public void flush(
      Map<String, InvertedDocEndConsumerPerField> fieldsToFlush, SegmentWriteState state)
      throws IOException {
    boolean success = false;
    boolean anythingFlushed = false;
    try {
      if (state.fieldInfos.hasNorms()) {
        for (FieldInfo fi : state.fieldInfos) {
          final NormsConsumerPerField toWrite = (NormsConsumerPerField) fieldsToFlush.get(fi.name);
          // we must check the final value of omitNorms for the fieldinfo, it could have
          // changed for this field since the first time we added it.
          if (!fi.omitsNorms()) {
            if (toWrite != null && toWrite.initialized()) {
              anythingFlushed = true;
              final Type type = toWrite.flush(state.segmentInfo.getDocCount());
              assert fi.getNormType() == type;
            } else if (fi.isIndexed()) {
              anythingFlushed = true;
              assert fi.getNormType() == null : "got " + fi.getNormType() + "; field=" + fi.name;
            }
          }
        }
      }

      success = true;
      if (!anythingFlushed && consumer != null) {
        consumer.abort();
      }
    } finally {
      if (success) {
        IOUtils.close(consumer);
      } else {
        IOUtils.closeWhileHandlingException(consumer);
      }
    }
  }
 @Override
 public void abort() {
   if (consumer != null) {
     consumer.abort();
   }
 }