Skip to content

Commit

Permalink
db:match
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Rehfeld committed Dec 1, 2011
1 parent b1f51cc commit 08bc056
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 14 additions & 1 deletion apps/warmup-database/src/db.erl
Expand Up @@ -29,7 +29,7 @@ read(Key, Db) ->

%% @doc db:match(Element, Db) ⇒ [Key1, ..., KeyN].
match(Element, Db) ->
[].
find(Element, Db).


%% @private
Expand All @@ -39,3 +39,16 @@ get(Key, [{DifferentKey, Element}|T]) ->
get(Key, T);
get(Key, []) ->
{error, instance}.

%% @private
find(Element, Db) ->
Matches = [],
find(Element, Db, Matches).

find(Element, [{Key, Element}|T], Matches) ->
find(Element, T, Matches ++ [Key]);
find(Element, [{DifferentKey, _Element}|T], Matches) ->
find(Element, T, Matches);

find(Element, [], Matches) ->
Matches.
9 changes: 9 additions & 0 deletions apps/warmup-database/test/db_tests.erl
Expand Up @@ -29,3 +29,12 @@ read_hit_test() ->
Db1 = db:write(ola, barcelona, Db),
Db2 = db:write(oliver, berlin, Db1),
?assertEqual({ok, barcelona}, db:read(ola, Db2)).

match_test() ->
Db = db:new(),

Db1 = db:write(francesco, london, Db),
Db2 = db:write(lelle, stockholm, Db1),
Db3 = db:write(joern, stockholm, Db2),

?assertEqual([joern,lelle], db:match(stockholm, Db3)).

0 comments on commit 08bc056

Please sign in to comment.