github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/index/model/segmentInfo.go (about) 1 package model 2 3 import ( 4 "bytes" 5 "fmt" 6 "github.com/balzaczyy/golucene/core/store" 7 "github.com/balzaczyy/golucene/core/util" 8 "regexp" 9 "strconv" 10 ) 11 12 // index/SegmentInfo.java 13 14 const NO = -1 15 const YES = 1 16 17 type SegmentInfo struct { 18 Dir store.Directory 19 version util.Version 20 Name string 21 docCount *util.SetOnce // number of docs in seg 22 isCompoundFile bool 23 codec interface{} 24 diagnostics map[string]string 25 files map[string]bool // must use CheckFileNames() 26 27 *AttributesMixin 28 } 29 30 func (info *SegmentInfo) SetDiagnostics(diagnostics map[string]string) { 31 info.diagnostics = diagnostics 32 } 33 34 /* Returns diagnostics saved into the segment when it was written .*/ 35 func (info *SegmentInfo) Diagnostics() map[string]string { 36 return info.diagnostics 37 } 38 39 func NewSegmentInfo(dir store.Directory, 40 version util.Version, name string, docCount int, 41 isCompoundFile bool, codec interface{}, 42 diagnostics map[string]string) *SegmentInfo { 43 return NewSegmentInfo2(dir, version, name, docCount, isCompoundFile, codec, diagnostics, nil) 44 } 45 46 func NewSegmentInfo2(dir store.Directory, 47 version util.Version, name string, docCount int, 48 isCompoundFile bool, codec interface{}, 49 diagnostics map[string]string, 50 attributes map[string]string) *SegmentInfo { 51 _, ok := dir.(*store.TrackingDirectoryWrapper) 52 assert(!ok) 53 return &SegmentInfo{ 54 Dir: dir, 55 version: version, 56 Name: name, 57 docCount: util.NewSetOnceOf(docCount), 58 isCompoundFile: isCompoundFile, 59 codec: codec, 60 diagnostics: diagnostics, 61 AttributesMixin: &AttributesMixin{attributes}, 62 } 63 } 64 65 // seprate norms are not supported in >= 4.0 66 func (si *SegmentInfo) HasSeparateNorms() bool { 67 return false 68 } 69 70 /* Mark whether this segment is stored as a compound file. */ 71 func (si *SegmentInfo) SetUseCompoundFile(isCompoundFile bool) { 72 si.isCompoundFile = isCompoundFile 73 } 74 75 /* Returns true if this segment is stored as a compound file */ 76 func (si *SegmentInfo) IsCompoundFile() bool { 77 return si.isCompoundFile 78 } 79 80 /* Can only be called once. */ 81 func (info *SegmentInfo) SetCodec(codec interface{}) { 82 assert(info.codec == nil) 83 assert2(codec != nil, "codecs must not be nil") 84 info.codec = codec 85 } 86 87 /* Return Codec that wrote this segment. */ 88 func (si *SegmentInfo) Codec() interface{} { 89 return si.codec 90 } 91 92 func (si *SegmentInfo) DocCount() int { 93 return si.docCount.Get().(int) 94 } 95 96 func (info *SegmentInfo) SetDocCount(docCount int) { 97 info.docCount.Set(docCount) 98 } 99 100 /* Return all files referenced by this SegmentInfo. */ 101 func (si *SegmentInfo) Files() map[string]bool { 102 assert2(si.files != nil, "files were not computed yet") 103 return si.files 104 } 105 106 func (si *SegmentInfo) String() string { 107 return si.StringOf(si.Dir, 0) 108 } 109 110 func (si *SegmentInfo) StringOf(dir store.Directory, delCount int) string { 111 var buf bytes.Buffer 112 buf.WriteString(si.Name) 113 buf.WriteString("(") 114 if len(si.version) == 0 { // empty check 115 buf.WriteString("?") 116 } else { 117 buf.WriteString(si.version.String()) 118 } 119 buf.WriteString("):") 120 if si.isCompoundFile { 121 buf.WriteString("c") 122 } else { 123 buf.WriteString("C") 124 } 125 126 if si.Dir != dir { 127 buf.WriteString("x") 128 } 129 fmt.Fprintf(&buf, "%v", si.docCount) 130 131 if delCount != 0 { 132 buf.WriteString("/") 133 buf.WriteString(strconv.Itoa(delCount)) 134 } 135 136 // TODO: we could append toString of attributes() here? 137 138 return buf.String() 139 } 140 141 /* Returns the version of the code which wrote the segment. */ 142 func (si *SegmentInfo) Version() util.Version { 143 return si.version 144 } 145 146 /* Sets the files written for this segment. */ 147 func (si *SegmentInfo) SetFiles(files map[string]bool) { 148 si.checkFileNames(files) 149 si.files = files 150 } 151 152 /* Add this file to the set of files written for this segment. */ 153 func (si *SegmentInfo) AddFile(file string) { 154 si.checkFileNames(map[string]bool{file: true}) 155 si.files[file] = true 156 } 157 158 var CODEC_FILE_PATTERN = regexp.MustCompile("_[a-z0-9]+(_.*)?\\..*") 159 160 func (si *SegmentInfo) checkFileNames(files map[string]bool) { 161 for file, _ := range files { 162 if !CODEC_FILE_PATTERN.MatchString(file) { 163 panic(fmt.Sprintf("invalid codec filename '%v', must match: %v", file, CODEC_FILE_PATTERN)) 164 } 165 } 166 } 167 168 func (si *SegmentInfo) cloneMap(m map[string]string) map[string]string { 169 panic("niy") 170 } 171 172 func (si *SegmentInfo) Clone() *SegmentInfo { 173 other := NewSegmentInfo2(si.Dir, si.version, si.Name, si.DocCount(), 174 si.isCompoundFile, si.codec, si.cloneMap(si.diagnostics), 175 si.cloneMap(si.attributes)) 176 if si.files != nil { 177 other.SetFiles(si.files) 178 } 179 return other 180 }