MMKV_IO.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2020 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #ifndef MMKV_IO_h
  21. #define MMKV_IO_h
  22. #ifdef __cplusplus
  23. #include "MMKV.h"
  24. MMKV_NAMESPACE_BEGIN
  25. std::string mmapedKVKey(const std::string &mmapID, const MMKVPath_t *rootPath = nullptr);
  26. std::string legacyMmapedKVKey(const std::string &mmapID, const MMKVPath_t *rootPath = nullptr);
  27. #ifndef MMKV_ANDROID
  28. MMKVPath_t mappedKVPathWithID(const std::string &mmapID, const MMKVPath_t *rootPath);
  29. #else
  30. MMKVPath_t mappedKVPathWithID(const std::string &mmapID, const MMKVPath_t *rootPath, MMKVMode mode = MMKV_MULTI_PROCESS);
  31. #endif
  32. MMKVPath_t crcPathWithPath(const MMKVPath_t &kvPath);
  33. MMKVRecoverStrategic onMMKVCRCCheckFail(const std::string &mmapID);
  34. MMKVRecoverStrategic onMMKVFileLengthError(const std::string &mmapID);
  35. template <typename T>
  36. void clearDictionary(T *dic) {
  37. if (!dic) {
  38. return;
  39. }
  40. #ifdef MMKV_APPLE
  41. for (auto &pair : *dic) {
  42. [pair.first release];
  43. }
  44. #endif
  45. dic->clear();
  46. }
  47. enum : bool {
  48. KeepSequence = false,
  49. IncreaseSequence = true,
  50. };
  51. #ifdef MMKV_ANDROID
  52. // status of migrating old file to new file
  53. enum class MigrateStatus: uint32_t {
  54. NotSpecial, // it's not specially (mistakenly) encoded
  55. NoneExist, // none of these files exist
  56. NewExist, // only new file exist
  57. OldToNewMigrated, // migrated, it's one time only operation
  58. OldToNewMigrateFail, // old file exist but fail to migrate (maybe other process opened)
  59. OldAndNewExist, // both old and new exist (fail to delete old file? old file migrated from old device?)
  60. };
  61. // historically Android mistakenly use mmapKey as mmapID, we try migrate back to normal when possible
  62. MigrateStatus tryMigrateLegacyMMKVFile(const std::string &mmapID, const MMKVPath_t *rootPath);
  63. #endif // MMKV_ANDROID
  64. MMKV_NAMESPACE_END
  65. #endif
  66. #endif /* MMKV_IO_h */