2012년 9월 3일 월요일

2i in riak leveldb

qrtest_driver_riakclient_leveldb.erl
다음과 같이 index를 줘서 put을 하고,

run(put2, KeyGen, ValueGen, State) ->
    DeviceQRBin = list_to_binary(get_random_string(2,"0123456789ABCDEF")),
    BucketBin = << <<"qr_log">>/binary , DeviceQRBin/binary >>,
    NowKey=term_to_binary(now()), %% for distionction

    ProviderID = random:uniform(10),
    ActionBin = list_to_binary(get_random_string(2,"ABCDEF")),
    ResultBin = list_to_binary(get_random_string(2,"123456")),

    T = util_time:epoch(), %% time in second
    It=#qr_log{ device_qr = DeviceQRBin , provider_id = ProviderID, action= ActionBin, result=ResultBin , time=T },

    MetaData = dict:store(<<"index">>, [ {"time_int", T}, {"action_bin", ActionBin} ], dict:new()),
    Robj = riak_object:new(BucketBin,NowKey, term_to_binary(It),MetaData),      

    case (State#state.client):put(Robj, State#state.replies) of
        ok ->
            io:format("."),
            {ok, State};
        {error, Reason} ->
            %%io:format("?~p~n",[Reason]),
            io:format("?"),
            {error, Reason, State};
        _ ->
            io:format("-")
    end;

다음과 같이 get을 하면 된다.






  
    qrtest_driver_riakclient_leveldb.erl
    
  
  
    
run(get_index1, KeyGen, _ValueGen, State) ->
    DeviceQRBin = list_to_binary(get_random_string(2,"0123456789ABCDEF")),
    BucketBin = << <<"qr_log">>/binary , DeviceQRBin/binary >>,
    ActionBin = list_to_binary(get_random_string(2,"ABCDEF")),
    Query = { eq , <<"action_bin">> , ActionBin  }, %%=> not to crash

    case (State#state.client):get_index(BucketBin, Query , 10000) of %% 10000 is timeout value
        {ok, R} ->
            io:format("!~p",[R]),
            {ok, State};
        {error, notfound} ->
            io:format("_"),
            {ok, State};
        {error, Reason} ->
            {error, Reason, State};
        {error, timeout , _ } ->
            io:format("T"),
            {ok, State};
        _ ->
            io:format("="),
            {ok, State}     
    end;