Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit a9234f827b64910a5c19e6b2f56ce31f6759000e 1 parent acf46cb
OJ Reeves authored
59 apps/csd_core/src/csd_riak.erl
... ... @@ -1,13 +1,10 @@
1   -%% @author OJ Reeves <oj@buffered.io>
2   -%% @copyright 2010 OJ Reeves
3   -
4   -%% @doc Functions which wrap up the communication with the Riak cluster
5   -%% plus a few other helper functions.
6   -
7 1 -module(csd_riak).
8 2 -author('OJ Reeves <oj@buffered.io>').
9 3
10   -%% Riak exports
  4 +%% ------------------------------------------------------------------
  5 +%% API Function Exports
  6 +%% ------------------------------------------------------------------
  7 +
11 8 -export([connect/1,
12 9 create/3,
13 10 create/4,
@@ -28,18 +25,17 @@
28 25 get_mapred_phase_reduce_js/3,
29 26 get_mapred_reduce_sort_js/1,
30 27 get_mapred_reduce_sort_js/2,
31   - get_mapred_reduce_sum_js/0,
32   - get_mapred_reduce_sum_js/1
  28 + new_key/0,
  29 + new_key/1
33 30 ]).
34 31
35   -%% helper functions for generating unique keys.
36   --export([new_key/0, new_key/1]).
37   -
38 32 -define(INDEX_KEY, <<"index">>).
39 33 -define(INDEX_SUFFIX_INT, "_int").
40 34 -define(INDEX_SUFFIX_BIN, "_bin").
41 35
42   -%% ----------------------------------------------- Exported Functions
  36 +%% ------------------------------------------------------------------
  37 +%% API Function Definitions
  38 +%% ------------------------------------------------------------------
43 39
44 40 %% @spec connect(connection_info()) -> pid()
45 41 %% @doc Create a connection to the specified Riak cluster and
@@ -129,10 +125,12 @@ save(RiakPid, RiakObj) ->
129 125 Result = riakc_pb_socket:put(RiakPid, RiakObj),
130 126 Result.
131 127
  128 +%% @doc Executes a map/reduce of the given Phases job over the Inputs.
132 129 mapred(RiakPid, Input, Phases) when is_list(Phases) ->
133 130 Result = riakc_pb_socket:mapred(RiakPid, Input, Phases),
134 131 Result.
135 132
  133 +%% @doc Creates a map/reduce Input phase for a secondary index input.
136 134 get_mapred_phase_input_index(Bucket, Type, Index, Value) when is_integer(Value) ->
137 135 get_mapred_phase_input_index(Bucket, Type, Index, integer_to_list(Value));
138 136 get_mapred_phase_input_index(Bucket, Type, Index, Value) when is_list(Value) ->
@@ -140,42 +138,45 @@ get_mapred_phase_input_index(Bucket, Type, Index, Value) when is_list(Value) ->
140 138 get_mapred_phase_input_index(Bucket, Type, Index, Value) when is_binary(Value) ->
141 139 {index, Bucket, index(Type, Index), Value}.
142 140
  141 +%% @doc Creates a map/reduce Map phase from raw JS source. This overload
  142 +%% defaults Keep to true and Arg to none.
143 143 get_mapred_phase_map_js(JsSource) ->
144 144 get_mapred_phase_map_js(JsSource, true).
145 145
  146 +%% @doc Creates a map/reduce Map phase from raw JS source. This overload
  147 +%% defaults Arg to none.
146 148 get_mapred_phase_map_js(JsSource, Keep) ->
147 149 get_mapred_phase_map_js(JsSource, Keep, none).
148 150
  151 +%% @doc Creates a map/reduce Map phase from raw JS source.
149 152 get_mapred_phase_map_js(JsSource, Keep, Arg) ->
150   - get_mapred_phase_map({jsanon, JsSource}, Keep, Arg).
151   -
152   -get_mapred_phase_map(Fun, Keep, Arg) ->
153   - {map, Fun, Arg, Keep}.
  153 + {map, {jsanon, JsSource}, Arg, Keep}.
154 154
  155 +%% @doc Creates a map/reduce Reduce phase from raw JS source. This overload
  156 +%% defaults Keep to true and Arg to none.
