changeset 437:2901b95ab462

db_impl: Handle error_if_exists option better.
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 17 Dec 2017 17:58:35 +0100
parents 207bae330c9d
children 4ae2a643666d
files src/db_impl.rs
diffstat 1 files changed, 9 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/db_impl.rs	Sun Dec 17 17:51:48 2017 +0100
+++ b/src/db_impl.rs	Sun Dec 17 17:58:35 2017 +0100
@@ -92,9 +92,11 @@
         self.vset.borrow().current()
     }
 
-    /// Opens or creates* a new or existing database.
+    /// Opens or creates a new or existing database. `name` is the name of the directory containing
+    /// the database.
     ///
-    /// *depending on the options set (create_if_missing, error_if_exists).
+    /// Whether a new database is created and what happens if a database exists at the given path
+    /// depends on the options set (`create_if_missing`, `error_if_exists`).
     pub fn open(name: &str, opt: Options) -> Result<DB> {
         let mut db = DB::new(name, opt);
         let mut ve = VersionEdit::new();
@@ -141,6 +143,10 @@
     /// recover recovers from the existing state on disk. If the wrapped result is `true`, then
     /// log_and_apply() should be called after recovery has finished.
     fn recover(&mut self, ve: &mut VersionEdit) -> Result<bool> {
+        if self.opt.error_if_exists && self.opt.env.exists(&self.name.as_ref()).unwrap_or(false) {
+            return err(StatusCode::AlreadyExists, "database already exists");
+        }
+
         self.opt.env.mkdir(Path::new(&self.name)).is_ok();
         self.acquire_lock()?;
 
@@ -151,9 +157,6 @@
                 return err(StatusCode::InvalidArgument,
                            "database does not exist and create_if_missing is false");
             }
-        } else if self.opt.error_if_exists {
-            return err(StatusCode::InvalidArgument,
-                       "database already exists and error_if_exists is true");
         }
 
         // If save_manifest is true, we should log_and_apply() later in order to write the new
@@ -947,6 +950,7 @@
         let mut builder = TableBuilder::new(opt.clone(), f);
         while from.advance() {
             assert!(from.current(&mut kbuf, &mut vbuf));
+            i += 1;
             if firstkey.is_none() {
                 firstkey = Some(kbuf.clone());
             }