Permalink
Browse files

db module using ets

  • Loading branch information...
1 parent bd1691f commit 54903b5c016a8b9d2b754ea2312ec4950c26777b @martinrehfeld committed Nov 29, 2011
Showing with 25 additions and 45 deletions.
  1. +14 −38 apps/warmup-database/src/db.erl
  2. +11 −7 apps/warmup-database/test/db_tests.erl
@@ -1,5 +1,5 @@
%% @doc `db' module as a warmup exercise for the erlang course held at wooga
-%% @version Exercise 3
+%% @version Exercise 4
%%
%% @author Martin Rehfeld <martin.rehfeld@glnetworks.de>
@@ -10,55 +10,31 @@
%% @doc db:new() ⇒ Db.
new() ->
- [].
+ ets:new(db, [private, {keypos,2}]).
%% @doc db:destroy(Db) ⇒ ok.
-destroy(_Db) ->
+destroy(Db) ->
+ ets:delete(Db),
ok.
%% @doc db:write(Key, Element, Db) ⇒ NewDb.
write(Key, Element, Db) ->
- [#data{key=Key, data=Element}|Db].
+ ets:insert(Db, #data{key=Key, data=Element}),
+ Db.
%% @doc db:delete(Key, Db) ⇒ NewDb.
delete(Key, Db) ->
- reject(Key, Db, []).
+ ets:delete(Db, Key),
+ Db.
%% @doc db:read(Key, Db) ⇒{ok, Element} | {error, instance}.
read(Key, Db) ->
- get(Key, Db).
+ case ets:lookup(Db, Key) of
+ [] -> {error, instance};
+ [Record] -> {ok, Record#data.data}
+ end.
%% @doc db:match(Element, Db) ⇒ [Key1, ..., KeyN].
match(Element, Db) ->
- find(Element, Db).
-
-
-%% @private
-get(Key, [#data{key=Key, data=Element}|_T]) ->
- {ok, Element};
-get(Key, [_Record|T]) ->
- get(Key, T);
-get(_Key, []) ->
- {error, instance}.
-
-%% @private
-find(Element, Db) ->
- Matches = [],
- find(Element, Db, Matches).
-
-find(Element, [#data{key=Key, data=Element}|T], Matches) ->
- find(Element, T, Matches ++ [Key]);
-find(Element, [_Record|T], Matches) ->
- find(Element, T, Matches);
-
-find(_Element, [], Matches) ->
- Matches.
-
-%% @private
-reject(Key, [#data{key=Key}|T], Matches) ->
- reject(Key, T, Matches);
-reject(Key, [Record|T], Matches) ->
- reject(Key, T, Matches ++ [Record]);
-
-reject(_Key, [], Matches) ->
- Matches.
+ Matches = ets:match(Db, #data{key='$1', data=Element}),
+ [hd(X) || X <- Matches].
@@ -3,7 +3,8 @@
-include("record.hrl").
create_new_db_test() ->
- ?assertEqual([], db:new()).
+ Db = db:new(),
+ ?assertEqual([], ets:tab2list(Db)).
destroy_db_test() ->
Db = db:new(),
@@ -13,16 +14,19 @@ write_keys_test() ->
Db = db:new(),
Db1 = db:write(francesco, london, Db),
- ?assertEqual([#data{key=francesco, data=london}], Db1),
+ ?assertEqual({ok, london}, db:read(francesco, Db1)),
+ ?assertMatch([_], ets:tab2list(Db1)),
Db2 = db:write(lelle, stockholm, Db1),
- ?assertEqual([#data{key=lelle, data=stockholm},
- #data{key=francesco, data=london}], Db2),
+ ?assertEqual({ok, stockholm}, db:read(lelle, Db2)),
+ ?assertEqual({ok, london}, db:read(francesco, Db2)),
+ ?assertMatch([_, _], ets:tab2list(Db2)),
Db3 = db:write(joern, stockholm, Db2),
- ?assertEqual([#data{key=joern, data=stockholm},
- #data{key=lelle, data=stockholm},
- #data{key=francesco, data=london}], Db3).
+ ?assertEqual({ok, stockholm}, db:read(joern, Db3)),
+ ?assertEqual({ok, stockholm}, db:read(lelle, Db3)),
+ ?assertEqual({ok, london}, db:read(francesco, Db3)),
+ ?assertMatch([_, _, _], ets:tab2list(Db3)).
read_miss_test() ->
Db = db:new(),

0 comments on commit 54903b5

Please sign in to comment.