github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/codec/lucene40/segmentInfoWriter.go (about)

     1  package lucene40
     2  
     3  import (
     4  	"github.com/balzaczyy/golucene/core/codec"
     5  	. "github.com/balzaczyy/golucene/core/index/model"
     6  	"github.com/balzaczyy/golucene/core/store"
     7  	"github.com/balzaczyy/golucene/core/util"
     8  )
     9  
    10  const (
    11  	SEGMENT_INFO_NO  = -1
    12  	SEGMENT_INFO_YES = 1
    13  )
    14  
    15  // lucene40/Lucne40SegmentInfoWriter.java
    16  
    17  // Lucene 4.0 implementation of SegmentInfoWriter
    18  
    19  type Lucene40SegmentInfoWriter struct{}
    20  
    21  func (w *Lucene40SegmentInfoWriter) Write(dir store.Directory,
    22  	si *SegmentInfo, fis FieldInfos, ctx store.IOContext) (err error) {
    23  
    24  	filename := util.SegmentFileName(si.Name, "", LUCENE40_SI_EXTENSION)
    25  	si.AddFile(filename)
    26  
    27  	var output store.IndexOutput
    28  	output, err = dir.CreateOutput(filename, ctx)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	var success = false
    34  	defer func() {
    35  		if !success {
    36  			util.CloseWhileSuppressingError(output)
    37  			si.Dir.DeleteFile(filename) // ignore error
    38  		} else {
    39  			err = mergeError(err, output.Close())
    40  		}
    41  	}()
    42  
    43  	err = codec.WriteHeader(output, LUCENE40_CODEC_NAME, LUCENE40_VERSION_CURRENT)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	// Write the Lucene version that created this segment, since 3.1
    48  	err = store.Stream(output).WriteString(si.Version().String()).
    49  		WriteInt(int32(si.DocCount())).
    50  		WriteByte(func() byte {
    51  		if si.IsCompoundFile() {
    52  			return SEGMENT_INFO_YES
    53  		}
    54  		return byte((SEGMENT_INFO_NO + 256) % 256) // Go byte is non-negative, unlike Java
    55  	}()).WriteStringStringMap(si.Diagnostics()).
    56  		WriteStringStringMap(map[string]string{}).
    57  		WriteStringSet(si.Files()).Close()
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	success = true
    63  	return nil
    64  }