github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/codec/spi/codec.go (about) 1 package spi 2 3 import ( 4 "fmt" 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 // codecs/Codec.java 11 12 /* 13 Encodes/decodes an inverted index segment. 14 15 Note, when extending this class, the name is written into the index. 16 In order for the segment to be read, the name must resolve to your 17 implementation via forName(). This method use hard-coded map to 18 resolve codec names. 19 20 If you implement your own codec, make sure that it is included so 21 SPI can load it. 22 */ 23 type Codec interface { 24 // Returns this codec's name 25 Name() string 26 // Encodes/decodes postings 27 PostingsFormat() PostingsFormat 28 // Encodes/decodes docvalues 29 DocValuesFormat() DocValuesFormat 30 // Encodes/decodes stored fields 31 StoredFieldsFormat() StoredFieldsFormat 32 // Encodes/decodes term vectors 33 TermVectorsFormat() TermVectorsFormat 34 // Encodes/decodes field infos file 35 FieldInfosFormat() FieldInfosFormat 36 // Encodes/decodes segment info file 37 SegmentInfoFormat() SegmentInfoFormat 38 // Encodes/decodes document normalization values 39 NormsFormat() NormsFormat 40 // Encodes/decodes live docs 41 LiveDocsFormat() LiveDocsFormat 42 } 43 44 type CodecImpl struct { 45 name string 46 fieldsFormat StoredFieldsFormat 47 vectorsFormat TermVectorsFormat 48 fieldInfosFormat FieldInfosFormat 49 infosFormat SegmentInfoFormat 50 liveDocsFormat LiveDocsFormat 51 postingsFormat PostingsFormat 52 docValuesFormat DocValuesFormat 53 normsFormat NormsFormat 54 } 55 56 func NewCodec(name string, 57 fieldsFormat StoredFieldsFormat, 58 vectorsFormat TermVectorsFormat, 59 fieldInfosFormat FieldInfosFormat, 60 infosFormat SegmentInfoFormat, 61 liveDocsFormat LiveDocsFormat, 62 postingsFormat PostingsFormat, 63 docValuesFormat DocValuesFormat, 64 normsFormat NormsFormat) *CodecImpl { 65 return &CodecImpl{name, fieldsFormat, vectorsFormat, fieldInfosFormat, 66 infosFormat, liveDocsFormat, postingsFormat, docValuesFormat, normsFormat} 67 } 68 69 func (codec *CodecImpl) Name() string { 70 return codec.name 71 } 72 73 func (codec *CodecImpl) PostingsFormat() PostingsFormat { 74 return codec.postingsFormat 75 } 76 77 func (codec *CodecImpl) DocValuesFormat() DocValuesFormat { 78 return codec.docValuesFormat 79 } 80 81 func (codec *CodecImpl) StoredFieldsFormat() StoredFieldsFormat { 82 return codec.fieldsFormat 83 } 84 85 func (codec *CodecImpl) TermVectorsFormat() TermVectorsFormat { 86 return codec.vectorsFormat 87 } 88 89 func (codec *CodecImpl) FieldInfosFormat() FieldInfosFormat { 90 return codec.fieldInfosFormat 91 } 92 93 func (codec *CodecImpl) SegmentInfoFormat() SegmentInfoFormat { 94 return codec.infosFormat 95 } 96 97 func (codec *CodecImpl) NormsFormat() NormsFormat { 98 return codec.normsFormat 99 } 100 101 func (codec *CodecImpl) LiveDocsFormat() LiveDocsFormat { 102 return codec.liveDocsFormat 103 } 104 105 /* 106 returns the codec's name. Subclass can override to provide more 107 detail (such as parameters.) 108 */ 109 func (codec *CodecImpl) String() string { 110 return codec.name 111 } 112 113 var allCodecs = make(map[string]Codec) 114 115 // workaround Lucene Java's SPI mechanism 116 func RegisterCodec(codecs ...Codec) { 117 for _, codec := range codecs { 118 fmt.Printf("Found codec: %v\n", codec.Name()) 119 allCodecs[codec.Name()] = codec 120 } 121 } 122 123 // looks up a codec by name 124 func LoadCodec(name string) Codec { 125 c, ok := allCodecs[name] 126 if !ok { 127 fmt.Println("Unknown codec:", name) 128 fmt.Println("Available codecs:", allCodecs) 129 assert(ok) 130 } 131 return c 132 } 133 134 // returns a list of all available codec names 135 func AvailableCodecs() []string { 136 ans := make([]string, 0, len(allCodecs)) 137 for name, _ := range allCodecs { 138 ans = append(ans, name) 139 } 140 return ans 141 } 142 143 // Expert: returns the default codec used for newly created IndexWriterConfig(s). 144 var DefaultCodec = func() Codec { 145 ans := LoadCodec("Lucene410") 146 assert(ans != nil) 147 return ans 148 } 149 150 // codecs/StoredFieldsFormat.java 151 152 // Controls the format of stored fields 153 type StoredFieldsFormat interface { 154 // Returns a StoredFieldsReader to load stored fields. 155 FieldsReader(d store.Directory, si *SegmentInfo, fn FieldInfos, context store.IOContext) (r StoredFieldsReader, err error) 156 // Returns a StoredFieldsWriter to write stored fields. 157 FieldsWriter(d store.Directory, si *SegmentInfo, context store.IOContext) (w StoredFieldsWriter, err error) 158 } 159 160 // codecs/TermVectorsFormat.java 161 162 // Controls the format of term vectors 163 type TermVectorsFormat interface { 164 // Returns a TermVectorsReader to read term vectors. 165 VectorsReader(d store.Directory, si *SegmentInfo, fn FieldInfos, ctx store.IOContext) (r TermVectorsReader, err error) 166 // Returns a TermVectorsWriter to write term vectors. 167 VectorsWriter(d store.Directory, si *SegmentInfo, ctx store.IOContext) (w TermVectorsWriter, err error) 168 } 169 170 // codecs/FieldInfosFormat.java 171 172 // Encodes/decodes FieldInfos 173 type FieldInfosFormat interface { 174 // Returns a FieldInfosReader to read field infos from the index 175 FieldInfosReader() FieldInfosReader 176 // Returns a FieldInfosWriter to write field infos to the index 177 FieldInfosWriter() FieldInfosWriter 178 } 179 180 // codecs/FieldInfosReader.java 181 182 // Codec API for reading FieldInfos. 183 type FieldInfosReader func(d store.Directory, name, suffix string, ctx store.IOContext) (infos FieldInfos, err error) 184 185 // Codec API for writing FieldInfos. 186 type FieldInfosWriter func(d store.Directory, name, suffix string, infos FieldInfos, ctx store.IOContext) error 187 188 // codecs/NormsFormat.java 189 190 // Encodes/decodes per-document score normalization values. 191 type NormsFormat interface { 192 // Returns a DocValuesConsumer to write norms to the index. 193 NormsConsumer(state *SegmentWriteState) (w DocValuesConsumer, err error) 194 // Returns a DocValuesProducer to read norms from the index. 195 // 196 // NOTE: by the time this call returns, it must 197 // hold open any files it will need to use; else, those files may 198 // be deleted. Additionally, required fiels may be deleted during 199 // the execution of this call before there is a chance to open them. 200 // Under these circumstances an IO error should be returned by the 201 // implementation. IO errors are expected and will automatically 202 // cause a retry of the segment opening logic with the newly 203 // revised segments. 204 NormsProducer(state SegmentReadState) (r DocValuesProducer, err error) 205 } 206 207 // codecs/LiveDocsFormat.java 208 209 /* Format for live/deleted documents */ 210 type LiveDocsFormat interface { 211 // Creates a new MutableBits, with all bits set, for the specified size. 212 NewLiveDocs(size int) util.MutableBits 213 // Creates a new MutableBits of the same bits set and size of existing. 214 // NewLiveDocs(existing util.Bits) (util.MutableBits, error) 215 // Persist live docs bits. Use SegmentCommitInfo.nextDelGen() to 216 // determine the generation of the deletes file you should write to. 217 WriteLiveDocs(bits util.MutableBits, dir store.Directory, 218 info *SegmentCommitInfo, newDelCount int, ctx store.IOContext) error 219 // Records all files in use by this SegmentCommitInfo 220 Files(*SegmentCommitInfo) []string 221 }