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

[couchdb] 09/23: Refactor mango indexer hook

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

garren pushed a commit to branch fdb-mango-indexes
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit cb383ed5c266dc568c9eda1dcb47c0b90d1f545c
Author: Jay Doane <ja...@apache.org>
AuthorDate: Sun Feb 2 20:33:31 2020 -0800

    Refactor mango indexer hook
---
 src/fabric/src/fabric2_fdb.erl  |  8 ++---
 src/mango/src/mango_indexer.erl | 68 +++++++++++++++++++++++------------------
 2 files changed, 43 insertions(+), 33 deletions(-)

diff --git a/src/fabric/src/fabric2_fdb.erl b/src/fabric/src/fabric2_fdb.erl
index fcee404..130901a 100644
--- a/src/fabric/src/fabric2_fdb.erl
+++ b/src/fabric/src/fabric2_fdb.erl
@@ -765,14 +765,14 @@ write_doc(#{} = Db0, Doc, NewWinner0, OldWinner, ToUpdate, ToRemove) ->
                 incr_stat(Db, <<"doc_design_count">>, 1)
             end,
             incr_stat(Db, <<"doc_count">>, 1),
-            mango_indexer:update(Db, created, Doc, not_found);
+            mango_indexer:create_doc(Db, Doc);
         recreated ->
             if not IsDDoc -> ok; true ->
                 incr_stat(Db, <<"doc_design_count">>, 1)
             end,
             incr_stat(Db, <<"doc_count">>, 1),
             incr_stat(Db, <<"doc_del_count">>, -1),
-            mango_indexer:update(Db, created, Doc, not_found);
+            mango_indexer:create_doc(Db, Doc);
         replicate_deleted ->
             incr_stat(Db, <<"doc_del_count">>, 1);
         ignore ->
@@ -783,9 +783,9 @@ write_doc(#{} = Db0, Doc, NewWinner0, OldWinner, ToUpdate, ToRemove) ->
             end,
             incr_stat(Db, <<"doc_count">>, -1),
             incr_stat(Db, <<"doc_del_count">>, 1),
-            mango_indexer:update(Db, deleted, not_found, PrevDoc);
+            mango_indexer:delete_doc(Db, PrevDoc);
         updated ->
-            mango_indexer:update(Db, updated, Doc, PrevDoc)
+            mango_indexer:update_doc(Db, Doc, PrevDoc)
     end,
 
     % Update database size
diff --git a/src/mango/src/mango_indexer.erl b/src/mango/src/mango_indexer.erl
index 0cb15f7..66dae63 100644
--- a/src/mango/src/mango_indexer.erl
+++ b/src/mango/src/mango_indexer.erl
@@ -15,7 +15,9 @@
 
 
 -export([
-    update/4
+    create_doc/2,
+    update_doc/3,
+    delete_doc/2
 ]).
 
 
@@ -23,9 +25,21 @@
 -include("mango_idx.hrl").
 
 
-update(Db, State, Doc, PrevDoc) ->
+create_doc(Db, Doc) ->
+    modify(Db, create, Doc, undefined).
+
+
+update_doc(Db, Doc, PrevDoc) ->
+    modify(Db, update, Doc, PrevDoc).
+
+
+delete_doc(Db, PrevDoc) ->
+    modify(Db, delete, undefined, PrevDoc).
+
+
+modify(Db, Change, Doc, PrevDoc) ->
     try
-        update_int(Db, State, Doc, PrevDoc)
+        modify_int(Db, Change, Doc, PrevDoc)
     catch
         Error:Reason ->
             io:format("ERROR ~p ~p ~p ~n", [Error, Reason, erlang:display(erlang:get_stacktrace())]),
@@ -33,44 +47,40 @@ update(Db, State, Doc, PrevDoc) ->
                 name := DbName
             } = Db,
 
-            Id = case Doc of
-                not_found when is_record(PrevDoc, doc) ->
-                    #doc{id = DocId} = PrevDoc,
-                    DocId;
-                not_found ->
-                    <<"unknown_doc_id">>;
-                #doc{} ->
-                    #doc{id = DocId} = Doc,
-                    DocId
-            end,
+            Id = doc_id(Doc, PrevDoc),
 
             couch_log:error("Mango index error for Db ~s Doc ~p ~p ~p",
                 [DbName, Id, Error, Reason])
     end,
     ok.
 
+
+doc_id(undefined, #doc{id = DocId}) ->
+    DocId;
+doc_id(undefined, _) ->
+    <<"unknown_doc_id">>;
+doc_id(#doc{id = DocId}, _) ->
+    DocId.
+
+
 % Design doc
 % Todo: Check if design doc is mango index and kick off background worker
 % to build new index
-update_int(Db, State, #doc{id = <<?DESIGN_DOC_PREFIX, _/binary>>} = Doc, PrevDoc) ->
+modify_int(_Db, _Change, #doc{id = <<?DESIGN_DOC_PREFIX, _/binary>>} = Doc,
+        _PrevDoc) ->
     io:format("DESIGN DOC SAVED ~p ~n", [Doc]),
     ok;
 
-update_int(Db, deleted, _, PrevDoc)  ->
-    Indexes = mango_idx:list(Db),
-    Indexes1 = filter_json_indexes(Indexes),
-    remove_doc(Db, PrevDoc, Indexes1);
+modify_int(Db, delete, _, PrevDoc)  ->
+    remove_doc(Db, PrevDoc, json_indexes(Db));
 
-update_int(Db, updated, Doc, PrevDoc) ->
-    Indexes = mango_idx:list(Db),
-    Indexes1 = filter_json_indexes(Indexes),
-    remove_doc(Db, PrevDoc, Indexes1),
-    write_doc(Db, Doc, Indexes1);
+modify_int(Db, update, Doc, PrevDoc) ->
+    Indexes = json_indexes(Db),
+    remove_doc(Db, PrevDoc, Indexes),
+    write_doc(Db, Doc, Indexes);
 
-update_int(Db, created, Doc, _) ->
-    Indexes = mango_idx:list(Db),
-    Indexes1 = filter_json_indexes(Indexes),
-    write_doc(Db, Doc, Indexes1).
+modify_int(Db, create, Doc, _) ->
+    write_doc(Db, Doc, json_indexes(Db)).
 
 
 remove_doc(Db, #doc{} = Doc, Indexes) ->
@@ -87,10 +97,10 @@ write_doc(Db, #doc{} = Doc, Indexes) ->
     mango_fdb:write_doc(Db, DocId, Results).
 
 
-filter_json_indexes(Indexes) ->
+json_indexes(Db) ->
     lists:filter(fun (Idx) ->
         Idx#idx.type == <<"json">>
-    end, Indexes).
+    end, mango_idx:list(Db)).
 
 
 index_doc(Indexes, Doc) ->