155 157 get_mapred_phase_reduce_js(JsSource) ->
156 158 get_mapred_phase_reduce_js(JsSource, true).
157 159
  160 +%% @doc Creates a map/reduce Reduce phase from raw JS source. This overload
  161 +%% defaults Keep to true.
158 162 get_mapred_phase_reduce_js(JsSource, Keep) ->
159 163 get_mapred_phase_reduce_js(JsSource, Keep, none).
160 164
  165 +%% @doc Creates a map/reduce Reduce phase from raw JS source.
161 166 get_mapred_phase_reduce_js(JsSource, Keep, Arg) ->
162   - get_mapred_phase_reduce({jsanon, JsSource}, Keep, Arg).
163   -
164   -get_mapred_phase_reduce(Fun, Keep, Arg) ->
165   - {reduce, Fun, Arg, Keep}.
  167 + {reduce, {jsanon, JsSource}, Arg, Keep}.
166 168
  169 +%% @doc Creates a map/reduce Reduce sort phase using Riak's built in sort function
  170 +%% using the specified comparison function written in raw JS. This overload
  171 +%% defaults Keep to true.
167 172 get_mapred_reduce_sort_js(CompareFun) ->
168 173 get_mapred_reduce_sort_js(CompareFun, true).
169 174
  175 +%% @doc Creates a map/reduce Reduce sort phase using Riak's built in sort function
  176 +%% using the specified comparison function written in raw JS.
170 177 get_mapred_reduce_sort_js(CompareFun, Keep) ->
171 178 {reduce, {jsfun, <<"Riak.reduceSort">>}, CompareFun, Keep}.
172 179
173   -get_mapred_reduce_sum_js() ->
174   - get_mapred_reduce_sum_js(true).
175   -
176   -get_mapred_reduce_sum_js(Keep) ->
177   - {reduce, {jsfun, <<"Riak.reduceSum">>}, <<"">>, Keep}.
178   -
179 180 %% @spec new_key() -> key()
180 181 %% @doc Generate an close-to-unique key that can be used to identify
181 182 %% an object in riak. This implementation is blatantly borrowed
@@ -193,6 +194,10 @@ new_key(List) ->
193 194 Hash = erlang:phash2(List),
194 195 base64:encode(<<Hash:32>>).
195 196
  197 +%% ------------------------------------------------------------------
  198 +%% Private Function Definitions
  199 +%% ------------------------------------------------------------------
  200 +
196 201 index(int, Name) ->
197 202 iolist_to_binary([Name, ?INDEX_SUFFIX_INT]);
198 203 index(bin, Name) ->
2  apps/csd_core/src/csd_snippet.erl
@@ -19,8 +19,6 @@
19 19 -record(snippet, {
20 20 user_id,
21 21 key,
22   - votes_left = 0,
23   - votes_right = 0,
24 22 data
25 23 }).
26 24
1  apps/csd_core/src/csd_snippet_store.erl
@@ -34,7 +34,6 @@ list_for_user(RiakPid, UserId) ->
34 34 Index = csd_riak:get_mapred_phase_input_index(?BUCKET, int, ?USERID_INDEX, UserId),
35 35 Map = csd_riak:get_mapred_phase_map_js(?LIST_MAP_JS, false),
36 36 Sort = csd_riak:get_mapred_reduce_sort_js(?REDUCE_SORT_JS),
37   - %Sort = csd_riak:get_mapred_phase_reduce_js(?REDUCE_SORT_JS),
38 37 Result = case csd_riak:mapred(RiakPid, Index, [Map, Sort]) of
39 38 {ok, [{1, List}]} -> List;
40 39 {ok, []} -> []
5 apps/csd_core/src/csd_user_store.erl
@@ -26,9 +26,8 @@ save(RiakPid, User) ->
26 26 UserId = list_to_binary(integer_to_list(IntId)),
27 27
28 28 case csd_riak:fetch(RiakPid, ?BUCKET, UserId) of
29   - {ok, RiakObj} ->
30   - NewRiakObj = csd_riak:update(RiakObj, csd_user:to_json(User)),
31   - ok = csd_riak:save(RiakPid, NewRiakObj),
  29 + {ok, _RiakObj} ->
  30 + % user already exists, we don't need to save anything.
