Skip to content

Commit

Permalink
fix fast remove corruption
Browse files Browse the repository at this point in the history
  • Loading branch information
lingol committed Jun 6, 2019
1 parent bfb99ca commit 548e10c
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions Android/MMKV/mmkv/src/main/cpp/MMKV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,10 +674,15 @@ bool MMKV::ensureMemorySize(size_t newSize) {
return false;
}

// make some room for placeholder
constexpr size_t ItemSizeHolderSize = 4;
if (m_dic.empty()) {
newSize += ItemSizeHolderSize;
}
if (newSize >= m_output->spaceLeft()) {
// try a full rewrite to make space
static const int offset = pbFixed32Size(0);
MMBuffer data = m_dic.empty() ? MMBuffer(0) : MiniPBCoder::encodeDataWithObject(m_dic);
MMBuffer data = MiniPBCoder::encodeDataWithObject(m_dic);
size_t lenNeeded = data.length() + offset + newSize;
if (m_isAshmem) {
if (lenNeeded > m_size) {
Expand Down Expand Up @@ -795,34 +800,20 @@ bool MMKV::removeDataForKey(const std::string &key) {
return false;
}

constexpr uint32_t ItemSizeHolder = 0x00ffffff, ItemSizeHolderSize = 4;

bool MMKV::appendDataWithKey(const MMBuffer &data, const std::string &key) {
size_t keyLength = key.length();
// size needed to encode the key
size_t size = keyLength + pbRawVarint32Size((int32_t) keyLength);
// size needed to encode the value
size += data.length() + pbRawVarint32Size((int32_t) data.length());

bool isFirstWrite = false;
if (m_actualSize == 0) {
isFirstWrite = true;
size += ItemSizeHolderSize; // size needed to encode the ItemSizeHolder
}

SCOPEDLOCK(m_exclusiveProcessLock);

bool hasEnoughSize = ensureMemorySize(size);

if (!hasEnoughSize || !isFileValid()) {
return false;
}
// write holder of size of m_dic
// which will be ignore while decoding
// yet it's required for decoding
if (isFirstWrite) {
m_output->writeInt32(ItemSizeHolder);
}

writeAcutalSize(m_actualSize + size);
m_output->writeString(key);
Expand Down

0 comments on commit 548e10c

Please sign in to comment.