TaggedStatement tagged_statements[] = {
{MySqlLeaseMgr::DELETE_LEASE4,
"DELETE FROM lease4 WHERE address = ?"},
+ {MySqlLeaseMgr::DELETE_LEASE4_STATE_EXPIRED,
+ "DELETE FROM lease4 "
+ "WHERE state = ? AND expire < ?"},
{MySqlLeaseMgr::DELETE_LEASE6,
"DELETE FROM lease6 WHERE address = ?"},
+ {MySqlLeaseMgr::DELETE_LEASE6_STATE_EXPIRED,
+ "DELETE FROM lease6 "
+ "WHERE state = ? AND expire < ?"},
{MySqlLeaseMgr::GET_LEASE4_ADDR,
"SELECT address, hwaddr, client_id, "
"valid_lifetime, expire, subnet_id, "
// case, a single method for both V4 and V6 addresses) and a common method that
// handles the common processing.
-bool
+uint64_t
MySqlLeaseMgr::deleteLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind) {
// Bind the input parameters to the statement
// See how many rows were affected. Note that the statement may delete
// multiple rows.
- return (mysql_stmt_affected_rows(statements_[stindex]) > 0);
+ return (static_cast<uint64_t>(mysql_stmt_affected_rows(statements_[stindex])));
}
inbind[0].buffer = reinterpret_cast<char*>(&addr4);
inbind[0].is_unsigned = MLM_TRUE;
- return (deleteLeaseCommon(DELETE_LEASE4, inbind));
+ return (deleteLeaseCommon(DELETE_LEASE4, inbind) > 0);
} else {
std::string addr6 = addr.toText();
inbind[0].buffer_length = addr6_length;
inbind[0].length = &addr6_length;
- return (deleteLeaseCommon(DELETE_LEASE6, inbind));
+ return (deleteLeaseCommon(DELETE_LEASE6, inbind) > 0);
}
}
uint64_t
-MySqlLeaseMgr::deleteExpiredReclaimedLeases4(const uint32_t) {
- isc_throw(NotImplemented, "MySqlLeaseMgr::deleteExpiredReclaimedLeases4"
- " is not implemented");
+MySqlLeaseMgr::deleteExpiredReclaimedLeases4(const uint32_t secs) {
+ return (deleteExpiredReclaimedLeasesCommon(secs, DELETE_LEASE4_STATE_EXPIRED));
}
uint64_t
-MySqlLeaseMgr::deleteExpiredReclaimedLeases6(const uint32_t) {
- isc_throw(NotImplemented, "MySqlLeaseMgr::deleteExpiredReclaimedLeases6"
- " is not implemented");
+MySqlLeaseMgr::deleteExpiredReclaimedLeases6(const uint32_t secs) {
+ return (deleteExpiredReclaimedLeasesCommon(secs, DELETE_LEASE6_STATE_EXPIRED));
}
+uint64_t
+MySqlLeaseMgr::deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
+ StatementIndex statement_index) {
+ // Set up the WHERE clause value
+ MYSQL_BIND inbind[2];
+ memset(inbind, 0, sizeof(inbind));
+
+ uint32_t state = static_cast<uint32_t>(Lease::STATE_EXPIRED_RECLAIMED);
+ inbind[0].buffer_type = MYSQL_TYPE_LONG;
+ inbind[0].buffer = reinterpret_cast<char*>(&state);
+ inbind[0].is_unsigned = MLM_TRUE;
+
+ // Expiration timestamp.
+ MYSQL_TIME expire_time;
+ convertToDatabaseTime(time(NULL) - static_cast<time_t>(secs), expire_time);
+ inbind[1].buffer_type = MYSQL_TYPE_TIMESTAMP;
+ inbind[1].buffer = reinterpret_cast<char*>(&expire_time);
+ inbind[1].buffer_length = sizeof(expire_time);
+
+ return (deleteLeaseCommon(statement_index, inbind));
+}
+
+
// Miscellaneous database methods.
///
/// The contents of the enum are indexes into the list of SQL statements
enum StatementIndex {
- DELETE_LEASE4, // Delete from lease4 by address
- DELETE_LEASE6, // Delete from lease6 by address
- GET_LEASE4_ADDR, // Get lease4 by address
- GET_LEASE4_CLIENTID, // Get lease4 by client ID
- GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
- GET_LEASE4_HWADDR, // Get lease4 by HW address
- GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
- GET_LEASE4_EXPIRE, // Get lease4 by expiration.
- GET_LEASE6_ADDR, // Get lease6 by address
- GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
- GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
- GET_LEASE6_EXPIRE, // Get lease6 by expiration.
- GET_VERSION, // Obtain version number
- INSERT_LEASE4, // Add entry to lease4 table
- INSERT_LEASE6, // Add entry to lease6 table
- UPDATE_LEASE4, // Update a Lease4 entry
- UPDATE_LEASE6, // Update a Lease6 entry
- NUM_STATEMENTS // Number of statements
+ DELETE_LEASE4, // Delete from lease4 by address
+ DELETE_LEASE4_STATE_EXPIRED, // Delete expired lease4 in a given state
+ DELETE_LEASE6, // Delete from lease6 by address
+ DELETE_LEASE6_STATE_EXPIRED, // Delete expired lease6 in a given state
+ GET_LEASE4_ADDR, // Get lease4 by address
+ GET_LEASE4_CLIENTID, // Get lease4 by client ID
+ GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
+ GET_LEASE4_HWADDR, // Get lease4 by HW address
+ GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
+ GET_LEASE4_EXPIRE, // Get lease4 by expiration.
+ GET_LEASE6_ADDR, // Get lease6 by address
+ GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
+ GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
+ GET_LEASE6_EXPIRE, // Get lease6 by expiration.
+ GET_VERSION, // Obtain version number
+ INSERT_LEASE4, // Add entry to lease4 table
+ INSERT_LEASE6, // Add entry to lease6 table
+ UPDATE_LEASE4, // Update a Lease4 entry
+ UPDATE_LEASE6, // Update a Lease6 entry
+ NUM_STATEMENTS // Number of statements
};
private:
/// (Note that the number is determined by the number of parameters
/// in the statement.)
///
- /// @return true if one or more rows were deleted, false if none were
- /// deleted.
+ /// @return Number of deleted leases.
///
/// @throw isc::dhcp::DbOperationError An operation on the open database has
/// failed.
- bool deleteLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind);
+ uint64_t deleteLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind);
+
+ /// @brief Delete expired-reclaimed leases.
+ ///
+ /// @param secs Number of seconds since expiration of leases before
+ /// they can be removed. Leases which have expired later than this
+ /// time will not be deleted.
+ /// @param statement_index One of the @c DELETE_LEASE4_STATE_EXPIRED or
+ /// @c DELETE_LEASE6_STATE_EXPIRED.
+ ///
+ /// @return Number of leases deleted.
+ uint64_t deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
+ StatementIndex statement_index);
+
/// @brief Check Error and Throw Exception
///