This is an automated email from the ASF dual-hosted git repository.

jiangphcn pushed a commit to branch softdeletion
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit d701795eb27d37c9defab800a5d30a53f3d23ee0
Author: jiangph <[email protected]>
AuthorDate: Wed Mar 11 19:02:40 2020 +0800

    soft-deletion for database
---
 src/fabric/include/fabric2.hrl |  1 +
 src/fabric/src/fabric2_fdb.erl | 19 ++++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/fabric/include/fabric2.hrl b/src/fabric/include/fabric2.hrl
index 189995d..62d847b 100644
--- a/src/fabric/include/fabric2.hrl
+++ b/src/fabric/include/fabric2.hrl
@@ -21,6 +21,7 @@
 
 -define(CLUSTER_CONFIG, 0).
 -define(ALL_DBS, 1).
+-define(DELETED_DBS, 2).
 -define(DBS, 15).
 -define(TX_IDS, 255).
 
diff --git a/src/fabric/src/fabric2_fdb.erl b/src/fabric/src/fabric2_fdb.erl
index fb2891b..672c988 100644
--- a/src/fabric/src/fabric2_fdb.erl
+++ b/src/fabric/src/fabric2_fdb.erl
@@ -148,7 +148,8 @@ create(#{} = Db0, Options) ->
     % Eventually DbPrefix will be HCA allocated. For now
     % we're just using the DbName so that debugging is easier.
     DbKey = erlfdb_tuple:pack({?ALL_DBS, DbName}, LayerPrefix),
-    DbPrefix = erlfdb_tuple:pack({?DBS, DbName}, LayerPrefix),
+    #{allocator := Allocator} = erlfdb_directory:root(),
+    DbPrefix = erlfdb_hca:allocate(Allocator, Tx),
     erlfdb:set(Tx, DbKey, DbPrefix),
 
     % This key is responsible for telling us when something in
@@ -285,8 +286,20 @@ delete(#{} = Db) ->
     } = ensure_current(Db),
 
     DbKey = erlfdb_tuple:pack({?ALL_DBS, DbName}, LayerPrefix),
-    erlfdb:clear(Tx, DbKey),
-    erlfdb:clear_range_startswith(Tx, DbPrefix),
+    DoRecovery = config:get_boolean("couchdb",
+        "enable_database_recovery", false),
+    case DoRecovery of
+        true ->
+            {Mega, Secs, _} = os:timestamp(),
+            NowSecs = Mega * 1000000 + Secs,
+            NewDbKey = erlfdb_tuple:pack({?DELETED_DBS, DbName, NowSecs},
+                LayerPrefix),
+            erlfdb:set(Tx, NewDbKey, DbPrefix),
+            erlfdb:clear(Tx, DbKey);
+        false ->
+            erlfdb:clear(Tx, DbKey),
+            erlfdb:clear_range_startswith(Tx, DbPrefix)
+    end,
     bump_metadata_version(Tx),
     ok.
 

Reply via email to