Skip to content

Instantly share code, notes, and snippets.

@seancribbs
Last active December 16, 2015 17:09
Show Gist options
  • Save seancribbs/5467861 to your computer and use it in GitHub Desktop.
Save seancribbs/5467861 to your computer and use it in GitHub Desktop.
Demo code from NoSQL Matters Cologne 2013
%% -------------------------------------------------------------------
%%
%% counters_demo: Sets up and clears node partitions
%%
%% Copyright (c) 2007-2012 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
-module(counters_demo).
-export([part/2]).
part(Friends0, Cookie) ->
OldCookie = erlang:get_cookie(),
Enemies = nodes() -- Friends0,
Friends = [node()|Friends0],
[ rpc:call(F, erlang, set_cookie, [F, Cookie]) || F <- Friends ],
[ rpc:call(F, erlang, disconnect_node, [E]) || F <- Friends,
E <- Enemies ],
fun() ->
heal(Friends, Enemies, OldCookie)
end.
heal(Friends, Enemies, Cookie) ->
[ rpc:call(F, erlang, set_cookie, [F, Cookie]) || F <- Friends ],
rpc:sbcast(Friends ++ Enemies, riak_core_node_watcher, broadcast).
<!DOCTYPE html>
<html>
<head>
<script src="jquery-2.0.0.min.js"></script>
<script type="text/javascript">
var t = window.setInterval(function() {
get_counter();}, 500);
function get_counter() {
$.ajax({
url: "/buckets/demo/counters/demo",
cache: false,
dataType: "text",
success: function(data) {
$('#counter').text(data);
}});
}
$(document).ready(function() {
$("#increment").click(function() {
$.ajax({
type: 'POST',
data: '1',
url: '/buckets/demo/counters/demo',
success: function() { get_counter() },
cache:false,
dataType: 'text'
});
});
});
$(document).ready(function() {
$("#decrement").click(function() {
$.ajax({
type: 'POST',
data: '-1',
url: '/buckets/demo/counters/demo',
success: function() { get_counter() },
cache:false,
dataType: 'text'
});
});
});
</script>
<style>
body {
background: #fbfbfb;
font-family:'titillium', verdana, arial, sans-serif;
font-weight: normal;
line-height: 1;
}
h1, h2, h3, h4, h5, h6 {
font-family:'titillium', verdana, arial, sans-serif;
font-weight: bold;
color: #444;
}
input, textarea {
border: none;
}
input {
color: #fff;
}
#counter {
background: #384945;
text-align: center;
margin: 0 auto;
color: #fe9925;
font-family:'titillium', verdana, arial, sans-serif;
font-weight: bold;
font-size: 84pt;
width: 30%;
padding: .2em
}
#controls {
padding: 3em;
text-align: center;
}
</style>
<title>Riak KV Counters Demo</title>
</head>
<body>
<div id="counter">
</div>
<div id="controls">
<a href="#" id="increment">Increment</a>&nbsp;
<a href="#" id="decrement">Decrement</a>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment