This is part of a series, see the previous one at Riak Core Tutorial Part 3: Ping Command

Implementing Get, Put and Delete

For our first commands we will copy the general structure of the ping command.

We will start by adding three new functions to the tanodb.erl file:

get ( Bucket , Key ) -> ReqId = make_ref (), send_to_one ( Bucket , Key , { get , ReqId , { Bucket , Key }}). put ( Bucket , Key , Value ) -> ReqId = make_ref (), send_to_one ( Bucket , Key , { put , ReqId , { Bucket , Key , Value }}). delete ( Bucket , Key ) -> ReqId = make_ref (), send_to_one ( Bucket , Key , { delete , ReqId , { Bucket , Key }}).

And generalizing the code used by ping to send a command to one vnode:

send_to_one ( Bucket , Key , Cmd ) -> DocIdx = riak_core_util : chash_key ({ Bucket , Key }), PrefList = riak_core_apl : get_primary_apl ( DocIdx , 1 , tanodb ), [{ IndexNode , _ Type }] = PrefList , riak_core_vnode_master : sync_spawn_command ( IndexNode , Cmd , tanodb_vnode_master ).

In tanodb_vnode.erl we will need to first create an instance of the key-value store per vnode at initialization and keep a reference to its state in the vnode state record:

- record ( state , { partition , kv_state }). init ([ Partition ]) -> { ok , KvState } = tanodb_kv_ets : new (#{ partition => Partition }), { ok , #state { partition = Partition , kv_state = KvState }}.

We then need to add three new clauses to the handle_command callback to handle our two new commands, which translate almost directly to calls in the kv module: