Skip to content

Commit

Permalink
visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
lingol committed Dec 21, 2018
1 parent a40911e commit b4bca7c
Show file tree
Hide file tree
Showing 41 changed files with 243 additions and 267 deletions.
4 changes: 4 additions & 0 deletions Win32/MMKV/CodedInputData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

using namespace std;

namespace mmkv {

CodedInputData::CodedInputData(const void *oData, int32_t length)
: m_ptr((uint8_t *) oData), m_size(length), m_position(0) {
assert(m_ptr);
Expand Down Expand Up @@ -176,3 +178,5 @@ int8_t CodedInputData::readRawByte() {
int8_t *bytes = (int8_t *) m_ptr;
return bytes[m_position++];
}

} // namespace mmkv
4 changes: 4 additions & 0 deletions Win32/MMKV/CodedInputData.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <cstdint>
#include <string>

namespace mmkv {

class CodedInputData {
uint8_t *m_ptr;
int32_t m_size;
Expand Down Expand Up @@ -66,4 +68,6 @@ class CodedInputData {
MMBuffer readData();
};

} // namespace mmkv

#endif //MMKV_CODEDINPUTDATA_H
4 changes: 4 additions & 0 deletions Win32/MMKV/CodedOutputData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

using namespace std;

namespace mmkv {

CodedOutputData::CodedOutputData(void *ptr, size_t len)
: m_ptr((uint8_t *) ptr), m_size(len), m_position(0) {
assert(m_ptr);
Expand Down Expand Up @@ -147,3 +149,5 @@ void CodedOutputData::writeRawLittleEndian64(int64_t value) {
this->writeRawByte(static_cast<uint8_t>((value >> 48) & 0xff));
this->writeRawByte(static_cast<uint8_t>((value >> 56) & 0xff));
}

} // namespace mmkv
4 changes: 4 additions & 0 deletions Win32/MMKV/CodedOutputData.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <cstdint>
#include <string>

namespace mmkv {

class CodedOutputData {
uint8_t *m_ptr;
size_t m_size;
Expand Down Expand Up @@ -70,4 +72,6 @@ class CodedOutputData {
void writeData(const MMBuffer &value);
};

} // namespace mmkv

#endif //MMKV_CODEDOUTPUTDATA_H
4 changes: 4 additions & 0 deletions Win32/MMKV/InterProcessLock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "InterProcessLock.h"
#include "MMKVLog.h"

namespace mmkv {

static DWORD LockType2Flag(LockType lockType) {
DWORD flag = 0;
switch (lockType) {
Expand Down Expand Up @@ -127,3 +129,5 @@ bool FileLock::unlock(LockType lockType) {
return true;
}
}

} // namespace mmkv
4 changes: 4 additions & 0 deletions Win32/MMKV/InterProcessLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "MMKVDef.h"
#include <assert.h>

namespace mmkv {

enum LockType {
SharedLockType,
ExclusiveLockType,
Expand Down Expand Up @@ -75,4 +77,6 @@ class InterProcessLock {
}
};

} // namespace mmkv

#endif //MMKV_INTERPROCESSLOCK_H
4 changes: 4 additions & 0 deletions Win32/MMKV/MMBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <cstring>
#include <utility>

namespace mmkv {

MMBuffer::MMBuffer(size_t length) : ptr(nullptr), size(length), isNoCopy(MMBufferCopy) {
if (size > 0) {
ptr = malloc(size);
Expand Down Expand Up @@ -58,3 +60,5 @@ MMBuffer::~MMBuffer() {
}
ptr = nullptr;
}

} // namespace mmkv
4 changes: 4 additions & 0 deletions Win32/MMKV/MMBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include <cstdint>

namespace mmkv {

enum MMBufferCopyFlag : bool {
MMBufferCopy = false,
MMBufferNoCopy = true,
Expand Down Expand Up @@ -53,4 +55,6 @@ class MMBuffer {
MMBuffer &operator=(const MMBuffer &other) = delete;
};

} // namespace mmkv

#endif //MMKV_MMBUFFER_H
31 changes: 16 additions & 15 deletions Win32/MMKV/MMKV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
*/

#include "MMKV.h"
#include "MMKVDef.h"
#include "CodedInputData.h"
#include "CodedOutputData.h"
#include "InterProcessLock.h"
#include "MMBuffer.h"
#include "MMKVDef.h"
#include "MMKVLog.h"
#include "MiniPBCoder.h"
#include "MmapedFile.h"
Expand All @@ -41,6 +41,7 @@
#include <cstring>

using namespace std;
using namespace mmkv;

static unordered_map<std::string, MMKV *> *g_instanceDic;
static ThreadLock g_instanceLock;
Expand All @@ -61,7 +62,7 @@ enum : bool {
IncreaseSequence = true,
};

MMKV::MMKV(const std::string &mmapID, int size, MMKVMode mode, string *cryptKey)
MMKV::MMKV(const std::string &mmapID, MMKVMode mode, int size, string *cryptKey)
: m_mmapID(mmapID)
, m_path(mappedKVPathWithID(m_mmapID, mode))
, m_crcPath(crcPathWithID(m_mmapID, mode))
Expand Down Expand Up @@ -106,7 +107,7 @@ MMKV::~MMKV() {
}

MMKV *MMKV::defaultMMKV(MMKVMode mode, string *cryptKey) {
return mmkvWithID(DEFAULT_MMAP_ID, DEFAULT_MMAP_SIZE, mode, cryptKey);
return mmkvWithID(DEFAULT_MMAP_ID, mode, DEFAULT_MMAP_SIZE, cryptKey);
}

void initialize() {
Expand All @@ -126,8 +127,7 @@ void MMKV::initializeMMKV(const std::wstring &rootDir) {
MMKVInfo("root dir: %ws", g_rootDir.c_str());
}

MMKV *MMKV::mmkvWithID(const std::string &mmapID, int size, MMKVMode mode, string *cryptKey) {

MMKV *MMKV::mmkvWithID(const std::string &mmapID, MMKVMode mode, int size, string *cryptKey) {
if (mmapID.empty()) {
return nullptr;
}
Expand All @@ -138,7 +138,7 @@ MMKV *MMKV::mmkvWithID(const std::string &mmapID, int size, MMKVMode mode, strin
MMKV *kv = itr->second;
return kv;
}
auto kv = new MMKV(mmapID, size, mode, cryptKey);
auto kv = new MMKV(mmapID, mode, size, cryptKey);
(*g_instanceDic)[mmapID] = kv;
return kv;
}
Expand Down Expand Up @@ -287,8 +287,8 @@ void MMKV::partialLoadFromFile() {
MMBuffer inputBuffer(m_ptr + Fixed32Size + oldActualSize, bufferSize,
MMBufferNoCopy);
// incremental update crc digest
m_crcDigest = (uint32_t) crc32(m_crcDigest, (const uint8_t *) inputBuffer.getPtr(),
inputBuffer.length());
m_crcDigest = (uint32_t) zlib::crc32(
m_crcDigest, (const uint8_t *) inputBuffer.getPtr(), inputBuffer.length());
if (m_crcDigest == m_metaInfo.m_crcDigest) {
if (m_crypter) {
decryptBuffer(*m_crypter, inputBuffer);
Expand Down Expand Up @@ -325,7 +325,7 @@ void MMKV::checkLoadData() {
}

// TODO: atomic lock m_metaFile?
MMKVMetaInfo metaInfo;
MetaInfo metaInfo;
metaInfo.read(m_metaFile.getMemory());
if (m_metaInfo.m_sequence != metaInfo.m_sequence) {
MMKVInfo("[%s] oldSeq %u, newSeq %u", m_mmapID.c_str(), m_metaInfo.m_sequence,
Expand Down Expand Up @@ -373,7 +373,8 @@ void MMKV::clearAll() {
}
if (m_fd >= 0) {
if (m_size != DEFAULT_MMAP_SIZE) {
MMKVInfo("truncating [%s] from %zu to %zd", m_mmapID.c_str(), m_size, DEFAULT_MMAP_SIZE);
MMKVInfo("truncating [%s] from %zu to %zd", m_mmapID.c_str(), m_size,
DEFAULT_MMAP_SIZE);
if (!ftruncate(m_fd, DEFAULT_MMAP_SIZE)) {
MMKVError("fail to truncate [%s] to size %zd", m_mmapID.c_str(), DEFAULT_MMAP_SIZE);
}
Expand Down Expand Up @@ -801,7 +802,7 @@ bool MMKV::isFileValid() {
bool MMKV::checkFileCRCValid() {
if (m_ptr) {
constexpr int offset = pbFixed32Size(0);
m_crcDigest = (uint32_t) crc32(0, (const uint8_t *) m_ptr + offset, m_actualSize);
m_crcDigest = (uint32_t) zlib::crc32(0, (const uint8_t *) m_ptr + offset, m_actualSize);
m_metaInfo.read(m_metaFile.getMemory());
if (m_crcDigest == m_metaInfo.m_crcDigest) {
return true;
Expand All @@ -824,7 +825,7 @@ void MMKV::updateCRCDigest(const uint8_t *ptr, size_t length, bool increaseSeque
if (!ptr) {
return;
}
m_crcDigest = (uint32_t) crc32(m_crcDigest, ptr, length);
m_crcDigest = (uint32_t) zlib::crc32(m_crcDigest, ptr, length);

void *crcPtr = m_metaFile.getMemory();
if (!crcPtr) {
Expand Down Expand Up @@ -1168,7 +1169,7 @@ bool MMKV::isFileValid(const std::string &mmapID) {
uint32_t crcFile = 0;
MMBuffer *data = readWholeFile(crcPath);
if (data) {
MMKVMetaInfo metaInfo;
MetaInfo metaInfo;
metaInfo.read(data->getPtr());
crcFile = metaInfo.m_crcDigest;
delete data;
Expand All @@ -1187,7 +1188,7 @@ bool MMKV::isFileValid(const std::string &mmapID) {
}

auto ptr = (const uint8_t *) fileData->getPtr() + offset;
auto crcDigest = (uint32_t) crc32(0, ptr, actualSize);
auto crcDigest = (uint32_t) zlib::crc32(0, ptr, actualSize);

delete fileData;
return crcFile == crcDigest;
Expand All @@ -1203,7 +1204,7 @@ static void mkSpecialCharacterFileDirectory() {
static string md5(const string &value) {
unsigned char md[MD5_DIGEST_LENGTH] = {0};
char tmp[3] = {0}, buf[33] = {0};
MD5((const unsigned char *) value.c_str(), value.size(), md);
openssl::MD5((const unsigned char *) value.c_str(), value.size(), md);
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
sprintf_s(tmp, sizeof(tmp), "%2.2x", md[i]);
strcat_s(buf, sizeof(tmp), tmp);
Expand Down
38 changes: 20 additions & 18 deletions Win32/MMKV/MMKV.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@
#include <unordered_map>
#include <vector>

class CodedOutputData;
namespace mmkv {
class AESCrypt;
class CodedOutputData;
} // namespace mmkv

enum MMKVMode : uint32_t {
MMKV_SINGLE_PROCESS = 0x1,
MMKV_MULTI_PROCESS = 0x2,
};

class MMKV_API MMKV {
std::unordered_map<std::string, MMBuffer> m_dic;
class MMKV {
std::unordered_map<std::string, mmkv::MMBuffer> m_dic;
std::string m_mmapID;
std::wstring m_path;
std::wstring m_crcPath;
Expand All @@ -50,21 +52,21 @@ class MMKV_API MMKV {
char *m_ptr;
size_t m_size;
size_t m_actualSize;
CodedOutputData *m_output;
mmkv::CodedOutputData *m_output;

bool m_needLoadFromFile;
bool m_hasFullWriteback;

uint32_t m_crcDigest;
MmapedFile m_metaFile;
MMKVMetaInfo m_metaInfo;
mmkv::MmapedFile m_metaFile;
mmkv::MetaInfo m_metaInfo;

AESCrypt *m_crypter;
mmkv::AESCrypt *m_crypter;

ThreadLock m_lock;
FileLock m_fileLock;
InterProcessLock m_sharedProcessLock;
InterProcessLock m_exclusiveProcessLock;
mmkv::ThreadLock m_lock;
mmkv::FileLock m_fileLock;
mmkv::InterProcessLock m_sharedProcessLock;
mmkv::InterProcessLock m_exclusiveProcessLock;

void loadFromFile();

Expand All @@ -86,13 +88,13 @@ class MMKV_API MMKV {

bool fullWriteback();

const MMBuffer &getDataForKey(const std::string &key);
const mmkv::MMBuffer &getDataForKey(const std::string &key);

bool setDataForKey(MMBuffer &&data, const std::string &key);
bool setDataForKey(mmkv::MMBuffer &&data, const std::string &key);

bool removeDataForKey(const std::string &key);

bool appendDataWithKey(const MMBuffer &data, const std::string &key);
bool appendDataWithKey(const mmkv::MMBuffer &data, const std::string &key);

void checkReSetCryptKey(int fd, int metaFD, std::string *cryptKey);

Expand All @@ -102,8 +104,8 @@ class MMKV_API MMKV {

public:
MMKV(const std::string &mmapID,
int size = DEFAULT_MMAP_SIZE,
MMKVMode mode = MMKV_SINGLE_PROCESS,
int size = mmkv::DEFAULT_MMAP_SIZE,
std::string *cryptKey = nullptr);

~MMKV();
Expand All @@ -116,8 +118,8 @@ class MMKV_API MMKV {
// mmapID: any unique ID (com.tencent.xin.pay, etc)
// if you want a per-user mmkv, you could merge user-id within mmapID
static MMKV *mmkvWithID(const std::string &mmapID,
int size = DEFAULT_MMAP_SIZE,
MMKVMode mode = MMKV_SINGLE_PROCESS,
int size = mmkv::DEFAULT_MMAP_SIZE,
std::string *cryptKey = nullptr);

static void onExit();
Expand All @@ -139,7 +141,7 @@ class MMKV_API MMKV {

bool setStringForKey(const std::string &value, const std::string &key);

bool setBytesForKey(const MMBuffer &value, const std::string &key);
bool setBytesForKey(const mmkv::MMBuffer &value, const std::string &key);

bool set(bool value, const std::string &key);

Expand All @@ -159,7 +161,7 @@ class MMKV_API MMKV {

bool getStringForKey(const std::string &key, std::string &result);

MMBuffer getBytesForKey(const std::string &key);
mmkv::MMBuffer getBytesForKey(const std::string &key);

bool getBoolForKey(const std::string &key, bool defaultValue = false);

Expand Down
12 changes: 6 additions & 6 deletions Win32/MMKV/MMKV.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="aes\AESCrypt.cpp" />
<ClCompile Include="aes\openssl\aes_cfb.c" />
<ClCompile Include="aes\openssl\aes_core.c" />
<ClCompile Include="aes\openssl\cfb128.c" />
<ClCompile Include="aes\openssl\md5_dgst.c" />
<ClCompile Include="aes\openssl\md5_one.c" />
<ClCompile Include="aes\openssl\aes_cfb.cpp" />
<ClCompile Include="aes\openssl\aes_core.cpp" />
<ClCompile Include="aes\openssl\cfb128.cpp" />
<ClCompile Include="aes\openssl\md5_dgst.cpp" />
<ClCompile Include="aes\openssl\md5_one.cpp" />
<ClCompile Include="CodedInputData.cpp" />
<ClCompile Include="CodedOutputData.cpp" />
<ClCompile Include="crc32\zlib\crc32.c" />
<ClCompile Include="crc32\zlib\crc32.cpp" />
<ClCompile Include="InterProcessLock.cpp" />
<ClCompile Include="MiniPBCoder.cpp" />
<ClCompile Include="MmapedFile.cpp" />
Expand Down
Loading

0 comments on commit b4bca7c

Please sign in to comment.