2012-10-10 22:14:06 +00:00
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% Copyright (c) 2012 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.
|
|
|
|
%%
|
|
|
|
%% -------------------------------------------------------------------
|
2012-09-11 18:44:48 +00:00
|
|
|
-module(secondary_index_tests).
|
|
|
|
-export([confirm/0]).
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
|
|
|
-define(BUCKET, <<"2ibucket">>).
|
|
|
|
|
|
|
|
confirm() ->
|
2012-09-24 17:01:31 +00:00
|
|
|
Nodes = rt:build_cluster(3),
|
2012-09-11 18:44:48 +00:00
|
|
|
?assertEqual(ok, rt:wait_until_nodes_ready(Nodes)),
|
|
|
|
|
|
|
|
Pid = rt:pbc(hd(Nodes)),
|
|
|
|
|
|
|
|
[put_an_object(Pid, N) || N <- lists:seq(0, 20)],
|
|
|
|
|
|
|
|
assertExactQuery(Pid, [<<"obj5">>], <<"field1_bin">>, <<"val5">>),
|
|
|
|
assertExactQuery(Pid, [<<"obj5">>], <<"field2_int">>, <<"5">>),
|
|
|
|
assertRangeQuery(Pid, [<<"obj10">>, <<"obj11">>, <<"obj12">>], <<"field1_bin">>, <<"val10">>, <<"val12">>),
|
|
|
|
assertRangeQuery(Pid, [<<"obj10">>, <<"obj11">>, <<"obj12">>], <<"field2_int">>, 10, 12),
|
|
|
|
assertRangeQuery(Pid, [<<"obj10">>, <<"obj11">>, <<"obj12">>], <<"$key">>, <<"obj10">>, <<"obj12">>),
|
|
|
|
|
|
|
|
lager:info("Delete an object, verify deletion..."),
|
|
|
|
riakc_pb_socket:delete(Pid, ?BUCKET, <<"obj5">>),
|
|
|
|
riakc_pb_socket:delete(Pid, ?BUCKET, <<"obj11">>),
|
|
|
|
|
|
|
|
lager:info("Sleeping for 5 seconds. Make sure the tombstone is reaped..."),
|
|
|
|
timer:sleep(5000),
|
|
|
|
|
|
|
|
assertExactQuery(Pid, [], <<"field1_bin">>, <<"val5">>),
|
|
|
|
assertExactQuery(Pid, [], <<"field2_int">>, <<"5">>),
|
|
|
|
assertRangeQuery(Pid, [<<"obj10">>, <<"obj12">>], <<"field1_bin">>, <<"val10">>, <<"val12">>),
|
|
|
|
assertRangeQuery(Pid, [<<"obj10">>, <<"obj12">>], <<"field2_int">>, 10, 12),
|
|
|
|
assertRangeQuery(Pid, [<<"obj10">>, <<"obj12">>], <<"$key">>, <<"obj10">>, <<"obj12">>),
|
|
|
|
|
2012-10-12 22:46:17 +00:00
|
|
|
%% Verify the $key index, and riak_kv#367 regression
|
2012-10-25 19:08:07 +00:00
|
|
|
assertRangeQuery(Pid, [<<"obj6">>], <<"$key">>, <<"obj6">>, <<"obj6">>),
|
|
|
|
assertRangeQuery(Pid, [<<"obj6">>, <<"obj7">>], <<"$key">>, <<"obj6">>, <<"obj7">>),
|
2012-09-11 18:44:48 +00:00
|
|
|
pass.
|
|
|
|
|
|
|
|
put_an_object(Pid, N) ->
|
|
|
|
lager:debug("Putting object ~p", [N]),
|
|
|
|
Indexes = [{"field1_bin", list_to_binary(io_lib:format("val~p", [N]))},
|
|
|
|
{"field2_int", N}],
|
|
|
|
MetaData = dict:from_list([{<<"index">>, Indexes}]),
|
|
|
|
Robj0 = riakc_obj:new(?BUCKET, list_to_binary(io_lib:format("obj~p", [N]))),
|
|
|
|
Robj1 = riakc_obj:update_value(Robj0, io_lib:format("data~p", [N])),
|
|
|
|
Robj2 = riakc_obj:update_metadata(Robj1, MetaData),
|
|
|
|
riakc_pb_socket:put(Pid, Robj2).
|
|
|
|
|
|
|
|
|
|
|
|
assertExactQuery(Pid, Expected, Index, Value) ->
|
|
|
|
lager:debug("Searching Index ~p for ~p", [Index, Value]),
|
|
|
|
{ok, Results} = riakc_pb_socket:get_index(Pid, ?BUCKET, Index, Value),
|
2012-09-14 17:53:39 +00:00
|
|
|
ActualKeys = lists:sort(Results),
|
2012-09-11 18:44:48 +00:00
|
|
|
lager:debug("Expected: ~p", [Expected]),
|
|
|
|
lager:debug("Actual : ~p", [ActualKeys]),
|
|
|
|
?assertEqual(Expected, ActualKeys).
|
|
|
|
|
|
|
|
assertRangeQuery(Pid, Expected, Index, StartValue, EndValue) ->
|
|
|
|
lager:debug("Searching Index ~p for ~p-~p", [Index, StartValue, EndValue]),
|
|
|
|
{ok, Results} = riakc_pb_socket:get_index(Pid, ?BUCKET, Index, StartValue, EndValue),
|
2012-09-14 17:53:39 +00:00
|
|
|
ActualKeys = lists:sort(Results),
|
2012-09-11 18:44:48 +00:00
|
|
|
lager:debug("Expected: ~p", [Expected]),
|
|
|
|
lager:debug("Actual : ~p", [ActualKeys]),
|
2012-10-12 22:46:17 +00:00
|
|
|
?assertEqual(Expected, ActualKeys).
|