From 5c1c2422dcf9673d67681f999ece72a019461306 Mon Sep 17 00:00:00 2001 From: Adam Kocoloski Date: Wed, 14 Jul 2010 14:49:22 -0400 Subject: support for revs_limit and security metadata --- src/fabric.erl | 16 +++++++++++++++- src/fabric_db_meta.erl | 30 ++++++++++++++++++++++++++++++ src/fabric_rpc.erl | 9 ++++++++- src/fabric_util.erl | 14 +++++++++++++- 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 src/fabric_db_meta.erl (limited to 'src') diff --git a/src/fabric.erl b/src/fabric.erl index 5d03c5fa..1be97a98 100644 --- a/src/fabric.erl +++ b/src/fabric.erl @@ -5,7 +5,8 @@ % DBs -export([all_dbs/0, all_dbs/1, create_db/1, create_db/2, delete_db/1, - delete_db/2, get_db_info/1, get_doc_count/1]). + delete_db/2, get_db_info/1, get_doc_count/1, set_revs_limit/3, + set_security/3, get_revs_limit/1, get_security/1]). % Documents -export([open_doc/3, open_revs/4, get_missing_revs/2, update_doc/3, @@ -57,6 +58,19 @@ delete_db(DbName) -> delete_db(DbName, Options) -> fabric_db_delete:go(dbname(DbName), opts(Options)). +set_revs_limit(DbName, Limit, Options) when is_integer(Limit), Limit > 0 -> + fabric_db_meta:set_revs_limit(dbname(DbName), Limit, opts(Options)). + +get_revs_limit(DbName) -> + {ok, Db} = fabric_util:get_db(dbname(DbName)), + try couch_db:get_revs_limit(Db) after catch couch_db:close(Db) end. + +set_security(DbName, SecObj, Options) -> + fabric_db_meta:set_security(dbname(DbName), SecObj, opts(Options)). + +get_security(DbName) -> + {ok, Db} = fabric_util:get_db(dbname(DbName)), + try couch_db:get_security(Db) after catch couch_db:close(Db) end. % doc operations open_doc(DbName, Id, Options) -> diff --git a/src/fabric_db_meta.erl b/src/fabric_db_meta.erl new file mode 100644 index 00000000..3b0ae109 --- /dev/null +++ b/src/fabric_db_meta.erl @@ -0,0 +1,30 @@ +-module(fabric_db_meta). + +-export([set_revs_limit/3, set_security/3]). + +-include("fabric.hrl"). +-include_lib("mem3/include/mem3.hrl"). + +set_revs_limit(DbName, Limit, Options) -> + Shards = mem3:shards(DbName), + Workers = fabric_util:submit_jobs(Shards, set_revs_limit, [Limit, Options]), + Waiting = length(Workers) - 1, + case fabric_util:recv(Workers, #shard.ref, fun handle_message/3, Waiting) of + {ok, ok} -> + ok; + Error -> + Error + end. + +set_security(DbName, SecObj, Options) -> + Shards = mem3:shards(DbName), + Workers = fabric_util:submit_jobs(Shards, set_security, [SecObj, Options]), + Waiting = length(Workers) - 1, + fabric_util:recv(Workers, #shard.ref, fun handle_message/3, Waiting). + +handle_message(ok, _, 0) -> + {stop, ok}; +handle_message(ok, _, Waiting) -> + {ok, Waiting - 1}; +handle_message(Error, _, _Waiting) -> + {error, Error}. \ No newline at end of file diff --git a/src/fabric_rpc.erl b/src/fabric_rpc.erl index 6be7c3cc..0a25948f 100644 --- a/src/fabric_rpc.erl +++ b/src/fabric_rpc.erl @@ -3,7 +3,8 @@ -export([get_db_info/1, get_doc_count/1, get_update_seq/1]). -export([open_doc/3, open_revs/4, get_missing_revs/2, update_docs/3]). -export([all_docs/2, changes/3, map_view/4, reduce_view/4, group_info/2]). --export([create_db/3, delete_db/3, reset_validation_funs/1]). +-export([create_db/3, delete_db/3, reset_validation_funs/1, set_security/3, + set_revs_limit/3]). -include("fabric.hrl"). -include_lib("couch/include/couch_db.hrl"). @@ -160,6 +161,12 @@ get_doc_count(DbName) -> get_update_seq(DbName) -> with_db(DbName, [], {couch_db, get_update_seq, []}). +set_security(DbName, SecObj, Options) -> + with_db(DbName, Options, {couch_db, set_security, [SecObj]}). + +set_revs_limit(DbName, Limit, Options) -> + with_db(DbName, Options, {couch_db, set_revs_limit, [Limit]}). + open_doc(DbName, DocId, Options) -> with_db(DbName, Options, {couch_db, open_doc, [DocId, Options]}). diff --git a/src/fabric_util.erl b/src/fabric_util.erl index e928c5ff..d4c4f5e2 100644 --- a/src/fabric_util.erl +++ b/src/fabric_util.erl @@ -1,6 +1,7 @@ -module(fabric_util). --export([submit_jobs/3, cleanup/1, recv/4, receive_loop/4, receive_loop/6]). +-export([submit_jobs/3, cleanup/1, recv/4, receive_loop/4, receive_loop/6, + get_db/1]). -include("fabric.hrl"). -include_lib("mem3/include/mem3.hrl"). @@ -75,3 +76,14 @@ process_message(RefList, Keypos, Fun, Acc0, TimeoutRef, PerMsgTO) -> after PerMsgTO -> timeout end. + +get_db(DbName) -> + Shards = mem3:shards(DbName), + case lists:partition(fun(#shard{node = N}) -> N =:= node() end, Shards) of + {[#shard{name = ShardName}|_], _} -> + % prefer node-local DBs + couch_db:open(ShardName, []); + {[], #shard{node = Node, name = ShardName}} -> + % but don't require them + rpc:call(Node, couch_db, open, [ShardName, []]) + end. -- cgit v1.2.3