You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2020/04/08 21:54:10 UTC

[couchdb] branch cleanup-expiring-key-on-update created (now 0c5a489)

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

jaydoane pushed a change to branch cleanup-expiring-key-on-update
in repository https://gitbox.apache.org/repos/asf/couchdb.git.


      at 0c5a489  Clean up existing expiry key on update

This branch includes the following new commits:

     new 0c5a489  Clean up existing expiry key on update

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[couchdb] 01/01: Clean up existing expiry key on update

Posted by ja...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jaydoane pushed a commit to branch cleanup-expiring-key-on-update
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 0c5a4896690a3d38dcfa523fe65154cad26e0df9
Author: Jay Doane <ja...@apache.org>
AuthorDate: Wed Apr 8 14:53:51 2020 -0700

    Clean up existing expiry key on update
    
    When an existing key is inserted with different timestamps, the primary
    key is the same but the primary value is different from the existing one.
    Currently, this results in a new expiry key being inserted, but the old
    one is not deleted, and lingers until it is removed by the inexorable
    advance of time via the `clear_expired/5` function.
    
    This checks whether there's already primary key for the inserted key,
    and if so, cleans up the existing expiry key before proceeding with the
    insert.
---
 src/couch_expiring_cache/src/couch_expiring_cache_fdb.erl | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/couch_expiring_cache/src/couch_expiring_cache_fdb.erl b/src/couch_expiring_cache/src/couch_expiring_cache_fdb.erl
index fa8508e..3a76b88 100644
--- a/src/couch_expiring_cache/src/couch_expiring_cache_fdb.erl
+++ b/src/couch_expiring_cache/src/couch_expiring_cache_fdb.erl
@@ -40,10 +40,21 @@
 insert(#{jtx := true} = JTx, Name, Key, Val, StaleTS, ExpiresTS) ->
     #{tx := Tx, layer_prefix := LayerPrefix} = couch_jobs_fdb:get_jtx(JTx),
     PK = primary_key(Name, Key, LayerPrefix),
+    case erlfdb:wait(erlfdb:get(Tx, PK)) of
+        not_found ->
+            ok;
+        Bin when is_binary(Bin) ->
+            %% Clean up existing expiry key. No need to clean up the
+            %% existing primary key since it will be overwritten
+            %% below with a new value.
+            {_OldVal, _OldStaleTS, OldExpiresTS} = erlfdb_tuple:unpack(Bin),
+            OldXK = expiry_key(OldExpiresTS, Name, Key, LayerPrefix),
+            ok = erlfdb:clear(Tx, OldXK)
+    end,
     PV = erlfdb_tuple:pack({Val, StaleTS, ExpiresTS}),
+    ok = erlfdb:set(Tx, PK, PV),
     XK = expiry_key(ExpiresTS, Name, Key, LayerPrefix),
     XV = erlfdb_tuple:pack({}),
-    ok = erlfdb:set(Tx, PK, PV),
     ok = erlfdb:set(Tx, XK, XV).