Parcourir la source

[3671] Addressed second round of review comments.

Simplified the method which loads leases from the CSV file.
Marcin Siodelski il y a 10 ans
Parent
commit
a13e1e0d36
1 fichiers modifiés avec 25 ajouts et 47 suppressions
  1. 25 47
      src/lib/dhcpsrv/memfile_lease_mgr.cc

+ 25 - 47
src/lib/dhcpsrv/memfile_lease_mgr.cc

@@ -481,57 +481,35 @@ loadLeasesFromFiles(const std::string& filename,
                     StorageType& storage) {
     storage.clear();
 
-    for (int i = 2; i >= 0; --i) {
-        bool completed_exists = false;
-
-        // Initialize the name of the lease file to parse. For the
-        // first two loops we're going to append .2 and .1 to the
-        // lease file name, unless the file with the .completed
-        // postfix exists.
-        std::ostringstream s;
-        s << filename;
-
-        // If this is the first file to load leases from, we should check
-        // if the file with the .completed prefix exists. If it exists
-        // we are going to skip loading leases from the files with
-        // postfixes .2 and .1.
-        if (i == 2) {
-            s << ".completed";
-            lease_file.reset(new LeaseFileType(s.str()));
-            // If the .completed file exists, reduce the value of i to
-            // skip loop for the file with postfix .1. We will only
-            // load leases from the .completed file and the lease
-            // file without postfix (for i = 0).
-            if (lease_file->exists()) {
-                --i;
-                completed_exists = true;
-            }
-        }
+    // Load the leasefile.completed, if exists.
+    lease_file.reset(new LeaseFileType(std::string(filename + ".completed")));
+    if (lease_file->exists()) {
+        LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
+                                               MAX_LEASE_ERRORS);
 
-        // If .completed file doesn't exist, load the files with postfixes
-        // .1 and .2.
-        if (!completed_exists) {
-            std::ostringstream s2;
-            s2 << filename;
-            if (i > 0) {
-                s2 << "." << i;
-            }
-            lease_file.reset(new LeaseFileType(s2.str()));
+    } else {
+        // If the leasefile.completed doesn't exist, let's load the leases
+        // from leasefile.2 and leasefile.1, if they exist.
+        lease_file.reset(new LeaseFileType(std::string(filename + ".2")));
+        if (lease_file->exists()) {
+            LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
+                                                   MAX_LEASE_ERRORS);
         }
 
-        // Don't open the file if it doesn't exist and it is not the
-        // primary lease file - not ending with .1 or .2 or .completed.
-        // Those files are optional and we don't want to create them if
-        // they don't exist.
-        if (i == 0 || lease_file->exists()) {
-            // If the file doesn't exist it will be created as an empty
-            // file (with no leases). The last parameter is set to false
-            // when the primary lease file is parsed. This is to
-            // indicate that the lease file should remain open after
-            // parsing. The backend will use this file to append future
-            // lease updates.
+        lease_file.reset(new LeaseFileType(std::string(filename + ".1")));
+        if (lease_file->exists()) {
             LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
-                                                   MAX_LEASE_ERRORS, (i != 0));
+                                                   MAX_LEASE_ERRORS);
         }
     }
+
+    // Always load leases from the primary lease file. If the lease file
+    // doesn't exist it will be created by the LeaseFileLoader. Note
+    // that the false value passed as the last parameter to load
+    // function causes the function to leave the file open after
+    // it is parsed. This file will be used by the backend to record
+    // future lease updates.
+    lease_file.reset(new LeaseFileType(filename));
+    LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
+                                           MAX_LEASE_ERRORS, false);;
 }