aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/documentmetastore/lid_gid_key_comparator.h
blob: 55cf1b8e5871503c40203da8d96e61ebdb73a06b (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "raw_document_meta_data.h"
#include "gid_to_lid_map_key.h"
#include <vespa/document/base/globalid.h>
#include <vespa/searchlib/common/idocumentmetastore.h>

namespace proton::documentmetastore {

/**
 * Comparator class used by the lid<->gid btree to get the lids
 * sorted by their gid counterpart.
 **/
class LidGidKeyComparator
{
private:
    using DocId = search::IDocumentMetaStore::DocId;
    using UnboundMetaDataView = const RawDocumentMetaData *;

    const document::GlobalId &_gid;
    UnboundMetaDataView       _metaDataView;
    const document::GlobalId::BucketOrderCmp _gidCompare;

    const document::GlobalId &getGid(const GidToLidMapKey &key) const {
        if (!key.is_find_key()) {
            return _metaDataView[key.get_lid()].getGid();
        }
        return _gid;
    }

public:
    /**
     * Creates a comparator that returns the given gid if
     * key is a find key. Otherwise the metadata store is
     * used to map from lid -> metadata (including gid).
     **/
    LidGidKeyComparator(const document::GlobalId &gid,
                        UnboundMetaDataView metaDataView);

    LidGidKeyComparator(const RawDocumentMetaData &metaData,
                        UnboundMetaDataView metaDataView);

    bool operator()(const GidToLidMapKey &lhs, const GidToLidMapKey &rhs) const {
        if (lhs.get_gid_key() != rhs.get_gid_key()) {
            return lhs.get_gid_key() < rhs.get_gid_key();
        }
        return _gidCompare(getGid(lhs), getGid(rhs));
    }

};

}