github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/codec/spi/postings.go (about) 1 package spi 2 3 import ( 4 "fmt" 5 . "github.com/balzaczyy/golucene/core/index/model" 6 "io" 7 ) 8 9 // codecs/PostingsFormat.java 10 11 /* 12 Encodes/decodes terms, postings, and proximity data. 13 14 Note, when extending this class, the name Name() may be written into 15 the index in certain configurations. In order for the segment to be 16 read, the name must resolve to your implemetation via LoadPostingsFormat(). 17 Since Go doesn't have Java's SPI locate mechanism, this method use 18 manual mappings to resolve format names. 19 20 If you implement your own format, make sure that it is manually 21 included. 22 */ 23 type PostingsFormat interface { 24 // Returns this posting format's name 25 Name() string 26 // Writes a new segment 27 FieldsConsumer(state *SegmentWriteState) (FieldsConsumer, error) 28 // Reads a segment. NOTE: by the time this call returns, it must 29 // hold open any files it will need to use; else, those files may 30 // be deleted. Additionally, required fiels may be deleted during 31 // the execution of this call before there is a chance to open them. 32 // Under these circumstances an IO error should be returned by the 33 // implementation. IO errors are expected and will automatically 34 // cause a retry of the segment opening logic with the newly 35 // revised segments. 36 FieldsProducer(state SegmentReadState) (FieldsProducer, error) 37 } 38 39 type PostingsFormatImpl struct { 40 name string 41 } 42 43 // Returns this posting format's name 44 func (pf *PostingsFormatImpl) Name() string { 45 return pf.name 46 } 47 48 func (pf *PostingsFormatImpl) String() string { 49 return fmt.Sprintf("PostingsFormat(name=%v)", pf.name) 50 } 51 52 var allPostingsFormats = map[string]PostingsFormat{} 53 54 // workaround Lucene Java's SPI mechanism 55 func RegisterPostingsFormat(formats ...PostingsFormat) { 56 for _, format := range formats { 57 fmt.Printf("Found postings format: %v\n", format.Name()) 58 allPostingsFormats[format.Name()] = format 59 } 60 } 61 62 /* looks up a format by name */ 63 func LoadPostingsFormat(name string) PostingsFormat { 64 v, ok := allPostingsFormats[name] 65 assert2(ok, "Service '%v' not found.", name) 66 return v 67 } 68 69 func assert(ok bool) { 70 if !ok { 71 panic("assert fail") 72 } 73 } 74 75 func assert2(ok bool, msg string, args ...interface{}) { 76 if !ok { 77 panic(fmt.Sprintf(msg, args...)) 78 } 79 } 80 81 /* Returns a list of all available format names. */ 82 func AvailablePostingsFormats() []string { 83 ans := make([]string, 0, len(allPostingsFormats)) 84 for name, _ := range allPostingsFormats { 85 ans = append(ans, name) 86 } 87 return ans 88 } 89 90 // codecs/FieldsConsumer.java 91 92 /* 93 Abstract API that consumes terms, doc, freq, prox, offset and 94 payloads postings. Concrete implementations of this actually do 95 "something" with the postings (write it into the index in a specific 96 format). 97 98 The lifecycle is: 99 100 1. FieldsConsumer is created by PostingsFormat.FieldsConsumer(). 101 2. For each field, AddField() is called, returning a TermsConsumer 102 for the field. 103 */ 104 type FieldsConsumer interface { 105 io.Closer 106 // Add a new field 107 AddField(field *FieldInfo) (TermsConsumer, error) 108 } 109 110 type FieldsProducer interface { 111 Fields 112 io.Closer 113 }