aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/base/documentid.h
blob: fc1537b9501bf146c5f9b096ea10d53be5d96cd7 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * \class document::DocumentId
 * \ingroup base
 *
 * \brief Class describing a document identifier.
 *
 * The document identifier is an URI set by the user. The URI must conform
 * to one of the accepted document identifier schemes.
 *
 * The IdString class represent such a scheme. See the various implementations
 * of IdString to see legal schemes.
 *
 * This class contains the identifier parsed into pieces. Thus, get() functions
 * are cheap to call.
 *
 * It's up to the users to ensure that they use unique document identifiers.
 */

#pragma once

#include "idstring.h"
#include "globalid.h"

namespace vespalib { class nbostream; }

namespace document {

class DocumentType;

class DocumentId
{
public:
    using UP = std::unique_ptr<DocumentId>;

    DocumentId();
    DocumentId(vespalib::nbostream & os);
    DocumentId(DocumentId && rhs) noexcept = default;
    DocumentId & operator = (DocumentId && rhs) noexcept = default;
    DocumentId(const DocumentId & rhs);
    DocumentId & operator = (const DocumentId & rhs);
    ~DocumentId() noexcept ;
    /**
     * Parse the given document identifier given as string, and create an
     * identifier object from it.
     *
     * Precondition: `id` MUST be null-terminated.
     *
     * @throws IdParseException If the identifier given is invalid.
     */
    explicit DocumentId(vespalib::stringref id);

    /**
     * Precondition: `id` MUST be null-terminated.
     */
    void set(vespalib::stringref id);

    /**
       Hides the printable toString() for effiency reasons.
    */
    vespalib::string toString() const;

    bool operator==(const DocumentId& other) const { return _id == other._id; }
    bool operator!=(const DocumentId& other) const { return ! (_id == other._id); }

    const IdString& getScheme() const { return _id; }
    bool hasDocType() const { return _id.hasDocType(); }
    vespalib::stringref getDocType() const { return _id.getDocType(); }

    const GlobalId& getGlobalId() const {
        if (!_globalId.first) { calculateGlobalId(); }
        return _globalId.second;
    }

    size_t getSerializedSize() const;
private:
    mutable std::pair<bool, GlobalId> _globalId;
    IdString _id;

    void calculateGlobalId() const;
};

std::ostream & operator << (std::ostream & os, const DocumentId & id);

} // document