Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create MMKV::instanceExists(..) #1347

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Core/MMKV.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,9 @@ class MMKV {
// Note: the existing instance (if any) will be closed & destroyed
static bool removeStorage(const std::string &mmapID, MMKVPath_t *relatePath = nullptr);

// check whether an instance with the given key exists or not
static bool instanceExists(const std::string &mmapID, MMKVPath_t *relatePath = nullptr);

// just forbid it for possibly misuse
explicit MMKV(const MMKV &other) = delete;
MMKV &operator=(const MMKV &other) = delete;
Expand Down
24 changes: 24 additions & 0 deletions Core/MMKV_IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,30 @@ bool MMKV::isFileValid(const string &mmapID, MMKVPath_t *relatePath) {
}
}

bool MMKV::instanceExists(const std::string &mmapID, MMKVPath_t *relatePath = nullptr) {
auto mmapKey = mmapedKVKey(mmapID, relatePath);
#ifdef MMKV_ANDROID
auto &realID = mmapKey; // historically Android mistakenly use mmapKey as mmapID
#else
auto &realID = mmapID;
#endif
MMKVDebug("mmapKey %s", mmapKey.c_str());

MMKVPath_t kvPath = mappedKVPathWithID(realID, MMKV_SINGLE_PROCESS, relatePath);
if (isFileExist(kvPath)) {
MMKVDebug("file exist (unencrypted) %s", kvPath.c_str());
return true;
}
MMKVPath_t crcPath = crcPathWithID(realID, MMKV_SINGLE_PROCESS, relatePath);
if (isFileExist(crcPath)) {
MMKVDebug("file exist (encrypted) %s", crcPath.c_str());
return true;
}

MMKVWarning("file does not exist %s", crcPath.c_str());
return false;
}

bool MMKV::removeStorage(const std::string &mmapID, MMKVPath_t *relatePath) {
auto mmapKey = mmapedKVKey(mmapID, relatePath);
#ifdef MMKV_ANDROID
Expand Down