Skip to content
Browse files

Various adjustments prior to posting

* Used 'offsetX' classes in the UI for padding.
* Added comments to the csd_riak module.
* Removed vote_* fields from snippet.
* General code tidying.
  • Loading branch information...
1 parent acf46cb commit a9234f827b64910a5c19e6b2f56ce31f6759000e @OJ committed May 3, 2012
View
59 apps/csd_core/src/csd_riak.erl
@@ -1,13 +1,10 @@
-%% @author OJ Reeves <oj@buffered.io>
-%% @copyright 2010 OJ Reeves
-
-%% @doc Functions which wrap up the communication with the Riak cluster
-%% plus a few other helper functions.
-
-module(csd_riak).
-author('OJ Reeves <oj@buffered.io>').
-%% Riak exports
+%% ------------------------------------------------------------------
+%% API Function Exports
+%% ------------------------------------------------------------------
+
-export([connect/1,
create/3,
create/4,
@@ -28,18 +25,17 @@
get_mapred_phase_reduce_js/3,
get_mapred_reduce_sort_js/1,
get_mapred_reduce_sort_js/2,
- get_mapred_reduce_sum_js/0,
- get_mapred_reduce_sum_js/1
+ new_key/0,
+ new_key/1
]).
-%% helper functions for generating unique keys.
--export([new_key/0, new_key/1]).
-
-define(INDEX_KEY, <<"index">>).
-define(INDEX_SUFFIX_INT, "_int").
-define(INDEX_SUFFIX_BIN, "_bin").
-%% ----------------------------------------------- Exported Functions
+%% ------------------------------------------------------------------
+%% API Function Definitions
+%% ------------------------------------------------------------------
%% @spec connect(connection_info()) -> pid()
%% @doc Create a connection to the specified Riak cluster and
@@ -129,53 +125,58 @@ save(RiakPid, RiakObj) ->
Result = riakc_pb_socket:put(RiakPid, RiakObj),
Result.
+%% @doc Executes a map/reduce of the given Phases job over the Inputs.
mapred(RiakPid, Input, Phases) when is_list(Phases) ->
Result = riakc_pb_socket:mapred(RiakPid, Input, Phases),
Result.
+%% @doc Creates a map/reduce Input phase for a secondary index input.
get_mapred_phase_input_index(Bucket, Type, Index, Value) when is_integer(Value) ->
get_mapred_phase_input_index(Bucket, Type, Index, integer_to_list(Value));
get_mapred_phase_input_index(Bucket, Type, Index, Value) when is_list(Value) ->
get_mapred_phase_input_index(Bucket, Type, Index, list_to_binary(Value));
get_mapred_phase_input_index(Bucket, Type, Index, Value) when is_binary(Value) ->
{index, Bucket, index(Type, Index), Value}.
+%% @doc Creates a map/reduce Map phase from raw JS source. This overload
+%% defaults Keep to true and Arg to none.
get_mapred_phase_map_js(JsSource) ->
get_mapred_phase_map_js(JsSource, true).
+%% @doc Creates a map/reduce Map phase from raw JS source. This overload
+%% defaults Arg to none.
get_mapred_phase_map_js(JsSource, Keep) ->
get_mapred_phase_map_js(JsSource, Keep, none).
+%% @doc Creates a map/reduce Map phase from raw JS source.
get_mapred_phase_map_js(JsSource, Keep, Arg) ->
- get_mapred_phase_map({jsanon, JsSource}, Keep, Arg).
-
-get_mapred_phase_map(Fun, Keep, Arg) ->
- {map, Fun, Arg, Keep}.
+ {map, {jsanon, JsSource}, Arg, Keep}.
+%% @doc Creates a map/reduce Reduce phase from raw JS source. This overload
+%% defaults Keep to true and Arg to none.
get_mapred_phase_reduce_js(JsSource) ->
get_mapred_phase_reduce_js(JsSource, true).
+%% @doc Creates a map/reduce Reduce phase from raw JS source. This overload
+%% defaults Keep to true.
get_mapred_phase_reduce_js(JsSource, Keep) ->
get_mapred_phase_reduce_js(JsSource, Keep, none).
+%% @doc Creates a map/reduce Reduce phase from raw JS source.
get_mapred_phase_reduce_js(JsSource, Keep, Arg) ->
- get_mapred_phase_reduce({jsanon, JsSource}, Keep, Arg).
-
-get_mapred_phase_reduce(Fun, Keep, Arg) ->
- {reduce, Fun, Arg, Keep}.
+ {reduce, {jsanon, JsSource}, Arg, Keep}.
+%% @doc Creates a map/reduce Reduce sort phase using Riak's built in sort function
+%% using the specified comparison function written in raw JS. This overload
+%% defaults Keep to true.
get_mapred_reduce_sort_js(CompareFun) ->
get_mapred_reduce_sort_js(CompareFun, true).
+%% @doc Creates a map/reduce Reduce sort phase using Riak's built in sort function
+%% using the specified comparison function written in raw JS.
get_mapred_reduce_sort_js(CompareFun, Keep) ->
{reduce, {jsfun, <<"Riak.reduceSort">>}, CompareFun, Keep}.
-get_mapred_reduce_sum_js() ->
- get_mapred_reduce_sum_js(true).
-
-get_mapred_reduce_sum_js(Keep) ->
- {reduce, {jsfun, <<"Riak.reduceSum">>}, <<"">>, Keep}.
-
%% @spec new_key() -> key()
%% @doc Generate an close-to-unique key that can be used to identify
%% an object in riak. This implementation is blatantly borrowed
@@ -193,6 +194,10 @@ new_key(List) ->
Hash = erlang:phash2(List),
base64:encode(<<Hash:32>>).
+%% ------------------------------------------------------------------
+%% Private Function Definitions
+%% ------------------------------------------------------------------
+
index(int, Name) ->
iolist_to_binary([Name, ?INDEX_SUFFIX_INT]);
index(bin, Name) ->
View
2 apps/csd_core/src/csd_snippet.erl
@@ -19,8 +19,6 @@
-record(snippet, {
user_id,
key,
- votes_left = 0,
- votes_right = 0,
data
}).
View
1 apps/csd_core/src/csd_snippet_store.erl
@@ -34,7 +34,6 @@ list_for_user(RiakPid, UserId) ->
Index = csd_riak:get_mapred_phase_input_index(?BUCKET, int, ?USERID_INDEX, UserId),
Map = csd_riak:get_mapred_phase_map_js(?LIST_MAP_JS, false),
Sort = csd_riak:get_mapred_reduce_sort_js(?REDUCE_SORT_JS),
- %Sort = csd_riak:get_mapred_phase_reduce_js(?REDUCE_SORT_JS),
Result = case csd_riak:mapred(RiakPid, Index, [Map, Sort]) of
{ok, [{1, List}]} -> List;
{ok, []} -> []
View
5 apps/csd_core/src/csd_user_store.erl
@@ -26,9 +26,8 @@ save(RiakPid, User) ->
UserId = list_to_binary(integer_to_list(IntId)),
case csd_riak:fetch(RiakPid, ?BUCKET, UserId) of
- {ok, RiakObj} ->
- NewRiakObj = csd_riak:update(RiakObj, csd_user:to_json(User)),
- ok = csd_riak:save(RiakPid, NewRiakObj),
+ {ok, _RiakObj} ->
+ % user already exists, we don't need to save anything.
{ok, User};
{error, notfound} ->
NewRiakObj = csd_riak:create(?BUCKET, UserId, csd_user:to_json(User)),
View
3 apps/csd_core/src/csd_vote_store.erl
@@ -5,7 +5,6 @@
-define(BUCKET, <<"vote">>).
-define(SNIPPET_INDEX, <<"snippetid">>).
--define(VOTETYPE_INDEX, <<"which">>).
-define(USER_INDEX, <<"userid">>).
-define(COUNT_SNIP_MAP_JS, <<"function(v){var d=Riak.mapValuesJson(v)[0];if(d.which===\"left\"){return[[1,0]];}return[[0,1]];}">>).
-define(COUNT_SNIP_RED_JS, <<"function(vals,arg){if(vals.length===0){return[[0,0]];}return[vals.reduce(function(a,v){return[a[0]+v[0],a[1]+v[1]];})];}">>).
@@ -43,7 +42,6 @@ count_for_snippet(RiakPid, SnippetId, UserId) ->
save(RiakPid, Vote) ->
VoteId = csd_vote:get_id(Vote),
UserId = csd_vote:get_user_id(Vote),
- Which = csd_vote:get_which(Vote),
SnippetId = csd_vote:get_snippet_id(Vote),
case csd_riak:fetch(RiakPid, ?BUCKET, VoteId) of
@@ -52,7 +50,6 @@ save(RiakPid, Vote) ->
{error, notfound} ->
RiakObj = csd_riak:create(?BUCKET, VoteId, csd_vote:to_json(Vote)),
Indexes = [
- {bin, ?VOTETYPE_INDEX, Which},
{bin, ?SNIPPET_INDEX, SnippetId},
{int, ?USER_INDEX, UserId}
],
View
4 apps/csd_web/priv/app.config
@@ -37,8 +37,8 @@
},
{twitter,
[
- {consumer_key, ""},
- {consumer_secret, ""},
+ {consumer_key, "< your key here >"},
+ {consumer_secret, "< your key here >"},
{request_token_url, "https://api.twitter.com/oauth/request_token"},
{access_token_url, "https://api.twitter.com/oauth/access_token"},
{authenticate_url, "https://api.twitter.com/oauth/authenticate"},
View
18 apps/csd_web/priv/www/static/views/vote-actions.handlebars
@@ -1,8 +1,6 @@
{{#if voted}}
<div class="row vote-buttons">
- <div class="span1">&nbsp;
- </div>
- <div class="span4 centered">
+ <div class="span4 offset1 centered">
{{#if voted_left}}
<div class="alert alert-success">
<button class="close" data-dismiss="alert">&times;</button>
@@ -12,9 +10,7 @@
&nbsp;
{{/if}}
</div>
- <div class="span2">&nbsp;
- </div>
- <div class="span4 centered">
+ <div class="span4 offset2 centered">
{{#if voted_right}}
<div class="alert alert-success">
<button class="close" data-dismiss="alert">&times;</button>
@@ -24,8 +20,6 @@
&nbsp;
{{/if}}
</div>
- <div class="span1">&nbsp;
- </div>
</div>
{{else}}
{{#if show_actions}}
@@ -38,9 +32,7 @@
</div>
</div>
<div class="row vote-complete no-show">
- <div class="span4">&nbsp;
- </div>
- <div class="span4 centered">
+ <div class="span4 offset4 centered">
<div class="alert alert-success">
<button class="close" data-dismiss="alert">&times;</button>
<strong>Thanks!</strong> Your vote has been submitted.
@@ -49,9 +41,7 @@
</div>
{{else}}
<div class="row vote-buttons">
- <div class="span4">&nbsp;
- </div>
- <div class="span4 centered">
+ <div class="span4 offset4 centered">
<div class="alert alert-error">
<button class="close" data-dismiss="alert">&times;</button>
<strong>Voting disabled!</strong> You need to be <a href="#login">signed in</a> to vote.

0 comments on commit a9234f8

Please sign in to comment.
Something went wrong with that request. Please try again.