fieldsIt.remove();
}
}
final String normsFileName = IndexFileNames.segmentFileName(state.segmentName, IndexFileNames.NORMS_EXTENSION);
IndexOutput normsOut = state.directory.createOutput(normsFileName);
boolean success = false;
try {
normsOut.writeBytes(SegmentNorms.NORMS_HEADER, 0, SegmentNorms.NORMS_HEADER.length);
final int numField = fieldInfos.size();
int normCount = 0;
for(int fieldNumber=0;fieldNumber<numField;fieldNumber++) {
final FieldInfo fieldInfo = fieldInfos.fieldInfo(fieldNumber);
List<NormsWriterPerField> toMerge = byField.get(fieldInfo);
int upto = 0;
if (toMerge != null) {
final int numFields = toMerge.size();
normCount++;
final NormsWriterPerField[] fields = new NormsWriterPerField[numFields];
int[] uptos = new int[numFields];
for(int j=0;j<numFields;j++)
fields[j] = toMerge.get(j);
int numLeft = numFields;
while(numLeft > 0) {
assert uptos[0] < fields[0].docIDs.length : " uptos[0]=" + uptos[0] + " len=" + (fields[0].docIDs.length);
int minLoc = 0;
int minDocID = fields[0].docIDs[uptos[0]];
for(int j=1;j<numLeft;j++) {
final int docID = fields[j].docIDs[uptos[j]];
if (docID < minDocID) {
minDocID = docID;
minLoc = j;
}
}
assert minDocID < state.numDocs;
// Fill hole
for(;upto<minDocID;upto++)
normsOut.writeByte(defaultNorm);
normsOut.writeByte(fields[minLoc].norms[uptos[minLoc]]);
(uptos[minLoc])++;
upto++;
if (uptos[minLoc] == fields[minLoc].upto) {
fields[minLoc].reset();
if (minLoc != numLeft-1) {
fields[minLoc] = fields[numLeft-1];
uptos[minLoc] = uptos[numLeft-1];
}
numLeft--;
}
}
// Fill final hole with defaultNorm
for(;upto<state.numDocs;upto++)
normsOut.writeByte(defaultNorm);
} else if (fieldInfo.isIndexed && !fieldInfo.omitNorms) {
normCount++;
// Fill entire field with default norm:
for(;upto<state.numDocs;upto++)
normsOut.writeByte(defaultNorm);
}
assert 4+normCount*state.numDocs == normsOut.getFilePointer() : ".nrm file size mismatch: expected=" + (4+normCount*state.numDocs) + " actual=" + normsOut.getFilePointer();
}
success = true;
} finally {
IOUtils.closeSafely(!success, normsOut);
}