Skip to content

Commit 4efe8f5

Browse files
mag1c-hFangRun2
andauthored
[opt] Share Infra implementation and unify status codes (#399)
share infra module Co-authored-by: Fang Run <Fang_Run@126.com>
1 parent 5779ce9 commit 4efe8f5

File tree

23 files changed

+123
-208
lines changed

23 files changed

+123
-208
lines changed

ucm/shared/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
add_subdirectory(vendor)
2+
add_subdirectory(infra)
23
add_subdirectory(trans)
34
add_subdirectory(test)

ucm/shared/infra/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
file(GLOB_RECURSE UCMINFRA_STATUS_SOURCE_FILES "status/*.*")
2+
add_library(infra_status OBJECT ${UCMINFRA_STATUS_SOURCE_FILES})
3+
target_include_directories(infra_status PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
4+
target_link_libraries(infra_status PUBLIC fmt)
5+
6+
file(GLOB UCMINFRA_LOGGER_SOURCE_FILES "logger/*.*")
7+
file(GLOB_RECURSE UCMINFRA_LOGGER_DETAIL_SOURCE_FILES "logger/${LOGGER_BACKEND}/*.cc")
8+
add_library(infra_logger OBJECT ${UCMINFRA_LOGGER_SOURCE_FILES} ${UCMINFRA_LOGGER_DETAIL_SOURCE_FILES})
9+
target_include_directories(infra_logger PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
10+
target_link_libraries(infra_logger PUBLIC fmt spdlog)
11+
12+
file(GLOB_RECURSE UCMINFRA_TEMPLATE_SOURCE_FILES "template/*.*")
13+
add_library(infra_template OBJECT ${UCMINFRA_TEMPLATE_SOURCE_FILES})
14+
target_include_directories(infra_template PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
15+
16+
file(GLOB_RECURSE UCMINFRA_THREAD_SOURCE_FILES "thread/*.*")
17+
add_library(infra_thread OBJECT ${UCMINFRA_THREAD_SOURCE_FILES})
18+
target_include_directories(infra_thread PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
19+
20+
file(GLOB_RECURSE UCMINFRA_TIME_SOURCE_FILES "time/*.*")
21+
add_library(infra_time OBJECT ${UCMINFRA_TIME_SOURCE_FILES})
22+
target_include_directories(infra_time PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
File renamed without changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
* */
24-
#ifndef UNIFIEDCACHE_LOGGER_H
25-
#define UNIFIEDCACHE_LOGGER_H
24+
#ifndef UNIFIEDCACHE_INFRA_LOGGER_H
25+
#define UNIFIEDCACHE_INFRA_LOGGER_H
2626

2727
#include <cstddef>
2828
#include <fmt/format.h>
File renamed without changes.
Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,70 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
* */
24-
#ifndef UNIFIEDCACHE_TRANS_STATUS_H
25-
#define UNIFIEDCACHE_TRANS_STATUS_H
24+
#ifndef UNIFIEDCACHE_INFRA_STATUS_H
25+
#define UNIFIEDCACHE_INFRA_STATUS_H
2626

27+
#include <cstdint>
2728
#include <fmt/format.h>
2829
#include <string>
2930

30-
namespace UC::Trans {
31+
namespace UC {
32+
33+
template <int32_t i>
34+
static inline constexpr int32_t __MakeStatusCode()
35+
{
36+
return -50000 - i;
37+
}
3138

3239
class Status {
3340
static constexpr int32_t OK_ = 0;
3441
static constexpr int32_t ERROR_ = -1;
42+
static constexpr int32_t EPARAM_ = __MakeStatusCode<0>();
43+
static constexpr int32_t EOOM_ = __MakeStatusCode<1>();
44+
static constexpr int32_t EOSERROR_ = __MakeStatusCode<2>();
45+
static constexpr int32_t EDUPLICATE_ = __MakeStatusCode<3>();
46+
static constexpr int32_t ERETRY_ = __MakeStatusCode<4>();
47+
static constexpr int32_t ENOOBJ_ = __MakeStatusCode<5>();
48+
static constexpr int32_t ESERIALIZE_ = __MakeStatusCode<6>();
49+
static constexpr int32_t EDESERIALIZE_ = __MakeStatusCode<7>();
50+
static constexpr int32_t EUNSUPPORTED_ = __MakeStatusCode<8>();
51+
static constexpr int32_t ENOSPACE_ = __MakeStatusCode<9>();
3552
int32_t code_;
3653
std::string message_;
3754
explicit Status(int32_t code) : code_(code) {}
3855

3956
public:
4057
bool operator==(const Status& other) const noexcept { return code_ == other.code_; }
4158
bool operator!=(const Status& other) const noexcept { return !(*this == other); }
42-
std::string ToString() const { return fmt::format("({}) {}", code_, message_); }
59+
int32_t Underlying() const { return code_; }
60+
std::string ToString() const
61+
{
62+
auto str = std::to_string(code_);
63+
if (message_.empty()) { return str; }
64+
return fmt::format("{}, {}", str, message_);
65+
}
4366
constexpr bool Success() const noexcept { return code_ == OK_; }
4467
constexpr bool Failure() const noexcept { return !Success(); }
4568

4669
public:
4770
Status(int32_t code, std::string message) : code_{code}, message_{std::move(message)} {}
4871
static Status OK() { return Status{OK_}; }
4972
static Status Error(std::string message) { return {ERROR_, std::move(message)}; }
73+
static Status Error() { return Status{ERROR_}; }
74+
static Status InvalidParam() { return Status{EPARAM_}; }
75+
static Status OutOfMemory() { return Status{EOOM_}; }
76+
static Status OsApiError() { return Status{EOSERROR_}; }
77+
static Status DuplicateKey() { return Status{EDUPLICATE_}; }
78+
static Status Retry() { return Status{ERETRY_}; }
79+
static Status NotFound() { return Status{ENOOBJ_}; }
80+
static Status SerializeFailed() { return Status{ESERIALIZE_}; }
81+
static Status DeserializeFailed() { return Status{EDESERIALIZE_}; }
82+
static Status Unsupported() { return Status{EUNSUPPORTED_}; }
83+
static Status NoSpace() { return Status{ENOSPACE_}; }
5084
};
5185

52-
} // namespace UC::Trans
86+
inline std::string format_as(const Status& status) { return status.ToString(); }
87+
88+
} // namespace UC
5389

5490
#endif
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
* */
24-
#ifndef UNIFIEDCACHE_HASHSET_H
25-
#define UNIFIEDCACHE_HASHSET_H
24+
#ifndef UNIFIEDCACHE_INFRA_HASHSET_H
25+
#define UNIFIEDCACHE_INFRA_HASHSET_H
2626

2727
#include <array>
2828
#include <atomic>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
* */
24-
#ifndef UNIFIEDCACHE_SINGLETON_H
25-
#define UNIFIEDCACHE_SINGLETON_H
24+
#ifndef UNIFIEDCACHE_INFRA_SINGLETON_H
25+
#define UNIFIEDCACHE_INFRA_SINGLETON_H
2626

2727
namespace UC {
2828

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,61 +21,58 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
* */
24-
#ifndef UNIFIEDCACHE_TIMER_H
25-
#define UNIFIEDCACHE_TIMER_H
24+
#ifndef UNIFIEDCACHE_INFRA_TIMER_H
25+
#define UNIFIEDCACHE_INFRA_TIMER_H
2626

27-
#include <chrono>
28-
#include <thread>
29-
#include <mutex>
3027
#include <atomic>
28+
#include <chrono>
3129
#include <condition_variable>
32-
#include "logger/logger.h"
33-
#include "status/status.h"
30+
#include <mutex>
31+
#include <thread>
3432

3533
namespace UC {
3634

3735
template <typename Callable>
3836
class Timer {
3937
public:
4038
Timer(const std::chrono::seconds& interval, Callable&& callable)
41-
: interval_(interval), callable_(callable), running_(false) {}
42-
~Timer() {
39+
: interval_(interval), callable_(callable), running_(false)
40+
{
41+
}
42+
~Timer()
43+
{
4344
{
4445
std::lock_guard<std::mutex> lg(this->mutex_);
4546
this->running_ = false;
47+
this->cv_.notify_one();
4648
}
47-
48-
this->cv_.notify_one();
4949
if (this->thread_.joinable()) { this->thread_.join(); }
5050
}
51-
Status Start()
51+
bool Start()
5252
{
5353
{
5454
std::lock_guard<std::mutex> lg(this->mutex_);
55-
if (this->running_) { return Status::OK(); }
55+
if (this->running_) { return true; }
5656
}
5757
try {
5858
this->running_ = true;
5959
this->thread_ = std::thread(&Timer::Runner, this);
60-
return Status::OK();
61-
} catch (const std::exception& e) {
62-
UC_ERROR("Failed({}) to start timer.", e.what());
63-
return Status::OutOfMemory();
60+
return true;
61+
} catch (...) {
62+
return false;
6463
}
6564
}
6665

6766
private:
6867
void Runner()
6968
{
7069
while (this->running_) {
71-
try {
72-
{
73-
std::unique_lock<std::mutex> lg(this->mutex_);
74-
this->cv_.wait_for(lg, this->interval_, [this] { return !this->running_; });
75-
if (!this->running_) { break; }
76-
}
77-
this->callable_();
78-
} catch (const std::exception& e) { UC_ERROR("Failed({}) to run timer.", e.what()); }
70+
{
71+
std::unique_lock<std::mutex> lg(this->mutex_);
72+
this->cv_.wait_for(lg, this->interval_, [this] { return !this->running_; });
73+
if (!this->running_) { break; }
74+
}
75+
this->callable_();
7976
}
8077
}
8178

@@ -88,6 +85,6 @@ class Timer {
8885
std::atomic<bool> running_;
8986
};
9087

91-
} // namespace UC
88+
} // namespace UC
9289

93-
#endif
90+
#endif
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
* SOFTWARE.
2323
* */
2424

25-
#ifndef UNIFIEDCACHE_TOP_N_HEAP_H
26-
#define UNIFIEDCACHE_TOP_N_HEAP_H
25+
#ifndef UNIFIEDCACHE_INFRA_TOP_N_HEAP_H
26+
#define UNIFIEDCACHE_INFRA_TOP_N_HEAP_H
2727

2828
#include <algorithm>
2929
#include <cstdint>
30+
#include <functional>
3031

3132
namespace UC {
3233

@@ -117,4 +118,4 @@ class TopNFixedHeap : public TopNHeap<T, Compare> {
117118

118119
} // namespace UC
119120

120-
#endif
121+
#endif

0 commit comments

Comments
 (0)