You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2014/08/01 11:09:32 UTC
[7/8] ddoc-cache commit: updated refs/heads/windsor-merge to 64bab93
Address code review feedback
Summary:
* removed the useless include inside ddoc_cache
* revised the export listing style and removed confusing comments
* added guards back to older ddoc_cache:open/2 API
* clarified a type declaration, still strict until it's moved
* replaced match_newest sort with the canonical comparison rule
* simplified setting of the OpenerKey in some clauses
* renamed Rev* variables for correctness
* added back precious whitespace in some places
Project: http://git-wip-us.apache.org/repos/asf/couchdb-ddoc-cache/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-ddoc-cache/commit/5f5c289f
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-ddoc-cache/tree/5f5c289f
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-ddoc-cache/diff/5f5c289f
Branch: refs/heads/windsor-merge
Commit: 5f5c289fd75843faaf9a8f5d05739f4e7eeb6e68
Parents: 4158945
Author: Brian Mitchell <br...@p2p.io>
Authored: Wed Oct 23 23:02:00 2013 -0400
Committer: Robert Newson <rn...@apache.org>
Committed: Tue Jul 29 11:56:58 2014 +0100
----------------------------------------------------------------------
src/ddoc_cache.erl | 25 +++++++++-----
src/ddoc_cache_opener.erl | 75 ++++++++++++++++++++----------------------
2 files changed, 52 insertions(+), 48 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-ddoc-cache/blob/5f5c289f/src/ddoc_cache.erl
----------------------------------------------------------------------
diff --git a/src/ddoc_cache.erl b/src/ddoc_cache.erl
index 6c54c36..40d9467 100644
--- a/src/ddoc_cache.erl
+++ b/src/ddoc_cache.erl
@@ -12,15 +12,20 @@
-module(ddoc_cache).
--include_lib("couch/include/couch_db.hrl").
+-export([
+ start/0,
+ stop/0
+]).
--export([start/0, stop/0]).
+-export([
+ open_doc/2,
+ open_doc/3,
+ open_validation_funs/1,
+ evict/2,
-% public API
--export([open_doc/2, open_doc/3, open_validation_funs/1, evict/2]).
-
-% deprecated API
--export([open/2]).
+ %% deprecated
+ open/2
+]).
start() ->
application:start(ddoc_cache).
@@ -67,5 +72,7 @@ evict(ShardDbName, DDocIds) ->
open(DbName, validation_funs) ->
open_validation_funs(DbName);
-open(DbName, DocId) ->
- open_doc(DbName, DocId).
+open(DbName, <<"_design/", _/binary>>=DDocId) when is_binary(DbName) ->
+ open_doc(DbName, DDocId);
+open(DbName, DDocId) when is_binary(DDocId) ->
+ open_doc(DbName, <<"_design/", DDocId/binary>>).
http://git-wip-us.apache.org/repos/asf/couchdb-ddoc-cache/blob/5f5c289f/src/ddoc_cache_opener.erl
----------------------------------------------------------------------
diff --git a/src/ddoc_cache_opener.erl b/src/ddoc_cache_opener.erl
index 780f9dd..2563cc2 100644
--- a/src/ddoc_cache_opener.erl
+++ b/src/ddoc_cache_opener.erl
@@ -16,10 +16,20 @@
-include_lib("couch/include/couch_db.hrl").
-include_lib("mem3/include/mem3.hrl").
-% worker API
--export([start_link/0]).
+-export([
+ start_link/0
+]).
+-export([
+ init/1,
+ terminate/2,
+
+ handle_call/3,
+ handle_cast/2,
+ handle_info/2,
+
+ code_change/3
+]).
-% public API
-export([
open_doc/2,
open_doc/3,
@@ -31,21 +41,9 @@
recover_doc/3,
recover_validation_funs/1
]).
-
-% couch_event listener callback
--export([handle_db_event/3]).
-
-% gen_server behavior
-export([
- init/1,
- terminate/2,
- handle_call/3,
- handle_cast/2,
- handle_info/2,
- code_change/3
+ handle_db_event/3
]).
-
-% sub-process spawn API
-export([
fetch_doc_data/1
]).
@@ -55,7 +53,8 @@
-type dbname() :: iodata().
-type docid() :: iodata().
--type revision() :: {pos_integer(), <<_:128>>}.
+-type doc_hash() :: <<_:128>>.
+-type revision() :: {pos_integer(), doc_hash()}.
-record(opener, {
key,
@@ -77,8 +76,8 @@ open_doc(DbName, DocId) ->
handle_open_response(Resp).
-spec open_doc(dbname(), docid(), revision()) -> {ok, #doc{}}.
-open_doc(DbName, DocId, RevId) ->
- Resp = gen_server:call(?MODULE, {open, {DbName, DocId, RevId}}, infinity),
+open_doc(DbName, DocId, Rev) ->
+ Resp = gen_server:call(?MODULE, {open, {DbName, DocId, Rev}}, infinity),
handle_open_response(Resp).
-spec open_validation_funs(dbname()) -> {ok, [fun()]}.
@@ -106,9 +105,10 @@ match_newest(Key) ->
[] ->
missing;
Docs ->
- Sorted = lists:sort(fun (#doc{revs=L}, #doc{revs=R}) ->
- L >= R
- end, Docs),
+ Sorted = lists:sort(
+ fun (#doc{deleted=DelL, revs=L}, #doc{deleted=DelR, revs=R}) ->
+ {not DelL, L} > {not DelR, R}
+ end, Docs),
{ok, hd(Sorted)}
catch
error:badarg ->
@@ -118,8 +118,8 @@ match_newest(Key) ->
recover_doc(DbName, DDocId) ->
fabric:open_doc(DbName, DDocId, []).
-recover_doc(DbName, DDocId, RevId) ->
- {ok, [Resp]} = fabric:open_revs(DbName, DDocId, [RevId], []),
+recover_doc(DbName, DDocId, Rev) ->
+ {ok, [Resp]} = fabric:open_revs(DbName, DDocId, [Rev], []),
Resp.
recover_validation_funs(DbName) ->
@@ -188,10 +188,10 @@ handle_cast({do_evict, DbName}, St) ->
handle_cast({do_evict, DbName, DDocIds}, St) ->
ets_lru:remove(?CACHE, {DbName, validation_funs}),
lists:foreach(fun(DDocId) ->
- RevIds = ets_lru:match(?CACHE, {DbName, DDocId, '$1'}, '_'),
- lists:foreach(fun([RevId]) ->
- ets_lru:remove(?CACHE, {DbName, DDocId, RevId})
- end, RevIds)
+ Revs = ets_lru:match(?CACHE, {DbName, DDocId, '$1'}, '_'),
+ lists:foreach(fun([Rev]) ->
+ ets_lru:remove(?CACHE, {DbName, DDocId, Rev})
+ end, Revs)
end, DDocIds),
{noreply, St};
@@ -231,18 +231,16 @@ code_change(_OldVsn, State, _Extra) ->
-spec fetch_doc_data({dbname(), validation_funs}) -> no_return();
({dbname(), docid()}) -> no_return();
({dbname(), docid(), revision()}) -> no_return().
-fetch_doc_data({DbName, validation_funs}) ->
- OpenerKey = {DbName, validation_funs},
+fetch_doc_data({DbName, validation_funs}=OpenerKey) ->
{ok, Funs} = recover_validation_funs(DbName),
ok = ets_lru:insert(?CACHE, OpenerKey, Funs),
exit({open_ok, OpenerKey, {ok, Funs}});
-fetch_doc_data({DbName, DocId}) ->
- OpenerKey = {DbName, DocId},
+fetch_doc_data({DbName, DocId}=OpenerKey) ->
try recover_doc(DbName, DocId) of
{ok, Doc} ->
- {RevCount, [RevHash| _]} = Doc#doc.revs,
- RevId = {RevCount, RevHash},
- ok = ets_lru:insert(?CACHE, {DbName, DocId, RevId}, Doc),
+ {RevDepth, [RevHash| _]} = Doc#doc.revs,
+ Rev = {RevDepth, RevHash},
+ ok = ets_lru:insert(?CACHE, {DbName, DocId, Rev}, Doc),
exit({open_ok, OpenerKey, {ok, Doc}});
Else ->
exit({open_ok, OpenerKey, Else})
@@ -250,11 +248,10 @@ fetch_doc_data({DbName, DocId}) ->
Type:Reason ->
exit({open_error, OpenerKey, Type, Reason})
end;
-fetch_doc_data({DbName, DocId, RevId}) ->
- OpenerKey = {DbName, DocId, RevId},
- try recover_doc(DbName, DocId, RevId) of
+fetch_doc_data({DbName, DocId, Rev}=OpenerKey) ->
+ try recover_doc(DbName, DocId, Rev) of
{ok, Doc} ->
- ok = ets_lru:insert(?CACHE, {DbName, DocId, RevId}, Doc),
+ ok = ets_lru:insert(?CACHE, {DbName, DocId, Rev}, Doc),
exit({open_ok, OpenerKey, {ok, Doc}});
Else ->
exit({open_ok, OpenerKey, Else})