]> git.proxmox.com Git - ceph.git/blame - ceph/src/rgw/driver/rados/config/impl.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rgw / driver / rados / config / impl.h
CommitLineData
1e59de90
TL
1// vim: ts=8 sw=2 smarttab ft=cpp
2
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2022 Red Hat, Inc.
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#pragma once
16
17#include "include/rados/librados.hpp"
18#include "common/dout.h"
19#include "rgw_basic_types.h"
20#include "rgw_tools.h"
21#include "rgw_sal_config.h"
22
23namespace rgw::rados {
24
25// write options that control object creation
26enum class Create {
27 MustNotExist, // fail with EEXIST if the object already exists
28 MayExist, // create if the object didn't exist, overwrite if it did
29 MustExist, // fail with ENOENT if the object doesn't exist
30};
31
32struct ConfigImpl {
33 librados::Rados rados;
34
35 const rgw_pool realm_pool;
36 const rgw_pool period_pool;
37 const rgw_pool zonegroup_pool;
38 const rgw_pool zone_pool;
39
40 ConfigImpl(const ceph::common::ConfigProxy& conf);
41
42 int read(const DoutPrefixProvider* dpp, optional_yield y,
43 const rgw_pool& pool, const std::string& oid,
44 bufferlist& bl, RGWObjVersionTracker* objv);
45
46 template <typename T>
47 int read(const DoutPrefixProvider* dpp, optional_yield y,
48 const rgw_pool& pool, const std::string& oid,
49 T& data, RGWObjVersionTracker* objv)
50 {
51 bufferlist bl;
52 int r = read(dpp, y, pool, oid, bl, objv);
53 if (r < 0) {
54 return r;
55 }
56 try {
57 auto p = bl.cbegin();
58 decode(data, p);
59 } catch (const buffer::error& err) {
60 ldpp_dout(dpp, 0) << "ERROR: failed to decode obj from "
61 << pool << ":" << oid << dendl;
62 return -EIO;
63 }
64 return 0;
65 }
66
67 int write(const DoutPrefixProvider* dpp, optional_yield y,
68 const rgw_pool& pool, const std::string& oid, Create create,
69 const bufferlist& bl, RGWObjVersionTracker* objv);
70
71 template <typename T>
72 int write(const DoutPrefixProvider* dpp, optional_yield y,
73 const rgw_pool& pool, const std::string& oid, Create create,
74 const T& data, RGWObjVersionTracker* objv)
75 {
76 bufferlist bl;
77 encode(data, bl);
78
79 return write(dpp, y, pool, oid, create, bl, objv);
80 }
81
82 int remove(const DoutPrefixProvider* dpp, optional_yield y,
83 const rgw_pool& pool, const std::string& oid,
84 RGWObjVersionTracker* objv);
85
86 int list(const DoutPrefixProvider* dpp, optional_yield y,
87 const rgw_pool& pool, const std::string& marker,
88 std::regular_invocable<std::string> auto filter,
89 std::span<std::string> entries,
90 sal::ListResult<std::string>& result)
91 {
92 librados::IoCtx ioctx;
93 int r = rgw_init_ioctx(dpp, &rados, pool, ioctx, true, false);
94 if (r < 0) {
95 return r;
96 }
97 librados::ObjectCursor oc;
98 if (!oc.from_str(marker)) {
99 ldpp_dout(dpp, 10) << "failed to parse cursor: " << marker << dendl;
100 return -EINVAL;
101 }
102 std::size_t count = 0;
103 try {
104 auto iter = ioctx.nobjects_begin(oc);
105 const auto end = ioctx.nobjects_end();
106 for (; count < entries.size() && iter != end; ++iter) {
107 std::string entry = filter(iter->get_oid());
108 if (!entry.empty()) {
109 entries[count++] = std::move(entry);
110 }
111 }
112 if (iter == end) {
113 result.next.clear();
114 } else {
115 result.next = iter.get_cursor().to_str();
116 }
117 } catch (const std::exception& e) {
118 ldpp_dout(dpp, 10) << "NObjectIterator exception " << e.what() << dendl;
119 return -EIO;
120 }
121 result.entries = entries.first(count);
122 return 0;
123 }
124
125 int notify(const DoutPrefixProvider* dpp, optional_yield y,
126 const rgw_pool& pool, const std::string& oid,
127 bufferlist& bl, uint64_t timeout_ms);
128};
129
130inline std::string_view name_or_default(std::string_view name,
131 std::string_view default_name)
132{
133 if (!name.empty()) {
134 return name;
135 }
136 return default_name;
137}
138
139} // namespace rgw::rados