1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2015 Erlang Solutions Ltd. |
3 |
|
%% |
4 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
%% you may not use this file except in compliance with the License. |
6 |
|
%% You may obtain a copy of the License at |
7 |
|
%% |
8 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
%% |
10 |
|
%% Unless required by applicable law or agreed to in writing, software |
11 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
%% See the License for the specific language governing permissions and |
14 |
|
%% limitations under the License. |
15 |
|
%%============================================================================== |
16 |
|
-module(mongoose_riak). |
17 |
|
|
18 |
|
-include_lib("riakc/include/riakc.hrl"). |
19 |
|
|
20 |
|
%% API |
21 |
|
-export([put/1, put/2]). |
22 |
|
-export([get/2, get/3]). |
23 |
|
-export([delete/2, delete/3]). |
24 |
|
-export([update_type/3, update_type/4]). |
25 |
|
-export([fetch_type/2, fetch_type/3]). |
26 |
|
-export([list_keys/1]). |
27 |
|
-export([list_buckets/1]). |
28 |
|
-export([create_new_map/1]). |
29 |
|
-export([update_map/2]). |
30 |
|
-export([mapred/2]). |
31 |
|
-export([search/2]). |
32 |
|
-export([search/3]). |
33 |
|
-export([get_index/4]). |
34 |
|
-export([get_index_range/5]). |
35 |
|
-export([get_worker/0]). |
36 |
|
|
37 |
|
-ignore_xref([fetch_type/3, get/3, list_buckets/1, put/2, search/2, update_type/4]). |
38 |
|
|
39 |
|
-compile({no_auto_import, [put/2]}). |
40 |
|
|
41 |
|
-define(CALL(F, Args), call_riak(F, Args)). |
42 |
|
|
43 |
|
-type riakc_map_op() :: {{binary(), MapDataType :: atom()}, |
44 |
|
fun((riakc_datatype:datatype()) -> riakc_datatype:datatype())}. |
45 |
|
|
46 |
|
%%%% |
47 |
|
|
48 |
|
-spec put(riakc_obj()) -> |
49 |
|
ok | {ok, riakc_obj()} | {ok, key()} | {error, term()}. |
50 |
|
put(Obj) -> |
51 |
:-( |
put(Obj, []). |
52 |
|
|
53 |
|
-spec put(riakc_obj(), timeout() | put_options()) -> |
54 |
|
ok | {ok, riakc_obj()} | {ok, key()} | {error, term()}. |
55 |
|
put(Obj, OptsOrTimeout) -> |
56 |
:-( |
?CALL(put, [Obj, OptsOrTimeout]). |
57 |
|
|
58 |
|
-spec get(bucket() | {binary(), bucket()}, key()) -> {ok, riakc_obj()} | {error, term()}. |
59 |
|
get(Bucket, Key) -> |
60 |
:-( |
get(Bucket, Key, []). |
61 |
|
|
62 |
|
-spec get(bucket() | {binary(), bucket()}, key(), get_options() | timeout()) -> |
63 |
|
{ok, riakc_obj()} | {error, term()}. |
64 |
|
get(Bucket, Key, OptsOrTimeout) -> |
65 |
:-( |
?CALL(get, [Bucket, Key, OptsOrTimeout]). |
66 |
|
|
67 |
|
|
68 |
|
-spec update_type({binary(), binary()}, binary(), riakc_datatype:update(term())) -> |
69 |
|
ok | {error, term()}. |
70 |
|
update_type(Bucket, Key, Update) -> |
71 |
:-( |
update_type(Bucket, Key, Update, []). |
72 |
|
|
73 |
|
-spec update_type({binary(), binary()}, binary(), |
74 |
|
riakc_datatype:update(term()), [proplists:property()]) -> |
75 |
|
ok | {error, term()}. |
76 |
|
update_type(Bucket, Key, Update, Options) -> |
77 |
:-( |
?CALL(update_type, [Bucket, Key, Update, Options]). |
78 |
|
|
79 |
|
-spec delete(bucket() | {binary(), bucket()}, key()) -> |
80 |
|
ok | {error, term()}. |
81 |
|
delete(Bucket, Key) -> |
82 |
:-( |
delete(Bucket, Key, []). |
83 |
|
|
84 |
|
-spec delete(bucket() | {binary(), bucket()}, key(), delete_options() | timeout()) -> |
85 |
|
ok | {error, term()}. |
86 |
|
delete(Bucket, Key, OptsOrTimeout) -> |
87 |
:-( |
?CALL(delete, [Bucket, Key, OptsOrTimeout]). |
88 |
|
|
89 |
|
-spec fetch_type({binary(), binary()}, binary()) -> |
90 |
|
{ok, riakc_datatype:datatype()} | {error, term()}. |
91 |
|
fetch_type(Bucket, Key) -> |
92 |
:-( |
fetch_type(Bucket, Key, []). |
93 |
|
|
94 |
|
-spec fetch_type({binary(), binary()}, binary(), [proplists:property()]) -> |
95 |
|
{ok, riakc_datatype:datatype()} | {error, term()}. |
96 |
|
fetch_type(Bucket, Key, Opts) -> |
97 |
:-( |
?CALL(fetch_type, [Bucket, Key, Opts]). |
98 |
|
|
99 |
|
-spec list_keys({binary(), binary()}) -> |
100 |
|
{ok, [binary()]} | {error, term()}. |
101 |
|
list_keys(Bucket) -> |
102 |
:-( |
?CALL(list_keys, [Bucket]). |
103 |
|
|
104 |
|
-spec list_buckets(binary()) -> list(). |
105 |
|
list_buckets(Type) -> |
106 |
39 |
?CALL(list_buckets, [Type]). |
107 |
|
|
108 |
|
-spec create_new_map([riakc_map_op()]) -> riakc_map:crdt_map(). |
109 |
|
create_new_map(Ops) -> |
110 |
:-( |
update_map(riakc_map:new(), Ops). |
111 |
|
|
112 |
|
-spec update_map(riakc_map:crdt_map(), [riakc_map_op()]) -> riakc_map:crdt_map(). |
113 |
|
update_map(Map, Ops) -> |
114 |
:-( |
lists:foldl(fun update_map_op/2, Map, Ops). |
115 |
|
|
116 |
|
-type mapred_bucket_type_idx_input() :: {index, riakc_obj:bucket(), |
117 |
|
binary()|secondary_index_id(), |
118 |
|
StartKey::key()|integer(), |
119 |
|
EndKey::key()|integer()}. |
120 |
|
|
121 |
|
-spec mapred(mapred_inputs() | mapred_bucket_type_idx_input(), [mapred_queryterm()]) -> |
122 |
|
{ok, mapred_result()} | {error, term()}. |
123 |
|
mapred(KeyFileters, MapRed) -> |
124 |
:-( |
?CALL(mapred, [KeyFileters, MapRed]). |
125 |
|
|
126 |
|
search(Index, Query) -> |
127 |
:-( |
search(Index, Query, []). |
128 |
|
|
129 |
|
search(Index, Query, Opts) -> |
130 |
:-( |
?CALL(search, [Index, Query, Opts]). |
131 |
|
|
132 |
|
-spec get_index(Bucket :: riakc_obj:bucket(), |
133 |
|
Index :: binary() | secondary_index_id(), |
134 |
|
Key :: key() | integer(), |
135 |
|
Opts :: [term()]) -> |
136 |
|
{ok, index_results()} | {error, term()}. |
137 |
|
get_index(BucketType, Index, Value, Opts) -> |
138 |
:-( |
?CALL(get_index_eq, [BucketType, Index, Value, Opts]). |
139 |
|
|
140 |
|
-spec get_index_range(Bucket :: riakc_obj:bucket(), |
141 |
|
Index :: binary() | secondary_index_id(), |
142 |
|
StartKey :: key() | integer() | list(), |
143 |
|
EndKey :: key() | integer() | list(), |
144 |
|
Opts :: [term()]) -> |
145 |
|
{ok, index_results()} | {error, term()}. |
146 |
|
get_index_range(Bucket, Index, StartKey, EndKey, Opts) -> |
147 |
:-( |
?CALL(get_index_range, [Bucket, Index, StartKey, EndKey, Opts]). |
148 |
|
|
149 |
|
update_map_op({Field, Fun}, Map) -> |
150 |
:-( |
riakc_map:update(Field, Fun, Map). |
151 |
|
|
152 |
|
call_riak(F, ArgsIn) -> |
153 |
39 |
Args = [get_worker() | ArgsIn], |
154 |
:-( |
apply(riakc_pb_socket, F, Args). |
155 |
|
|
156 |
|
get_worker() -> |
157 |
39 |
{ok, Worker} = mongoose_wpool:get_worker(riak), |
158 |
:-( |
Worker. |