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. nasm预处理器(1)

    与处理器将所有以反斜杠结尾的连续行合并为一行. 单行的宏以%define来定义:当单行的宏被扩展后还含有其他宏时,会在执行时而不是定义时展开. %define a(x) 1+b(x) %define ...

  2. 【省带宽、压成本专题】深入解析 H.265 编码模式,带你了解 Apple 全面推进 H.265 的原因

    过去几年,又拍云一直在点播.直播等视频应用方面潜心钻研,取得了不俗的成果.我们结合点播.直播.短视频等业务中的用户场景,推出了"省带宽.压成本"系列文章,从编码技术.网络架构等角度 ...

  3. 关于iOS9 HTTP不能正常使用的解决方法

    在工程的info.plist文件中添加NSAPPTransportSecurity类型为Dictionary,在NSAPPTransportSecurity下添加NSAllowsArbitraryLo ...

  4. IOS常用第三方库《转》

    UI 动画 网络相关 Model 其他 数据库 缓存处理 PDF 图像浏览及处理 摄像照相视频音频处理 响应式框架 消息相关 版本新API的Demo 代码安全与密码 测试及调试 AppleWatch ...

  5. 学习jQuery必须知道的几种常用方法

    jQuery事件处理 ready(fn) 代码: $(document).ready(function(){  // Your code here...}): 作用:它可以极大地提高web应用程序的响 ...

  6. jquery.js

    /*! jQuery v1.10.2 | (c) 2005, 2013 jQuery Foundation, Inc. | jquery.org/license //@ sourceMappingUR ...

  7. 用python-webdriver实现自动填表

    在日常工作中常常需要重复填写某些表单,如果人工完成,费时费力,而且网络延迟令人十分崩溃.如果能够用程序实现自动填表,效率可以提高一倍以上,并且能够移植到多台计算机,进一步提高工作效率.webdrive ...

  8. 自制无线共享工具C++源代码

    // wire.cpp : 定义控制台应用程序的入口点. // #include <iostream> #include <string.h> using namespace ...

  9. RDC去省赛玩前の日常训练 Chapter 1

    4/3 技能点 A. 生成树的计数 论文:周冬<生成树的计数及其应用>(看不懂 pending) 一个栗子:Codeforces 719D 两个做法 Matrix-Tree + 高斯消元, ...

  10. python 脚本自动登陆校园网

    学校的校园网每次重开电脑时都要重新打开浏览器进行网页登录,繁琐的操作比较麻烦,于是便写了个python的脚本进行自动登录,下面说下具体的操作过程: 1. 方法说明 博主采用的python的 reque ...