Skip to content

Commit

Permalink
Merge pull request #176 from Tencent/dev
Browse files Browse the repository at this point in the history
v1.0.16
  • Loading branch information
lingol authored Jan 4, 2019
2 parents 7ff0322 + 1d3bfb6 commit 17d0dc4
Show file tree
Hide file tree
Showing 82 changed files with 8,249 additions and 129 deletions.
14 changes: 13 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@ xcuserdata
*.xcscmblueprint
*.xcscheme

## OS X
.DS_Store

## Xcode
DerivedData
DerivedData

## Visual Studio
.vs
[Dd]ebug/
[Rr]elease/
*.user
*.VC.opendb
*.VC.db
ipch/
2 changes: 1 addition & 1 deletion Android/MMKV/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ org.gradle.jvmargs=-Xmx1536m
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

VERSION_NAME_PREFIX=1.0.15
VERSION_NAME_PREFIX=1.0.16
#VERSION_NAME_SUFFIX=-SNAPSHOT
VERSION_NAME_SUFFIX=
93 changes: 68 additions & 25 deletions Android/MMKV/mmkv/src/main/cpp/MMKV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ static std::string g_rootDir;
#define SPECIAL_CHARACTER_DIRECTORY_NAME "specialCharacter"
constexpr uint32_t Fixed32Size = pbFixed32Size(0);

static string mappedKVPathWithID(const string &mmapID, MMKVMode mode);
static string crcPathWithID(const string &mmapID, MMKVMode mode);
static string mmapedKVKey(const string &mmapID, string *relativePath = nullptr);
static string
mappedKVPathWithID(const string &mmapID, MMKVMode mode, string *relativePath = nullptr);
static string crcPathWithID(const string &mmapID, MMKVMode mode, string *relativePath = nullptr);
static void mkSpecialCharacterFileDirectory();
static string md5(const string &value);
static string encodeFilePath(const string &mmapID);
Expand All @@ -64,10 +66,11 @@ enum : bool {
IncreaseSequence = true,
};

MMKV::MMKV(const std::string &mmapID, int size, MMKVMode mode, string *cryptKey)
: m_mmapID(mmapID)
, m_path(mappedKVPathWithID(m_mmapID, mode))
, m_crcPath(crcPathWithID(m_mmapID, mode))
MMKV::MMKV(
const std::string &mmapID, int size, MMKVMode mode, string *cryptKey, string *relativePath)
: m_mmapID(mmapedKVKey(mmapID, relativePath))
, m_path(mappedKVPathWithID(m_mmapID, mode, relativePath))
, m_crcPath(crcPathWithID(m_mmapID, mode, relativePath))
, m_metaFile(m_crcPath, DEFAULT_MMAP_SIZE, (mode & MMKV_ASHMEM) ? MMAP_ASHMEM : MMAP_FILE)
, m_crypter(nullptr)
, m_fileLock(m_metaFile.getFd())
Expand Down Expand Up @@ -201,20 +204,32 @@ void MMKV::initializeMMKV(const std::string &rootDir) {
MMKVInfo("root dir: %s", g_rootDir.c_str());
}

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

if (mmapID.empty()) {
return nullptr;
}
SCOPEDLOCK(g_instanceLock);

auto itr = g_instanceDic->find(mmapID);
auto mmapKey = mmapedKVKey(mmapID, relativePath);
auto itr = g_instanceDic->find(mmapKey);
if (itr != g_instanceDic->end()) {
MMKV *kv = itr->second;
return kv;
}
auto kv = new MMKV(mmapID, size, mode, cryptKey);
(*g_instanceDic)[mmapID] = kv;
if (relativePath) {
auto filePath = mappedKVPathWithID(mmapID, mode, relativePath);
if (!isFileExist(filePath)) {
if (!createFile(filePath)) {
return nullptr;
}
}
MMKVInfo("prepare to load %s (id %s) from relativePath %s", mmapID.c_str(), mmapKey.c_str(),
relativePath->c_str());
}
auto kv = new MMKV(mmapID, size, mode, cryptKey, relativePath);
(*g_instanceDic)[mmapKey] = kv;
return kv;
}

Expand Down Expand Up @@ -280,7 +295,9 @@ void MMKV::loadFromFile() {
return;
}

m_metaInfo.read(m_metaFile.getMemory());
if (m_metaFile.isFileValid()) {
m_metaInfo.read(m_metaFile.getMemory());
}

