Просмотр исходного кода

all index queries moved to backend drivers

Maxim Sokhatsky 12 лет назад
Родитель
Сommit
bde0372202
9 измененных файлов с 45 добавлено и 18 удалено
  1. 4 1
      include/config.hrl
  2. 0 2
      src/kvs.erl
  3. 2 3
      src/kvs_comment.erl
  4. 3 4
      src/kvs_group.erl
  5. 3 5
      src/kvs_meeting.erl
  6. 2 2
      src/kvs_purchase.erl
  7. 5 1
      src/kvs_user.erl
  8. 13 0
      src/store_mnesia.erl
  9. 13 0
      src/store_riak.erl

+ 4 - 1
include/config.hrl

@@ -1 +1,4 @@
--record(config, {key, value}).
+-record(config, {key, value}).
+
+-define(DBA, store_mnesia).
+

+ 0 - 2
src/kvs.erl

@@ -16,8 +16,6 @@
 -include_lib("kvs/include/feed_state.hrl").
 -compile(export_all).
 
--define(DBA, store_mnesia).
-
 start() -> DBA = ?DBA, DBA:start().
 dir() -> DBA = ?DBA, DBA:dir().
 stop() -> DBA = ?DBA, DBA:stop().

+ 2 - 3
src/kvs_comment.erl

@@ -2,6 +2,7 @@
 -copyright('Synrc Research Center s.r.o.').
 -author('Alexander Kalenuk').
 -include_lib("kvs/include/feeds.hrl").
+-include_lib("kvs/include/config.hrl").
 -compile(export_all).
 
 add(FId, User, EntryId, ParentComment, CommentId, Content, Medias) ->
