summaryrefslogtreecommitdiff
path: root/src/partitions.erl
diff options
context:
space:
mode:
authorAdam Kocoloski <adam@cloudant.com>2010-06-14 11:47:01 -0400
committerAdam Kocoloski <adam@cloudant.com>2010-08-12 01:05:14 -0400
commit6195e39d61c991a463377106310d7db971415928 (patch)
tree6d4eb49da211b32e57b423bf5f9825a887f9a091 /src/partitions.erl
parent9d4e5646f1387d310b91d9cc40cbbe7db96aa33b (diff)
better handling of repeated create_db attempts. BugzID 10300
Diffstat (limited to 'src/partitions.erl')
-rw-r--r--src/partitions.erl20
1 files changed, 17 insertions, 3 deletions
diff --git a/src/partitions.erl b/src/partitions.erl
index 09337561..4fba0011 100644
--- a/src/partitions.erl
+++ b/src/partitions.erl
@@ -163,10 +163,24 @@ jsonify(nodes, Nodes) ->
end, Nodes).
write_db_doc(EDoc) ->
- Doc = couch_doc:from_json_obj(EDoc),
{ok, Db} = couch_db:open(<<"dbs">>, []),
- {ok, NewRev} = couch_db:update_doc(Db, Doc, []),
- NewRev.
+ try
+ update_db_doc(Db, couch_doc:from_json_obj(EDoc))
+ catch {conflict, _} ->
+ ?LOG_ERROR("conflict writing db doc, must be a race", [])
+ after
+ couch_db:close(Db)
+ end.
+
+update_db_doc(Db, #doc{id=Id, body=Body} = Doc) ->
+ case couch_db:open_doc(Db, Id, []) of
+ {not_found, _} ->
+ {ok, _} = couch_db:update_doc(Db, Doc, []);
+ {ok, #doc{body=Body}} ->
+ ok;
+ {ok, OldDoc} ->
+ {ok, _} = couch_db:update_doc(Db, OldDoc#doc{body=Body}, [])
+ end.
shard_name(Part, DbName) when is_list(DbName) ->
shard_name(Part, ?l2b(DbName));