xref: /6.6.0/kv_engine/include/xattr/utils.h (revision 5c64d40a)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2016 Couchbase, Inc.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17#pragma once
18
19#include <platform/sized_buffer.h>
20#include <xattr/visibility.h>
21
22#include <string>
23
24namespace cb {
25/**
26 * The XATTR support in Couchbase is implemented in the core by storing them
27 * together with the actual data. Their presence is flagged by setting the
28 * XATTR bit in the datatype member. The actual layout is a four byte
29 * integer value containing the length of the XATTR "blob" and then the
30 * actual body.
31 */
32namespace xattr {
33
34/**
35 * Validate that the first content of a blob contains a valid XATTR
36 * encoded blob. The motivation behind this function is to ensure that
37 * we don't crash if someone provides an illegal blob via DCP or SetWithMeta
38 *
39 * @param blob the blob to validate
40 * @return true if the blob contains a valid xattr encoded blob (and
41 *              that it is safe to use the rest of the methods in
42 *              cb::xattr to access them
43 */
44XATTR_PUBLIC_API
45bool validate(const cb::const_char_buffer& blob);
46
47/**
48 * Get the offset of the body into the specified payload
49 *
50 * @param payload the payload to check
51 * @return The number of bytes into the payload where the body lives
52 *         (the body size == payload.len - the returned value)
53 */
54XATTR_PUBLIC_API
55uint32_t get_body_offset(const cb::const_char_buffer& payload);
56
57/**
58 * Get the segment where the actual body lives
59 *
60 * @param payload the document blob as it is stored in the engine
61 * @return a buffer representing the body blob
62 */
63XATTR_PUBLIC_API
64cb::const_char_buffer get_body(const cb::const_char_buffer& payload);
65
66/**
67 * Check to see if the provided attribute represents a system
68 * attribute or not.
69 *
70 * @param attr the attribute to check (CAN'T BE EMPTY!)
71 */
72static inline bool is_system_xattr(cb::const_char_buffer& attr) {
73    return *attr.data() == '_';
74}
75
76/**
77 * Check if the attribute is a virtual xattr or not
78 *
79 * @param attr the attribute to check
80 */
81static inline bool is_vattr(cb::const_char_buffer attr) {
82    return !attr.empty() && *attr.data() == '$';
83}
84
85namespace macros {
86struct macro {
87    cb::const_char_buffer name;
88    size_t expandedSize;
89};
90static macro CAS = {"\"${Mutation.CAS}\"", 8};
91static macro SEQNO = {"\"${Mutation.seqno}\"", 8};
92static macro VALUE_CRC32C = {"\"${Mutation.value_crc32c}\"", 4};
93}
94
95namespace vattrs {
96static constexpr cb::const_char_buffer DOCUMENT = {"$document", 9};
97static constexpr cb::const_char_buffer VBUCKET = {"$vbucket", 8};
98static constexpr cb::const_char_buffer XTOC = {"$XTOC", 5};
99}
100
101/**
102 * Get the number of bytes the system xattrs contains in the provided
103 * document (which may not contain xattrs at all)
104 *
105 * @param datatype the datatype for the provided document
106 * @param doc the document to inspect
107 * @return the number of bytes of system xattrs
108 */
109XATTR_PUBLIC_API
110size_t get_system_xattr_size(uint8_t datatype, const cb::const_char_buffer doc);
111
112/**
113 * Get the size of the body chunk in the provided value, which may not contain
114 * any xattr.
115 *
116 * @param datatype
117 * @param value
118 * @return the body size
119 */
120XATTR_PUBLIC_API
121size_t get_body_size(uint8_t datatype, cb::const_char_buffer value);
122}
123}
124