m_fd = open(m_path.c_str(), O_RDWR | O_CREAT, S_IRWXU);
if (m_fd < 0) {
Expand Down Expand Up @@ -364,7 +381,9 @@ void MMKV::loadFromFile() {
}

void MMKV::loadFromAshmem() {
m_metaInfo.read(m_metaFile.getMemory());
if (m_metaFile.isFileValid()) {
m_metaInfo.read(m_metaFile.getMemory());
}

if (m_fd < 0 || !m_ashmemFile) {
MMKVError("ashmem file invalid %s, fd:%d", m_path.c_str(), m_fd);
Expand Down Expand Up @@ -958,6 +977,10 @@ bool MMKV::isFileValid() {
// assuming m_ptr & m_size is set
bool MMKV::checkFileCRCValid() {
if (m_ptr && m_ptr != MAP_FAILED) {
if (!m_metaFile.isFileValid()) {
MMKVError("Meta file not valid %s", m_mmapID.c_str());
return false;
}
constexpr int offset = pbFixed32Size(0);
m_crcDigest =
(uint32_t) crc32(0, (const uint8_t *) m_ptr + offset, (uint32_t) m_actualSize);
Expand All @@ -980,23 +1003,19 @@ void MMKV::recaculateCRCDigest() {
}

void MMKV::updateCRCDigest(const uint8_t *ptr, size_t length, bool increaseSequence) {
if (!ptr) {
if (!ptr || !m_metaFile.isFileValid()) {
return;
}
m_crcDigest = (uint32_t) crc32(m_crcDigest, ptr, (uint32_t) length);

void *crcPtr = m_metaFile.getMemory();
if (crcPtr == nullptr || crcPtr == MAP_FAILED) {
return;
}

m_metaInfo.m_crcDigest = m_crcDigest;
if (increaseSequence) {
m_metaInfo.m_sequence++;
}
if (m_metaInfo.m_version == 0) {
m_metaInfo.m_version = 1;
}
auto crcPtr = m_metaFile.getMemory();
m_metaInfo.write(crcPtr);
}

Expand Down Expand Up @@ -1189,6 +1208,15 @@ bool MMKV::getVectorForKey(const std::string &key, std::vector<std::string> &res
return false;
}

size_t MMKV::getValueSizeForKey(const std::string &key) {
if (key.empty()) {
return 0;
}
SCOPEDLOCK(m_lock);
auto &data = getDataForKey(key);
return data.length();
}

#pragma mark - enumerate

bool MMKV::containsKey(const std::string &key) {
Expand Down Expand Up @@ -1319,7 +1347,7 @@ static string md5(const string &value) {
sprintf(tmp, "%2.2x", md[i]);
strcat(buf, tmp);
}
return buf;
return string(buf);
}

static string encodeFilePath(const string &mmapID) {
Expand All @@ -1342,12 +1370,27 @@ static string encodeFilePath(const string &mmapID) {
}
}

static string mappedKVPathWithID(const string &mmapID, MMKVMode mode) {
return (mode & MMKV_ASHMEM) == 0 ? g_rootDir + "/" + encodeFilePath(mmapID)
: string(ASHMEM_NAME_DEF) + "/" + encodeFilePath(mmapID);
static string mmapedKVKey(const string &mmapID, string *relativePath) {
if (relativePath && g_rootDir != (*relativePath)) {
return md5(*relativePath + "/" + mmapID);
}
return mmapID;
}

static string crcPathWithID(const string &mmapID, MMKVMode mode) {
return (mode & MMKV_ASHMEM) == 0 ? g_rootDir + "/" + encodeFilePath(mmapID) + ".crc"
: encodeFilePath(mmapID) + ".crc";
static string mappedKVPathWithID(const string &mmapID, MMKVMode mode, string *relativePath) {
if (mode & MMKV_ASHMEM) {
return string(ASHMEM_NAME_DEF) + "/" + encodeFilePath(mmapID);
} else if (relativePath) {
return *relativePath + "/" + encodeFilePath(mmapID);
}
return g_rootDir + "/" + encodeFilePath(mmapID);
}

static string crcPathWithID(const string &mmapID, MMKVMode mode, string *relativePath) {
if (mode & MMKV_ASHMEM) {
return encodeFilePath(mmapID) + ".crc";
} else if (relativePath) {
return *relativePath + "/" + encodeFilePath(mmapID) + ".crc";
}
return g_rootDir + "/" + encodeFilePath(mmapID) + ".crc";
}
12 changes: 8 additions & 4 deletions Android/MMKV/mmkv/src/main/cpp/MMKV.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ class MMKV {

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

MMKV(const std::string &mmapID,
int ashmemFD,
Expand All @@ -127,7 +128,8 @@ class MMKV {
static MMKV *mmkvWithID(const std::string &mmapID,
int size = DEFAULT_MMAP_SIZE,
MMKVMode mode = MMKV_SINGLE_PROCESS,
std::string *cryptKey = nullptr);
std::string *cryptKey = nullptr,
std::string *relativePath = nullptr);

static MMKV *mmkvWithAshmemFD(const std::string &mmapID,
int fd,
Expand Down Expand Up @@ -189,6 +191,8 @@ class MMKV {

bool getVectorForKey(const std::string &key, std::vector<std::string> &result);

size_t getValueSizeForKey(const std::string &key);

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

size_t count();
Expand Down
34 changes: 32 additions & 2 deletions Android/MMKV/mmkv/src/main/cpp/MmapedFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ MmapedFile::MmapedFile(const std::string &path, size_t size, bool fileType)
if (ioctl(m_fd, ASHMEM_SET_NAME, m_name.c_str()) != 0) {
MMKVError("fail to set ashmem name:%s, %s", m_name.c_str(), strerror(errno));
} else if (ioctl(m_fd, ASHMEM_SET_SIZE, size) != 0) {
MMKVError("fail to set ashmem:%s, size %d, %s", m_name.c_str(), size,
MMKVError("fail to set ashmem:%s, size %zu, %s", m_name.c_str(), size,
strerror(errno));
} else {
m_segmentSize = static_cast<size_t>(size);
Expand Down Expand Up @@ -154,7 +154,7 @@ bool mkPath(char *path) {

if (stat(path, &sb) != 0) {
if (errno != ENOENT || mkdir(path, 0777) != 0) {
MMKVWarning("%s", path);
MMKVWarning("%s : %s", path, strerror(errno));
return false;
}
} else if (!S_ISDIR(sb.st_mode)) {
Expand All @@ -168,6 +168,36 @@ bool mkPath(char *path) {
return true;
}

bool createFile(const std::string &filePath) {
bool ret = false;

// try create at once
auto fd = open(filePath.c_str(), O_RDWR | O_CREAT, S_IRWXU);
if (fd >= 0) {
close(fd);
ret = true;
} else {
// create parent dir
char *path = strdup(filePath.c_str());
auto ptr = strrchr(path, '/');
if (ptr) {
*ptr = '\0';
}
if (mkPath(path)) {
// try again
fd = open(filePath.c_str(), O_RDWR | O_CREAT, S_IRWXU);
if (fd >= 0) {
close(fd);
ret = true;
} else {
MMKVWarning("fail to create file %s, %s", filePath.c_str(), strerror(errno));
}
}
free(path);
}
return ret;
}

bool removeFile(const string &nsFilePath) {
int ret = unlink(nsFilePath.c_str());
if (ret != 0) {
Expand Down
6 changes: 6 additions & 0 deletions Android/MMKV/mmkv/src/main/cpp/MmapedFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <string>
#include <sys/ioctl.h>
#include <sys/mman.h>

#define ASHMEM_NAME_LEN 256
#define ASHMEM_NAME_DEF "/dev/ashmem"
Expand Down Expand Up @@ -63,6 +64,10 @@ class MmapedFile {
std::string &getName() { return m_name; }

int getFd() { return m_fd; }

bool isFileValid() {
return m_fd >= 0 && m_segmentSize > 0 && m_segmentPtr && m_segmentPtr != MAP_FAILED;
}
};

class MMBuffer;
Expand All @@ -72,5 +77,6 @@ extern bool isFileExist(const std::string &nsFilePath);
extern bool removeFile(const std::string &nsFilePath);
extern MMBuffer *readWholeFile(const char *path);
extern bool zeroFillFile(int fd, size_t startPos, size_t size);
extern bool createFile(const std::string &filePath);

#endif //MMKV_MMAPEDFILE_H
Loading

0 comments on commit 17d0dc4

Please sign in to comment.