github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/codec/lucene41/postingsFormat.go (about) 1 package lucene41 2 3 import ( 4 "fmt" 5 "github.com/balzaczyy/golucene/core/codec/blocktree" 6 . "github.com/balzaczyy/golucene/core/codec/spi" 7 . "github.com/balzaczyy/golucene/core/index/model" 8 "github.com/balzaczyy/golucene/core/util" 9 ) 10 11 func init() { 12 RegisterPostingsFormat(NewLucene41PostingsFormat()) 13 } 14 15 // codecs/lucene41/Lucene41PostingsFormat.java 16 17 const ( 18 LUCENE41_DOC_EXTENSION = "doc" 19 LUCENE41_POS_EXTENSION = "pos" 20 LUCENE41_PAY_EXTENSION = "pay" 21 22 LUCENE41_BLOCK_SIZE = 128 23 ) 24 25 type Lucene41PostingsFormat struct { 26 minTermBlockSize int 27 maxTermBlockSize int 28 } 29 30 /* Creates Lucene41PostingsFormat wit hdefault settings. */ 31 func NewLucene41PostingsFormat() *Lucene41PostingsFormat { 32 return NewLucene41PostingsFormatWith(blocktree.DEFAULT_MIN_BLOCK_SIZE, blocktree.DEFAULT_MAX_BLOCK_SIZE) 33 } 34 35 /* 36 Creates Lucene41PostingsFormat with custom values for minBlockSize 37 and maxBlockSize passed to block terms directory. 38 */ 39 func NewLucene41PostingsFormatWith(minTermBlockSize, maxTermBlockSize int) *Lucene41PostingsFormat { 40 assert(minTermBlockSize > 1) 41 assert(minTermBlockSize <= maxTermBlockSize) 42 return &Lucene41PostingsFormat{ 43 minTermBlockSize: minTermBlockSize, 44 maxTermBlockSize: maxTermBlockSize, 45 } 46 } 47 48 func (f *Lucene41PostingsFormat) Name() string { 49 return "Lucene41" 50 } 51 52 func (f *Lucene41PostingsFormat) String() { 53 panic("not implemented yet") 54 } 55 56 func (f *Lucene41PostingsFormat) FieldsConsumer(state *SegmentWriteState) (FieldsConsumer, error) { 57 postingsWriter, err := newLucene41PostingsWriterCompact(state) 58 if err != nil { 59 return nil, err 60 } 61 var success = false 62 defer func() { 63 if !success { 64 util.CloseWhileSuppressingError(postingsWriter) 65 } 66 }() 67 ret, err := blocktree.NewBlockTreeTermsWriter(state, postingsWriter, f.minTermBlockSize, f.maxTermBlockSize) 68 if err != nil { 69 return nil, err 70 } 71 success = true 72 return ret, nil 73 } 74 75 func (f *Lucene41PostingsFormat) FieldsProducer(state SegmentReadState) (FieldsProducer, error) { 76 postingsReader, err := NewLucene41PostingsReader(state.Dir, 77 state.FieldInfos, 78 state.SegmentInfo, 79 state.Context, 80 state.SegmentSuffix) 81 if err != nil { 82 return nil, err 83 } 84 success := false 85 defer func() { 86 if !success { 87 fmt.Printf("Failed to load FieldsProducer for %v.\n", f.Name()) 88 util.CloseWhileSuppressingError(postingsReader) 89 } 90 }() 91 92 fp, err := blocktree.NewBlockTreeTermsReader(state.Dir, 93 state.FieldInfos, 94 state.SegmentInfo, 95 postingsReader, 96 state.Context, 97 state.SegmentSuffix, 98 state.TermsIndexDivisor) 99 if err != nil { 100 return fp, err 101 } 102 success = true 103 return fp, nil 104 }