Skip to content

Commit

Permalink
MB-51719: Add missing formattors for enum classes
Browse files Browse the repository at this point in the history
Upgrading to fmtlib 8.1.1 removed support for implicitly converting
strongly-typed enums (enum class) to int when printing via fmtlib -
see fmtlib/fmt#1841

This is considered a bug by fmtlib, as strongly-typed enums should be
treated as such - to print them either provide a formatter, or cast to
their underlying type.

This highlighted that we had missed a number of operator<< overloads
for custom enum classes - and one instance where our operator<< was
not used as we were missing an include of <fmt/ostream.h>

Change-Id: If0f4e19f3eff4ebf4b4e3ccec1f0815c794a709b
Reviewed-on: https://review.couchbase.org/c/kv_engine/+/173823
Tested-by: Build Bot <[email protected]>
Reviewed-by: Trond Norbye <[email protected]>
  • Loading branch information
daverigby committed Apr 20, 2022
1 parent fd1d095 commit 86bd405
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
1 change: 1 addition & 0 deletions engines/ep/src/dcp/backfill_disk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "kv_bucket.h"
#include "kvstore/kvstore.h"
#include "vbucket.h"
#include <fmt/ostream.h>

CacheCallback::CacheCallback(KVBucket& bucket, std::shared_ptr<ActiveStream> s)
: bucket(bucket), streamPtr(s) {
Expand Down
52 changes: 39 additions & 13 deletions engines/ep/src/kvstore/kvstore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -487,32 +487,32 @@ void KVStore::delSystemEvent(TransactionContext& txnCtx,
del(txnCtx, item);
}

std::string to_string(FlushStateDeletion state) {
std::ostream& operator<<(std::ostream& os, const FlushStateDeletion& state) {
switch (state) {
case FlushStateDeletion::Delete:
return "FlushStateDeletion::Delete";
return os << "FlushStateDeletion::Delete";
case FlushStateDeletion::LogicallyDocNotFound:
return "FlushStateDeletion::LogicallyDocNotFound";
return os << "FlushStateDeletion::LogicallyDocNotFound";
case FlushStateDeletion::DocNotFound:
return "FlushStateDeletion::DocNotFound";
return os << "FlushStateDeletion::DocNotFound";
case FlushStateDeletion::Failed:
return "FlushStateDeletion::Failed";
return os << "FlushStateDeletion::Failed";
}
folly::assume_unreachable();
return os << "INVALID FlushStateDeletion value:" << static_cast<int>(state);
}

std::string to_string(FlushStateMutation state) {
std::ostream& operator<<(std::ostream& os, const FlushStateMutation& state) {
switch (state) {
case FlushStateMutation::Failed:
return "FlushStateMutation::Failed";
return os << "FlushStateMutation::Failed";
case FlushStateMutation::Insert:
return "FlushStateMutation::Insert";
return os << "FlushStateMutation::Insert";
case FlushStateMutation::LogicalInsert:
return "FlushStateMutation::LogicalInsert";
return os << "FlushStateMutation::LogicalInsert";
case FlushStateMutation::Update:
return "FlushStateMutation::Update";
return os << "FlushStateMutation::Update";
}
folly::assume_unreachable();
return os << "INVALID FlushStateMutation value:" << static_cast<int>(state);
}

IORequest::IORequest(queued_item itm)
Expand Down Expand Up @@ -724,4 +724,30 @@ std::string to_string(KVStoreIface::ReadVBStateStatus status) {
return "Error";
}
folly::assume_unreachable();
}
}

std::ostream& operator<<(std::ostream& os, const CompactDBStatus& status) {
switch (status) {
case CompactDBStatus::Success:
return os << "CompactDBStatus::Success";
case CompactDBStatus::Aborted:
return os << "CompactDBStatus::Aborted";
case CompactDBStatus::Failed:
return os << "CompactDBStatus::Failed";
}
return os << "INVALID CompactDBStatus value:" << static_cast<int>(status);
}

std::ostream& operator<<(std::ostream& os,
const KVStoreIface::GetCollectionStatsStatus& status) {
switch (status) {
case KVStoreIface::GetCollectionStatsStatus::Success:
return os << "GetCollectionStatsStatus::Success";
case KVStoreIface::GetCollectionStatsStatus::NotFound:
return os << "GetCollectionStatsStatus::NotFound";
case KVStoreIface::GetCollectionStatsStatus::Failed:
return os << "GetCollectionStatsStatus::Failed";
}
return os << "INVALID GetCollectionStatsStatus value:"
<< static_cast<int>(status);
}
5 changes: 1 addition & 4 deletions engines/ep/src/kvstore/kvstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -1025,9 +1025,6 @@ class KVStore : public KVStoreIface {
TestingHook<> saveDocsPostWriteDocsHook;
};

std::string to_string(FlushStateDeletion status);
std::string to_string(FlushStateMutation state);

/**
* The KVStoreFactory creates the correct KVStore instance(s) when
* needed by EPStore.
Expand Down Expand Up @@ -1064,4 +1061,4 @@ class RollbackCB : public StatusCallback<GetValue> {
};

std::ostream& operator<<(std::ostream& os, const ValueFilter& vf);
std::ostream& operator<<(std::ostream& os, const DocumentFilter& df);
std::ostream& operator<<(std::ostream& os, const DocumentFilter& df);
4 changes: 4 additions & 0 deletions engines/ep/src/kvstore/kvstore_fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ enum class FlushStateDeletion {
Failed
};

std::ostream& operator<<(std::ostream& os, const FlushStateDeletion&);

/// Result of flushing a Mutation, passed to the PersistenceCallback.
enum class FlushStateMutation {
// An item was inserted (item did not exist before or was previously
Expand All @@ -56,3 +58,5 @@ enum class FlushStateMutation {
// The persistence of the mutation failed
Failed
};

std::ostream& operator<<(std::ostream& os, const FlushStateMutation&);
5 changes: 5 additions & 0 deletions engines/ep/src/kvstore/kvstore_iface.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class KVStoreRevision {
*/
enum class CompactDBStatus { Success, Aborted, Failed };

std::ostream& operator<<(std::ostream&, const CompactDBStatus&);

/**
* Functional interface of a KVStore. Each KVStore implementation must implement
* each of these functions.
Expand Down Expand Up @@ -759,3 +761,6 @@ class KVStoreIface {
};

std::string to_string(KVStoreIface::ReadVBStateStatus status);

std::ostream& operator<<(std::ostream&,
const KVStoreIface::GetCollectionStatsStatus&);
4 changes: 2 additions & 2 deletions engines/ep/src/kvstore/magma-kvstore/magma-kvstore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ void MagmaKVStore::commitCallback(MagmaKVStoreTransactionContext& txnCtx,
txnCtx.vbid,
cb::UserData(req.getKey().to_string()),
errCode,
to_string(state));
state);
}

txnCtx.deleteCallback(req.getItem(), state);
Expand Down Expand Up @@ -848,7 +848,7 @@ void MagmaKVStore::commitCallback(MagmaKVStoreTransactionContext& txnCtx,
txnCtx.vbid,
cb::UserData(req.getKey().to_string()),
errCode,
to_string(state));
state);
}

txnCtx.setCallback(req.getItem(), state);
Expand Down

0 comments on commit 86bd405

Please sign in to comment.