32 31 {ok, User};
33 32 {error, notfound} ->
34 33 NewRiakObj = csd_riak:create(?BUCKET, UserId, csd_user:to_json(User)),
3  apps/csd_core/src/csd_vote_store.erl
@@ -5,7 +5,6 @@
5 5
6 6 -define(BUCKET, <<"vote">>).
7 7 -define(SNIPPET_INDEX, <<"snippetid">>).
8   --define(VOTETYPE_INDEX, <<"which">>).
9 8 -define(USER_INDEX, <<"userid">>).
10 9 -define(COUNT_SNIP_MAP_JS, <<"function(v){var d=Riak.mapValuesJson(v)[0];if(d.which===\"left\"){return[[1,0]];}return[[0,1]];}">>).
11 10 -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) ->
43 42 save(RiakPid, Vote) ->
44 43 VoteId = csd_vote:get_id(Vote),
45 44 UserId = csd_vote:get_user_id(Vote),
46   - Which = csd_vote:get_which(Vote),
47 45 SnippetId = csd_vote:get_snippet_id(Vote),
48 46
49 47 case csd_riak:fetch(RiakPid, ?BUCKET, VoteId) of
@@ -52,7 +50,6 @@ save(RiakPid, Vote) ->
52 50 {error, notfound} ->
53 51 RiakObj = csd_riak:create(?BUCKET, VoteId, csd_vote:to_json(Vote)),
54 52 Indexes = [
55   - {bin, ?VOTETYPE_INDEX, Which},
56 53 {bin, ?SNIPPET_INDEX, SnippetId},
57 54 {int, ?USER_INDEX, UserId}
58 55 ],
4 apps/csd_web/priv/app.config
@@ -37,8 +37,8 @@
37 37 },
38 38 {twitter,
39 39 [
40   - {consumer_key, ""},
41   - {consumer_secret, ""},
  40 + {consumer_key, "< your key here >"},
  41 + {consumer_secret, "< your key here >"},
42 42 {request_token_url, "https://api.twitter.com/oauth/request_token"},
43 43 {access_token_url, "https://api.twitter.com/oauth/access_token"},
44 44 {authenticate_url, "https://api.twitter.com/oauth/authenticate"},
18 apps/csd_web/priv/www/static/views/vote-actions.handlebars
... ... @@ -1,8 +1,6 @@
1 1 {{#if voted}}
2 2 <div class="row vote-buttons">
3   - <div class="span1">&nbsp;
4   - </div>
5   - <div class="span4 centered">
  3 + <div class="span4 offset1 centered">
6 4 {{#if voted_left}}
7 5 <div class="alert alert-success">
8 6 <button class="close" data-dismiss="alert">&times;</button>
@@ -12,9 +10,7 @@
12 10 &nbsp;
13 11 {{/if}}
14 12 </div>
15   - <div class="span2">&nbsp;
16   - </div>
17   - <div class="span4 centered">
  13 + <div class="span4 offset2 centered">
18 14 {{#if voted_right}}
19 15 <div class="alert alert-success">
20 16 <button class="close" data-dismiss="alert">&times;</button>
@@ -24,8 +20,6 @@
24 20 &nbsp;
25 21 {{/if}}
26 22 </div>
27   - <div class="span1">&nbsp;
28   - </div>
29 23 </div>
30 24 {{else}}
31 25 {{#if show_actions}}
@@ -38,9 +32,7 @@
38 32 </div>
39 33 </div>
40 34 <div class="row vote-complete no-show">
41   - <div class="span4">&nbsp;
42   - </div>
43   - <div class="span4 centered">
  35 + <div class="span4 offset4 centered">
44 36 <div class="alert alert-success">
45 37 <button class="close" data-dismiss="alert">&times;</button>
46 38 <strong>Thanks!</strong> Your vote has been submitted.
@@ -49,9 +41,7 @@
49 41 </div>
50 42 {{else}}
51 43 <div class="row vote-buttons">
52   - <div class="span4">&nbsp;
53   - </div>
54   - <div class="span4 centered">
  44 + <div class="span4 offset4 centered">
55 45 <div class="alert alert-error">
56 46 <button class="close" data-dismiss="alert">&times;</button>
57 47 <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.