void plPageOptimizer::IRewritePage() { hsUNIXStream newPage; if (newPage.Open(fTempPagePath, "wb")) { hsUNIXStream oldPage; oldPage.Open(fPagePath); const plPageInfo& pageInfo = fPageNode->GetPageInfo(); uint32_t dataStart = pageInfo.GetDataStart(); fBuf.resize(dataStart); oldPage.Read(dataStart, &fBuf[0]); newPage.Write(dataStart, &fBuf[0]); int size = (int)fKeyLoadOrder.size(); for (int i = 0; i < size; i++) IWriteKeyData(&oldPage, &newPage, fKeyLoadOrder[i]); // If there are any objects that we didn't write (because they didn't load for // some reason), put them at the end for (int i = 0; i < fAllKeys.size(); i++) { bool found = (fLoadedKeys.find(fAllKeys[i]) != fLoadedKeys.end()); if (!found) IWriteKeyData(&oldPage, &newPage, fAllKeys[i]); } uint32_t newKeyStart = newPage.GetPosition(); uint32_t oldKeyStart = pageInfo.GetIndexStart(); oldPage.SetPosition(oldKeyStart); uint32_t numTypes = oldPage.ReadLE32(); newPage.WriteLE32(numTypes); for (uint32_t i = 0; i < numTypes; i++) { uint16_t classType = oldPage.ReadLE16(); uint32_t len = oldPage.ReadLE32(); uint8_t flags = oldPage.ReadByte(); uint32_t numKeys = oldPage.ReadLE32(); newPage.WriteLE16(classType); newPage.WriteLE32(len); newPage.WriteByte(flags); newPage.WriteLE32(numKeys); for (uint32_t j = 0; j < numKeys; j++) { plUoid uoid; uoid.Read(&oldPage); uint32_t startPos = oldPage.ReadLE32(); uint32_t dataLen = oldPage.ReadLE32(); // Get the new start pos plKeyImp* key = (plKeyImp*)fResMgr->FindKey(uoid); startPos = key->GetStartPos(); uoid.Write(&newPage); newPage.WriteLE32(startPos); newPage.WriteLE32(dataLen); } } newPage.Close(); oldPage.Close(); } }