1 //  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 //
6 #pragma once
7 
8 #include <string>
9 
10 #include "rocksdb/rocksdb_namespace.h"
11 
12 #ifdef FAILED
13 #undef FAILED
14 #endif
15 
16 namespace ROCKSDB_NAMESPACE {
17 
18 class LDBCommandExecuteResult {
19  public:
20   enum State {
21     EXEC_NOT_STARTED = 0,
22     EXEC_SUCCEED = 1,
23     EXEC_FAILED = 2,
24   };
25 
LDBCommandExecuteResult()26   LDBCommandExecuteResult() : state_(EXEC_NOT_STARTED), message_("") {}
27 
LDBCommandExecuteResult(State state,std::string & msg)28   LDBCommandExecuteResult(State state, std::string& msg)
29       : state_(state), message_(msg) {}
30 
ToString()31   std::string ToString() {
32     std::string ret;
33     switch (state_) {
34       case EXEC_SUCCEED:
35         break;
36       case EXEC_FAILED:
37         ret.append("Failed: ");
38         break;
39       case EXEC_NOT_STARTED:
40         ret.append("Not started: ");
41     }
42     if (!message_.empty()) {
43       ret.append(message_);
44     }
45     return ret;
46   }
47 
Reset()48   void Reset() {
49     state_ = EXEC_NOT_STARTED;
50     message_ = "";
51   }
52 
IsSucceed()53   bool IsSucceed() { return state_ == EXEC_SUCCEED; }
54 
IsNotStarted()55   bool IsNotStarted() { return state_ == EXEC_NOT_STARTED; }
56 
IsFailed()57   bool IsFailed() { return state_ == EXEC_FAILED; }
58 
Succeed(std::string msg)59   static LDBCommandExecuteResult Succeed(std::string msg) {
60     return LDBCommandExecuteResult(EXEC_SUCCEED, msg);
61   }
62 
Failed(std::string msg)63   static LDBCommandExecuteResult Failed(std::string msg) {
64     return LDBCommandExecuteResult(EXEC_FAILED, msg);
65   }
66 
67  private:
68   State state_;
69   std::string message_;
70 
71   bool operator==(const LDBCommandExecuteResult&);
72   bool operator!=(const LDBCommandExecuteResult&);
73 };
74 
75 }  // namespace ROCKSDB_NAMESPACE
76