/** Call this method when an object is ready to be written * out to disk. * * When the to-write buffer is full, all of it gets written * out to disk using writeOldObjects() * * @param item :: item that can be written to disk. */ void DiskBuffer::toWrite(ISaveable *item) { if (item == nullptr) return; // if (!m_useWriteBuffer) return; if (item->getBufPostion()) // already in the buffer and probably have changed // its size in memory { // forget old memory size m_mutex.lock(); m_writeBufferUsed -= item->getBufferSize(); // add new size size_t newMemorySize = item->getDataMemorySize(); m_writeBufferUsed += newMemorySize; m_mutex.unlock(); item->setBufferSize(newMemorySize); } else { m_mutex.lock(); m_toWriteBuffer.push_front(item); m_writeBufferUsed += item->setBufferPosition(m_toWriteBuffer.begin()); m_nObjectsToWrite++; m_mutex.unlock(); } // Should we now write out the old data? if (m_writeBufferUsed > m_writeBufferSize) writeOldObjects(); }
/** Flush out all the data in the memory; and writes out everything in the to-write cache. */ void DiskBuffer::flushCache() { m_mutex.lock(); // Now write everything out. writeOldObjects(); m_mutex.unlock(); }
/** Call this method when an object is ready to be written * out to disk. * * When the to-write buffer is full, all of it gets written * out to disk using writeOldObjects() * * @param item :: item that can be written to disk. */ void DiskBuffer::toWrite(const ISaveable * item) { if (item == NULL) return; // if (!m_useWriteBuffer) return; m_mutex.lock(); // And put it in the queue of stuff to write. // std::cout << "DiskBuffer adding ID " << item->getId() << " to current size " << m_writeBuffer.size() << std::endl; // TODO: check what happens when the same element is inserted with different size std::pair<writeBuffer_t::iterator,bool> result = m_writeBuffer.insert(item); // Result.second is FALSE if the item was already there if (result.second) { // Track the memory change m_writeBufferUsed += item->getDataMemorySize(); // Should we now write out the old data? if (m_writeBufferUsed >= m_writeBufferSize) writeOldObjects(); } m_mutex.unlock(); }
/** Flush out all the data in the memory; and writes out everything in the * to-write cache. */ void DiskBuffer::flushCache() { // Now write everything out. writeOldObjects(); }