From a807f89ce34b67e7a514f8c9b2bda65a9368fdb9 Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Fri, 19 May 2017 17:30:11 -0400 Subject: [PATCH] cls/rgw: rgw_bi_log_trim() uses cls_cxx_map_remove_range() Signed-off-by: Casey Bodley --- src/cls/rgw/cls_rgw.cc | 85 ++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/src/cls/rgw/cls_rgw.cc b/src/cls/rgw/cls_rgw.cc index 8babb8a7fae..7288cd0475e 100644 --- a/src/cls/rgw/cls_rgw.cc +++ b/src/cls/rgw/cls_rgw.cc @@ -2737,35 +2737,6 @@ static int rgw_bi_log_list(cls_method_context_t hctx, bufferlist *in, bufferlist return 0; } -static int bi_log_list_trim_cb(cls_method_context_t hctx, const string& key, rgw_bi_log_entry& info, void *param) -{ - list *entries = (list *)param; - - entries->push_back(info); - return 0; -} - -static int bi_log_remove_entry(cls_method_context_t hctx, rgw_bi_log_entry& entry) -{ - string key; - key = BI_PREFIX_CHAR; - key.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]); - key.append(entry.id); - return cls_cxx_map_remove_key(hctx, key); -} - -static int bi_log_list_trim_entries(cls_method_context_t hctx, - const string& start_marker, const string& end_marker, - list& entries, bool *truncated) -{ - string key_iter; -#define MAX_TRIM_ENTRIES 1000 /* max entries to trim in a single operation */ - int ret = bi_log_iterate_entries(hctx, start_marker, end_marker, - key_iter, MAX_TRIM_ENTRIES, truncated, - bi_log_list_trim_cb, &entries); - return ret; -} - static int rgw_bi_log_trim(cls_method_context_t hctx, bufferlist *in, bufferlist *out) { auto in_iter = in->cbegin(); @@ -2778,26 +2749,52 @@ static int rgw_bi_log_trim(cls_method_context_t hctx, bufferlist *in, bufferlist return -EINVAL; } - cls_rgw_bi_log_list_ret op_ret; - list entries; -#define MAX_TRIM_ENTRIES 1000 /* don't do more than that in a single operation */ - bool truncated; - int ret = bi_log_list_trim_entries(hctx, op.start_marker, op.end_marker, entries, &truncated); - if (ret < 0) - return ret; + string key_begin(1, BI_PREFIX_CHAR); + key_begin.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]); + key_begin.append(op.start_marker); - if (entries.empty()) - return -ENODATA; + string key_end; + if (op.end_marker.empty()) { + key_end = BI_PREFIX_CHAR; + key_end.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX + 1]); + } else { + key_end = BI_PREFIX_CHAR; + key_end.append(bucket_index_prefixes[BI_BUCKET_LOG_INDEX]); + key_end.append(op.end_marker); + // cls_cxx_map_remove_range() expects one-past-end + key_end.append(1, '\0'); + } - list::iterator iter; - for (iter = entries.begin(); iter != entries.end(); ++iter) { - rgw_bi_log_entry& entry = *iter; + // list a single key to detect whether the range is empty + const size_t max_entries = 1; + std::set keys; + bool more = false; - ret = bi_log_remove_entry(hctx, entry); - if (ret < 0) - return ret; + int rc = cls_cxx_map_get_keys(hctx, key_begin, max_entries, &keys, &more); + if (rc < 0) { + CLS_LOG(1, "ERROR: cls_cxx_map_get_keys failed rc=%d", rc); + return rc; + } + + if (keys.empty()) { + CLS_LOG(20, "range is empty key_begin=%s", key_begin.c_str()); + return -ENODATA; + } + + const std::string& first_key = *keys.begin(); + if (key_end < first_key) { + CLS_LOG(20, "listed key %s past key_end=%s", first_key.c_str(), key_end.c_str()); + return -ENODATA; } + CLS_LOG(20, "listed key %s, removing through %s", + first_key.c_str(), key_end.c_str()); + + rc = cls_cxx_map_remove_range(hctx, first_key, key_end); + if (rc < 0) { + CLS_LOG(1, "ERROR: cls_cxx_map_remove_range failed rc=%d", rc); + return rc; + } return 0; } -- 2.39.5