美文网首页
MMKV修改数据源码

MMKV修改数据源码

作者: 董江鹏 | 来源:发表于2024-01-26 15:30 被阅读0次

    大家都知道MMKV性能好,因为是直接操作内存。
    内存其实就是一个数组结构,根据地址去寻址查找数据的,就跟数组通过index查找数据一样,
    但是数组修改是很不方便的,比如删除中间一个数据,受影响的都要往前移动,会影响性能。
    MMKV每次修改数据,不会去修改文件原有数据,而是在尾部追加。
    但这样会导致文件无限膨胀,MMKV在每次追加数据的时候,会去检查数据和文件大小。
    修改数据的时候检查大小,完成检查整理扩容之后,再追加数据:

    bool MMKV::appendDataWithKey(const MMBuffer &data, MMKVKey_t key) {
    #ifdef MMKV_APPLE
        auto keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
        size_t keyLength = keyData.length;
    #else
        size_t keyLength = key.length();
    #endif
        // 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());
    
        SCOPED_LOCK(m_exclusiveProcessLock);
    
        bool hasEnoughSize = ensureMemorySize(size);
        if (!hasEnoughSize || !isFileValid()) {
            return false;
        }
    
    #ifdef MMKV_IOS
        auto ret = protectFromBackgroundWriting(m_output->curWritePointer(), size, ^{
          m_output->writeData(MMBuffer(keyData, MMBufferNoCopy));
          m_output->writeData(data); // note: write size of data
        });
        if (!ret) {
            return false;
        }
    #else
    #ifdef MMKV_APPLE
        m_output->writeData(MMBuffer(keyData, MMBufferNoCopy));
    #else
        m_output->writeString(key);
    #endif
        m_output->writeData(data); // note: write size of data
    #endif
    
        auto ptr = (uint8_t *) m_file->getMemory() + Fixed32Size + m_actualSize;
        if (m_crypter) {
            m_crypter->encrypt(ptr, ptr, size);
        }
        m_actualSize += size;
        updateCRCDigest(ptr, size);
    
        return true;
    }
    

    当前表数据超过文件大小,或者表数据的1.5倍超过文件大小,则将文件大小翻倍,直到满足上述条件:

    // since we use append mode, when -[setData: forKey:] many times, space may not be enough
    // try a full rewrite to make space
    bool MMKV::ensureMemorySize(size_t newSize) {
        if (!isFileValid()) {
            MMKVWarning("[%s] file not valid", m_mmapID.c_str());
            return false;
        }
    
        // make some room for placeholder
        constexpr size_t ItemSizeHolderSize = 4;
        if (m_dic.empty()) {
            newSize += ItemSizeHolderSize;
        }
        if (newSize >= m_output->spaceLeft() || m_dic.empty()) {
            // try a full rewrite to make space
            auto fileSize = m_file->getFileSize();
            MMBuffer data = MiniPBCoder::encodeDataWithObject(m_dic);
            size_t lenNeeded = data.length() + Fixed32Size + newSize;
            size_t avgItemSize = lenNeeded / std::max<size_t>(1, m_dic.size());
            size_t futureUsage = avgItemSize * std::max<size_t>(8, (m_dic.size() + 1) / 2);
            // 1. no space for a full rewrite, double it
            // 2. or space is not large enough for future usage, double it to avoid frequently full rewrite
            if (lenNeeded >= fileSize || (lenNeeded + futureUsage) >= fileSize) {
                size_t oldSize = fileSize;
                do {
                    fileSize *= 2;
                } while (lenNeeded + futureUsage >= fileSize);
                MMKVInfo("extending [%s] file size from %zu to %zu, incoming size:%zu, future usage:%zu", m_mmapID.c_str(),
                         oldSize, fileSize, newSize, futureUsage);
    
                // if we can't extend size, rollback to old state
                if (!m_file->truncate(fileSize)) {
                    return false;
                }
    
                // check if we fail to make more space
                if (!isFileValid()) {
                    MMKVWarning("[%s] file not valid", m_mmapID.c_str());
                    return false;
                }
            }
            return doFullWriteBack(std::move(data));
        }
        return true;
    }
    

    最后将整个表数据以pb格式全部写入文件:

    bool MMKV::doFullWriteBack(MMBuffer &&allData) {
    #ifdef MMKV_IOS
        unsigned char oldIV[AES_KEY_LEN];
        unsigned char newIV[AES_KEY_LEN];
        if (m_crypter) {
            memcpy(oldIV, m_crypter->m_vector, sizeof(oldIV));
    #else
        unsigned char newIV[AES_KEY_LEN];
        if (m_crypter) {
    #endif
            AESCrypt::fillRandomIV(newIV);
            m_crypter->resetIV(newIV, sizeof(newIV));
            auto ptr = allData.getPtr();
            m_crypter->encrypt(ptr, ptr, allData.length());
        }
    
        auto ptr = (uint8_t *) m_file->getMemory();
        delete m_output;
        m_output = new CodedOutputData(ptr + Fixed32Size, m_file->getFileSize() - Fixed32Size);
    #ifdef MMKV_IOS
        auto ret = protectFromBackgroundWriting(m_output->curWritePointer(), allData.length(), ^{
          m_output->writeRawData(allData); // note: don't write size of data
        });
        if (!ret) {
            // revert everything
            if (m_crypter) {
                m_crypter->resetIV(oldIV);
            }
            delete m_output;
            m_output = new CodedOutputData(ptr + Fixed32Size, m_file->getFileSize() - Fixed32Size);
            m_output->seek(m_actualSize);
            return false;
        }
    #else
        m_output->writeRawData(allData); // note: don't write size of data
    #endif
    
        m_actualSize = allData.length();
        if (m_crypter) {
            recaculateCRCDigestWithIV(newIV);
        } else {
            recaculateCRCDigestWithIV(nullptr);
        }
        m_hasFullWriteback = true;
        // make sure lastConfirmedMetaInfo is saved
        sync(MMKV_SYNC);
        return true;
    }
    

    相关文章

      网友评论

          本文标题:MMKV修改数据源码

          本文链接:https://www.haomeiwen.com/subject/qvsbodtx.html