From d5ac140040ea2f17e33d2846870cb033c7ae6ffa Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Thu, 30 Jun 2016 00:13:47 -0400 Subject: [PATCH] rgw: add rgw_bucket_parse_bucket_key() Signed-off-by: Casey Bodley --- src/rgw/rgw_bucket.cc | 49 +++++++++++++++++++++++++++++++++++++++++++ src/rgw/rgw_bucket.h | 2 ++ 2 files changed, 51 insertions(+) diff --git a/src/rgw/rgw_bucket.cc b/src/rgw/rgw_bucket.cc index 46c5243a06235..d6a148b22d10d 100644 --- a/src/rgw/rgw_bucket.cc +++ b/src/rgw/rgw_bucket.cc @@ -6,6 +6,8 @@ #include #include +#include + #include "common/errno.h" #include "common/ceph_json.h" #include "rgw_rados.h" @@ -346,6 +348,53 @@ int rgw_bucket_parse_bucket_instance(const string& bucket_instance, string *targ return 0; } +// parse key in format: [tenant/]name:instance[:shard_id] +int rgw_bucket_parse_bucket_key(CephContext *cct, const string& key, + rgw_bucket *bucket, int *shard_id) +{ + boost::string_ref name{key}; + boost::string_ref instance; + + // split tenant/name + auto pos = name.find('/'); + if (pos != boost::string_ref::npos) { + auto tenant = name.substr(0, pos); + bucket->tenant.assign(tenant.begin(), tenant.end()); + name = name.substr(pos + 1); + } + + // split name:instance + pos = name.find(':'); + if (pos != boost::string_ref::npos) { + instance = name.substr(pos + 1); + name = name.substr(0, pos); + } + bucket->name.assign(name.begin(), name.end()); + + // split instance:shard + pos = instance.find(':'); + if (pos == boost::string_ref::npos) { + bucket->bucket_id.assign(instance.begin(), instance.end()); + *shard_id = -1; + return 0; + } + + // parse shard id + auto shard = instance.substr(pos + 1); + string err; + auto id = strict_strtol(shard.data(), 10, &err); + if (!err.empty()) { + ldout(cct, 0) << "ERROR: failed to parse bucket shard '" + << instance.data() << "': " << err << dendl; + return -EINVAL; + } + + *shard_id = id; + instance = instance.substr(0, pos); + bucket->bucket_id.assign(instance.begin(), instance.end()); + return 0; +} + int rgw_bucket_set_attrs(RGWRados *store, RGWBucketInfo& bucket_info, map& attrs, RGWObjVersionTracker *objv_tracker) diff --git a/src/rgw/rgw_bucket.h b/src/rgw/rgw_bucket.h index 2d0b05607a4a1..12182d12935ab 100644 --- a/src/rgw/rgw_bucket.h +++ b/src/rgw/rgw_bucket.h @@ -34,6 +34,8 @@ extern int rgw_bucket_instance_store_info(RGWRados *store, string& oid, bufferli real_time mtime); extern int rgw_bucket_parse_bucket_instance(const string& bucket_instance, string *target_bucket_instance, int *shard_id); +extern int rgw_bucket_parse_bucket_key(CephContext *cct, const string& key, + rgw_bucket* bucket, int *shard_id); extern int rgw_bucket_instance_remove_entry(RGWRados *store, string& entry, RGWObjVersionTracker *objv_tracker); extern void rgw_bucket_instance_key_to_oid(string& key); -- 2.39.5