func (this *StaticIndexer) parseDoc() { // context context := NewStyContext() // 一直从chan中获取doc,直到这个chan被close for doc := range this.parseDocChan { var err error // parse parseRes := &docParsed{} parseRes.outId, parseRes.termList, parseRes.value, parseRes.data, err = this.strategy.ParseDoc(doc, context) if err != nil { log.Error(err) parseRes = nil } // 打印策略日志 context.Log.PrintAllInfo() // toWriteDbQueue是待写入db的队列. // 阻塞等待队列有空余位置然后写入队列. this.writeDbQueue <- parseRes } log.Info("Finish parseDoc , goroutine exit.") }
func (this *StaticIndexer) writeDoc() { for parseRes := range this.writeDbQueue { if parseRes == nil { log.Error("get nil pointer from queue") this.finishedWg.Done() continue } // id inId, err := this.db.AllocID(parseRes.outId) if err != nil { log.Error(err) this.finishedWg.Done() continue } // index err = this.db.WriteIndex(inId, parseRes.termList) if err != nil { log.Error(err) this.finishedWg.Done() continue } // value err = this.db.WriteValue(inId, parseRes.value) if err != nil { log.Error(err) this.finishedWg.Done() continue } // data err = this.db.WriteData(inId, parseRes.data) if err != nil { log.Error(err) this.finishedWg.Done() continue } this.finishedWg.Done() } log.Info("Finish writeDoc,goroutine exit.") }