提交 853d8a3e 编写于 作者: L liuqi

Replace KVStorageStatus with int.

上级 91f2b780
...@@ -49,25 +49,29 @@ std::unique_ptr<KVStorage> FileStorageFactory::CreateStorage( ...@@ -49,25 +49,29 @@ std::unique_ptr<KVStorage> FileStorageFactory::CreateStorage(
FileStorage::FileStorage(const std::string &file_path): FileStorage::FileStorage(const std::string &file_path):
file_path_(file_path) {} file_path_(file_path) {}
KVStorageStatus FileStorage::Load() { int FileStorage::Load() {
struct stat st; struct stat st;
if (stat(file_path_.c_str(), &st) == -1) { if (stat(file_path_.c_str(), &st) == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
return KVStorageStatus::STORAGE_FILE_NOT_EXIST; LOG(INFO) << "File " << file_path_
<< " does not exist";
return 0;
} else { } else {
LOG(WARNING) << "stat file " << file_path_ LOG(WARNING) << "Stat file " << file_path_
<< " failed, error code: " << errno; << " failed, error code: " << errno;
return KVStorageStatus::STORAGE_ERROR; return -1;
} }
} }
int fd = open(file_path_.c_str(), O_RDONLY); int fd = open(file_path_.c_str(), O_RDONLY);
if (fd < 0) { if (fd < 0) {
if (errno == ENOENT) { if (errno == ENOENT) {
return KVStorageStatus::STORAGE_FILE_NOT_EXIST; LOG(INFO) << "File " << file_path_
<< " does not exist";
return 0;
} else { } else {
LOG(WARNING) << "open file " << file_path_ LOG(WARNING) << "open file " << file_path_
<< " failed, error code: " << errno; << " failed, error code: " << errno;
return KVStorageStatus::STORAGE_ERROR; return -1;
} }
} }
size_t file_size = st.st_size; size_t file_size = st.st_size;
...@@ -84,7 +88,7 @@ KVStorageStatus FileStorage::Load() { ...@@ -84,7 +88,7 @@ KVStorageStatus FileStorage::Load() {
LOG(WARNING) << "close file " << file_path_ LOG(WARNING) << "close file " << file_path_
<< " failed, error code: " << errno; << " failed, error code: " << errno;
} }
return KVStorageStatus::STORAGE_ERROR; return -1;
} }
unsigned char *file_data_ptr = file_data; unsigned char *file_data_ptr = file_data;
...@@ -121,15 +125,15 @@ KVStorageStatus FileStorage::Load() { ...@@ -121,15 +125,15 @@ KVStorageStatus FileStorage::Load() {
LOG(WARNING) << "close file " << file_path_ LOG(WARNING) << "close file " << file_path_
<< " failed, error code: " << errno; << " failed, error code: " << errno;
} }
return KVStorageStatus::STORAGE_ERROR; return -1;
} }
res = close(fd); res = close(fd);
if (res != 0) { if (res != 0) {
LOG(WARNING) << "close file " << file_path_ LOG(WARNING) << "close file " << file_path_
<< " failed, error code: " << errno; << " failed, error code: " << errno;
return KVStorageStatus::STORAGE_ERROR; return -1;
} }
return KVStorageStatus::STORAGE_SUCCESS; return 0;
} }
bool FileStorage::Insert(const std::string &key, bool FileStorage::Insert(const std::string &key,
...@@ -145,12 +149,12 @@ const std::vector<unsigned char> *FileStorage::Find(const std::string &key) { ...@@ -145,12 +149,12 @@ const std::vector<unsigned char> *FileStorage::Find(const std::string &key) {
return &(iter->second); return &(iter->second);
} }
KVStorageStatus FileStorage::Flush() { int FileStorage::Flush() {
int fd = open(file_path_.c_str(), O_WRONLY | O_CREAT, 0600); int fd = open(file_path_.c_str(), O_WRONLY | O_CREAT, 0600);
if (fd < 0) { if (fd < 0) {
LOG(WARNING) << "open file " << file_path_ LOG(WARNING) << "open file " << file_path_
<< " failed, error code:" << errno; << " failed, error code:" << errno;
return KVStorageStatus::STORAGE_ERROR; return -1;
} }
const size_t int_size = sizeof(int32_t); const size_t int_size = sizeof(int32_t);
...@@ -194,7 +198,7 @@ KVStorageStatus FileStorage::Flush() { ...@@ -194,7 +198,7 @@ KVStorageStatus FileStorage::Flush() {
LOG(WARNING) << "close file " << file_path_ LOG(WARNING) << "close file " << file_path_
<< " failed, error code: " << errno; << " failed, error code: " << errno;
} }
return KVStorageStatus::STORAGE_ERROR; return -1;
} }
remain_size -= buffer_size; remain_size -= buffer_size;
buffer_ptr += buffer_size; buffer_ptr += buffer_size;
...@@ -204,9 +208,9 @@ KVStorageStatus FileStorage::Flush() { ...@@ -204,9 +208,9 @@ KVStorageStatus FileStorage::Flush() {
if (res != 0) { if (res != 0) {
LOG(WARNING) << "close file " << file_path_ LOG(WARNING) << "close file " << file_path_
<< " failed, error code: " << errno; << " failed, error code: " << errno;
return KVStorageStatus::STORAGE_ERROR; return -1;
} }
return KVStorageStatus::STORAGE_SUCCESS; return 0;
} }
}; // namespace mace }; // namespace mace
...@@ -18,11 +18,11 @@ class FileStorage : public KVStorage { ...@@ -18,11 +18,11 @@ class FileStorage : public KVStorage {
explicit FileStorage(const std::string &file_path); explicit FileStorage(const std::string &file_path);
public: public:
KVStorageStatus Load() override; int Load() override;
bool Insert(const std::string &key, bool Insert(const std::string &key,
const std::vector<unsigned char> &value) override; const std::vector<unsigned char> &value) override;
const std::vector<unsigned char> *Find(const std::string &key) override; const std::vector<unsigned char> *Find(const std::string &key) override;
KVStorageStatus Flush() override; int Flush() override;
private: private:
std::string file_path_; std::string file_path_;
......
...@@ -317,7 +317,7 @@ OpenCLRuntime::OpenCLRuntime(GPUPerfHint gpu_perf_hint, ...@@ -317,7 +317,7 @@ OpenCLRuntime::OpenCLRuntime(GPUPerfHint gpu_perf_hint,
storage_ = kStorageFactory->CreateStorage(cl_compiled_file_name); storage_ = kStorageFactory->CreateStorage(cl_compiled_file_name);
if (platform_info_ != kCompiledProgramPlatform) { if (platform_info_ != kCompiledProgramPlatform) {
if (storage_->Load() == KVStorageStatus::STORAGE_ERROR) { if (storage_->Load() != 0) {
LOG(FATAL) << "Load opencl compiled kernel file failed"; LOG(FATAL) << "Load opencl compiled kernel file failed";
} }
} }
...@@ -506,7 +506,7 @@ cl::Kernel OpenCLRuntime::BuildKernel( ...@@ -506,7 +506,7 @@ cl::Kernel OpenCLRuntime::BuildKernel(
void OpenCLRuntime::SaveBuiltCLProgram() { void OpenCLRuntime::SaveBuiltCLProgram() {
if (program_map_changed_ && storage_ != nullptr) { if (program_map_changed_ && storage_ != nullptr) {
if (storage_->Flush() != KVStorageStatus::STORAGE_SUCCESS) { if (storage_->Flush() != 0) {
LOG(FATAL) << "Store opencl compiled kernel to file failed"; LOG(FATAL) << "Store opencl compiled kernel to file failed";
} }
program_map_changed_ = false; program_map_changed_ = false;
......
...@@ -31,19 +31,15 @@ enum GPUPriorityHint { ...@@ -31,19 +31,15 @@ enum GPUPriorityHint {
enum CPUPowerOption { DEFAULT = 0, HIGH_PERFORMANCE = 1, BATTERY_SAVE = 2 }; enum CPUPowerOption { DEFAULT = 0, HIGH_PERFORMANCE = 1, BATTERY_SAVE = 2 };
enum KVStorageStatus {
STORAGE_SUCCESS = 0,
STORAGE_FILE_NOT_EXIST = 1,
STORAGE_ERROR = 2
};
class KVStorage { class KVStorage {
public: public:
virtual KVStorageStatus Load() = 0; // return: 0 for success, -1 for error
virtual int Load() = 0;
virtual bool Insert(const std::string &key, virtual bool Insert(const std::string &key,
const std::vector<unsigned char> &value) = 0; const std::vector<unsigned char> &value) = 0;
virtual const std::vector<unsigned char> *Find(const std::string &key) = 0; virtual const std::vector<unsigned char> *Find(const std::string &key) = 0;
virtual KVStorageStatus Flush() = 0; // return: 0 for success, -1 for error
virtual int Flush() = 0;
}; };
class KVStorageFactory { class KVStorageFactory {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册