mirror of
https://github.com/Tencent/MMKV.git
synced 2026-10-16 20:21:12 +08:00
@@ -53,5 +53,10 @@ set_target_properties(mmkv PROPERTIES
|
||||
target_link_libraries( # Specifies the target library.
|
||||
mmkv PRIVATE
|
||||
core
|
||||
pthread
|
||||
)
|
||||
|
||||
IF (NOT WIN32)
|
||||
target_link_libraries(mmkv
|
||||
PRIVATE
|
||||
pthread)
|
||||
ENDIF()
|
||||
|
||||
+13
-6
@@ -4,6 +4,7 @@
|
||||
import sys
|
||||
import mmkv
|
||||
import time
|
||||
import tempfile
|
||||
|
||||
|
||||
def functional_test(mmap_id, decode_only):
|
||||
@@ -58,7 +59,8 @@ def functional_test(mmap_id, decode_only):
|
||||
|
||||
|
||||
def test_backup():
|
||||
root_dir = "/tmp/mmkv_backup"
|
||||
temp_dir = tempfile.gettempdir()
|
||||
root_dir = temp_dir + "/mmkv_backup"
|
||||
mmap_id = "test_python"
|
||||
ret = mmkv.MMKV.backupOneToDirectory(mmap_id, root_dir)
|
||||
print("backup one return: ", ret)
|
||||
@@ -100,7 +102,8 @@ def test_expected_capacity():
|
||||
|
||||
|
||||
def test_restore():
|
||||
root_dir = "/tmp/mmkv_backup"
|
||||
temp_dir = tempfile.gettempdir()
|
||||
root_dir = temp_dir + "/mmkv_backup"
|
||||
mmap_id = "test/Encrypt"
|
||||
aes_key = "cryptKey"
|
||||
a_kv = mmkv.MMKV(mmap_id, mmkv.MMKVMode.SingleProcess, aes_key)
|
||||
@@ -161,7 +164,7 @@ def test_compare_before_set():
|
||||
actualSize2 = kv.actualSize()
|
||||
print("testCompareBeforeSet: actualSize2 = ", actualSize2)
|
||||
if actualSize1 != actualSize2:
|
||||
panic("size not match")
|
||||
raise("size not match")
|
||||
|
||||
kv.set(False, key)
|
||||
print("testCompareBeforeSet: bool value = ", kv.getBool(key))
|
||||
@@ -201,7 +204,8 @@ def test_remove_storage():
|
||||
if kv.count() != 0:
|
||||
print("storage not successfully remove")
|
||||
|
||||
rootDir = "/dev/mmkv_sg"
|
||||
temp_dir = tempfile.gettempdir()
|
||||
rootDir = temp_dir + "/dev/mmkv_sg"
|
||||
kv = mmkv.MMKV("test_remove/sg", rootDir = rootDir)
|
||||
kv.set(True, "bool")
|
||||
|
||||
@@ -230,9 +234,12 @@ def content_change_handler(mmap_id):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
temp_dir = tempfile.gettempdir()
|
||||
root_dir = temp_dir + '/mmkv'
|
||||
|
||||
# you can enable logging & log handler
|
||||
# mmkv.MMKV.initializeMMKV('/tmp/mmkv', mmkv.MMKVLogLevel.Info, logger)
|
||||
mmkv.MMKV.initializeMMKV('/tmp/mmkv')
|
||||
# mmkv.MMKV.initializeMMKV(root_dir, mmkv.MMKVLogLevel.Info, logger)
|
||||
mmkv.MMKV.initializeMMKV(root_dir)
|
||||
|
||||
# redirect logging
|
||||
# mmkv.MMKV.registerLogHandler(logger)
|
||||
|
||||
+23
-18
@@ -23,6 +23,11 @@
|
||||
#include <pybind11/pybind11.h>
|
||||
#include <pybind11/stl.h>
|
||||
|
||||
#ifdef MMKV_WIN32
|
||||
#include <basetsd.h>
|
||||
typedef SSIZE_T ssize_t;
|
||||
#endif
|
||||
|
||||
using namespace mmkv;
|
||||
using namespace std;
|
||||
namespace py = pybind11;
|
||||
@@ -99,10 +104,10 @@ PYBIND11_MODULE(mmkv, m) {
|
||||
// py::arg("cryptKey") = (string*) nullptr,
|
||||
// py::arg("rootDir") = (string*) nullptr);
|
||||
|
||||
clsMMKV.def(py::init([](const string &mmapID, MMKVMode mode, const string &cryptKey, const string &rootDir,
|
||||
clsMMKV.def(py::init([](const string &mmapID, MMKVMode mode, const string &cryptKey, const MMKVPath_t &rootDir,
|
||||
const size_t expectedCapacity) {
|
||||
string *cryptKeyPtr = (cryptKey.length() > 0) ? (string *) &cryptKey : nullptr;
|
||||
string *rootDirPtr = (rootDir.length() > 0) ? (string *) &rootDir : nullptr;
|
||||
MMKVPath_t *rootDirPtr = (rootDir.length() > 0) ? (MMKVPath_t *) &rootDir : nullptr;
|
||||
return MMKV::mmkvWithID(mmapID, mode, cryptKeyPtr, rootDirPtr, expectedCapacity);
|
||||
}),
|
||||
"Parameters:\n"
|
||||
@@ -118,7 +123,7 @@ PYBIND11_MODULE(mmkv, m) {
|
||||
|
||||
clsMMKV.def_static(
|
||||
"initializeMMKV",
|
||||
[](const string &rootDir, MMKVLogLevel logLevel, decltype(g_logHandler) logHandler) {
|
||||
[](const MMKVPath_t &rootDir, MMKVLogLevel logLevel, decltype(g_logHandler) logHandler) {
|
||||
if (logHandler) {
|
||||
g_logHandler = std::move(logHandler);
|
||||
MMKV::initializeMMKV(rootDir, logLevel, MyLogHandler);
|
||||
@@ -339,46 +344,46 @@ PYBIND11_MODULE(mmkv, m) {
|
||||
|
||||
clsMMKV.def_static(
|
||||
"backupOneToDirectory",
|
||||
[](const string &mmapID, const string &dstDir, const string &srcDir) {
|
||||
string *srcDirPtr = (srcDir.length() > 0) ? (string *) &srcDir : nullptr;
|
||||
[](const string &mmapID, const MMKVPath_t &dstDir, const MMKVPath_t &srcDir) {
|
||||
MMKVPath_t *srcDirPtr = (srcDir.length() > 0) ? (MMKVPath_t *) &srcDir : nullptr;
|
||||
return MMKV::backupOneToDirectory(mmapID, dstDir, srcDirPtr);
|
||||
},
|
||||
"backup one MMKV instance from srcDir (default to the root dir of MMKV) to dstDir", py::arg("mmapID"),
|
||||
py::arg("dstDir"), py::arg("srcDir") = string());
|
||||
py::arg("dstDir"), py::arg("srcDir") = MMKVPath_t());
|
||||
|
||||
clsMMKV.def_static(
|
||||
"restoreOneFromDirectory",
|
||||
[](const string &mmapID, const string &srcDir, const string &dstDir) {
|
||||
string *dstDirPtr = (dstDir.length() > 0) ? (string *) &dstDir : nullptr;
|
||||
[](const string &mmapID, const MMKVPath_t &srcDir, const MMKVPath_t &dstDir) {
|
||||
MMKVPath_t *dstDirPtr = (dstDir.length() > 0) ? (MMKVPath_t *) &dstDir : nullptr;
|
||||
return MMKV::restoreOneFromDirectory(mmapID, srcDir, dstDirPtr);
|
||||
},
|
||||
"restore one MMKV instance from srcDir to dstDir (default to the root dir of MMKV)", py::arg("mmapID"),
|
||||
py::arg("srcDir"), py::arg("dstDir") = string());
|
||||
py::arg("srcDir"), py::arg("dstDir") = MMKVPath_t());
|
||||
|
||||
clsMMKV.def_static(
|
||||
"backupAllToDirectory",
|
||||
[](const string &dstDir, const string &srcDir) {
|
||||
string *srcDirPtr = (srcDir.length() > 0) ? (string *) &srcDir : nullptr;
|
||||
[](const MMKVPath_t &dstDir, const MMKVPath_t &srcDir) {
|
||||
MMKVPath_t *srcDirPtr = (srcDir.length() > 0) ? (MMKVPath_t *) &srcDir : nullptr;
|
||||
return MMKV::backupAllToDirectory(dstDir, srcDirPtr);
|
||||
},
|
||||
"backup all MMKV instance from srcDir (default to the root dir of MMKV) to dstDir", py::arg("dstDir"),
|
||||
py::arg("srcDir") = string());
|
||||
py::arg("srcDir") = MMKVPath_t());
|
||||
|
||||
clsMMKV.def_static(
|
||||
"restoreAllFromDirectory",
|
||||
[](const string &srcDir, const string &dstDir) {
|
||||
string *dstDirPtr = (dstDir.length() > 0) ? (string *) &dstDir : nullptr;
|
||||
[](const MMKVPath_t &srcDir, const MMKVPath_t &dstDir) {
|
||||
MMKVPath_t *dstDirPtr = (dstDir.length() > 0) ? (MMKVPath_t *) &dstDir : nullptr;
|
||||
return MMKV::restoreAllFromDirectory(srcDir, dstDirPtr);
|
||||
},
|
||||
"restore all MMKV instance from srcDir to dstDir (default to the root dir of MMKV)", py::arg("srcDir"),
|
||||
py::arg("dstDir") = string());
|
||||
py::arg("dstDir") = MMKVPath_t());
|
||||
|
||||
clsMMKV.def_static(
|
||||
"removeStorage",
|
||||
[](const string &mmapID, const string &rootDir) {
|
||||
string *rootDirPtr = (rootDir.length() > 0) ? (string *) &rootDir : nullptr;
|
||||
[](const string &mmapID, const MMKVPath_t &rootDir) {
|
||||
MMKVPath_t *rootDirPtr = (rootDir.length() > 0) ? (MMKVPath_t *) &rootDir : nullptr;
|
||||
return MMKV::removeStorage(mmapID, rootDirPtr);
|
||||
},
|
||||
"remove the storage of the MMKV, including the data file & meta file (.crc)", py::arg("mmapID"),
|
||||
py::arg("rootDir") = string());
|
||||
py::arg("rootDir") = MMKVPath_t());
|
||||
}
|
||||
|
||||
+13
-14
@@ -2,9 +2,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
# import re
|
||||
import re
|
||||
import sys
|
||||
# import platform
|
||||
import platform
|
||||
import subprocess
|
||||
|
||||
from setuptools import setup, Extension
|
||||
@@ -26,10 +26,10 @@ class CMakeBuild(build_ext):
|
||||
raise RuntimeError("CMake must be installed to build the following extensions: " +
|
||||
", ".join(e.name for e in self.extensions))
|
||||
|
||||
# if platform.system() == "Windows":
|
||||
# cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1))
|
||||
# if cmake_version < '3.1.0':
|
||||
# raise RuntimeError("CMake >= 3.1.0 is required on Windows")
|
||||
if platform.system() == "Windows":
|
||||
cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1))
|
||||
if cmake_version < '3.1.0':
|
||||
raise RuntimeError("CMake >= 3.1.0 is required on Windows")
|
||||
|
||||
for ext in self.extensions:
|
||||
self.build_extension(ext)
|
||||
@@ -46,13 +46,12 @@ class CMakeBuild(build_ext):
|
||||
cfg = 'Debug' if self.debug else 'Release'
|
||||
build_args = ['--config', cfg]
|
||||
|
||||
# if platform.system() == "Windows":
|
||||
# cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]
|
||||
# if sys.maxsize > 2 ** 32:
|
||||
# cmake_args += ['-A', 'x64']
|
||||
# build_args += ['--', '/m']
|
||||
# else:
|
||||
if True:
|
||||
if platform.system() == "Windows":
|
||||
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]
|
||||
if sys.maxsize > 2 ** 32:
|
||||
cmake_args += ['-A', 'x64']
|
||||
build_args += ['--', '/m']
|
||||
else:
|
||||
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
|
||||
build_args += ['--', '-j8']
|
||||
|
||||
@@ -66,7 +65,7 @@ class CMakeBuild(build_ext):
|
||||
|
||||
setup(
|
||||
name='mmkv',
|
||||
version='1.2.13',
|
||||
version='1.3.6',
|
||||
author='Tencent',
|
||||
description='An efficient, small key-value storage framework developed by WeChat Team.',
|
||||
long_description='The MMKV, for Python. MMKV is an efficient, generic, easy-to-use key-value storage framework '
|
||||
|
||||
+4
-1
@@ -3,6 +3,7 @@
|
||||
|
||||
import sys
|
||||
import mmkv
|
||||
import tempfile
|
||||
|
||||
KeyNotExist = 'KeyNotExist'
|
||||
|
||||
@@ -193,7 +194,9 @@ def test_equal(kv, mmap_id):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
mmkv.MMKV.initializeMMKV('/tmp/mmkv')
|
||||
temp_dir = tempfile.gettempdir()
|
||||
root_dir = temp_dir + '/mmkv'
|
||||
mmkv.MMKV.initializeMMKV(root_dir)
|
||||
kv = mmkv.MMKV('unit_test_python')
|
||||
test_bool(kv)
|
||||
test_int(kv)
|
||||
|
||||
Reference in New Issue
Block a user