Browse Source

[3965] Use tags rather than numbers to access memfile indexes.

Marcin Siodelski 9 years ago
parent
commit
f4f12277ec
2 changed files with 62 additions and 43 deletions
  1. 31 43
      src/lib/dhcpsrv/memfile_lease_mgr.cc
  2. 31 0
      src/lib/dhcpsrv/memfile_lease_storage.h

+ 31 - 43
src/lib/dhcpsrv/memfile_lease_mgr.cc

@@ -320,10 +320,9 @@ Memfile_LeaseMgr::getLease4(const isc::asiolink::IOAddress& addr) const {
     LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
               DHCPSRV_MEMFILE_GET_ADDR4).arg(addr.toText());
 
-    typedef Lease4Storage::nth_index<0>::type SearchIndex;
-    const SearchIndex& idx = storage4_.get<0>();
-    Lease4Storage::iterator l = idx.find(addr);
-    if (l == storage4_.end()) {
+    const Lease4StorageAddressIndex& idx = storage4_.get<AddressIndexTag>();
+    Lease4StorageAddressIndex::iterator l = idx.find(addr);
+    if (l == idx.end()) {
         return (Lease4Ptr());
     } else {
         return (Lease4Ptr(new Lease4(**l)));
@@ -334,10 +333,9 @@ Lease4Collection
 Memfile_LeaseMgr::getLease4(const HWAddr& hwaddr) const {
     LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
               DHCPSRV_MEMFILE_GET_HWADDR).arg(hwaddr.toText());
-    typedef Lease4Storage::nth_index<0>::type SearchIndex;
     Lease4Collection collection;
-    const SearchIndex& idx = storage4_.get<0>();
-    for(SearchIndex::const_iterator lease = idx.begin();
+    const Lease4StorageAddressIndex& idx = storage4_.get<AddressIndexTag>();
+    for(Lease4StorageAddressIndex::const_iterator lease = idx.begin();
         lease != idx.end(); ++lease) {
 
         // Every Lease4 has a hardware address, so we can compare it
@@ -355,14 +353,11 @@ Memfile_LeaseMgr::getLease4(const HWAddr& hwaddr, SubnetID subnet_id) const {
               DHCPSRV_MEMFILE_GET_SUBID_HWADDR).arg(subnet_id)
         .arg(hwaddr.toText());
 
-    // We are going to use index #1 of the multi index container.
-    // We define SearchIndex locally in this function because
-    // currently only this function uses this index.
-    typedef Lease4Storage::nth_index<1>::type SearchIndex;
-    // Get the index.
-    const SearchIndex& idx = storage4_.get<1>();
+    // Get the index by HW Address and Subnet Identifier.
+    const Lease4StorageHWAddressSubnetIdIndex& idx =
+        storage4_.get<HWAddressSubnetIdIndexTag>();
     // Try to find the lease using HWAddr and subnet id.
-    SearchIndex::const_iterator lease =
+    Lease4StorageHWAddressSubnetIdIndex::const_iterator lease =
         idx.find(boost::make_tuple(hwaddr.hwaddr_, subnet_id));
     // Lease was not found. Return empty pointer to the caller.
     if (lease == idx.end()) {
@@ -377,10 +372,9 @@ Lease4Collection
 Memfile_LeaseMgr::getLease4(const ClientId& client_id) const {
     LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
               DHCPSRV_MEMFILE_GET_CLIENTID).arg(client_id.toText());
-    typedef Lease4Storage::nth_index<0>::type SearchIndex;
     Lease4Collection collection;
-    const SearchIndex& idx = storage4_.get<0>();
-    for(SearchIndex::const_iterator lease = idx.begin();
+    const Lease4StorageAddressIndex& idx = storage4_.get<AddressIndexTag>();
+    for(Lease4StorageAddressIndex::const_iterator lease = idx.begin();
         lease != idx.end(); ++ lease) {
 
         // client-id is not mandatory in DHCPv4. There can be a lease that does
@@ -402,14 +396,11 @@ Memfile_LeaseMgr::getLease4(const ClientId& client_id,
                                                         .arg(hwaddr.toText())
                                                         .arg(subnet_id);
 
-    // We are going to use index #3 of the multi index container.
-    // We define SearchIndex locally in this function because
-    // currently only this function uses this index.
-    typedef Lease4Storage::nth_index<3>::type SearchIndex;
-    // Get the index.
-    const SearchIndex& idx = storage4_.get<3>();
+    // Get the index by client id, HW address and subnet id.
+    const Lease4StorageClientIdHWAddressSubnetIdIndex& idx =
+        storage4_.get<ClientIdHWAddressSubnetIdIndexTag>();
     // Try to get the lease using client id, hardware address and subnet id.
-    SearchIndex::const_iterator lease =
+    Lease4StorageClientIdHWAddressSubnetIdIndex::const_iterator lease =
         idx.find(boost::make_tuple(client_id.getClientId(), hwaddr.hwaddr_,
                                    subnet_id));
 
@@ -429,14 +420,11 @@ Memfile_LeaseMgr::getLease4(const ClientId& client_id,
               DHCPSRV_MEMFILE_GET_SUBID_CLIENTID).arg(subnet_id)
               .arg(client_id.toText());
 
-    // We are going to use index #2 of the multi index container.
-    // We define SearchIndex locally in this function because
-    // currently only this function uses this index.
-    typedef Lease4Storage::nth_index<2>::type SearchIndex;
-    // Get the index.
-    const SearchIndex& idx = storage4_.get<2>();
+    // Get the index by client and subnet id.
+    const Lease4StorageClientIdSubnetIdIndex& idx =
+        storage4_.get<ClientIdSubnetIdIndexTag>();
     // Try to get the lease using client id and subnet id.
-    SearchIndex::const_iterator lease =
+    Lease4StorageClientIdSubnetIdIndex::const_iterator lease =
         idx.find(boost::make_tuple(client_id.getClientId(), subnet_id));
     // Lease was not found. Return empty pointer to the caller.
     if (lease == idx.end()) {
@@ -470,15 +458,15 @@ Memfile_LeaseMgr::getLeases6(Lease::Type type,
         .arg(duid.toText())
         .arg(Lease::typeToText(type));
 
-    // We are going to use index #1 of the multi index container.
-    typedef Lease6Storage::nth_index<1>::type SearchIndex;
-    // Get the index.
-    const SearchIndex& idx = storage6_.get<1>();
+    // Get the index by DUID, IAID, lease type.
+    const Lease6StorageDuidIaidTypeIndex& idx = storage6_.get<DuidIaidTypeIndexTag>();
     // Try to get the lease using the DUID, IAID and lease type.
-    std::pair<SearchIndex::iterator, SearchIndex::iterator> l =
+    std::pair<Lease6StorageDuidIaidTypeIndex::const_iterator,
+              Lease6StorageDuidIaidTypeIndex::const_iterator> l =
         idx.equal_range(boost::make_tuple(duid.getDuid(), iaid, type));
     Lease6Collection collection;
-    for(SearchIndex::iterator lease = l.first; lease != l.second; ++lease) {
+    for(Lease6StorageDuidIaidTypeIndex::const_iterator lease =
+            l.first; lease != l.second; ++lease) {
         collection.push_back(Lease6Ptr(new Lease6(**lease)));
     }
 
@@ -496,15 +484,15 @@ Memfile_LeaseMgr::getLeases6(Lease::Type type,
         .arg(duid.toText())
         .arg(Lease::typeToText(type));
 
-    // We are going to use index #1 of the multi index container.
-    typedef Lease6Storage::nth_index<1>::type SearchIndex;
-    // Get the index.
-    const SearchIndex& idx = storage6_.get<1>();
+    // Get the index by DUID, IAID, lease type.
+    const Lease6StorageDuidIaidTypeIndex& idx = storage6_.get<DuidIaidTypeIndexTag>();
     // Try to get the lease using the DUID, IAID and lease type.
-    std::pair<SearchIndex::iterator, SearchIndex::iterator> l =
+    std::pair<Lease6StorageDuidIaidTypeIndex::const_iterator,
+              Lease6StorageDuidIaidTypeIndex::const_iterator> l =
         idx.equal_range(boost::make_tuple(duid.getDuid(), iaid, type));
     Lease6Collection collection;
-    for(SearchIndex::iterator lease = l.first; lease != l.second; ++lease) {
+    for(Lease6StorageDuidIaidTypeIndex::const_iterator lease =
+            l.first; lease != l.second; ++lease) {
         // Filter out the leases which subnet id doesn't match.
         if((*lease)->subnet_id_ == subnet_id) {
             collection.push_back(Lease6Ptr(new Lease6(**lease)));

+ 31 - 0
src/lib/dhcpsrv/memfile_lease_storage.h

@@ -35,9 +35,21 @@ namespace dhcp {
 /// @brief Tag for indexes by address.
 struct AddressIndexTag { };
 
+/// @brief Tag for indexes by DUID, IAID, lease type tuple.
+struct DuidIaidTypeIndexTag { };
+
 /// @brief Tag for indexes by expiration time.
 struct ExpirationIndexTag { };
 
+/// @brief Tag for indexes by HW address, subnet identifier tuple.
+struct HWAddressSubnetIdIndexTag { };
+
+/// @brief Tag for indexes by client and subnet identifiers.
+struct ClientIdSubnetIdIndexTag { };
+
+/// @brief Tag for indexes by client id, HW address and subnet id.
+struct ClientIdHWAddressSubnetIdIndexTag { };
+
 /// @name Multi index containers holding DHCPv4 and DHCPv6 leases.
 ///
 //@{
@@ -61,6 +73,7 @@ typedef boost::multi_index_container<
 
         // Specification of the second index starts here.
         boost::multi_index::ordered_non_unique<
+            boost::multi_index::tag<DuidIaidTypeIndexTag>,
             // This is a composite index that will be used to search for
             // the lease using three attributes: DUID, IAID and lease type.
             boost::multi_index::composite_key<
@@ -120,6 +133,7 @@ typedef boost::multi_index_container<
 
         // Specification of the second index starts here.
         boost::multi_index::ordered_non_unique<
+            boost::multi_index::tag<HWAddressSubnetIdIndexTag>,
             // This is a composite index that combines two attributes of the
             // Lease4 object: hardware address and subnet id.
             boost::multi_index::composite_key<
@@ -140,6 +154,7 @@ typedef boost::multi_index_container<
 
         // Specification of the third index starts here.
         boost::multi_index::ordered_non_unique<
+            boost::multi_index::tag<ClientIdSubnetIdIndexTag>,
             // This is a composite index that uses two values to search for a
             // lease: client id and subnet id.
             boost::multi_index::composite_key<
@@ -155,6 +170,7 @@ typedef boost::multi_index_container<
 
         // Specification of the fourth index starts here.
         boost::multi_index::ordered_non_unique<
+            boost::multi_index::tag<ClientIdHWAddressSubnetIdIndexTag>,
             // This is a composite index that uses three values to search for a
             // lease: client id, HW address and subnet id.
             boost::multi_index::composite_key<
@@ -203,6 +219,9 @@ typedef boost::multi_index_container<
 /// @brief DHCPv6 lease storage index by address.
 typedef Lease6Storage::index<AddressIndexTag>::type Lease6StorageAddressIndex;
 
+/// @brief DHCPv6 lease storage index by DUID, IAID, lease type.
+typedef Lease6Storage::index<DuidIaidTypeIndexTag>::type Lease6StorageDuidIaidTypeIndex;
+
 /// @brief DHCPv6 lease storage index by expiration time.
 typedef Lease6Storage::index<ExpirationIndexTag>::type Lease6StorageExpirationIndex;
 
@@ -212,6 +231,18 @@ typedef Lease4Storage::index<AddressIndexTag>::type Lease4StorageAddressIndex;
 /// @brief DHCPv4 lease storage index by exiration time.
 typedef Lease4Storage::index<ExpirationIndexTag>::type Lease4StorageExpirationIndex;
 
+/// @brief DHCPv4 lease storage index by HW address and subnet identifier.
+typedef Lease4Storage::index<HWAddressSubnetIdIndexTag>::type
+Lease4StorageHWAddressSubnetIdIndex;
+
+/// @brief DHCPv4 lease storage index by client and subnet identifier.
+typedef Lease4Storage::index<ClientIdSubnetIdIndexTag>::type
+Lease4StorageClientIdSubnetIdIndex;
+
+/// @brief DHCPv4 lease storage index by client id, HW address and subnet id.
+typedef Lease4Storage::index<ClientIdHWAddressSubnetIdIndexTag>::type
+Lease4StorageClientIdHWAddressSubnetIdIndex;
+
 //@}
 } // end of isc::dhcp namespace
 } // end of isc namespace