diff options
author | Paul J. Davis <paul.joseph.davis@gmail.com> | 2019-04-16 12:20:35 -0500 |
---|---|---|
committer | Paul J. Davis <paul.joseph.davis@gmail.com> | 2019-04-16 12:20:35 -0500 |
commit | 4af77731ee0cb03013c60756ec1b89a464d23f23 (patch) | |
tree | afaeb118629d75aaf69f89917a33afd2ae2f19e8 | |
parent | 5b6583a382e1bb5bce1560d14ded580438d0f633 (diff) | |
download | couchdb-4af77731ee0cb03013c60756ec1b89a464d23f23.tar.gz |
WIP
-rw-r--r-- | src/fabric/src/fabric2.hrl | 2 | ||||
-rw-r--r-- | src/fabric/src/fabric2_db.erl | 51 | ||||
-rw-r--r-- | src/fabric/src/fabric2_fdb.erl | 10 |
3 files changed, 30 insertions, 33 deletions
diff --git a/src/fabric/src/fabric2.hrl b/src/fabric/src/fabric2.hrl index da62f5eba..61c34230c 100644 --- a/src/fabric/src/fabric2.hrl +++ b/src/fabric/src/fabric2.hrl @@ -53,7 +53,7 @@ -define(PDICT_LAYER_CACHE, '$fabric_layer_id'). -define(PDICT_TX_ID_KEY, '$fabric_tx_id'). -define(PDICT_TX_RES_KEY, '$fabric_tx_result'). --define(COMMIT_UNKOWN_RESULT, 1021). +-define(COMMIT_UNKNOWN_RESULT, 1021). % Various utility macros diff --git a/src/fabric/src/fabric2_db.erl b/src/fabric/src/fabric2_db.erl index 52ddc816c..2b47d9255 100644 --- a/src/fabric/src/fabric2_db.erl +++ b/src/fabric/src/fabric2_db.erl @@ -140,7 +140,7 @@ create(DbName, Options) -> - Result = transactional(DbName, Options, fun(TxDb) -> + Result = fabric2_fdb:transactional(DbName, Options, fun(TxDb) -> case fabric2_fdb:exists(TxDb) of true -> {error, file_exists}; @@ -164,7 +164,7 @@ open(DbName, Options) -> #{} = Db -> {ok, maybe_set_user_ctx(Db, Options)}; undefined -> - transactional(DbName, Options, fun(TxDb) -> + fabric2_fdb:transactional(DbName, Options, fun(TxDb) -> Opened = fabric2_fdb:open(TxDb, Options), ok = fabric2_server:store(Opened), {ok, Opened#{tx => undefined}} @@ -635,7 +635,7 @@ update_doc_interactive(Db, Doc0, _Options) -> % Check that a revision was specified if required Doc0RevId = doc_to_revid(Doc0), if Doc0RevId /= {0, <<>>} orelse WinnerRevId == {0, <<>>} -> ok; true -> - ?RETURN({error, conflict}); + ?RETURN({error, conflict}) end, % Check that we're not trying to create a deleted doc @@ -644,7 +644,7 @@ update_doc_interactive(Db, Doc0, _Options) -> end, % Get the target revision to update - Target = case DocId0RevId == WinnerRevId of + Target = case Doc0RevId == WinnerRevId of true -> Winner; false -> @@ -686,12 +686,12 @@ update_doc_interactive(Db, Doc0, _Options) -> } = Doc3 = new_revid(Doc2), NewRevInfo = #{ - winner := undefined, - deleted := NewDeleted, - rev_id := {NewRevPos, NewRev}, - rev_path := NewRevPath, - sequence := undefined, - branch_count := undefined + winner => undefined, + deleted => NewDeleted, + rev_id => {NewRevPos, NewRev}, + rev_path => NewRevPath, + sequence => undefined, + branch_count => undefined }, % Gather the list of possible winnig revisions @@ -713,7 +713,7 @@ update_doc_interactive(Db, Doc0, _Options) -> [W, NW] -> {W, NW} end, - NewWinner = NewWinnner0#{branch_count := maps:get(branch_count, Winner)}, + NewWinner = NewWinner0#{branch_count := maps:get(branch_count, Winner)}, ToUpdate = if NonWinner == not_found -> []; true -> [NonWinner] end, ToRemove = if Target == not_found -> []; true -> [Target] end, @@ -729,7 +729,7 @@ update_doc_interactive(Db, Doc0, _Options) -> {ok, {NewRevPos, NewRev}}. -update_doc_replicated(Db, Doc0, Options) -> +update_doc_replicated(Db, Doc0, _Options) -> #doc{ id = DocId, deleted = Deleted, @@ -745,15 +745,15 @@ update_doc_replicated(Db, Doc0, Options) -> branch_count => undefined }, - AllRevs = fabric2_fdb:get_all_revs(Db, DocId), + AllRevInfos = fabric2_fdb:get_all_revs(Db, DocId), RevTree = lists:foldl(fun(RI, TreeAcc) -> RIPath = fabric2_util:revinfo_to_path(RI), - {Merged, _} = couch_key_tree:merge(TreeAcc, RevTree), + {Merged, _} = couch_key_tree:merge(TreeAcc, RIPath), Merged - end, [], AllRevs), + end, [], AllRevInfos), - DocRevPath = fabric2_util:revinfo_to_path(RI), + DocRevPath = fabric2_util:revinfo_to_path(DocRevInfo), {NewTree, Status} = couch_key_tree:merge(RevTree, DocRevPath), if Status /= internal_node -> ok; true -> % We already know this revision so nothing @@ -763,7 +763,7 @@ update_doc_replicated(Db, Doc0, Options) -> AllLeafsFull = couch_key_tree:get_all_leafs_full(NewTree), LeafPath = get_leaf_path(RevPos, Rev, AllLeafsFull), - PrevRevInfo = find_prev_revinfo(LeafPath), + PrevRevInfo = find_prev_revinfo(RevPos, LeafPath), Doc1 = prep_and_validate(Db, Doc0, PrevRevInfo), % Possible winners are the previous winner and @@ -777,7 +777,7 @@ update_doc_replicated(Db, Doc0, Options) -> {DocRevInfo, not_found}; false -> [W, NW] = fabric2_util:sort_revinfos([Winner, DocRevInfo]), - {N, NW} + {W, NW} end, NewWinner = NewWinner0#{branch_count := length(AllLeafsFull)}, @@ -786,13 +786,16 @@ update_doc_replicated(Db, Doc0, Options) -> ok = fabric2_fdb:write_doc( Db, - Doc3, + Doc1, NewWinner, Winner, ToUpdate, ToRemove ), + #doc{ + revs = {NewRevPos, [NewRev | _]} + } = Doc1, {ok, {NewRevPos, NewRev}}. @@ -872,14 +875,14 @@ validate_ddoc(Db, DDoc) -> get_leaf_path(Pos, Rev, [{Pos, [{Rev, _RevInfo} | LeafPath]} | _]) -> LeafPath; -get_doc_leaf(Pos, Rev, [_WrongLeaf | RestLeafs]) -> - get_doc_leaf(Pos, Rev, RestLeafs). +get_leaf_path(Pos, Rev, [_WrongLeaf | RestLeafs]) -> + get_leaf_path(Pos, Rev, RestLeafs). find_prev_revinfo(_Pos, []) -> not_found; find_prev_revinfo(Pos, [{_Rev, ?REV_MISSING} | RestPath]) -> - find_prev_known_rev(Pos - 1, RestPath); + find_prev_revinfo(Pos - 1, RestPath); find_prev_revinfo(_Pos, [{_Rev, #{} = RevInfo} | _]) -> RevInfo. @@ -889,7 +892,3 @@ doc_to_revid(#doc{revs = Revs}) -> {0, []} -> {0, <<>>}; {RevPos, [Rev | _]} -> {RevPos, Rev} end. - - -doc_to_revpath(#doc{revs = {_, [_ | RevPath]}}) -> - RevPath. diff --git a/src/fabric/src/fabric2_fdb.erl b/src/fabric/src/fabric2_fdb.erl index 9b148a360..abc7fed57 100644 --- a/src/fabric/src/fabric2_fdb.erl +++ b/src/fabric/src/fabric2_fdb.erl @@ -18,8 +18,6 @@ transactional/3, transactional/2, - sweep_tx_ids/2, - create/2, open/2, delete/1, @@ -37,7 +35,7 @@ get_all_revs/2, get_winning_revs/3, - get_non_deleted_rev/2, + get_non_deleted_rev/3, get_doc_body/3, @@ -346,7 +344,7 @@ get_all_revs(#{} = Db, DocId) -> Prefix = erlfdb_tuple:pack({?DB_REVS, DocId}, DbPrefix), Options = [{streaming_mode, want_all}], - Future = erlfdb:get_range_startswith(Tx, Prefix, Options) + Future = erlfdb:get_range_startswith(Tx, Prefix, Options), lists:map(fun({K, V}) -> Key = erlfdb_tuple:unpack(K, DbPrefix), Val = erlfdb_tuple:unpack(V), @@ -424,7 +422,7 @@ write_doc(Db, Doc, NewWinner0, OldWinner, ToUpdate, ToRemove) -> NewWinner = NewWinner0#{winner := true}, NewRevId = maps:get(rev_id, NewWinner), - {WKey, WVal} = revinfo_to_fdb(DbPrefix, DocId, Winner), + {WKey, WVal} = revinfo_to_fdb(DbPrefix, DocId, NewWinner), ok = erlfdb:set_versionstamped_value(Tx, WKey, WVal), lists:foreach(fun(RI0) -> @@ -611,7 +609,7 @@ debug_cluster(Start, End) -> end). -init(Tx, DbName, Options) -> +init_db(Tx, DbName, Options) -> Root = erlfdb_directory:root(), CouchDB = erlfdb_directory:create_or_open(Tx, Root, [<<"couchdb">>]), Prefix = erlfdb_directory:get_name(CouchDB), |