Permalink
Browse files

Adding a full table fetch.

In some circumstances where bertconf still is the mechanism
used but the config data needs further treatment, it should be possible
to fetch entries without knowing the key.

This patch adds a new function to fetch the entire content of a
table, batched by chunks of 500 to avoid locking up a scheduler
for too long on copying data. A single list with all entries is
returned to the user.

The value is not configurable for now -- if there is a need for it,
the code can be extended to allow it.
  • Loading branch information...
1 parent 1af9f8f commit 68590e23bc833b4a5a11cdcac9146fba157a5432 @ferd committed May 14, 2012
Showing with 26 additions and 5 deletions.
  1. +17 −3 src/bertconf.erl
  2. +9 −2 test/bertconf_SUITE.erl
View
@@ -1,8 +1,9 @@
-module(bertconf).
-behaviour(application).
-export([start/2, stop/1]).
--export([read/2]).
+-export([read/2, all/1]).
-define(TABLE, bertconf_bert_loader).
+-define(MATCH_LIMIT, 500).
-include("bertconf.hrl").
%%% APPLICATION CALLBACKS %%%
@@ -14,9 +15,22 @@ stop(_) -> ok.
%%% PUBLIC INTERFACE %%%
read(NameSpace, Key) ->
- [#tab{id=Tid}] = ets:lookup(?TABLE, NameSpace),
- case ets:lookup(Tid, Key) of
+ case ets:lookup(table(NameSpace), Key) of
[{_Key,Val}] -> {ok, Val};
[] -> undefined
end.
+all(NameSpace) ->
+ loop_all({table(NameSpace), '_', ?MATCH_LIMIT}).
+
+%%% PRIVATE
+table(NameSpace) ->
+ [#tab{id=Tid}] = ets:lookup(?TABLE, NameSpace),
+ Tid.
+
+loop_all('$end_of_table') ->
+ [];
+loop_all({Match, Continuation}) ->
+ [Match | loop_all(ets:match_object(Continuation))];
+loop_all({Tid, Pat, Limit}) ->
+ lists:append(loop_all(ets:match_object(Tid, Pat, Limit))).
View
@@ -6,9 +6,9 @@
-include_lib("common_test/include/ct.hrl").
-export([all/0, init_per_suite/1, end_per_suite/1,
init_per_testcase/2, end_per_testcase/2]).
--export([access_table/1]).
+-export([access_table/1, all_data/1]).
-all() -> [access_table].
+all() -> [access_table, all_data].
init_per_suite(Config) ->
application:load({application, bertconf, []}),
@@ -39,3 +39,10 @@ access_table(_Config) ->
Key = {728,90},
{ok, [9881]} = bertconf:read(NameSpace,Key),
undefined = bertconf:read(NameSpace,make_ref()).
+
+all_data(_Config) ->
+ [{9880, _},
+ {9881, _},
+ {14611, _},
+ {14612, _},
+ {14613, _}] = lists:sort(bertconf:all(placement)).

0 comments on commit 68590e2

Please sign in to comment.