Maxim Sokhatsky 12 лет назад
Родитель
Сommit
d4570e84a1
5 измененных файлов с 76 добавлено и 104 удалено
  1. 39 47
      src/kvs_comment.erl
  2. 7 27
      src/kvs_feed.erl
  3. 1 13
      src/kvs_sup.erl
  4. 1 0
      src/map_reduce.erl
  5. 28 17
      src/store_riak.erl

+ 39 - 47
src/kvs_comment.erl

@@ -1,64 +1,37 @@
 -module(kvs_comment).
+-copyright('Synrc Research Center s.r.o.').
+-author('Alexander Kalenuk').
 -include("feeds.hrl").
 -compile(export_all).
 
-select_by_entry_id(EntryId) ->
-    kvs:comments_by_entry(EntryId).
-
-read_comments(undefined) -> [];
-read_comments([#comment{comments = C} | Rest]) -> [read_comments(C) | read_comments(Rest)];
-read_comments(C) -> kvs:traversal(comment, #comment.prev, C, all).
-
-read_comments_rev(undefined) -> [];
-read_comments_rev([#comment{comments = C} | Rest]) -> [read_comments_rev(C) | read_comments_rev(Rest)];
-read_comments_rev(C) -> kvs:traversal(comment, #comment.next, C, all).
-
-% @spec comment_by_id({{EntryId::term(), FeedId::term()}, CommentId::term()}) -> {ok, #comment{}}.
-comment_by_id(CommentId) -> kvs:get(CommentId).
-
-% @spec comments_by_entry(EId::{string(), term()}) -> [#comment{}].
-comments_by_entry({EId, FId}) ->
-    case kvs:entry_by_id({EId, FId}) of
-        {ok, #entry{comments_rear = undefined}} ->
-            [];
-        {ok, #entry{comments_rear = First}} ->
-            lists:flatten(read_comments_rev(First));
-        _ ->
-            []
-    end.
-
-
 add(FId, User, EntryId, ParentComment, CommentId, Content, Medias) ->
+     case kvs:get(entry,{EntryId, FId}) of
+         {ok, _E} -> add(FId, User, EntryId, ParentComment, CommentId, Content, Medias, dont_check);
+         _ -> ok end.
+
+add(FId, User, EntryId, ParentComment, CommentId, Content, Medias, _) ->
     FullId = {CommentId, {EntryId, FId}},
 
     Prev = case ParentComment of
         undefined ->
-            {ok, Entry} = kvs:entry_by_id({EntryId, FId}),
+            {ok, Entry} = kvs:get(entry,{EntryId, FId}),
             {PrevC, E} = case Entry#entry.comments of
-                        undefined ->
-                            {undefined, Entry#entry{comments_rear = FullId}};
-                        Id ->
-                            {ok, PrevTop} = kvs:get(comment, Id),
-                            kvs:put(PrevTop#comment{next = FullId}),
-                            {Id, Entry}
-                   end,
-
+                        undefined -> {undefined, Entry#entry{comments_rear = FullId}};
+                        Id -> {ok, PrevTop} = kvs:get(comment, Id),
+                              kvs:put(PrevTop#comment{next = FullId}),
+                              {Id, Entry} end,
             kvs:put(E#entry{comments=FullId}),
             PrevC;
-
         _ ->
             {ok, Parent} = kvs:get(comment, {{EntryId, FId}, ParentComment}),
             {PrevC, CC} = case Parent#comment.comments of
-                        undefined ->
-                            {undefined, Parent#comment{comments_rear = FullId}};
-                        Id ->
-                            {ok, PrevTop} = kvs:get(comment, Id),
-                            kvs:put(PrevTop#comment{next = FullId}),
-                            {Id, Parent}
-                    end,
+                        undefined -> {undefined, Parent#comment{comments_rear = FullId}};
+                        Id -> {ok, PrevTop} = kvs:get(comment, Id),
+                              kvs:put(PrevTop#comment{next = FullId}),
+                              {Id, Parent} end,
             kvs:put(CC#comment{comments = FullId}),
-            PrevC
-    end,
+            PrevC end,
+
     Comment = #comment{id = FullId,
                        author_id = User,
                        comment_id = CommentId,
@@ -68,8 +41,27 @@ add(FId, User, EntryId, ParentComment, CommentId, Content, Medias) ->
                        media = Medias,
                        create_time = now(),
                        prev = Prev,
-                       next = undefined
-                      },
+                       next = undefined},
+
     kvs:put(Comment),
     {ok, Comment}.
 
+
+read_comments(undefined) -> [];
+read_comments([#comment{comments = C} | Rest]) -> [read_comments(C) | read_comments(Rest)];
+read_comments(C) -> kvs:traversal(comment, #comment.next, C, all).
+
+feed_comments({EId, FId}) ->
+    case kvs:get(entry,{EId, FId}) of
+        {ok, #entry{comments_rear = undefined}} -> [];
+        {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]).
+
+remove(FId, EId) ->
+    AllComments = feed_comments({EId, FId}),
+    [begin kvs:delete(comment, ID) end || #comment{id = ID, media = M} <- AllComments].
+

+ 7 - 27
src/kvs_feed.erl

@@ -1,4 +1,7 @@
 -module(kvs_feed).
+-author('Maxim Sokhatsky').
+-author('Andrii Zadorozhnii').
+-author('Alexander Kalenuk').
 -copyright('Synrc Research Center, s.r.o.').
 -compile(export_all).
 -include_lib("kvs/include/feeds.hrl").
@@ -151,15 +154,6 @@ edit_entry(FeedId, EId, NewDescription) ->
             kvs:put(NewEntry);
         {error, notfound}-> {error, notfound} end.
 
-remove_entry_comments(FId, EId) ->
-    AllComments = kvs:comments_by_entry(FId, EId),
-    [begin kvs:delete(comment, ID) end || #comment{id = ID, media = M} <- AllComments].
-
-entry_add_comment(FId, User, EntryId, ParentComment, CommentId, Content, Medias) ->
-     case kvs:get(entry,{EntryId, FId}) of
-         {ok, _E} -> kvs_comment:add(FId, User, EntryId, ParentComment, CommentId, Content, Medias);
-         _ -> ok end.
-
 like_list(undefined) -> [];
 like_list(Id) -> {ok, OneLike} = kvs:get(one_like, Id), [OneLike] ++ like_list(OneLike#one_like.next).
 like_list(undefined, _) -> [];
@@ -168,7 +162,7 @@ like_list(Id, N) -> {ok, OneLike} = kvs:get(one_like, Id), [OneLike] ++ like_lis
 
 entry_likes(Entry_id) ->
     case kvs:get(entry_likes, Entry_id) of
-        {ok, Likes} -> get_one_like_list(Likes#entry_likes.one_like_head);
+        {ok, Likes} -> like_list(Likes#entry_likes.one_like_head);
         {error, notfound} -> [] end.
 
 entry_likes_count(Entry_id) ->
@@ -183,28 +177,14 @@ user_likes_count(UserId) ->
 
 user_likes(UserId) ->
     case kvs:get(user_likes, UserId) of
-        {ok, Likes} -> get_one_like_list(Likes#user_likes.one_like_head);
+        {ok, Likes} -> like_list(Likes#user_likes.one_like_head);
         {error, notfound} -> [] end.
 
-
 user_likes(UserId, {Page, PageAmount}) ->
     case kvs:get(user_likes, UserId) of
-        {ok, Likes} -> lists:nthtail((Page-1)*PageAmount, get_one_like_list(Likes#user_likes.one_like_head, PageAmount*Page));
+        {ok, Likes} -> lists:nthtail((Page-1)*PageAmount, like_list(Likes#user_likes.one_like_head, PageAmount*Page));
         {error, notfound} -> [] end.
 
-comments_entries(UserUid, _, Page, PageAmount) ->
-    Pids = [Eid || #comment{entry_id=Eid} <- kvs:select(comment,
-        fun(#comment{author_id=Who}) when Who=:=UserUid ->true;(_)->false end)],
-    lists:flatten([kvs:select(entry,[{where, fun(#entry{entry_id=ID})-> ID=:=Pid end},
-        {order, {1, descending}},{limit, {1,1}}]) || Pid <- Pids]).
-
-get_my_discussions(FId, Page, PageAmount, UserUid) ->
-    Offset = case (Page-1)*PageAmount of 0 -> 1; M-> M  end,
-    Pids = [Eid || #comment{entry_id=Eid} <- kvs:select(comment,
-        fun(#comment{author_id=Who}) when Who=:=UserUid ->true;(_)->false end)],
-    lists:flatten([kvs:select(entry,[{where, fun(#entry{entry_id=ID})-> ID=:=Pid end},
-        {order, {1, descending}},{limit, {1,1}}]) || Pid <- Pids]).
-
 purge_feed(FeedId) ->
     {ok,Feed} = kvs:get(feed,FeedId),
     Removal = entry_traversal(Feed#feed.top, -1),
@@ -212,7 +192,7 @@ purge_feed(FeedId) ->
     kvs:put(Feed#feed{top=undefined}).
 
 purge_unverified_feeds() ->
-    [purge_feed(FeedId) || #user{feed=FeedId,status=S,email=E} <- kvs:all(user),E==undefined].
+    [purge_feed(FeedId) || #user{feed=FeedId,status=S,email=E} <- kvs:all(user), E==undefined].
 
 %% MQ API
 

+ 1 - 13
src/kvs_sup.erl

@@ -1,21 +1,12 @@
 -module(kvs_sup).
 -behaviour(supervisor).
--export([start_link/0, stop_riak/0]).
+-export([start_link/0]).
 -export([init/1]).
 -define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
 
 start_link() ->
     supervisor:start_link({local, ?MODULE}, ?MODULE, []).
 
-stop_riak() ->
-    application:stop(riak_kv), 
-    application:stop(riak_pipe),
-    application:stop(eleveldb),
-    application:stop(erlang_js),
-    application:stop(webmachine),
-    application:stop(mochiweb),
-    application:stop(bitcask).
-
 init([]) ->
   RestartStrategy = one_for_one,
   MaxRestarts = 1000,
@@ -35,12 +26,9 @@ init([]) ->
     {ok, _Value} -> skip end,
 
   kvs:initialize(),
-  kvs:init_indexes(),
 
   case application:get_env(kvs, riak_srv_node) of
     undefined ->
-      kvs:init_indexes(),
-
       case application:get_env(kvs, sync_nodes) of
          true -> [error_logger:info_msg("Joined: ~p ~p~n", [N, riak_core:join(N)]) || N <- application:get_env(kvs, nodes) -- [node()] ];
          _ -> skip end,

+ 1 - 0
src/map_reduce.erl

@@ -1,4 +1,5 @@
 -module(map_reduce).
+-author('Maxim Sokhatsky').
 -include_lib("log.hrl").
 -include_lib("stdlib/include/qlc.hrl").
 -compile(export_all).

+ 28 - 17
src/store_riak.erl

@@ -24,18 +24,6 @@ initialize() ->
     ets:insert(config, #config{ key = "riak_client", value = C}),
     ok.
 
-init_indexes() ->
-    C = riak_client(),
-    C:set_bucket(key_to_bin(id_seq), [{backend, leveldb_backend}]),
-    C:set_bucket(key_to_bin(subscription), [{backend, leveldb_backend}]),
-    C:set_bucket(key_to_bin(user), [{backend, leveldb_backend}]),
-    C:set_bucket(key_to_bin(group), [{backend, leveldb_backend}]),
-    C:set_bucket(key_to_bin(translation), [{backend, leveldb_backend}]),
-    C:set_bucket(key_to_bin(group_subscription), [{backend, leveldb_backend}]),
-    C:set_bucket(key_to_bin(user_bought_gifts), [{backend, leveldb_backend}]),
-    C:set_bucket(key_to_bin(play_record), [{backend, leveldb_backend}]),
-    ok.
-
 dir() ->
     C = riak_client(),
     {ok,Buckets} = C:list_buckets(),
@@ -61,11 +49,34 @@ make_object(T) ->
     error_logger:info_msg("RIAK PUT IDX ~p",[Indices]),
     Obj2.
 
-make_indices(#subscription{who=Who, whom=Whom}) -> [{<<"subs_who_bin">>, key_to_bin(Who)}, {<<"subs_whom_bin">>, key_to_bin(Whom)}];
-make_indices(#group_subscription{user_id=UId, group_id=GId}) -> [{<<"group_subs_user_bin">>, key_to_bin(UId)}, {<<"group_subs_group_bin">>, key_to_bin(GId)}];
-make_indices(#user_bought_gifts{username=UId}) -> [{<<"user_bought_gifts_username_bin">>, key_to_bin(UId)}];
-make_indices(#user{username=UId,zone=Zone}) -> [{<<"user_bin">>, key_to_bin(UId)}];
-make_indices(Record) -> [{key_to_bin(atom_to_list(element(1,Record))++"_bin"),key_to_bin(element(2,Record))}].
+make_indices(#subscription{who=Who, whom=Whom}) -> [
+    {<<"who_bin">>, key_to_bin(Who)},
+    {<<"whom_bin">>, key_to_bin(Whom)}];
+
+make_indices(#group_subscription{user_id=UId, group_id=GId}) -> [
+    {<<"who_bin">>, key_to_bin(UId)},
+    {<<"where_bin">>, key_to_bin(GId)}];
+
+make_indices(#user_bought_gifts{username=UId}) -> [
+    {<<"user_bought_gifts_username_bin">>, key_to_bin(UId)}];
+
+make_indices(#user{username=UId,zone=Zone}) -> [
+    {<<"user_bin">>, key_to_bin(UId)},
+    {<<"zone_bin">>, key_to_bin(Zone)}];
+
+make_indices(#comment{id={CID,EID},author_id=Who}) -> [
+    {<<"comment_bin">>, key_to_bin({CID,EID})},
+    {<<"author_bin">>, key_to_bin(Who)}];
+
+make_indices(#entry{id={EID,FID},entry_id=EntryId,feed_id=Feed,from=From,to=To}) -> [
+    {<<"entry_feed_bin">>, key_to_bin({EID,FID})},
+    {<<"entry_bin">>, key_to_bin(EntryId)},
+    {<<"from_bin">>, key_to_bin(From)},
+    {<<"to_bin">>, key_to_bin(To)},
+    {<<"feed_bin">>, key_to_bin(Feed)}];
+
+make_indices(Record) -> [
+    {key_to_bin(atom_to_list(element(1,Record))++"_bin"),key_to_bin(element(2,Record))}].
 
 riak_client() -> [{_,_,{_,C}}] = ets:lookup(config, "riak_client"), C.