Int32 HDBNode::write(HDBHandle& hdl, EWriteHeaderFlag onlyHeader) { if (!m_pdata) { OW_THROW(HDBException, "Internal error: Cannot write null node"); } bool newRecord = false; m_pdata->m_blk.keyLength = m_pdata->m_key.length() + 1; m_pdata->m_blk.dataLength = m_pdata->m_bfrLen + m_pdata->m_key.length() + 1; File file = hdl.getFile(); HDB* phdb = hdl.getHDB(); int totalSize = m_pdata->m_blk.dataLength + sizeof(m_pdata->m_blk); m_pdata->m_blk.isFree = false; if (m_pdata->m_offset <= 0) // Is this a new node? { newRecord = true; m_pdata->m_blk.size = totalSize; m_pdata->m_offset = phdb->findBlock(file, totalSize); if (m_pdata->m_blk.parent <= 0) { phdb->addRootNode(file, m_pdata->m_blk, m_pdata->m_offset); } else { HDB::writeBlock(m_pdata->m_blk, file, m_pdata->m_offset); } // Add this entry to the index if (!hdl.addIndexEntry(m_pdata->m_key.c_str(), m_pdata->m_offset)) { OW_THROW(HDBException, "Failed to write index entry"); } } else { // If the size required to hold this node has increased, // then delete the old block and add a new one. if (totalSize > int(m_pdata->m_blk.size)) { HDBBlock node = m_pdata->m_blk; phdb->addBlockToFreeList(file, node, m_pdata->m_offset); // delete old m_pdata->m_blk.size = totalSize; updateOffsets(hdl, phdb->findBlock(file, totalSize)); } HDB::writeBlock(m_pdata->m_blk, file, m_pdata->m_offset); } if (onlyHeader == false || newRecord == true) { // Now write key and data for node if (file.write(m_pdata->m_key.c_str(), m_pdata->m_key.length()+1) != m_pdata->m_key.length()+1) { OW_THROW_ERRNO_MSG(HDBException, "Failed to write node key"); } if (file.write(m_pdata->m_bfr, m_pdata->m_bfrLen) != size_t(m_pdata->m_bfrLen)) { OW_THROW_ERRNO_MSG(HDBException, "Failed to write node data"); } } m_pdata->m_version = hdl.registerWrite(); return m_pdata->m_offset; }