package org.apache.lucene.codecs.lucene40;
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.IOException;
import java.util.Set;

import org.apache.lucene.codecs.NormsFormat;
import org.apache.lucene.codecs.PerDocConsumer;
import org.apache.lucene.codecs.PerDocProducer;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.DocValues.Type;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfos;
import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.PerDocWriteState;
import org.apache.lucene.index.SegmentInfo;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.store.Directory;

/**
 * Norms Format for the default codec. 
 * @lucene.experimental
 */
public class Lucene40NormsFormat extends NormsFormat {
  private final static String NORMS_SEGMENT_SUFFIX = "nrm";
  
  @Override
  public PerDocConsumer docsConsumer(PerDocWriteState state) throws IOException {
    return new Lucene40NormsDocValuesConsumer(state, NORMS_SEGMENT_SUFFIX);
  }

  @Override
  public PerDocProducer docsProducer(SegmentReadState state) throws IOException {
    return new Lucene40NormsDocValuesProducer(state, NORMS_SEGMENT_SUFFIX);
  }

  @Override
  public void files(Directory dir, SegmentInfo info, Set<String> files)
      throws IOException {
    Lucene40NormsDocValuesConsumer.files(dir, info, files);

  }

  @Override
  public PerDocProducer docsProducer(SegmentReadState state,
      Directory separateNormsDir) throws IOException {
    return docsProducer(state);
  }
  
 
  public static class Lucene40NormsDocValuesProducer extends Lucene40DocValuesProducer {

    public Lucene40NormsDocValuesProducer(SegmentReadState state,
        String segmentSuffix) throws IOException {
      super(state, segmentSuffix);
    }

    @Override
    protected boolean canLoad(FieldInfo info) {
      return !info.omitNorms && info.isIndexed;
    }

    @Override
    protected Type getDocValuesType(FieldInfo info) {
      return Type.BYTES_FIXED_STRAIGHT;
    }

    @Override
    protected boolean anyDocValuesFields(FieldInfos infos) {
      return infos.hasNorms();
    }
    
  }
  
  public static class Lucene40NormsDocValuesConsumer extends Lucene40DocValuesConsumer {

    public Lucene40NormsDocValuesConsumer(PerDocWriteState state,
        String segmentSuffix) throws IOException {
      super(state, segmentSuffix);
    }

    @Override
    protected DocValues getDocValuesForMerge(IndexReader reader, FieldInfo info)
        throws IOException {
      return reader.normValues(info.name);
    }

    @Override
    protected boolean canMerge(FieldInfo info) {
      return !info.omitNorms && info.isIndexed;
    }

    @Override
    protected Type getDocValuesType(FieldInfo info) {
      return Type.BYTES_FIXED_STRAIGHT;
    }
    
    public static void files(Directory dir, SegmentInfo segmentInfo, Set<String> files) throws IOException {
      FieldInfos fieldInfos = segmentInfo.getFieldInfos();
      for (FieldInfo fieldInfo : fieldInfos) {
        if (!fieldInfo.omitNorms && fieldInfo.isIndexed) {
          files.add(IndexFileNames.segmentFileName(segmentInfo.name, NORMS_SEGMENT_SUFFIX, IndexFileNames.COMPOUND_FILE_EXTENSION));
          files.add(IndexFileNames.segmentFileName(segmentInfo.name, NORMS_SEGMENT_SUFFIX, IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION));
          assert dir.fileExists(IndexFileNames.segmentFileName(segmentInfo.name, NORMS_SEGMENT_SUFFIX, IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION)); 
          assert dir.fileExists(IndexFileNames.segmentFileName(segmentInfo.name, NORMS_SEGMENT_SUFFIX, IndexFileNames.COMPOUND_FILE_EXTENSION)); 
          break;
        }
      }
    }
    
  }
  
 


}

@@ -3751,187 +3751,199 @@
     
-FieldInfos fieldInfos = segmentInfo.getFieldInfos();%0A      for (FieldInfo fieldInfo : fieldInfos) %7B%0A        if (!
+// see the comment in all the other codecs... its bogus that we do fileExists here, but its %0A      // a harder problem since 
 field
-Info.omitNorms && fieldInfo.isIndexed) %7B%0A          files.add(
+infos are never 'cleaned'%0A      final String normsFileName = 
 Inde
@@ -4044,33 +4044,32 @@
 E_EXTENSION)
-)
 ;%0A      
     files.ad
@@ -4060,175 +4060,89 @@
     
-    files.add(IndexFileNames.segmentFileName(segmentInfo.name, NORMS_SEGMENT_SUFFIX, IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION));%0A          assert dir.fileExists(
+if (dir.fileExists(normsFileName)) %7B%0A        final String normsEntriesFileName = 
 Inde
@@ -4247,38 +4247,34 @@
 TRIES_EXTENSION)
-); %0A  
+;%0A
         assert d
@@ -4291,121 +4291,64 @@
 sts(
-IndexFileNames.segmentFileName(segmentInfo.name, NORMS_SEGMENT_SUFFIX, IndexFileNames.COMPOUND_FILE_EXTENSION)
+normsEntriesFileName);%0A        files.add(normsFileName
 );
- 
 %0A   
@@ -4356,27 +4356,49 @@
     
-  break;%0A  
+files.add(normsEntriesFileName);%0A
       %7D%0A
     
@@ -4393,26 +4393,24 @@
       %7D%0A
-  
     %7D%0A  
   %7D%0A    
@@ -4405,26 +4405,9 @@
 %7D%0A  
-  %7D%0A    %0A  %7D%0A  %0A %0A
+%7D
 %0A%0A%7D%0A

fix TestAddIndexes fail