66#include < memory>
77#include < set>
88#include < string>
9+ #include < string_view>
910#include < errno.h>
1011#include < unistd.h>
1112#include < sys/types.h>
@@ -47,6 +48,7 @@ using std::ostream;
4748using std::pair;
4849using std::set;
4950using std::string;
51+ using std::string_view;
5052using std::unique_ptr;
5153using std::vector;
5254
@@ -1992,7 +1994,7 @@ int RocksDBStore::split_key(rocksdb::Slice in, string *prefix, string *key)
19921994
19931995 // Find separator inside Slice
19941996 char * separator = (char *) memchr (in.data (), 0 , in.size ());
1995- if (separator == NULL )
1997+ if (separator == nullptr )
19961998 return -EINVAL;
19971999 prefix_len = size_t (separator - in.data ());
19982000 if (prefix_len >= in.size ())
@@ -2006,6 +2008,27 @@ int RocksDBStore::split_key(rocksdb::Slice in, string *prefix, string *key)
20062008 return 0 ;
20072009}
20082010
2011+ // TODO: deduplicate the code, preferrably by removing the string variant
2012+ int RocksDBStore::split_key (rocksdb::Slice in, string_view *prefix, string_view *key)
2013+ {
2014+ size_t prefix_len = 0 ;
2015+
2016+ // Find separator inside Slice
2017+ char * separator = (char *) memchr (in.data (), 0 , in.size ());
2018+ if (separator == nullptr )
2019+ return -EINVAL;
2020+ prefix_len = size_t (separator - in.data ());
2021+ if (prefix_len >= in.size ())
2022+ return -EINVAL;
2023+
2024+ // Fetch prefix and/or key directly from Slice
2025+ if (prefix)
2026+ *prefix = string_view (in.data (), prefix_len);
2027+ if (key)
2028+ *key = string_view (separator + 1 , in.size () - prefix_len - 1 );
2029+ return 0 ;
2030+ }
2031+
20092032void RocksDBStore::compact ()
20102033{
20112034 dout (2 ) << __func__ << " starting" << dendl;
@@ -2226,7 +2249,13 @@ int RocksDBStore::RocksDBWholeSpaceIteratorImpl::prev()
22262249string RocksDBStore::RocksDBWholeSpaceIteratorImpl::key ()
22272250{
22282251 string out_key;
2229- split_key (dbiter->key (), 0 , &out_key);
2252+ split_key (dbiter->key (), nullptr , &out_key);
2253+ return out_key;
2254+ }
2255+ string_view RocksDBStore::RocksDBWholeSpaceIteratorImpl::key_as_sv ()
2256+ {
2257+ string_view out_key;
2258+ split_key (dbiter->key (), nullptr , &out_key);
22302259 return out_key;
22312260}
22322261pair<string,string> RocksDBStore::RocksDBWholeSpaceIteratorImpl::raw_key ()
@@ -2235,6 +2264,12 @@ pair<string,string> RocksDBStore::RocksDBWholeSpaceIteratorImpl::raw_key()
22352264 split_key (dbiter->key (), &prefix, &key);
22362265 return make_pair (prefix, key);
22372266}
2267+ pair<string_view,string_view> RocksDBStore::RocksDBWholeSpaceIteratorImpl::raw_key_as_sv ()
2268+ {
2269+ string_view prefix, key;
2270+ split_key (dbiter->key (), &prefix, &key);
2271+ return make_pair (prefix, key);
2272+ }
22382273
22392274bool RocksDBStore::RocksDBWholeSpaceIteratorImpl::raw_key_is_prefixed (const string &prefix) {
22402275 // Look for "prefix\0" right in rocksb::Slice
@@ -2267,6 +2302,12 @@ bufferptr RocksDBStore::RocksDBWholeSpaceIteratorImpl::value_as_ptr()
22672302 return bufferptr (val.data (), val.size ());
22682303}
22692304
2305+ std::string_view RocksDBStore::RocksDBWholeSpaceIteratorImpl::value_as_sv ()
2306+ {
2307+ rocksdb::Slice val = dbiter->value ();
2308+ return std::string_view{val.data (), val.size ()};
2309+ }
2310+
22702311int RocksDBStore::RocksDBWholeSpaceIteratorImpl::status ()
22712312{
22722313 return dbiter->status ().ok () ? 0 : -1 ;
@@ -2348,16 +2389,26 @@ class CFIteratorImpl : public KeyValueDB::IteratorImpl {
23482389 string key () override {
23492390 return dbiter->key ().ToString ();
23502391 }
2392+ string_view key_as_sv () override {
2393+ return dbiter->key ().ToStringView ();
2394+ }
23512395 std::pair<std::string, std::string> raw_key () override {
23522396 return make_pair (prefix, key ());
23532397 }
2398+ std::pair<std::string_view, std::string_view> raw_key_as_sv () override {
2399+ return make_pair (prefix, dbiter->key ().ToStringView ());
2400+ }
23542401 bufferlist value () override {
23552402 return to_bufferlist (dbiter->value ());
23562403 }
23572404 bufferptr value_as_ptr () override {
23582405 rocksdb::Slice val = dbiter->value ();
23592406 return bufferptr (val.data (), val.size ());
23602407 }
2408+ std::string_view value_as_sv () override {
2409+ rocksdb::Slice val = dbiter->value ();
2410+ return std::string_view{val.data (), val.size ()};
2411+ }
23612412 int status () override {
23622413 return dbiter->status ().ok () ? 0 : -1 ;
23632414 }
@@ -2668,6 +2719,15 @@ class WholeMergeIteratorImpl : public KeyValueDB::WholeSpaceIteratorImpl {
26682719 }
26692720 }
26702721
2722+ std::string_view key_as_sv () override
2723+ {
2724+ if (smaller == on_main) {
2725+ return main->key_as_sv ();
2726+ } else {
2727+ return current_shard->second ->key_as_sv ();
2728+ }
2729+ }
2730+
26712731 std::pair<std::string,std::string> raw_key () override
26722732 {
26732733 if (smaller == on_main) {
@@ -2677,6 +2737,15 @@ class WholeMergeIteratorImpl : public KeyValueDB::WholeSpaceIteratorImpl {
26772737 }
26782738 }
26792739
2740+ std::pair<std::string_view,std::string_view> raw_key_as_sv () override
2741+ {
2742+ if (smaller == on_main) {
2743+ return main->raw_key_as_sv ();
2744+ } else {
2745+ return { current_shard->first , current_shard->second ->key_as_sv () };
2746+ }
2747+ }
2748+
26802749 bool raw_key_is_prefixed (const std::string &prefix) override
26812750 {
26822751 if (smaller == on_main) {
@@ -2695,6 +2764,15 @@ class WholeMergeIteratorImpl : public KeyValueDB::WholeSpaceIteratorImpl {
26952764 }
26962765 }
26972766
2767+ std::string_view value_as_sv () override
2768+ {
2769+ if (smaller == on_main) {
2770+ return main->value_as_sv ();
2771+ } else {
2772+ return current_shard->second ->value_as_sv ();
2773+ }
2774+ }
2775+
26982776 int status () override
26992777 {
27002778 // because we already had to inspect key, it must be ok
@@ -3017,16 +3095,26 @@ class ShardMergeIteratorImpl : public KeyValueDB::IteratorImpl {
30173095 string key () override {
30183096 return iters[0 ]->key ().ToString ();
30193097 }
3098+ string_view key_as_sv () override {
3099+ return iters[0 ]->key ().ToStringView ();
3100+ }
30203101 std::pair<std::string, std::string> raw_key () override {
30213102 return make_pair (prefix, key ());
30223103 }
3104+ std::pair<std::string_view, std::string_view> raw_key_as_sv () override {
3105+ return make_pair (prefix, iters[0 ]->key ().ToStringView ());
3106+ }
30233107 bufferlist value () override {
30243108 return to_bufferlist (iters[0 ]->value ());
30253109 }
30263110 bufferptr value_as_ptr () override {
30273111 rocksdb::Slice val = iters[0 ]->value ();
30283112 return bufferptr (val.data (), val.size ());
30293113 }
3114+ std::string_view value_as_sv () override {
3115+ rocksdb::Slice val = iters[0 ]->value ();
3116+ return std::string_view{val.data (), val.size ()};
3117+ }
30303118 int status () override {
30313119 return iters[0 ]->status ().ok () ? 0 : -1 ;
30323120 }
0 commit comments