bool CDB::Rewrite(const std::string &strFile, const char *pszSkip) { while (true) { { LOCK(bitdb.cs_db); if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0) { // Flush log data to the dat file bitdb.CloseDb(strFile); bitdb.CheckpointLSN(strFile); bitdb.mapFileUseCount.erase(strFile); bool fSuccess = true; LogPrintf("CDB::Rewrite: Rewriting %s...\n", strFile); std::string strFileRes = strFile + ".rewrite"; { // surround usage of db with extra {} CDB db(strFile.c_str(), "r"); Db *pdbCopy = new Db(bitdb.dbenv, 0); int ret = pdbCopy->open(NULL, // Txn pointer strFileRes.c_str(), // Filename "main", // Logical db name DB_BTREE, // Database type DB_CREATE, // Flags 0); if (ret > 0) { LogPrintf("CDB::Rewrite: Can't create database file %s\n", strFileRes); pdbCopy->close(0); fSuccess = false; } Dbc *pcursor = db.GetCursor(); if (pcursor) { while (fSuccess) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION); int ret1 = db.ReadAtCursor(pcursor, ssKey, ssValue, DB_NEXT); if (ret1 == DB_NOTFOUND) { pcursor->close(); break; } else if (ret1 != 0) { pcursor->close(); fSuccess = false; break; } if (pszSkip && strncmp(&ssKey[0], pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0) continue; if (strncmp(&ssKey[0], "\x07version", 8) == 0) { // Update version: ssValue.clear(); ssValue << CLIENT_VERSION; } Dbt datKey(&ssKey[0], ssKey.size()); Dbt datValue(&ssValue[0], ssValue.size()); int ret2 = pdbCopy->put(NULL, &datKey, &datValue, DB_NOOVERWRITE); if (ret2 > 0) fSuccess = false; } } if (fSuccess) { db.Close(); bitdb.CloseDb(strFile); if (pdbCopy->close(0)) { fSuccess = false; } else { pdbCopy->close(0); } delete pdbCopy; } } if (fSuccess) { Db dbA(bitdb.dbenv, 0); if (dbA.remove(strFile.c_str(), NULL, 0)) fSuccess = false; Db dbB(bitdb.dbenv, 0); if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0)) fSuccess = false; } if (!fSuccess) LogPrintf("CDB::Rewrite: Failed to rewrite database file %s\n", strFileRes); return fSuccess; } } MilliSleep(100); } return false; }
bool BerkeleyBatch::Rewrite(BerkeleyDatabase& database, const char* pszSkip) { if (database.IsDummy()) { return true; } BerkeleyEnvironment *env = database.env; const std::string& strFile = database.strFile; while (true) { { LOCK(cs_db); if (!env->mapFileUseCount.count(strFile) || env->mapFileUseCount[strFile] == 0) { // Flush log data to the dat file env->CloseDb(strFile); env->CheckpointLSN(strFile); env->mapFileUseCount.erase(strFile); bool fSuccess = true; LogPrintf("BerkeleyBatch::Rewrite: Rewriting %s...\n", strFile); std::string strFileRes = strFile + ".rewrite"; { // surround usage of db with extra {} BerkeleyBatch db(database, "r"); std::unique_ptr<Db> pdbCopy = MakeUnique<Db>(env->dbenv.get(), 0); int ret = pdbCopy->open(nullptr, // Txn pointer strFileRes.c_str(), // Filename "main", // Logical db name DB_BTREE, // Database type DB_CREATE, // Flags 0); if (ret > 0) { LogPrintf("BerkeleyBatch::Rewrite: Can't create database file %s\n", strFileRes); fSuccess = false; } Dbc* pcursor = db.GetCursor(); if (pcursor) while (fSuccess) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION); int ret1 = db.ReadAtCursor(pcursor, ssKey, ssValue); if (ret1 == DB_NOTFOUND) { pcursor->close(); break; } else if (ret1 != 0) { pcursor->close(); fSuccess = false; break; } if (pszSkip && strncmp(ssKey.data(), pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0) continue; if (strncmp(ssKey.data(), "\x07version", 8) == 0) { // Update version: ssValue.clear(); ssValue << CLIENT_VERSION; } Dbt datKey(ssKey.data(), ssKey.size()); Dbt datValue(ssValue.data(), ssValue.size()); int ret2 = pdbCopy->put(nullptr, &datKey, &datValue, DB_NOOVERWRITE); if (ret2 > 0) fSuccess = false; } if (fSuccess) { db.Close(); env->CloseDb(strFile); if (pdbCopy->close(0)) fSuccess = false; } else { pdbCopy->close(0); } } if (fSuccess) { Db dbA(env->dbenv.get(), 0); if (dbA.remove(strFile.c_str(), nullptr, 0)) fSuccess = false; Db dbB(env->dbenv.get(), 0); if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(), 0)) fSuccess = false; } if (!fSuccess) LogPrintf("BerkeleyBatch::Rewrite: Failed to rewrite database file %s\n", strFileRes); return fSuccess; } } MilliSleep(100); } }
int EEBeamColumn2d::update() { int rValue = 0; // get current time Domain *theDomain = this->getDomain(); (*t)(0) = theDomain->getCurrentTime(); // update the coordinate transformation theCoordTransf->update(); // determine dsp, vel and acc in basic system A const Vector &dbA = theCoordTransf->getBasicTrialDisp(); const Vector &vbA = theCoordTransf->getBasicTrialVel(); const Vector &abA = theCoordTransf->getBasicTrialAccel(); const Vector &dbDeltaA = theCoordTransf->getBasicIncrDeltaDisp(); /* transform displacements from basic sys A to basic sys B (linear) Vector dbDelta(3); (*db)[0] = dbA(0); (*db)[1] = -L*dbA(1); (*db)[2] = -dbA(1)+dbA(2); (*vb)[0] = vbA(0); (*vb)[1] = -L*vbA(1); (*vb)[2] = -vbA(1)+vbA(2); (*ab)[0] = abA(0); (*ab)[1] = -L*abA(1); (*ab)[2] = -abA(1)+abA(2); dbDelta(0) = dbDeltaA(0); dbDelta(1) = -L*dbDeltaA(1); dbDelta(2) = -dbDeltaA(1)+dbDeltaA(2);*/ // transform displacements from basic sys A to basic sys B (nonlinear) Vector dbDelta(3); (*db)[0] = (L+dbA(0))*cos(dbA(1))-L; (*db)[1] = -(L+dbA(0))*sin(dbA(1)); (*db)[2] = -dbA(1)+dbA(2); (*vb)[0] = vbA(0)*cos(dbA(1))-(L+dbA(0))*sin(dbA(1))*vbA(1); (*vb)[1] = -vbA(0)*sin(dbA(1))-(L+dbA(0))*cos(dbA(1))*vbA(1); (*vb)[2] = -vbA(1)+vbA(2); (*ab)[0] = abA(0)*cos(dbA(1))-2*vbA(0)*sin(dbA(1))*vbA(1)-(L+dbA(0))*cos(dbA(1))*pow(vbA(1),2)-(L+dbA(0))*sin(dbA(1))*abA(1); (*ab)[1] = -abA(0)*sin(dbA(1))-2*vbA(0)*cos(dbA(1))*vbA(1)+(L+dbA(0))*sin(dbA(1))*pow(vbA(1),2)-(L+dbA(0))*cos(dbA(1))*abA(1); (*ab)[2] = -abA(1)+abA(2); dbDelta(0) = (L+dbDeltaA(0))*cos(dbDeltaA(1))-L; dbDelta(1) = -(L+dbDeltaA(0))*sin(dbDeltaA(1)); dbDelta(2) = -dbDeltaA(1)+dbDeltaA(2); // do not check time for right now because of transformation constraint // handler calling update at beginning of new step when applying load // if (dbDelta.pNorm(0) > DBL_EPSILON || (*t)(0) > tLast) { if (dbDelta.pNorm(0) > DBL_EPSILON) { // set the trial response at the site if (theSite != 0) { theSite->setTrialResponse(db, vb, ab, (Vector*)0, t); } else { sData[0] = OF_RemoteTest_setTrialResponse; rValue += theChannel->sendVector(0, 0, *sendData, 0); } } // save the last time tLast = (*t)(0); return rValue; }