package engine

import (
    "github.com/huichen/wukong/types"
    "sync/atomic"
)

type indexerAddDocumentRequest struct {
    document    *types.DocumentIndex
    forceUpdate bool
}

type indexerLookupRequest struct {
    countDocsOnly       bool
    tokens              []string
    labels              []string
    docIds              map[uint64]bool
    options             types.RankOptions
    rankerReturnChannel chan rankerReturnRequest
    orderless           bool
}

type indexerRemoveDocRequest struct {
    docId       uint64
    forceUpdate bool
}

func (engine *Engine) indexerAddDocumentWorker(shard int) {
    for {
        request := <-engine.indexerAddDocChannels[shard]
        engine.indexers[shard].AddDocumentToCache(request.document, request.forceUpdate)
        if request.document != nil {
            atomic.AddUint64(&engine.numTokenIndexAdded,
                uint64(len(request.document.Keywords)))
            atomic.AddUint64(&engine.numDocumentsIndexed, 1)
        }
        if request.forceUpdate {
            atomic.AddUint64(&engine.numDocumentsForceUpdated, 1)
        }
    }
}

func (engine *Engine) indexerRemoveDocWorker(shard int) {
    for {
        request := <-engine.indexerRemoveDocChannels[shard]
        engine.indexers[shard].RemoveDocumentToCache(request.docId, request.forceUpdate)
        if request.docId != 0 {
            atomic.AddUint64(&engine.numDocumentsRemoved, 1)
        }
        if request.forceUpdate {
            atomic.AddUint64(&engine.numDocumentsForceUpdated, 1)
        }
    }
}

func (engine *Engine) indexerLookupWorker(shard int) {
    for {
        request := <-engine.indexerLookupChannels[shard]

        var docs []types.IndexedDocument
        var numDocs int
        if request.docIds == nil {
            docs, numDocs = engine.indexers[shard].Lookup(request.tokens, request.labels, nil, request.countDocsOnly)
        } else {
            docs, numDocs = engine.indexers[shard].Lookup(request.tokens, request.labels, request.docIds, request.countDocsOnly)
        }

        if request.countDocsOnly {
            request.rankerReturnChannel <- rankerReturnRequest{numDocs: numDocs}
            continue
        }

        if len(docs) == 0 {
            request.rankerReturnChannel <- rankerReturnRequest{}
            continue
        }

        if request.orderless {
            var outputDocs []types.ScoredDocument
            for _, d := range docs {
                outputDocs = append(outputDocs, types.ScoredDocument{
                    DocId: d.DocId,
                    TokenSnippetLocations: d.TokenSnippetLocations,
                    TokenLocations:        d.TokenLocations})
            }
            request.rankerReturnChannel <- rankerReturnRequest{
                docs:    outputDocs,
                numDocs: len(outputDocs),
            }
            continue
        }

        rankerRequest := rankerRankRequest{
            countDocsOnly:       request.countDocsOnly,
            docs:                docs,
            options:             request.options,
            rankerReturnChannel: request.rankerReturnChannel,
        }
        engine.rankerRankChannels[shard] <- rankerRequest
    }
}

indexer_worker.go的更多相关文章

随机推荐

  1. ruby技巧001:求md5散列

    ruby核心库中未包含md5之类的功能,不过在其标准库digest中可以方便的使用该功能: = Digest (from ruby core) ---------------------------- ...

  2. How--to-deploy-smart-contracts-on

    The following smart contract code is only an example and is NOT to be used in Production systems. pr ...

  3. javascript学习(三)——常用方法(2)

    一.兼容性较高的浏览器页面关闭 //关闭网页,不支持火狐(火狐返回上次浏览页面)   //FireFox非window.open()等弹出页面,需要在地址栏中输入about:config, 然后将do ...

  4. linux上安装redis的踩坑过程

    redis用处很广泛,我不再啰嗦了,我按照网上教程想在linux上安装下,开始了踩坑过程,网上买了一个linux centos7.3,滴滴云的,巨坑无比啊,不建议大家用这家的! redis 为4.0, ...

  5. JS(原生js和jq方式)获取元素属性(自定义属性),删除属性(自定义属性)

    JS(原生js和jq方式)获取元素属性(自定义属性),删除属性(自定义属性) 以下内容: 一.获取元素的属性 二.设置元素的属性 三.删除元素的属性 一.获取元素的属性 1-原生JS 获取属性 .ge ...

  6. FFPLAY的原理(七)

    同步音频 现在我们已经有了一个比较像样的播放器.所以让我们看一下还有哪些零碎的东西没处理.上次,我们掩饰了一点同步问题,也就是同步音频到视频而不是其它的同 步方式.我们将采用和视频一样的方式:做一个内 ...

  7. pandas用法小结

    前言 个人感觉网上对pandas的总结感觉不够详尽细致,在这里我对pandas做个相对细致的小结吧,在数据分析与人工智能方面会有所涉及到的东西在这里都说说吧,也是对自己学习的一种小结! pandas用 ...

  8. 通过jstack与jmap分析一次cpu打满的线上故障

    一.发现问题 下面是线上机器的cpu使用率,可以看到从4月8日开始,随着时间cpu使用率在逐步增高,最终使用率达到100%导致线上服务不可用,后面重启了机器后恢复. 二.排查思路 简单分析下可能出问题 ...

  9. Java并发之ReentrantLock

    一.ReentrantLock简介 ReentrantLock字面意义上理解为可重入锁.那么怎么理解可重入这个概念呢?或者说和我们经常用的synchronized又什么区别呢? ReentrantLo ...

  10. 战争热诚的python全栈开发之路

    从学习python开始,一直是自己摸索,但是时间不等人啊,所以自己为了节省时间,决定报个班系统学习,下面整理的文章都是自己学习后,认为重要的需要弄懂的知识点,做出链接,一方面是为了自己找的话方便,一方 ...