diff --git a/curvefs/src/metaserver/dentry_storage.cpp b/curvefs/src/metaserver/dentry_storage.cpp index dae9d7fd7d..0fbc96f5cd 100644 --- a/curvefs/src/metaserver/dentry_storage.cpp +++ b/curvefs/src/metaserver/dentry_storage.cpp @@ -379,8 +379,7 @@ MetaStatusCode DentryStorage::List(const Dentry& dentry, time.start(); for (iterator->SeekToFirst(); iterator->Valid(); iterator->Next()) { seekTimes++; - std::string skey = iterator->Key(); - std::string svalue = iterator->Value(); + absl::string_view skey = iterator->Key(); if (!StringStartWith(skey, sprefix)) { break; } else if (!iterator->ParseFromValue(¤t)) { diff --git a/curvefs/src/metaserver/inode_storage.cpp b/curvefs/src/metaserver/inode_storage.cpp index da175f5ba2..5c95ba6cfb 100644 --- a/curvefs/src/metaserver/inode_storage.cpp +++ b/curvefs/src/metaserver/inode_storage.cpp @@ -209,7 +209,7 @@ bool InodeStorage::GetAllInodeId(std::list* ids) { Key4Inode key; for (iterator->SeekToFirst(); iterator->Valid(); iterator->Next()) { - if (!conv_.ParseFromString(iterator->Key(), &key)) { + if (!conv_.ParseFromStringView(iterator->Key(), &key)) { return false; } ids->push_back(key.inodeId); @@ -372,12 +372,12 @@ MetaStatusCode InodeStorage::DelS3ChunkInfoList( } Key4S3ChunkInfoList key; - std::vector key2del; + std::vector key2del; for (iterator->SeekToFirst(); iterator->Valid(); iterator->Next()) { - std::string skey = iterator->Key(); + absl::string_view skey = iterator->Key(); if (!StringStartWith(skey, sprefix)) { break; - } else if (!conv_.ParseFromString(skey, &key)) { + } else if (!conv_.ParseFromStringView(skey, &key)) { return MetaStatusCode::PARSE_FROM_STRING_FAILED; } @@ -397,8 +397,9 @@ MetaStatusCode InodeStorage::DelS3ChunkInfoList( } } - for (const auto& skey : key2del) { - if (!txn->SDel(table4S3ChunkInfo_, skey).ok()) { + // TODO: how to avoid copy here? + for (auto skey : key2del) { + if (!txn->SDel(std::string(table4S3ChunkInfo_), std::string(skey)).ok()) { LOG(ERROR) << "Delete key failed, skey=" << skey; return MetaStatusCode::STORAGE_INTERNAL_ERROR; } @@ -475,9 +476,8 @@ MetaStatusCode InodeStorage::PaddingInodeS3ChunkInfo(int32_t fsId, Key4S3ChunkInfoList key; S3ChunkInfoList list; for (iterator->SeekToFirst(); iterator->Valid(); iterator->Next()) { - std::string skey = iterator->Key(); - std::string svalue = iterator->Value(); - if (!conv_.ParseFromString(skey, &key)) { + absl::string_view skey = iterator->Key(); + if (!conv_.ParseFromStringView(skey, &key)) { return MetaStatusCode::PARSE_FROM_STRING_FAILED; } else if (!iterator->ParseFromValue(&list)) { return MetaStatusCode::PARSE_FROM_STRING_FAILED; @@ -541,7 +541,8 @@ MetaStatusCode InodeStorage::GetAllVolumeExtent(uint32_t fsId, continue; } - if (!slice->ParseFromString(iter->Value())) { + absl::string_view val = iter->Value(); + if (!slice->ParseFromArray(val.data(), val.size())) { LOG(ERROR) << "Parse ExtentSlice failed, fsId: " << fsId << ", inodeId: " << inodeId; extents->Clear(); diff --git a/curvefs/src/metaserver/metastore.cpp b/curvefs/src/metaserver/metastore.cpp index fdb8ff9131..f8386a5f8b 100644 --- a/curvefs/src/metaserver/metastore.cpp +++ b/curvefs/src/metaserver/metastore.cpp @@ -738,11 +738,18 @@ MetaStatusCode MetaStoreImpl::GetOrModifyS3ChunkInfo( void MetaStoreImpl::PrepareStreamBuffer(butil::IOBuf* buffer, uint64_t chunkIndex, - const std::string& value) { + absl::string_view value) { buffer->clear(); + /* buffer->append(std::to_string(chunkIndex)); buffer->append(":"); buffer->append(value); + */ + butil::IOBufAppender append_buffer; + append_buffer.append(std::to_string(chunkIndex)); + append_buffer.push_back(':'); + append_buffer.append(value.data(), value.size()); + append_buffer.move_to(*buffer); } MetaStatusCode MetaStoreImpl::SendS3ChunkInfoByStream( @@ -752,8 +759,8 @@ MetaStatusCode MetaStoreImpl::SendS3ChunkInfoByStream( Key4S3ChunkInfoList key; Converter conv; for (iterator->SeekToFirst(); iterator->Valid(); iterator->Next()) { - std::string skey = iterator->Key(); - if (!conv.ParseFromString(skey, &key)) { + absl::string_view skey = iterator->Key(); + if (!conv.ParseFromStringView(skey, &key)) { return MetaStatusCode::PARSE_FROM_STRING_FAILED; } diff --git a/curvefs/src/metaserver/metastore.h b/curvefs/src/metaserver/metastore.h index cc941c665b..026e07d3d2 100644 --- a/curvefs/src/metaserver/metastore.h +++ b/curvefs/src/metaserver/metastore.h @@ -30,6 +30,7 @@ #include #include +#include "absl/strings/string_view.h" #include "curvefs/proto/metaserver.pb.h" #include "curvefs/src/common/rpc_stream.h" #include "curvefs/src/metaserver/copyset/snapshot_closure.h" @@ -275,7 +276,7 @@ class MetaStoreImpl : public MetaStore { void PrepareStreamBuffer(butil::IOBuf* buffer, uint64_t chunkIndex, - const std::string& value); + absl::string_view value); void SaveBackground(const std::string& path, DumpFileClosure* child, diff --git a/curvefs/src/metaserver/metastore_fstream.cpp b/curvefs/src/metaserver/metastore_fstream.cpp index 7744eaae4c..e1f6f1727b 100644 --- a/curvefs/src/metaserver/metastore_fstream.cpp +++ b/curvefs/src/metaserver/metastore_fstream.cpp @@ -73,10 +73,10 @@ std::shared_ptr MetaStoreFStream::GetPartition( } bool MetaStoreFStream::LoadPartition(uint32_t partitionId, - const std::string& key, - const std::string& value) { + absl::string_view key, + absl::string_view value) { PartitionInfo partitionInfo; - if (!conv_->ParseFromString(value, &partitionInfo)) { + if (!conv_->ParseFromArray(value, &partitionInfo)) { LOG(ERROR) << "Decode PartitionInfo failed"; return false; } @@ -98,8 +98,8 @@ bool MetaStoreFStream::LoadPartition(uint32_t partitionId, } bool MetaStoreFStream::LoadInode(uint32_t partitionId, - const std::string& key, - const std::string& value) { + absl::string_view key, + absl::string_view value) { auto partition = GetPartition(partitionId); if (nullptr == partition) { LOG(ERROR) << "Partition not found, partitionId = " << partitionId; @@ -107,7 +107,7 @@ bool MetaStoreFStream::LoadInode(uint32_t partitionId, } Inode inode; - if (!conv_->ParseFromString(value, &inode)) { + if (!conv_->ParseFromArray(value, &inode)) { LOG(ERROR) << "Decode inode failed"; return false; } @@ -123,8 +123,8 @@ bool MetaStoreFStream::LoadInode(uint32_t partitionId, bool MetaStoreFStream::LoadDentry(uint8_t version, uint32_t partitionId, - const std::string& key, - const std::string& value) { + absl::string_view key, + absl::string_view value) { auto partition = GetPartition(partitionId); if (nullptr == partition) { LOG(ERROR) << "Partition not found, partitionId = " << partitionId; @@ -134,12 +134,12 @@ bool MetaStoreFStream::LoadDentry(uint8_t version, DentryVec vec; if (version == 1) { Dentry dentry; - if (!conv_->ParseFromString(value, &dentry)) { + if (!conv_->ParseFromArray(value, &dentry)) { LOG(ERROR) << "Decode dentry failed"; return false; } *vec.add_dentrys() = dentry; - } else if (!conv_->ParseFromString(value, &vec)) { + } else if (!conv_->ParseFromArray(value, &vec)) { LOG(ERROR) << "Decode dentry vector failed"; return false; } @@ -154,8 +154,8 @@ bool MetaStoreFStream::LoadDentry(uint8_t version, } bool MetaStoreFStream::LoadPendingTx(uint32_t partitionId, - const std::string& key, - const std::string& value) { + absl::string_view key, + absl::string_view value) { auto partition = GetPartition(partitionId); if (nullptr == partition) { LOG(ERROR) << "Partition not found, partitionId = " << partitionId; @@ -163,7 +163,7 @@ bool MetaStoreFStream::LoadPendingTx(uint32_t partitionId, } PrepareRenameTxRequest pendingTx; - if (!conv_->ParseFromString(value, &pendingTx)) { + if (!conv_->ParseFromArray(value, &pendingTx)) { LOG(ERROR) << "Decode pending tx failed"; return false; } @@ -176,8 +176,8 @@ bool MetaStoreFStream::LoadPendingTx(uint32_t partitionId, } bool MetaStoreFStream::LoadInodeS3ChunkInfoList(uint32_t partitionId, - const std::string& key, - const std::string& value) { + absl::string_view key, + absl::string_view value) { auto partition = GetPartition(partitionId); if (nullptr == partition) { LOG(ERROR) << "Partition not found, partitionId = " << partitionId; @@ -186,10 +186,10 @@ bool MetaStoreFStream::LoadInodeS3ChunkInfoList(uint32_t partitionId, S3ChunkInfoList list; Key4S3ChunkInfoList key4list; - if (!conv_->ParseFromString(key, &key4list)) { + if (!conv_->ParseFromStringView(key, &key4list)) { LOG(ERROR) << "Decode Key4S3ChunkInfoList failed"; return false; - } else if (!conv_->ParseFromString(value, &list)) { + } else if (!conv_->ParseFromArray(value, &list)) { LOG(ERROR) << "Decode S3ChunkInfoList failed"; return false; } @@ -209,8 +209,8 @@ bool MetaStoreFStream::LoadInodeS3ChunkInfoList(uint32_t partitionId, } bool MetaStoreFStream::LoadVolumeExtentList(uint32_t partitionId, - const std::string& key, - const std::string& value) { + absl::string_view key, + absl::string_view value) { auto partition = GetPartition(partitionId); if (!partition) { LOG(ERROR) << "Partition not found, partitionId: " << partitionId; @@ -220,13 +220,13 @@ bool MetaStoreFStream::LoadVolumeExtentList(uint32_t partitionId, Key4VolumeExtentSlice sliceKey; VolumeExtentSlice slice; - if (!sliceKey.ParseFromString(key)) { + if (!sliceKey.ParseFromStringView(key)) { LOG(ERROR) << "Fail to decode Key4VolumeExtentSlice, key: `" << key << "`"; return false; } - if (!conv_->ParseFromString(value, &slice)) { + if (!conv_->ParseFromArray(value, &slice)) { LOG(ERROR) << "Decode VolumeExtentSlice failed"; return false; } @@ -337,8 +337,8 @@ bool MetaStoreFStream::Load(const std::string& pathname, uint8_t* version) { auto callback = [&](uint8_t version, ENTRY_TYPE entryType, uint32_t partitionId, - const std::string& key, - const std::string& value) -> bool { + absl::string_view key, + absl::string_view value) -> bool { switch (entryType) { case ENTRY_TYPE::PARTITION: ++totalPartition; diff --git a/curvefs/src/metaserver/metastore_fstream.h b/curvefs/src/metaserver/metastore_fstream.h index fe124c97f1..46836536c5 100644 --- a/curvefs/src/metaserver/metastore_fstream.h +++ b/curvefs/src/metaserver/metastore_fstream.h @@ -58,29 +58,29 @@ class MetaStoreFStream { private: bool LoadPartition(uint32_t partitionId, - const std::string& key, - const std::string& value); + absl::string_view key, + absl::string_view value); bool LoadInode(uint32_t partitionId, - const std::string& key, - const std::string& value); + absl::string_view key, + absl::string_view value); bool LoadDentry(uint8_t version, uint32_t partitionId, - const std::string& key, - const std::string& value); + absl::string_view key, + absl::string_view value); bool LoadPendingTx(uint32_t partitionId, - const std::string& key, - const std::string& value); + absl::string_view key, + absl::string_view value); bool LoadInodeS3ChunkInfoList(uint32_t partitionId, - const std::string& key, - const std::string& value); + absl::string_view key, + absl::string_view value); bool LoadVolumeExtentList(uint32_t partitionId, - const std::string& key, - const std::string& value); + absl::string_view key, + absl::string_view value); std::shared_ptr NewPartitionIterator(); diff --git a/curvefs/src/metaserver/storage/converter.h b/curvefs/src/metaserver/storage/converter.h index 0042db91eb..c6ecbf06fb 100644 --- a/curvefs/src/metaserver/storage/converter.h +++ b/curvefs/src/metaserver/storage/converter.h @@ -28,6 +28,7 @@ #include #include +#include "absl/strings/string_view.h" #include "curvefs/src/metaserver/storage/common.h" namespace curvefs { @@ -81,6 +82,10 @@ class StorageKey { virtual std::string SerializeToString() const = 0; virtual bool ParseFromString(const std::string& value) = 0; + + virtual bool ParseFromStringView(absl::string_view value) { + return ParseFromString(std::string(value)); + } }; /* rules for key serialization: @@ -344,6 +349,22 @@ class Converter { bool ParseFromString(const std::string& value, Entry* entry) { return entry->ParseFromString(value); } + + template ::value || + std::is_base_of::value>::type> + bool ParseFromStringView(absl::string_view value, Entry* entry) { + return entry->ParseFromStringView(value); + } + + template ::value || + std::is_base_of::value>::type> + bool ParseFromArray(absl::string_view value, Entry* entry) { + return entry->ParseFromArray(value.data(), value.size()); + } }; } // namespace storage diff --git a/curvefs/src/metaserver/storage/dumpfile.cpp b/curvefs/src/metaserver/storage/dumpfile.cpp index 3465c1919e..bc0432379d 100644 --- a/curvefs/src/metaserver/storage/dumpfile.cpp +++ b/curvefs/src/metaserver/storage/dumpfile.cpp @@ -216,20 +216,20 @@ DUMPFILE_ERROR DumpFile::SaveInt(Int num, off_t* offset, uint32_t* checkSum) { return retCode; } -DUMPFILE_ERROR DumpFile::SaveString(const std::string& str, +DUMPFILE_ERROR DumpFile::SaveString(absl::string_view str, off_t* offset, uint32_t* checkSum) { size_t length = str.size(); - auto retCode = Write(str.c_str(), *offset, length); + auto retCode = Write(str.data(), *offset, length); if (retCode == DUMPFILE_ERROR::OK) { *offset = (*offset) + length; - *checkSum = CRC32(*checkSum, str.c_str(), length); + *checkSum = CRC32(*checkSum, str.data(), length); } return retCode; } -DUMPFILE_ERROR DumpFile::SaveEntry(const std::string& entry, +DUMPFILE_ERROR DumpFile::SaveEntry(absl::string_view entry, off_t* offset, uint32_t* checkSum) { auto retCode = SaveInt(entry.size(), offset, checkSum); @@ -613,11 +613,11 @@ void DumpFileIterator::Next() { iter_.second = value; } -std::string DumpFileIterator::Key() { +absl::string_view DumpFileIterator::Key() { return iter_.first; } -std::string DumpFileIterator::Value() { +absl::string_view DumpFileIterator::Value() { return iter_.second; } diff --git a/curvefs/src/metaserver/storage/dumpfile.h b/curvefs/src/metaserver/storage/dumpfile.h index 3bcc3104a3..1fc3f66fb0 100644 --- a/curvefs/src/metaserver/storage/dumpfile.h +++ b/curvefs/src/metaserver/storage/dumpfile.h @@ -29,6 +29,7 @@ #include #include +#include "absl/strings/string_view.h" #include "curvefs/src/metaserver/storage/iterator.h" namespace curve { @@ -174,11 +175,11 @@ class DumpFile { template DUMPFILE_ERROR SaveInt(Int num, off_t* offset, uint32_t* checkSum); - DUMPFILE_ERROR SaveString(const std::string& str, + DUMPFILE_ERROR SaveString(absl::string_view str, off_t* offset, uint32_t* checkSum); - DUMPFILE_ERROR SaveEntry(const std::string& entry, + DUMPFILE_ERROR SaveEntry(absl::string_view entry, off_t* offset, uint32_t* checkSum); @@ -246,9 +247,9 @@ class DumpFileIterator : public Iterator { void Next() override; - std::string Key() override; + absl::string_view Key() override; - std::string Value() override; + absl::string_view Value() override; bool ParseFromValue(ValueType* value) override; diff --git a/curvefs/src/metaserver/storage/iterator.h b/curvefs/src/metaserver/storage/iterator.h index 0c591cf2e6..62d08e7ab1 100644 --- a/curvefs/src/metaserver/storage/iterator.h +++ b/curvefs/src/metaserver/storage/iterator.h @@ -27,6 +27,7 @@ #include #include +#include "absl/strings/string_view.h" #include "curvefs/src/metaserver/storage/common.h" namespace curvefs { @@ -46,9 +47,9 @@ class Iterator { virtual void Next() = 0; - virtual std::string Key() = 0; + virtual absl::string_view Key() = 0; - virtual std::string Value() = 0; + virtual absl::string_view Value() = 0; virtual const ValueType* RawValue() const { return nullptr; } @@ -92,11 +93,11 @@ class MergeIterator : public Iterator { FindCurrent(); } - std::string Key() override { + absl::string_view Key() override { return current_->Key(); } - std::string Value() override { + absl::string_view Value() override { return current_->Value(); } @@ -147,11 +148,11 @@ class ContainerIterator : public Iterator { iter_++; } - std::string Key() override { + absl::string_view Key() override { return iter_->first; } - std::string Value() override { + absl::string_view Value() override { return iter_->second; } diff --git a/curvefs/src/metaserver/storage/memory_storage.h b/curvefs/src/metaserver/storage/memory_storage.h index f5c18a6474..1e5f671e2f 100644 --- a/curvefs/src/metaserver/storage/memory_storage.h +++ b/curvefs/src/metaserver/storage/memory_storage.h @@ -177,11 +177,11 @@ class MemoryStorageIterator : public Iterator { current_++; } - std::string Key() override { + absl::string_view Key() override { return current_->first; } - std::string Value() override { + absl::string_view Value() override { return ""; } @@ -203,6 +203,8 @@ class MemoryStorageIterator : public Iterator { template class UnorderedContainerIterator : public MemoryStorageIterator { +private: + std::string svalue_; public: using MemoryStorageIterator::MemoryStorageIterator; @@ -210,13 +212,13 @@ class UnorderedContainerIterator : public MemoryStorageIterator { this->current_ = this->container_->begin(); } - std::string Value() override { - std::string svalue; + absl::string_view Value() override { + svalue_.clear(); auto message = this->current_->second.Message(); - if (!message->SerializeToString(&svalue)) { + if (!message->SerializeToString(&svalue_)) { this->status_ = -1; } - return svalue; + return svalue_; } const ValueType* RawValue() const override { @@ -240,7 +242,7 @@ public MemoryStorageIterator { this->current_ = this->container_->begin(); } - std::string Value() override { + absl::string_view Value() override { return this->current_->second; } @@ -254,6 +256,8 @@ public MemoryStorageIterator { template class OrderedContainerIterator : public MemoryStorageIterator { + private: + std::string svalue_; public: using MemoryStorageIterator::MemoryStorageIterator; @@ -261,13 +265,13 @@ class OrderedContainerIterator : public MemoryStorageIterator { this->current_ = this->container_->lower_bound(this->prefix_); } - std::string Value() override { - std::string svalue; + absl::string_view Value() override { + svalue_.clear(); auto message = this->current_->second.Message(); - if (!message->SerializeToString(&svalue)) { + if (!message->SerializeToString(&svalue_)) { this->status_ = -1; } - return svalue; + return svalue_; } const ValueType* RawValue() const override { @@ -291,7 +295,7 @@ public MemoryStorageIterator { this->current_ = this->container_->lower_bound(this->prefix_); } - std::string Value() override { + absl::string_view Value() override { return this->current_->second; } diff --git a/curvefs/src/metaserver/storage/rocksdb_storage.cpp b/curvefs/src/metaserver/storage/rocksdb_storage.cpp index 1080b857a3..7386e9987a 100644 --- a/curvefs/src/metaserver/storage/rocksdb_storage.cpp +++ b/curvefs/src/metaserver/storage/rocksdb_storage.cpp @@ -194,8 +194,8 @@ std::string RocksDBStorage::ToInternalKey(const std::string& name, } // extract user key from internal key: prefix:key => key -std::string RocksDBStorage::ToUserKey(const std::string& ikey) { - return ikey.substr(GetKeyPrefixLength() + kDelimiter_.size()); +absl::string_view RocksDBStorage::ToUserKey(absl::string_view ikey_view) { + return ikey_view.substr(GetKeyPrefixLength() + kDelimiter_.size()); } Status RocksDBStorage::Get(const std::string& name, diff --git a/curvefs/src/metaserver/storage/rocksdb_storage.h b/curvefs/src/metaserver/storage/rocksdb_storage.h index 5ab36cc5a1..8465e3f49b 100644 --- a/curvefs/src/metaserver/storage/rocksdb_storage.h +++ b/curvefs/src/metaserver/storage/rocksdb_storage.h @@ -144,7 +144,7 @@ class RocksDBStorage : public KVStorage, public StorageTransaction { const std::string& key, bool ordered); - std::string ToUserKey(const std::string& ikey); + absl::string_view ToUserKey(absl::string_view ikey_view); Status Get(const std::string& name, const std::string& key, @@ -343,17 +343,17 @@ class RocksDBStorageIterator : public Iterator { iter_->Next(); } - std::string Key() { + absl::string_view Key() { RocksDBPerfGuard guard(OP_ITERATOR_GET_KEY); auto slice = iter_->key(); - auto ikey = std::string(slice.data(), slice.size()); - return storage_->ToUserKey(ikey); + absl::string_view ikey_view(slice.data(), slice.size()); + return storage_->ToUserKey(ikey_view); } - std::string Value() { + absl::string_view Value() { RocksDBPerfGuard guard(OP_ITERATOR_GET_VALUE); auto slice = iter_->value(); - return std::string(slice.data(), slice.size()); + return absl::string_view(slice.data(), slice.size()); } bool ParseFromValue(ValueType* value) override { diff --git a/curvefs/src/metaserver/storage/storage_fstream.h b/curvefs/src/metaserver/storage/storage_fstream.h index 1b2d3551b5..cdbe0ea565 100644 --- a/curvefs/src/metaserver/storage/storage_fstream.h +++ b/curvefs/src/metaserver/storage/storage_fstream.h @@ -30,6 +30,7 @@ #include "absl/cleanup/cleanup.h" #include "absl/strings/str_cat.h" +#include "absl/strings/str_split.h" #include "src/common/string_util.h" #include "curvefs/src/metaserver/storage/dumpfile.h" @@ -71,7 +72,7 @@ static std::string Type2Str(ENTRY_TYPE t) { return ""; } -static ENTRY_TYPE Str2Type(const std::string& s) { +static ENTRY_TYPE Str2Type(absl::string_view s) { for (const auto& pair : pairs) { if (pair.second == s) { return pair.first; @@ -82,14 +83,14 @@ static ENTRY_TYPE Str2Type(const std::string& s) { static std::string InternalKey(ENTRY_TYPE t, uint32_t partitionId, - const std::string& ukey) { + absl::string_view ukey) { return absl::StrCat(Type2Str(t), partitionId, ":", ukey); } -static std::pair UserKey(const std::string& ikey) { - std::string prefix, ukey; - std::vector items; - SplitString(ikey, ":", &items); +static std::pair UserKey(absl::string_view ikey) { + absl::string_view prefix, ukey; + std::vector items = absl::StrSplit(ikey, ":"); + //SplitString(ikey, ":", &items); if (items.size() >= 2) { prefix = items[0]; ukey = ikey.substr(prefix.size() + 1); @@ -99,19 +100,19 @@ static std::pair UserKey(const std::string& ikey) { return std::make_pair(prefix, ukey); } -static std::pair Extract(const std::string& prefix) { +static std::pair Extract(absl::string_view prefix) { if (prefix.size() == 0) { return std::make_pair(ENTRY_TYPE::UNKNOWN, 0); } - std::vector items{ + std::vector items{ prefix.substr(0, 1), // eg: i prefix.substr(1), // eg: 100 }; ENTRY_TYPE entryType = Str2Type(items[0]); uint32_t partitionId = 0; - if (!StringToUl(items[1], &partitionId)) { + if (!StringToUl(std::string(items[1]), &partitionId)) { partitionId = 0; } return std::make_pair(entryType, partitionId); @@ -148,8 +149,8 @@ template inline bool InvokeCallback(uint8_t version, ENTRY_TYPE entryType, uint32_t partitionId, - const std::string& key, - const std::string& value, + absl::string_view key, + absl::string_view value, Callback&& callback) { bool succ = std::forward(callback)( version, entryType, partitionId, key, value); @@ -185,8 +186,8 @@ inline bool LoadFromFile(const std::string& pathname, uint8_t* version, ENTRY_TYPE entryType = pair.first; uint32_t partitionId = pair.second; - std::string key = ukey.second; - std::string value = iter->Value(); + absl::string_view key = ukey.second; + absl::string_view value = iter->Value(); uint8_t version = dumpfile.GetVersion(); switch (entryType) { CASE_TYPE_CALLBACK(INODE); @@ -232,12 +233,13 @@ class IteratorWrapper : public Iterator { iterator_->Next(); } - std::string Key() override { + absl::string_view Key() override { auto key = iterator_->Key(); - return InternalKey(entryType_, partitionId_, key); + last_key_ = InternalKey(entryType_, partitionId_, key); + return last_key_; } - std::string Value() override { + absl::string_view Value() override { return iterator_->Value(); } @@ -253,6 +255,7 @@ class IteratorWrapper : public Iterator { ENTRY_TYPE entryType_; uint32_t partitionId_; std::shared_ptr iterator_; + std::string last_key_; }; } // namespace storage diff --git a/curvefs/test/metaserver/storage/dumpfile_test.cpp b/curvefs/test/metaserver/storage/dumpfile_test.cpp index 62d9170350..7bfcd020d1 100644 --- a/curvefs/test/metaserver/storage/dumpfile_test.cpp +++ b/curvefs/test/metaserver/storage/dumpfile_test.cpp @@ -47,8 +47,8 @@ class HashIterator : public Iterator { bool Valid() override { return iter_ != hash_->end(); } void SeekToFirst() override { iter_ = hash_->begin(); } void Next() override { iter_++; } - std::string Key() override { return iter_->first; } - std::string Value() override { return iter_->second; } + absl::string_view Key() override { return iter_->first; } + absl::string_view Value() override { return iter_->second; } int Status() override { return 0; } private: @@ -93,8 +93,8 @@ class DumpFileTest : public ::testing::Test { auto key = iter->Key(); auto value = iter->Value(); ASSERT_EQ(key, value); - ASSERT_TRUE(hash->find(key) != hash->end()); - hash->erase(key); + ASSERT_TRUE(hash->find(std::string(key)) != hash->end()); + hash->erase(std::string(key)); } ASSERT_EQ(hash->size(), 0); diff --git a/curvefs/test/metaserver/storage/iterator_test.cpp b/curvefs/test/metaserver/storage/iterator_test.cpp index 1e2a54a667..0da03835a7 100644 --- a/curvefs/test/metaserver/storage/iterator_test.cpp +++ b/curvefs/test/metaserver/storage/iterator_test.cpp @@ -49,8 +49,8 @@ class HashIterator : public Iterator { bool Valid() override { return iter_ != hash_->end(); } void SeekToFirst() override { iter_ = hash_->begin(); } void Next() override { iter_++; } - std::string Key() override { return iter_->first; } - std::string Value() override { return iter_->second; } + absl::string_view Key() override { return iter_->first; } + absl::string_view Value() override { return iter_->second; } bool ParseFromValue(ValueType* value) { return true; } int Status() override { return 0; } @@ -171,7 +171,7 @@ TEST_F(IteratorTest, MergeIterator) { for (auto& hash : hashs) { children.push_back(std::make_shared(&hash)); for (const auto& item : hash) { - except.push_back(std::make_pair(item.first, item.second)); + except.push_back(std::make_pair(std::string(item.first), std::string(item.second))); } } @@ -179,7 +179,7 @@ TEST_F(IteratorTest, MergeIterator) { for (iter.SeekToFirst(); iter.Valid(); iter.Next()) { auto key = iter.Key(); auto value = iter.Value(); - out.push_back(std::make_pair(key, value)); + out.push_back(std::make_pair(std::string(key), std::string(value))); } ASSERT_EQ(except, out); diff --git a/curvefs/test/metaserver/storage/storage_fstream_test.cpp b/curvefs/test/metaserver/storage/storage_fstream_test.cpp index cec9cd6c16..7d6c32965c 100644 --- a/curvefs/test/metaserver/storage/storage_fstream_test.cpp +++ b/curvefs/test/metaserver/storage/storage_fstream_test.cpp @@ -133,8 +133,8 @@ void StorageFstreamTest::TestSave(std::shared_ptr kvStorage, auto callback = [&](uint8_t version, ENTRY_TYPE type, uint32_t partitionId, - const std::string& key, - const std::string& value) { + absl::string_view key, + absl::string_view value) { if (version == 2) { nVersion++; } @@ -241,8 +241,8 @@ TEST_F(StorageFstreamTest, MiscTest) { auto callback = [&](uint8_t version, ENTRY_TYPE type, uint32_t partitionId, - const std::string& key, - const std::string& value) { + absl::string_view key, + absl::string_view value) { return true; }; bool succ = LoadFromFile("__not_found__", &dummyVersion, callback); @@ -254,8 +254,8 @@ TEST_F(StorageFstreamTest, MiscTest) { auto callback = [&](uint8_t version, ENTRY_TYPE type, uint32_t partitionId, - const std::string& key, - const std::string& value) { + absl::string_view key, + absl::string_view value) { return false; }; bool succ = LoadFromFile(pathname_, &dummyVersion, callback); diff --git a/src/common/BUILD b/src/common/BUILD index 5513d307cc..1ff48246f1 100644 --- a/src/common/BUILD +++ b/src/common/BUILD @@ -53,6 +53,7 @@ cc_library( "//external:glog", "//src/common/concurrent:curve_concurrent", ":macros", + "@com_google_absl//absl/strings", ], linkopts = [ "-luuid", diff --git a/src/common/string_util.h b/src/common/string_util.h index 9fce326cd0..f4ebcdc4f1 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -37,6 +37,8 @@ #include #include +#include "absl/strings/string_view.h" + namespace curve { namespace common { @@ -119,6 +121,11 @@ inline bool StringStartWith(const std::string& value, return value.rfind(starting, 0) == 0; } +inline bool StringStartWith(absl::string_view value, + absl::string_view starting) { + return value.rfind(starting, 0) == 0; +} + inline bool StringEndsWith(const std::string& value, const std::string& ending) { if (ending.size() > value.size())