aboutsummaryrefslogtreecommitdiffstats
path: root/persistence/src/vespa/persistence/spi/doctype_gid_and_timestamp.h
blob: 9ff4d0493fa986093edef1d0c7bdecd933ce975c (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "types.h"
#include <vespa/document/base/globalid.h>
#include <vespa/vespalib/stllike/hash_fun.h>
#include <vespa/vespalib/stllike/string.h>
#include <iosfwd>

namespace vespalib { class asciistream; }

namespace storage::spi {

/**
 * Convenience wrapper for referencing a document type and global id with
 * a timestamp.
 *
 * Prefer this instead of a std::tuple due to named fields and a pre-provided hash function.
 */
struct DocTypeGidAndTimestamp {
    vespalib::string doc_type;
    document::GlobalId gid;
    Timestamp timestamp;

    DocTypeGidAndTimestamp();
    DocTypeGidAndTimestamp(const vespalib::string& doc_type_, document::GlobalId gid_, Timestamp timestamp_) noexcept;

    DocTypeGidAndTimestamp(const DocTypeGidAndTimestamp&);
    DocTypeGidAndTimestamp& operator=(const DocTypeGidAndTimestamp&);
    DocTypeGidAndTimestamp(DocTypeGidAndTimestamp&&) noexcept;
    DocTypeGidAndTimestamp& operator=(DocTypeGidAndTimestamp&&) noexcept;

    bool operator==(const DocTypeGidAndTimestamp& rhs) const noexcept {
        return ((doc_type == rhs.doc_type) && (gid == rhs.gid) &&
                (timestamp == rhs.timestamp));
    }

    void print(vespalib::asciistream&) const;
    vespalib::string to_string() const;

    struct hash {
        size_t operator()(const DocTypeGidAndTimestamp& dt_gid_ts) const noexcept {
            size_t h = document::GlobalId::hash()(dt_gid_ts.gid);
            h = h ^ (vespalib::hash<vespalib::string>()(dt_gid_ts.doc_type) + 0x9e3779b9U + (h << 6U) + (h >> 2U));
            return h ^ (dt_gid_ts.timestamp + 0x9e3779b9U + (h << 6U) + (h >> 2U)); // Basically boost::hash_combine
        }
    };
};

vespalib::asciistream& operator<<(vespalib::asciistream&, const DocTypeGidAndTimestamp&);
std::ostream& operator<<(std::ostream&, const DocTypeGidAndTimestamp&);

}