summaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/server/documentretrieverbase.cpp
blob: 183e9eb0c0a0ea4be9288ad57a8aa304785e2182 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "documentretrieverbase.h"
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/document/datatype/documenttype.h>
#include <vespa/vespalib/stllike/lrucache_map.hpp>
#include <vespa/vespalib/util/stringfmt.h>

using document::DocumentId;
using document::GlobalId;

namespace proton {

DocumentRetrieverBase::DocumentRetrieverBase(
        const DocTypeName &docTypeName,
        const document::DocumentTypeRepo &repo,
        const IDocumentMetaStoreContext &meta_store,
        bool hasFields)
    : IDocumentRetriever(),
      _docTypeName(docTypeName),
      _repo(repo),
      _meta_store(meta_store),
      _selectCache(256u),
      _lock(),
      _emptyDoc(),
      _hasFields(hasFields)
{
    const document::DocumentType * docType(_repo.getDocumentType(_docTypeName.getName()));
    _emptyDoc = std::make_unique<document::Document>(*docType, DocumentId("id:empty:" + _docTypeName.getName() + "::empty"));
    _emptyDoc->setRepo(_repo);
}

DocumentRetrieverBase::~DocumentRetrieverBase() = default;

const document::DocumentTypeRepo &
DocumentRetrieverBase::getDocumentTypeRepo() const {
    return _repo;
}

void
DocumentRetrieverBase::getBucketMetaData(
        const storage::spi::Bucket &bucket,
        search::DocumentMetaData::Vector &result) const {
    IDocumentMetaStoreContext::IReadGuard::UP readGuard = _meta_store.getReadGuard();
    const search::IDocumentMetaStore &meta_store = readGuard->get();
    meta_store.getMetaData(bucket, result);
}

search::DocumentMetaData
DocumentRetrieverBase::getDocumentMetaData(const DocumentId &id) const {
    const GlobalId &gid = id.getGlobalId();
    IDocumentMetaStoreContext::IReadGuard::UP readGuard = _meta_store.getReadGuard();
    const search::IDocumentMetaStore &meta_store = readGuard->get();
    return meta_store.getMetaData(gid);
}


const search::IAttributeManager *
DocumentRetrieverBase::getAttrMgr() const
{
    return nullptr;
}

    
CachedSelect::SP
DocumentRetrieverBase::parseSelect(const vespalib::string &selection) const
{
    {
        std::lock_guard<std::mutex> guard(_lock);
        if (_selectCache.hasKey(selection))
            return _selectCache[selection];
    }
    
    auto nselect = std::make_shared<CachedSelect>();
    
    nselect->set(selection,
                 _docTypeName.getName(),
                 *_emptyDoc,
                 getDocumentTypeRepo(),
                 getAttrMgr(),
                 _hasFields);

    std::lock_guard<std::mutex> guard(_lock);
    if (_selectCache.hasKey(selection))
        return _selectCache[selection];
    _selectCache.insert(selection, nselect);
    return nselect;
}


}  // namespace proton