@@ -57,9 +58,7 @@ feed_comments({EId, FId}) ->
         {ok, #entry{comments_rear = First}} -> lists:flatten(read_comments(First));
         _ -> [] end.
 
-author_comments(Who) ->
-    EIDs = [E || #comment{entry_id=E} <- kvs:all_by_index(comment,<<"author_bin">>, Who) ],
-    lists:flatten([ kvs:all_by_index(entry,<<"entry_bin">>,EID) || EID <- EIDs]).
+author_comments(Who) -> DBA=?DBA,DBA:author_comments(Who).
 
 remove(FId, EId) ->
     AllComments = feed_comments({EId, FId}),

+ 3 - 4
src/kvs_group.erl

@@ -5,6 +5,7 @@
 -include_lib("kvs/include/accounts.hrl").
 -include_lib("kvs/include/log.hrl").
 -include_lib("kvs/include/feed_state.hrl").
+-include_lib("kvs/include/config.hrl").
 -include_lib("mqs/include/mqs.hrl").
 
 retrieve_groups(User) ->
@@ -46,10 +47,8 @@ delete(GroupName) ->
                     mqs_channel:close(Channel);
                 {error,Reason} -> ?ERROR("delete group failed: ~p",[Reason]) end end.
 
-participate(UserName) -> [GroupName || #group_subscription{where=GroupName} <- kvs:all_by_index(group_subscription, <<"who_bin">>, UserName) ].
-members(GroupName) -> [UserName || #group_subscription{who=UserName} <- kvs:all_by_index(group_subscription, <<"where_bin">>, GroupName) ].
-members_by_type(GroupName, Type) -> [UserName || #group_subscription{who=UserName, type=T} <- kvs:all_by_index(group_subscription, <<"where_bin">>, GroupName), T == Type ].
-members_with_types(GroupName) -> [{UserName, Type} || #group_subscription{who=UserName, type=Type} <- kvs:all_by_index(group_subscriptioin, <<"where_bin">>, list_to_binary(GroupName)) ].
+participate(UserName) -> DBA=?DBA,DBA:participate(UserName).
+members(GroupName) -> DBA=?DBA,DBA:members(GroupName).
 
 owner(UserName, GroupName) ->
     case kvs:get(group, GroupName) of

+ 3 - 5
src/kvs_meeting.erl

@@ -3,6 +3,7 @@
 -include_lib("kvs/include/meetings.hrl").
 -include_lib("kvs/include/feed_state.hrl").
 -include_lib("kvs/include/log.hrl").
+-include_lib("kvs/include/config.hrl").
 -compile(export_all).
 
 create_team(Name) ->
@@ -110,8 +111,5 @@ leave_tournament(UserId, TournamentId) ->
         _ -> ok
     end.
 
-user_tournaments(UId) -> 
-    kvs:all_by_index(play_record, <<"play_record_who_bin">>, list_to_binary(UId)).
-
-tournament_waiting_queue(TId) ->
-    kvs:all_by_index(play_record, <<"play_record_tournament_bin">>, list_to_binary(integer_to_list(TId))).
+user_tournaments(UId) -> DBA=?DBA,DBA:user_tournaments(UId).
+tournament_users(TId) -> DBA=?DBA,DBA:tournament_users(TId).

+ 2 - 2
src/kvs_purchase.erl

@@ -2,6 +2,7 @@
 -include_lib("kvs/include/products.hrl").
 -include_lib("kvs/include/purchases.hrl").
 -include_lib("kvs/include/log.hrl").
+-include_lib("kvs/include/config.hrl").
 -include_lib("kvs/include/accounts.hrl").
 -include_lib("kvs/include/feed_state.hrl").
 -compile(export_all).
@@ -19,8 +20,7 @@ buy(UId, ProductId) ->
 give(UId, PId) ->
     kvs:put(#user_product{username=UId, timestamp=now(), product_id = PId}).
 
-products(UId) ->
-    kvs:all_by_index(user_product, <<"user_bin">>, list_to_binary(UId)).
+products(UId) -> DBA=?DBA, DBA:products(UId).
 
 handle_notice(["kvs_purchase", "user", UId, "buy"] = Route,
     Message, #state{owner = Owner, type =Type} = State) ->

+ 5 - 1
src/kvs_user.erl

@@ -3,10 +3,12 @@
 -include_lib("kvs/include/groups.hrl").
 -include_lib("kvs/include/accounts.hrl").
 -include_lib("kvs/include/log.hrl").
+-include_lib("kvs/include/config.hrl").
 -include_lib("kvs/include/feed_state.hrl").
 -include_lib("mqs/include/mqs.hrl").
 -compile(export_all).
 
+
 register(#user{username=U, email=Email, facebook_id = FbId} = RegisterData0) ->
     FindUser = case check_username(U, FbId) of
         {error, E} -> {error, E};
@@ -79,7 +81,9 @@ unsubscribe(Who, Whom) ->
 
 subscriptions(undefined)-> [];
 subscriptions(#user{username = UId}) -> subscriptions(UId);
-subscriptions(UId) when is_list(UId) -> lists:sort( kvs:all_by_index(subs, <<"subs_who_bin">>, list_to_binary(UId)) ).
+
+subscriptions(UId) -> DBA=?DBA, DBA:subscriptions(UId).
+subscribed(Who) -> DBA=?DBA, DBA:subscribed(Who).
 
 subscribed(Who, Whom) ->
     case kvs:get(subscription, {Who, Whom}) of

+ 13 - 0
src/store_mnesia.erl

@@ -44,6 +44,7 @@ initialize() ->
     ?CREATE_TAB(transaction),
     ?CREATE_TAB(translation),
     ok = add_table_index(comment, entry_id),
+    ok = add_table_index(comment, author_id),
     ok = add_table_index(subscription, who),
     ok = add_table_index(subscription, whom),
     ok = add_table_index(group_subscription, who),
@@ -141,3 +142,15 @@ storage_to_mnesia_type(ondisk) -> {disc_only_copies, [node()]}.
 
 exec(Q) -> F = fun() -> qlc:e(Q) end, {atomic, Val} = mnesia:transaction(F), Val.
 
+%
+
+products(UId) -> all_by_index(user_product, #user_product.username, UId).
+subscriptions(UId) -> all_by_index(subsciption, #subscription.who, UId).
+subscribed(Who) -> all_by_index(subscription, #subscription.whom, Who).
+participate(UserName) -> all_by_index(group_subscription, #group_subscription.who, UserName).
+members(GroupName) -> all_by_index(group_subscription, #group_subscription.where, GroupName).
+user_tournaments(UId) -> all_by_index(play_record, #play_record.who, UId).
+tournament_users(TId) -> all_by_index(play_record, #play_record.tournament, TId).
+author_comments(Who) ->
+    EIDs = [E || #comment{entry_id=E} <- all_by_index(comment,#comment.author_id, Who) ],
+    lists:flatten([ all_by_index(entry, #entry.id,EID) || EID <- EIDs]).

+ 13 - 0
src/store_riak.erl

@@ -213,3 +213,16 @@ next_id(CounterId, Default, Incr) ->
     case Riak:put(Object, Options) of
         ok -> Value;
         {error, _} -> next_id(CounterId, Incr) end.
+
+%%%%%%%%%%%%%%%%
+
+products(UId) -> all_by_index(user_product, <<"user_bin">>, list_to_binary(UId)).
+subscriptions(UId) -> all_by_index(subsciption, <<"subs_who_bin">>, list_to_binary(UId)).
+subscribed(Who) -> all_by_index(subscription, <<"subs_whom_bin">>, list_to_binary(Who)).
+participate(UserName) -> all_by_index(group_subscription, <<"who_bin">>, UserName).
+members(GroupName) -> all_by_index(group_subscription, <<"where_bin">>, GroupName).
+user_tournaments(UId) -> all_by_index(play_record, <<"play_record_who_bin">>, list_to_binary(UId)).
+tournament_users(TId) -> all_by_index(play_record, <<"play_record_tournament_bin">>, list_to_binary(integer_to_list(TId))).
+author_comments(Who) ->
+    EIDs = [E || #comment{entry_id=E} <- all_by_index(comment,<<"author_bin">>, Who) ],
+    lists:flatten([ all_by_index(entry,<<"entry_bin">>,EID) || EID <- EIDs]).