Browse Source

[4294] Refactored PgSql stats classes

src/lib/dhcpsrv/pgsql_lease_mgr.h
src/lib/dhcpsrv/pgsql_lease_mgr.cc
        Replaced this class heirarchy:
        AddressStatsQuery4 <-- PgSqlAddressStatsQuery4
        AddressStatsQuery6 <-- PgSqlAddressStatsQuery6

        With this one:
        LeaseStatsQuery <-- PgSqlLeaseStatsQuery
Thomas Markwalder 8 years ago
parent
commit
92ffb41363

+ 41 - 122
src/lib/dhcpsrv/pgsql_lease_mgr.cc

@@ -694,34 +694,33 @@ private:
     //@}
     //@}
 };
 };
 
 
-/// @brief PgSql derivation of the IPv4 statistical lease data query
+/// @brief Base PgSql derivation of the statistical lease data query
 ///
 ///
-/// This class is used to recalculate IPv4 lease statistics for MySQL
-/// lease storage.  It does so by executing a query which returns a result
-/// containining contain one row per monitored state per subnet, ordered
-/// by subnet id in ascending order.
+/// This class provides the functionality such as results storgae and row
+/// fetching common to fulfilling the statistical lease data query.
 ///
 ///
-class PgSqlAddressStatsQuery4 : public AddressStatsQuery4 {
+class PgSqlLeaseStatsQuery : public LeaseStatsQuery {
 public:
 public:
     /// @brief Constructor
     /// @brief Constructor
     ///
     ///
     /// @param conn A open connection to the database housing the lease data
     /// @param conn A open connection to the database housing the lease data
-    PgSqlAddressStatsQuery4(PgSqlConnection& conn)
-        : conn_(conn), statement_(tagged_statements[PgSqlLeaseMgr
-                                                    ::RECOUNT_LEASE4_STATS]),
-        result_set_(), next_row_(0) {
+    /// @param statement The lease data SQL prepared statement to execute
+    /// @param fetch_statement Indicates whether or not lease_type should be
+    /// fetched from the result set
+    PgSqlLeaseStatsQuery(PgSqlConnection& conn, PgSqlTaggedStatement& statement,
+                         const bool fetch_type)
+        : conn_(conn), statement_(statement), result_set_(), next_row_(0),
+         fetch_type_(fetch_type) {
     }
     }
 
 
     /// @brief Destructor
     /// @brief Destructor
-    virtual ~PgSqlAddressStatsQuery4() {};
+    virtual ~PgSqlLeaseStatsQuery() {};
 
 
-    /// @brief Creates the IPv4 lease statistical data result set
+    /// @brief Creates the lease statistical data result set
     ///
     ///
-    /// The result set is populated by executing an SQL query against the
-    /// lease4 table which sums the leases per lease state per subnet id.
-    /// The query used is the prepared statement identified by
-    /// PgSqlLeaseMgr::RECOUNT_LEASE4_STATS. This method executes the
-    /// statement which creates the result set.
+    /// The result set is populated by executing a  prepared SQL query
+    /// against the database which sums the leases per lease state per
+    /// subnet id.
     void start() {
     void start() {
         // The query has no parameters, so we only need it's name.
         // The query has no parameters, so we only need it's name.
         result_set_.reset(new PgSqlResult(PQexecPrepared(conn_, statement_.name,
         result_set_.reset(new PgSqlResult(PQexecPrepared(conn_, statement_.name,
@@ -741,7 +740,7 @@ public:
     ///
     ///
     /// @return True if the fetch succeeded, false if there are no more
     /// @return True if the fetch succeeded, false if there are no more
     /// rows to fetch.
     /// rows to fetch.
-    bool getNextRow(AddressStatsRow4& row) {
+    bool getNextRow(LeaseStatsRow& row) {
         // If we're past the end, punt.
         // If we're past the end, punt.
         if (next_row_ >= result_set_->getRows()) {
         if (next_row_ >= result_set_->getRows()) {
             return (false);
             return (false);
@@ -754,6 +753,15 @@ public:
         row.subnet_id_ = static_cast<SubnetID>(subnet_id);
         row.subnet_id_ = static_cast<SubnetID>(subnet_id);
         ++col;
         ++col;
 
 
+        // Fetch the lease type if we were told to do so.
+        if (fetch_type_) {
+            uint32_t lease_type;
+            PgSqlExchange::getColumnValue(*result_set_, next_row_ , col,
+                                          lease_type);
+            row.lease_type_ = static_cast<Lease::Type>(lease_type);
+            ++col;
+        }
+
         // Fetch the lease state.
         // Fetch the lease state.
         uint32_t state;
         uint32_t state;
         PgSqlExchange::getColumnValue(*result_set_, next_row_ , col,
         PgSqlExchange::getColumnValue(*result_set_, next_row_ , col,
@@ -769,7 +777,7 @@ public:
         return (true);
         return (true);
     }
     }
 
 
-private:
+protected:
     /// @brief Database connection to use to execute the query
     /// @brief Database connection to use to execute the query
     PgSqlConnection& conn_;
     PgSqlConnection& conn_;
 
 
@@ -781,104 +789,9 @@ private:
 
 
     /// @brief Index of the next row to fetch
     /// @brief Index of the next row to fetch
     uint32_t next_row_;
     uint32_t next_row_;
-};
-
-/// @brief PgSql derivation of the IPv6 statistical lease data query
-///
-/// This class is used to recalculate IPv6 lease statistics for MySQL
-/// lease storage.  It does so by executing a query which returns a result
-/// containining contain one row per monitored state per subnet, ordered
-/// by subnet id in ascending order.
-///
-class PgSqlAddressStatsQuery6 : public AddressStatsQuery6 {
-public:
-    /// @brief Constructor
-    ///
-    /// @param conn A open connection to the database housing the lease data
-    PgSqlAddressStatsQuery6(PgSqlConnection& conn)
-        : conn_(conn), statement_(tagged_statements[PgSqlLeaseMgr
-                                                    ::RECOUNT_LEASE6_STATS]),
-        result_set_(), next_row_(0) {
-    }
-
-    /// @brief Destructor
-    virtual ~PgSqlAddressStatsQuery6() {};
-
-    /// @brief Creates the IPv6 lease statistical data result set
-    ///
-    /// The result set is populated by executing an SQL query against the
-    /// lease6 table which sums the leases per lease state per lease type
-    /// per subnet id.  The query used is the prepared statement identified by
-    /// PgSqlLeaseMgr::RECOUNT_LEASE6_STATS. This method executes the
-    /// statement which creates the result set.
-    void start() {
-        // The query has no parameters, so we only need it's name.
-        result_set_.reset(new PgSqlResult(PQexecPrepared(conn_, statement_.name,
-                                                         0, NULL, NULL, NULL,
-                                                         0)));
-
-        conn_.checkStatementError(*result_set_, statement_);
-    }
-
-    /// @brief Fetches the next row in the result set
-    ///
-    /// Once the internal result set has been populated by invoking the
-    /// the start() method, this method is used to iterate over the
-    /// result set rows. Once the last row has been fetched, subsequent
-    /// calls will return false.
-    ///
-    /// @param row Storage for the fetched row
-    ///
-    /// @return True if the fetch succeeded, false if there are no more
-    /// rows to fetch.
-    bool getNextRow(AddressStatsRow6& row) {
-        // If we're past the end, punt.
-        if (next_row_ >= result_set_->getRows()) {
-            return (false);
-        }
-
-        // Fetch the subnet id.
-        uint32_t col = 0;
-        uint32_t subnet_id;
-        PgSqlExchange::getColumnValue(*result_set_, next_row_, col, subnet_id);
-        row.subnet_id_ = static_cast<SubnetID>(subnet_id);
-        ++col;
 
 
-        // Fetch the lease type.
-        uint32_t lease_type;
-        PgSqlExchange::getColumnValue(*result_set_, next_row_ , col,
-                                      lease_type);
-        row.lease_type_ = static_cast<Lease::Type>(lease_type);
-        ++col;
-
-        // Fetch the lease state.
-        uint32_t state;
-        PgSqlExchange::getColumnValue(*result_set_, next_row_ , col, 
-                                      row.lease_state_);
-        ++col;
-
-        // Fetch the state count.
-        PgSqlExchange::getColumnValue(*result_set_, next_row_, col,
-                                      row.state_count_);
-
-         // Point to the next row.
-         ++next_row_;
-
-        return (true);
-    }
-
-private:
-    /// @brief Database connection to use to execute the query
-    PgSqlConnection& conn_;
-
-    /// @brief The query's prepared statement
-    PgSqlTaggedStatement& statement_;
-
-    /// @brief The result set returned by Postgres.
-    boost::shared_ptr<PgSqlResult> result_set_;
-
-    /// @brief Index of the next row to fetch
-    uint32_t next_row_;
+    /// @brief Indicates if query supplies lease type
+    bool fetch_type_;
 };
 };
 
 
 PgSqlLeaseMgr::PgSqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters)
 PgSqlLeaseMgr::PgSqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters)
@@ -1422,16 +1335,22 @@ PgSqlLeaseMgr::deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
     return (deleteLeaseCommon(statement_index, bind_array));
     return (deleteLeaseCommon(statement_index, bind_array));
 }
 }
 
 
-AddressStatsQuery4Ptr
-PgSqlLeaseMgr::startAddressStatsQuery4() {
-    AddressStatsQuery4Ptr query(new PgSqlAddressStatsQuery4(conn_));
+LeaseStatsQueryPtr
+PgSqlLeaseMgr::startLeaseStatsQuery4() {
+    LeaseStatsQueryPtr query(
+        new PgSqlLeaseStatsQuery(conn_,
+                                 tagged_statements[RECOUNT_LEASE4_STATS],
+                                 false));
     query->start();
     query->start();
     return(query);
     return(query);
 }
 }
 
 
-AddressStatsQuery6Ptr
-PgSqlLeaseMgr::startAddressStatsQuery6() {
-    AddressStatsQuery6Ptr query(new PgSqlAddressStatsQuery6(conn_));
+LeaseStatsQueryPtr
+PgSqlLeaseMgr::startLeaseStatsQuery6() {
+    LeaseStatsQueryPtr query(
+        new PgSqlLeaseStatsQuery(conn_,
+                                 tagged_statements[RECOUNT_LEASE6_STATS],
+                                 true));
     query->start();
     query->start();
     return(query);
     return(query);
 }
 }

+ 6 - 6
src/lib/dhcpsrv/pgsql_lease_mgr.h

@@ -319,23 +319,23 @@ public:
 
 
     /// @brief Creates and runs the IPv4 lease stats query
     /// @brief Creates and runs the IPv4 lease stats query
     ///
     ///
-    /// It creates an instance of a PgSqlAddressStatsQuery4 and then
+    /// It creates an instance of a PgSqlLeaseStatsQuery4 and then
     /// invokes its start method, which fetches its statistical data
     /// invokes its start method, which fetches its statistical data
     /// result set by executing the RECOUNT_LEASE_STATS4 query.
     /// result set by executing the RECOUNT_LEASE_STATS4 query.
     /// The query object is then returned.
     /// The query object is then returned.
     /// 
     /// 
-    /// @return The populated query as a pointer to an AddressStatsQuery4
-    virtual AddressStatsQuery4Ptr startAddressStatsQuery4();
+    /// @return The populated query as a pointer to an LeaseStatsQuery
+    virtual LeaseStatsQueryPtr startLeaseStatsQuery4();
 
 
     /// @brief Creates and runs the IPv6 lease stats query
     /// @brief Creates and runs the IPv6 lease stats query
     ///
     ///
-    /// It creates an instance of a PgSqlAddressStatsQuery6 and then
+    /// It creates an instance of a PgSqlLeaseStatsQuery and then
     /// invokes its start method, which fetches its statistical data
     /// invokes its start method, which fetches its statistical data
     /// result set by executing the RECOUNT_LEASE_STATS6 query.
     /// result set by executing the RECOUNT_LEASE_STATS6 query.
     /// The query object is then returned.
     /// The query object is then returned.
     /// 
     /// 
-    /// @return The populated query as a pointer to an AddressStatsQuery6
-    virtual AddressStatsQuery6Ptr startAddressStatsQuery6();
+    /// @return The populated query as a pointer to an LeaseStatsQuery
+    virtual LeaseStatsQueryPtr startLeaseStatsQuery6();
 
 
     /// @brief Return backend type
     /// @brief Return backend type
     ///
     ///

+ 4 - 4
src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc

@@ -403,13 +403,13 @@ TEST_F(PgSqlLeaseMgrTest, getExpiredLeases6) {
 }
 }
 
 
 // Verifies that IPv4 lease statistics can be recalculated.
 // Verifies that IPv4 lease statistics can be recalculated.
-TEST_F(PgSqlLeaseMgrTest, recountAddressStats4) {
-    testRecountAddressStats4();
+TEST_F(PgSqlLeaseMgrTest, recountLeaseStats4) {
+    testRecountLeaseStats4();
 }
 }
 
 
 // Verifies that IPv6 lease statistics can be recalculated.
 // Verifies that IPv6 lease statistics can be recalculated.
-TEST_F(PgSqlLeaseMgrTest, recountAddressStats6) {
-    testRecountAddressStats6();
+TEST_F(PgSqlLeaseMgrTest, recountLeaseStats6) {
+    testRecountLeaseStats6();
 }
 }
 
 
 }; // namespace
 }; // namespace