diff options
author | Adam Kocoloski <adam@cloudant.com> | 2010-07-02 10:23:14 -0400 |
---|---|---|
committer | Adam Kocoloski <adam@cloudant.com> | 2010-08-12 01:23:42 -0400 |
commit | 56bdbc73f05e5450ff9c36f8e369f2399d113641 (patch) | |
tree | eb9b77c3f476677d86ec4558ea63cb2c282e93d9 /src/mem3_util.erl | |
parent | c02b2f9115d8d5a6bd48e9eaf644950bda14e29d (diff) |
failover to reading dbs.couch directly when ets is MIA
Diffstat (limited to 'src/mem3_util.erl')
-rw-r--r-- | src/mem3_util.erl | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/mem3_util.erl b/src/mem3_util.erl index b05faa15..e58bbd4c 100644 --- a/src/mem3_util.erl +++ b/src/mem3_util.erl @@ -2,7 +2,8 @@ -author('brad@cloudant.com'). -export([hash/1, name_shard/1, create_partition_map/4, build_shards/2, - n_val/2, to_atom/1, to_integer/1, write_db_doc/1, delete_db_doc/1]). + n_val/2, to_atom/1, to_integer/1, write_db_doc/1, delete_db_doc/1, + load_shards_from_disk/1, load_shards_from_disk/2]). -define(RINGTOP, 2 bsl 31). % CRC32 space @@ -114,3 +115,21 @@ n_val(N, NodeCount) when N > NodeCount -> NodeCount; n_val(N, _) -> N. + +load_shards_from_disk(DbName) when is_binary(DbName) -> + {ok, Db} = couch_db:open(<<"dbs">>, []), + try load_shards_from_db(Db, DbName) after couch_db:close(Db) end. + +load_shards_from_db(#db{} = ShardDb, DbName) -> + case couch_db:open_doc(ShardDb, DbName, []) of + {ok, #doc{body = {Props}}} -> + ?LOG_INFO("dbs cache miss for ~s", [DbName]), + build_shards(DbName, Props); + {not_found, _} -> + erlang:error(database_does_not_exist) + end. + +load_shards_from_disk(DbName, DocId)-> + Shards = load_shards_from_disk(DbName), + HashKey = hash(DocId), + [S || #shard{range = [B,E]} = S <- Shards, B < HashKey, HashKey =< E]. |