]> git.proxmox.com Git - ceph.git/blame - ceph/src/cls/log/cls_log_client.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / cls / log / cls_log_client.cc
CommitLineData
f67539c2
TL
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
7c673cae
FG
3#include <errno.h>
4
7c673cae
FG
5#include "cls/log/cls_log_ops.h"
6#include "include/rados/librados.hpp"
7#include "include/compat.h"
8
9
f67539c2
TL
10using std::list;
11using std::string;
12
13using ceph::bufferlist;
14
7c673cae
FG
15using namespace librados;
16
17
18
19void cls_log_add(librados::ObjectWriteOperation& op, list<cls_log_entry>& entries, bool monotonic_inc)
20{
21 bufferlist in;
22 cls_log_add_op call;
23 call.entries = entries;
11fdf7f2 24 encode(call, in);
7c673cae
FG
25 op.exec("log", "add", in);
26}
27
28void cls_log_add(librados::ObjectWriteOperation& op, cls_log_entry& entry)
29{
30 bufferlist in;
31 cls_log_add_op call;
32 call.entries.push_back(entry);
11fdf7f2 33 encode(call, in);
7c673cae
FG
34 op.exec("log", "add", in);
35}
36
37void cls_log_add_prepare_entry(cls_log_entry& entry, const utime_t& timestamp,
38 const string& section, const string& name, bufferlist& bl)
39{
40 entry.timestamp = timestamp;
41 entry.section = section;
42 entry.name = name;
43 entry.data = bl;
44}
45
46void cls_log_add(librados::ObjectWriteOperation& op, const utime_t& timestamp,
47 const string& section, const string& name, bufferlist& bl)
48{
49 cls_log_entry entry;
50
51 cls_log_add_prepare_entry(entry, timestamp, section, name, bl);
52 cls_log_add(op, entry);
53}
54
55void cls_log_trim(librados::ObjectWriteOperation& op, const utime_t& from_time, const utime_t& to_time,
56 const string& from_marker, const string& to_marker)
57{
58 bufferlist in;
59 cls_log_trim_op call;
60 call.from_time = from_time;
61 call.to_time = to_time;
62 call.from_marker = from_marker;
63 call.to_marker = to_marker;
11fdf7f2 64 encode(call, in);
7c673cae
FG
65 op.exec("log", "trim", in);
66}
67
68int cls_log_trim(librados::IoCtx& io_ctx, const string& oid, const utime_t& from_time, const utime_t& to_time,
69 const string& from_marker, const string& to_marker)
70{
71 bool done = false;
72
73 do {
74 ObjectWriteOperation op;
75
76 cls_log_trim(op, from_time, to_time, from_marker, to_marker);
77
78 int r = io_ctx.operate(oid, &op);
79 if (r == -ENODATA)
80 done = true;
81 else if (r < 0)
82 return r;
83
84 } while (!done);
85
86
87 return 0;
88}
89
90class LogListCtx : public ObjectOperationCompletion {
91 list<cls_log_entry> *entries;
92 string *marker;
93 bool *truncated;
94public:
95 LogListCtx(list<cls_log_entry> *_entries, string *_marker, bool *_truncated) :
96 entries(_entries), marker(_marker), truncated(_truncated) {}
97 void handle_completion(int r, bufferlist& outbl) override {
98 if (r >= 0) {
99 cls_log_list_ret ret;
100 try {
11fdf7f2
TL
101 auto iter = outbl.cbegin();
102 decode(ret, iter);
7c673cae 103 if (entries)
224ce89b 104 *entries = std::move(ret.entries);
7c673cae
FG
105 if (truncated)
106 *truncated = ret.truncated;
107 if (marker)
224ce89b 108 *marker = std::move(ret.marker);
f67539c2 109 } catch (ceph::buffer::error& err) {
7c673cae
FG
110 // nothing we can do about it atm
111 }
112 }
113 }
114};
115
f67539c2
TL
116void cls_log_list(librados::ObjectReadOperation& op, const utime_t& from,
117 const utime_t& to, const string& in_marker, int max_entries,
7c673cae
FG
118 list<cls_log_entry>& entries,
119 string *out_marker, bool *truncated)
120{
121 bufferlist inbl;
122 cls_log_list_op call;
123 call.from_time = from;
124 call.to_time = to;
125 call.marker = in_marker;
126 call.max_entries = max_entries;
127
11fdf7f2 128 encode(call, inbl);
7c673cae
FG
129
130 op.exec("log", "list", inbl, new LogListCtx(&entries, out_marker, truncated));
131}
132
133class LogInfoCtx : public ObjectOperationCompletion {
134 cls_log_header *header;
135public:
136 explicit LogInfoCtx(cls_log_header *_header) : header(_header) {}
137 void handle_completion(int r, bufferlist& outbl) override {
138 if (r >= 0) {
139 cls_log_info_ret ret;
140 try {
11fdf7f2
TL
141 auto iter = outbl.cbegin();
142 decode(ret, iter);
7c673cae
FG
143 if (header)
144 *header = ret.header;
f67539c2 145 } catch (ceph::buffer::error& err) {
7c673cae
FG
146 // nothing we can do about it atm
147 }
148 }
149 }
150};
151
152void cls_log_info(librados::ObjectReadOperation& op, cls_log_header *header)
153{
154 bufferlist inbl;
155 cls_log_info_op call;
156
11fdf7f2 157 encode(call, inbl);
7c673cae
FG
158
159 op.exec("log", "info", inbl, new LogInfoCtx(header));
160}