diff --git a/Builds/VisualStudio/stellar-core.vcxproj b/Builds/VisualStudio/stellar-core.vcxproj index 31c4d0d724..b5dadc4f87 100644 --- a/Builds/VisualStudio/stellar-core.vcxproj +++ b/Builds/VisualStudio/stellar-core.vcxproj @@ -276,6 +276,7 @@ exit /b 0 + @@ -550,6 +551,7 @@ exit /b 0 + diff --git a/Builds/VisualStudio/stellar-core.vcxproj.filters b/Builds/VisualStudio/stellar-core.vcxproj.filters index e243adac3e..abde465739 100644 --- a/Builds/VisualStudio/stellar-core.vcxproj.filters +++ b/Builds/VisualStudio/stellar-core.vcxproj.filters @@ -843,6 +843,9 @@ main + + database + @@ -1451,6 +1454,9 @@ main + + database + diff --git a/src/database/DatabaseUtils.cpp b/src/database/DatabaseUtils.cpp new file mode 100644 index 0000000000..445a892347 --- /dev/null +++ b/src/database/DatabaseUtils.cpp @@ -0,0 +1,31 @@ +// Copyright 2018 Stellar Development Foundation and contributors. Licensed +// under the Apache License, Version 2.0. See the COPYING file at the root +// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 + +#include "DatabaseUtils.h" + +namespace stellar +{ +namespace DatabaseUtils +{ +void +deleteOldEntriesHelper(soci::session& sess, uint32_t ledgerSeq, uint32_t count, + std::string const& tableName, + std::string const& ledgerSeqColumn) +{ + uint32_t curMin = 0; + soci::indicator gotMin; + soci::statement st = (sess.prepare << "SELECT MIN(" << ledgerSeqColumn + << ") FROM " << tableName, + soci::into(curMin, gotMin)); + + st.execute(true); + if (st.got_data() && gotMin == soci::i_ok) + { + uint32 m = std::min(curMin + count, ledgerSeq); + sess << "DELETE FROM " << tableName << " WHERE " << ledgerSeqColumn + << " <= " << m; + } +} +} +} \ No newline at end of file diff --git a/src/database/DatabaseUtils.h b/src/database/DatabaseUtils.h new file mode 100644 index 0000000000..e36b67e07c --- /dev/null +++ b/src/database/DatabaseUtils.h @@ -0,0 +1,17 @@ +#pragma once + +// Copyright 2018 Stellar Development Foundation and contributors. Licensed +// under the Apache License, Version 2.0. See the COPYING file at the root +// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 + +#include "Database.h" + +namespace stellar +{ +namespace DatabaseUtils +{ +void deleteOldEntriesHelper(soci::session& sess, uint32_t ledgerSeq, + uint32_t count, std::string const& tableName, + std::string const& ledgerSeqColumn); +} +} diff --git a/src/herder/HerderPersistenceImpl.cpp b/src/herder/HerderPersistenceImpl.cpp index d0d9d97f0a..81f058899a 100644 --- a/src/herder/HerderPersistenceImpl.cpp +++ b/src/herder/HerderPersistenceImpl.cpp @@ -5,6 +5,7 @@ #include "herder/HerderPersistenceImpl.h" #include "crypto/Hex.h" #include "database/Database.h" +#include "database/DatabaseUtils.h" #include "herder/Herder.h" #include "main/Application.h" #include "scp/Slot.h" @@ -269,11 +270,9 @@ void HerderPersistence::deleteOldEntries(Database& db, uint32_t ledgerSeq, uint32_t count) { - db.getSession() << "DELETE FROM scphistory WHERE ledgerseq IN (SELECT " - "ledgerseq FROM scphistory WHERE ledgerseq <= " - << ledgerSeq << " LIMIT " << count << ")"; - db.getSession() << "DELETE FROM scpquorums WHERE lastledgerseq IN (SELECT " - "lastledgerseq FROM scpquorums WHERE lastledgerseq <= " - << ledgerSeq << " LIMIT " << count << ")"; + DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count, + "scphistory", "ledgerseq"); + DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count, + "scpquorums", "lastledgerseq"); } } diff --git a/src/ledger/LedgerHeaderFrame.cpp b/src/ledger/LedgerHeaderFrame.cpp index f015c59499..bbc216ade0 100644 --- a/src/ledger/LedgerHeaderFrame.cpp +++ b/src/ledger/LedgerHeaderFrame.cpp @@ -8,6 +8,7 @@ #include "crypto/Hex.h" #include "crypto/SHA.h" #include "database/Database.h" +#include "database/DatabaseUtils.h" #include "util/Logging.h" #include "util/XDRStream.h" #include "util/format.h" @@ -243,9 +244,8 @@ void LedgerHeaderFrame::deleteOldEntries(Database& db, uint32_t ledgerSeq, uint32_t count) { - db.getSession() << "DELETE FROM ledgerheaders WHERE ledgerseq IN (SELECT " - "ledgerseq FROM ledgerheaders WHERE ledgerseq <= " - << ledgerSeq << " LIMIT " << count << ")"; + DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count, + "ledgerheaders", "ledgerseq"); } void diff --git a/src/transactions/TransactionFrame.cpp b/src/transactions/TransactionFrame.cpp index 93e9a6f002..315b844a9b 100644 --- a/src/transactions/TransactionFrame.cpp +++ b/src/transactions/TransactionFrame.cpp @@ -9,6 +9,7 @@ #include "crypto/SHA.h" #include "crypto/SignerKey.h" #include "database/Database.h" +#include "database/DatabaseUtils.h" #include "herder/TxSetFrame.h" #include "invariant/InvariantManager.h" #include "ledger/LedgerDelta.h" @@ -841,11 +842,9 @@ void TransactionFrame::deleteOldEntries(Database& db, uint32_t ledgerSeq, uint32_t count) { - db.getSession() << "DELETE FROM txhistory WHERE ledgerseq IN (SELECT " - "ledgerseq FROM txhistory WHERE ledgerseq <= " - << ledgerSeq << " LIMIT " << count << ")"; - db.getSession() << "DELETE FROM txfeehistory WHERE ledgerseq IN (SELECT " - "ledgerseq FROM txfeehistory WHERE ledgerseq <= " - << ledgerSeq << " LIMIT " << count << ")"; + DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count, + "txhistory", "ledgerseq"); + DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count, + "txfeehistory", "ledgerseq"); } }