riak_test/tests/ts_basic.erl

114 lines
3.9 KiB
Erlang
Raw Normal View History

%% -------------------------------------------------------------------
%%
%% Copyright (c) 2015 Basho Technologies, Inc.
%%
%% 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.
%%
%% -------------------------------------------------------------------
%% @doc A module to test riak_ts basic create bucket/put/select cycle.
-module(ts_basic).
-behavior(riak_test).
-export([confirm/0]).
-include_lib("eunit/include/eunit.hrl").
2015-09-16 12:33:34 +00:00
-define(BUCKET, <<"ts_test_bucket_one">>).
-define(PKEY_P1, <<"sensor">>).
-define(PKEY_P2, <<"time">>).
2015-09-20 23:25:14 +00:00
-define(PVAL_P1, <<"ZXC11">>).
-define(TIMEBASE, (10*1000*1000)).
confirm() ->
2015-10-14 02:51:21 +00:00
io:format("Data to be written: ~p\n", [make_data()]),
2015-09-16 12:33:34 +00:00
ClusterSize = 1,
lager:info("Building cluster"),
_Nodes = [Node1|_] =
build_cluster(
ClusterSize),
%% 1. use riak-admin to create a bucket
TableDef = io_lib:format(
"CREATE TABLE ~s "
"(~s varchar not null, "
" ~s timestamp not null, "
2015-09-18 09:28:35 +00:00
" score float not null, "
" PRIMARY KEY((quantum(time, 10, s)), time, sensor))",
[?BUCKET, ?PKEY_P1, ?PKEY_P2]),
2015-09-16 12:33:34 +00:00
Props = io_lib:format("{\\\"props\\\": {\\\"n_val\\\": 3, \\\"table_def\\\": \\\"~s\\\"}}", [TableDef]),
rt:admin(Node1, ["bucket-type", "create", binary_to_list(?BUCKET), lists:flatten(Props)]),
2015-09-18 09:28:35 +00:00
rt:admin(Node1, ["bucket-type", "activate", binary_to_list(?BUCKET)]),
%% 2. set up a client
C = rt:pbc(Node1),
?assert(is_pid(C)),
%% 3. put some data
2015-10-14 02:51:21 +00:00
Data0 = make_data(),
ResPut = riakc_ts:put(C, ?BUCKET, Data0),
io:format("Put ~b records: ~p\n", [length(Data0), ResPut]),
2015-10-14 02:51:21 +00:00
%% 4. delete one
ElementToDelete = 15,
DelRecord = [DelSensor, DelTimepoint, _Score] =
lists:nth(ElementToDelete, Data0),
DelKey = [DelTimepoint, DelSensor],
2015-10-15 20:04:47 +00:00
DelNXKey = [DelTimepoint, <<"keke">>],
2015-10-14 02:51:21 +00:00
%% Data = lists:delete(DelRecord, Data0),
ResDel = riakc_ts:delete(C, ?BUCKET, DelKey, []),
2015-10-15 20:04:47 +00:00
?assertEqual(ResDel, ok),
io:format("Deleted key ~b (~p): ~p\n", [ElementToDelete, DelRecord, ResDel]),
ResDelNX = riakc_ts:delete(C, ?BUCKET, DelNXKey, []),
?assertEqual(ResDelNX, ok),
io:format("Not deleted non-existing key: ~p\n", [ResDelNX]),
2015-10-14 02:51:21 +00:00
%% 5. select
Query =
lists:flatten(
io_lib:format(
2015-09-20 23:25:14 +00:00
"select * from ~s where ~s > ~b and ~s < ~b and sensor = \"~s\"",
[?BUCKET, ?PKEY_P2, ?TIMEBASE + 10, ?PKEY_P2, ?TIMEBASE + 20, ?PVAL_P1])),
io:format("Running query: ~p\n", [Query]),
{_Columns, Rows} = riakc_ts:query(C, Query),
io:format("Got ~b rows back\n~p\n", [length(Rows), Rows]),
2015-10-14 02:51:21 +00:00
?assertEqual(length(Rows), 10 - 1 - 1),
{_Columns, Rows} = riakc_ts:query(C, Query),
io:format("Got ~b rows back again\n", [length(Rows)]),
?assertEqual(length(Rows), 10 - 1 - 1),
pass.
%% @ignore
-spec build_cluster(non_neg_integer()) -> [node()].
build_cluster(Size) ->
2015-10-15 20:04:26 +00:00
build_cluster(Size, [{yokozuna, [{enabled, true}]}]).
-spec build_cluster(pos_integer(), list()) -> [node()].
build_cluster(Size, Config) ->
2015-09-16 12:33:34 +00:00
[_Node1|_] = Nodes = rt:deploy_nodes(Size, Config),
rt:join_cluster(Nodes),
Nodes.
2015-09-20 23:25:14 +00:00
-define(LIFESPAN, 30).
make_data() ->
lists:foldl(
fun(T, Q) ->
2015-09-20 23:25:14 +00:00
[[?PVAL_P1,
?TIMEBASE + ?LIFESPAN - T + 1,
2015-09-16 12:33:34 +00:00
math:sin(float(T) / 100 * math:pi())] | Q]
end,
2015-09-18 09:28:35 +00:00
[], lists:seq(?LIFESPAN, 0, -1)).