Make insert an upsert

The old couch_lru would blindly overwrite entries in the LRU possibly
leaving old entries in the gb_tree instance. This detects the situation
and accounts for it.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/3a040351
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/3a040351
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/3a040351

Branch: refs/heads/HACK-benchmark-COUCHDB-3191-improve-couch-lru-performance
Commit: 3a04035128835dc333614a65c7ee274ff6925678
Parents: 8ca26a8
Author: Paul J. Davis <paul.joseph.da...@gmail.com>
Authored: Mon Oct 17 15:30:19 2016 -0500
Committer: Paul J. Davis <paul.joseph.da...@gmail.com>
Committed: Mon Oct 17 15:30:19 2016 -0500

----------------------------------------------------------------------
 src/couch_lru.erl | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/3a040351/src/couch_lru.erl
----------------------------------------------------------------------
diff --git a/src/couch_lru.erl b/src/couch_lru.erl
index c5c13e8..fa68733 100644
--- a/src/couch_lru.erl
+++ b/src/couch_lru.erl
@@ -64,22 +64,26 @@ insert(DbName, Lru) ->
     Node = #node{
         dbname = DbName
     },
-    Ref = erlang:make_ref(),
-    not_found = khash:lookup(Dbs, DbName),
-    ok = khash:put(Dbs, DbName, Ref),
-    ok = khash:put(Nodes, Ref, Node),
-    {NewHead, NewTail} = case {Head, Tail} of
-        {undefined, undefined} ->
-            {Ref, Ref};
-        {_, _} when is_reference(Head), is_reference(Tail) ->
-            set_next(Lru, Tail, Ref),
-            set_prev(Lru, Ref, Tail),
-            {Head, Ref}
-    end,
-    Lru#lru{
-        head = NewHead,
-        tail = NewTail
-    }.
+    case khash:lookup(Dbs, DbName) of
+        {value, Ref} when is_reference(Ref) ->
+            update(DbName, Lru);
+        not_found ->
+            Ref = erlang:make_ref(),
+            ok = khash:put(Dbs, DbName, Ref),
+            ok = khash:put(Nodes, Ref, Node),
+            {NewHead, NewTail} = case {Head, Tail} of
+                {undefined, undefined} ->
+                    {Ref, Ref};
+                {_, _} when is_reference(Head), is_reference(Tail) ->
+                    set_next(Lru, Tail, Ref),
+                    set_prev(Lru, Ref, Tail),
+                    {Head, Ref}
+            end,
+            Lru#lru{
+                head = NewHead,
+                tail = NewTail
+            }
+    end.
 
 
 update(DbName, Lru) ->

Reply via email to