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

     1  package lucene46
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/balzaczyy/golucene/core/codec"
     7  	. "github.com/balzaczyy/golucene/core/codec/spi"
     8  	. "github.com/balzaczyy/golucene/core/index/model"
     9  	"github.com/balzaczyy/golucene/core/store"
    10  	"github.com/balzaczyy/golucene/core/util"
    11  )
    12  
    13  // lucene46/Lucene46SegmentInfoFormat.java
    14  
    15  const (
    16  	// File extension used to store SegmentInfo.
    17  	SI_EXTENSION        = "si"
    18  	SI_CODEC_NAME       = "Lucene46SegmentInfo"
    19  	SI_VERSION_START    = 0
    20  	SI_VERSION_CHECKSUM = 1
    21  	SI_VERSION_CURRENT  = SI_VERSION_CHECKSUM
    22  )
    23  
    24  type Lucene46SegmentInfoFormat struct {
    25  }
    26  
    27  func NewLucene46SegmentInfoFormat() *Lucene46SegmentInfoFormat {
    28  	return &Lucene46SegmentInfoFormat{}
    29  }
    30  
    31  func (f *Lucene46SegmentInfoFormat) SegmentInfoReader() SegmentInfoReader {
    32  	return f
    33  }
    34  
    35  func (f *Lucene46SegmentInfoFormat) SegmentInfoWriter() SegmentInfoWriter {
    36  	return f
    37  }
    38  
    39  // codecs/lucene46/Lucene46SegmentInfoReader.java
    40  
    41  func (f *Lucene46SegmentInfoFormat) Read(dir store.Directory,
    42  	segName string, ctx store.IOContext) (si *SegmentInfo, err error) {
    43  
    44  	filename := util.SegmentFileName(segName, "", SI_EXTENSION)
    45  	var input store.ChecksumIndexInput
    46  	if input, err = dir.OpenChecksumInput(filename, ctx); err != nil {
    47  		return
    48  	}
    49  
    50  	var success = false
    51  	defer func() {
    52  		if !success {
    53  			util.CloseWhileSuppressingError(input)
    54  		} else {
    55  			err = input.Close()
    56  		}
    57  	}()
    58  
    59  	var codecVersion int
    60  	if codecVersion, err = asInt(codec.CheckHeader(input, SI_CODEC_NAME, SI_VERSION_START, SI_VERSION_CURRENT)); err != nil {
    61  		return
    62  	}
    63  	var versionStr string
    64  	if versionStr, err = input.ReadString(); err != nil {
    65  		return
    66  	}
    67  	var version util.Version
    68  	if version, err = util.ParseVersion(versionStr); err != nil {
    69  		err = errors.New(fmt.Sprintf("unable to parse version string (resource=%v): %v", input, err))
    70  		return
    71  	}
    72  	var docCount int
    73  	if docCount, err = asInt(input.ReadInt()); err != nil {
    74  		return
    75  	} else if docCount < 0 {
    76  		return nil, errors.New(fmt.Sprintf("invalid docCount: %v (resource=%v)", docCount, input))
    77  	}
    78  	var b byte
    79  	if b, err = input.ReadByte(); err != nil {
    80  		return
    81  	}
    82  	var isCompoundFile = b == YES
    83  	var diagnostics map[string]string
    84  	if diagnostics, err = input.ReadStringStringMap(); err != nil {
    85  		return
    86  	}
    87  	var files map[string]bool
    88  	if files, err = input.ReadStringSet(); err != nil {
    89  		return
    90  	}
    91  
    92  	if codecVersion >= SI_VERSION_CHECKSUM {
    93  		_, err = codec.CheckFooter(input)
    94  	} else {
    95  		err = codec.CheckEOF(input)
    96  	}
    97  	if err != nil {
    98  		return
    99  	}
   100  
   101  	si = NewSegmentInfo(dir, version, segName, docCount, isCompoundFile, nil, diagnostics)
   102  	si.SetFiles(files)
   103  
   104  	success = true
   105  	return si, nil
   106  }
   107  
   108  func asInt(n int32, err error) (int, error) {
   109  	if err != nil {
   110  		return 0, err
   111  	}
   112  	return int(n), nil
   113  }
   114  
   115  func (f *Lucene46SegmentInfoFormat) Write(dir store.Directory, si *SegmentInfo, fis FieldInfos, ctx store.IOContext) error {
   116  	filename := util.SegmentFileName(si.Name, "", SI_EXTENSION)
   117  	si.AddFile(filename)
   118  
   119  	output, err := dir.CreateOutput(filename, ctx)
   120  	if err != nil {
   121  		return err
   122  	}
   123  
   124  	var success = false
   125  	defer func() {
   126  		if !success {
   127  			util.CloseWhileSuppressingError(output)
   128  			si.Dir.DeleteFile(filename) // ignore error
   129  		} else {
   130  			output.Close()
   131  		}
   132  	}()
   133  
   134  	if err = codec.WriteHeader(output, SI_CODEC_NAME, SI_VERSION_CURRENT); err == nil {
   135  		version := si.Version()
   136  		assert2(version[0] == 3 || version[0] == 4,
   137  			"invalid major version: should be 3 or 4 but got: %v", version[0])
   138  		// write the Lucene version that created this segment, since 3.1
   139  		if err = output.WriteString(version.String()); err == nil {
   140  			if err = output.WriteInt(int32(si.DocCount())); err == nil {
   141  
   142  				flag := NO
   143  				if si.IsCompoundFile() {
   144  					flag = YES
   145  				}
   146  				if err = output.WriteByte(byte(flag)); err == nil {
   147  					if err = output.WriteStringStringMap(si.Diagnostics()); err == nil {
   148  						if err = output.WriteStringSet(si.Files()); err == nil {
   149  							if err = codec.WriteFooter(output); err == nil {
   150  								success = true
   151  							}
   152  						}
   153  					}
   154  				}
   155  			}
   156  		}
   157  	}
   158  	if err != nil {
   159  		return err
   160  	}
   161  	success = true
   162  	return nil
   163  }