|
| 1 | +%% ------------------------------------------------------------------- |
| 2 | +%% |
| 3 | +%% basho_bench: Benchmarking Suite |
| 4 | +%% |
| 5 | +%% Copyright (c) 2009-2012 Basho Techonologies |
| 6 | +%% |
| 7 | +%% This file is provided to you under the Apache License, |
| 8 | +%% Version 2.0 (the "License"); you may not use this file |
| 9 | +%% except in compliance with the License. You may obtain |
| 10 | +%% a copy of the License at |
| 11 | +%% |
| 12 | +%% http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +%% |
| 14 | +%% Unless required by applicable law or agreed to in writing, |
| 15 | +%% software distributed under the License is distributed on an |
| 16 | +%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | +%% KIND, either express or implied. See the License for the |
| 18 | +%% specific language governing permissions and limitations |
| 19 | +%% under the License. |
| 20 | +%% |
| 21 | +%% ------------------------------------------------------------------- |
| 22 | +-module(basho_bench_driver_cassandra_cql). |
| 23 | + |
| 24 | +-export([new/1, |
| 25 | + run/4]). |
| 26 | + |
| 27 | +-include("basho_bench.hrl"). |
| 28 | + |
| 29 | +-record(state, { client, |
| 30 | + keyspace, |
| 31 | + columnfamily, |
| 32 | + column |
| 33 | + }). |
| 34 | + |
| 35 | + |
| 36 | +%% ==================================================================== |
| 37 | +%% API |
| 38 | +%% ==================================================================== |
| 39 | + |
| 40 | +new(Id) -> |
| 41 | + Host = basho_bench_config:get(cassandra_host, "localhost"), |
| 42 | + Port = basho_bench_config:get(cassandra_port, 9042), |
| 43 | + Keyspace = basho_bench_config:get(cassandra_keyspace, "Keyspace1"), |
| 44 | + ColumnFamily = basho_bench_config:get(cassandra_columnfamily, "ColumnFamily1"), |
| 45 | + Column = basho_bench_config:get(cassandra_column, "Column"), |
| 46 | + |
| 47 | + % connect to client |
| 48 | + {ok, C} = erlcql:start_link(Host, [{port, Port}]), |
| 49 | + error_logger:info_msg("Id: ~p, " |
| 50 | + "Connected to Cassandra at Host ~p and Port ~p\n", [Id, Host, Port]), |
| 51 | + |
| 52 | + |
| 53 | + case ksbarrier(C, Keyspace) of |
| 54 | + ok -> |
| 55 | + {ok, #state { client = C, |
| 56 | + keyspace = Keyspace, |
| 57 | + columnfamily = ColumnFamily, |
| 58 | + column = Column}}; |
| 59 | + {error, Reason} -> |
| 60 | + error_logger:error_msg("Failed to get a erlcql client for ~p: ~p\n", |
| 61 | + [Host, Reason]) |
| 62 | + end. |
| 63 | + |
| 64 | + |
| 65 | +ksbarrier(C, Keyspace) -> |
| 66 | + case erlcql:q(C, lists:concat(["USE ", Keyspace, ";"])) of |
| 67 | + {ok, _KSBin} -> ok; |
| 68 | + {error, not_ready} -> |
| 69 | + %% Not ready yet, try again |
| 70 | + timer:sleep(100), |
| 71 | + ksbarrier(C, Keyspace); |
| 72 | + {error, _} = Error -> |
| 73 | + Error |
| 74 | + end. |
| 75 | + |
| 76 | +run(get, KeyGen, _ValueGen, |
| 77 | + #state{client=C, columnfamily=ColumnFamily, column=Column}=State) -> |
| 78 | + Key = KeyGen(), |
| 79 | + Query = ["SELECT ", Column ," FROM ", ColumnFamily ," where KEY = '", Key ,"';"], |
| 80 | + case erlcql:q(C, Query, one) of |
| 81 | + {ok,void} -> |
| 82 | + {ok, State}; |
| 83 | + {ok, {_Rows, _Cols}} -> |
| 84 | + {ok, State}; |
| 85 | + Error -> |
| 86 | + {error, Error, State} |
| 87 | + end; |
| 88 | +run(insert, KeyGen, ValueGen, |
| 89 | + #state{client=C, columnfamily=ColumnFamily, column=Column}=State) -> |
| 90 | + Key = KeyGen(), |
| 91 | + Val = ValueGen(), |
| 92 | + Query = ["INSERT INTO ", ColumnFamily , |
| 93 | + " (KEY, ", Column, ") VALUES " |
| 94 | + "('", Key ,"', ", bin_to_hexstr(Val) ,");"], |
| 95 | + |
| 96 | + case erlcql:q(C, Query, any) of |
| 97 | + {ok,void} -> |
| 98 | + {ok, State}; |
| 99 | + {ok, {_Rows, _Cols}} -> |
| 100 | + {ok, State}; |
| 101 | + Error -> |
| 102 | + {error, Error, State} |
| 103 | + end; |
| 104 | +run(put, KeyGen, ValueGen, |
| 105 | + #state{client=C, columnfamily=ColumnFamily, column=Column}=State) -> |
| 106 | + Key = KeyGen(), |
| 107 | + Val = ValueGen(), |
| 108 | + Query = ["UPDATE ", ColumnFamily, |
| 109 | + " SET ", Column, " = ", bin_to_hexstr(Val), |
| 110 | + " WHERE KEY = '", Key, "';"], |
| 111 | + |
| 112 | + case erlcql:q(C, Query, any) of |
| 113 | + {ok,void} -> |
| 114 | + {ok, State}; |
| 115 | + {ok, {_Rows, _Cols}} -> |
| 116 | + {ok, State}; |
| 117 | + Error -> |
| 118 | + {error, Error, State} |
| 119 | + end; |
| 120 | +run(delete, KeyGen, _ValueGen, |
| 121 | + #state{client=C, columnfamily=ColumnFamily}=State) -> |
| 122 | + Key = KeyGen(), |
| 123 | + Query = ["DELETE FROM ", ColumnFamily ," WHERE KEY = '", Key ,"';"], |
| 124 | + case erlcql:q(C, Query, any) of |
| 125 | + {ok,void} -> |
| 126 | + {ok, State}; |
| 127 | + {ok, {_Rows, _Cols}} -> |
| 128 | + {ok, State}; |
| 129 | + Error -> |
| 130 | + {error, Error, State} |
| 131 | + end. |
| 132 | + |
| 133 | +%% Internal Functions |
| 134 | + |
| 135 | +hex(N) when N < 10 -> |
| 136 | + $0+N; |
| 137 | +hex(N) when N >= 10, N < 16 -> |
| 138 | + $a+(N-10). |
| 139 | + |
| 140 | +bin_to_hexstr(Bin) -> |
| 141 | + List = binary_to_list(Bin), |
| 142 | + ["0x", [ [hex(N div 16), hex(N rem 16)] || N <- List, N < 256 ] ]. |
0 commit comments