Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Share per-table cursors for get/put/delete operations.

  • Loading branch information...
commit e4b3acbd11cb1e199b9c36860683d8fd1b35019a 1 parent f94336f
Gregory Burd gburd authored

Showing 1 changed file with 27 additions and 6 deletions. Show diff stats Hide diff stats

  1. +27 6 src/riak_kv_wterl_backend.erl
33 src/riak_kv_wterl_backend.erl
@@ -53,6 +53,7 @@
53 53 -record(state, {conn :: wterl:connection(),
54 54 table :: string(),
55 55 session :: wterl:session(),
  56 + cursors :: ets:tid(),
56 57 partition :: integer()}).
57 58
58 59 -type state() :: #state{}.
@@ -145,18 +146,24 @@ start(Partition, Config) ->
145 146
146 147 %% @doc Stop the wterl backend
147 148 -spec stop(state()) -> ok.
148   -stop(#state{conn=ConnRef, session=SRef}) ->
  149 +stop(#state{conn=ConnRef, session=SRef, cursors=undefined}) ->
149 150 ok = wterl:session_close(SRef),
150   - wterl_conn:close(ConnRef).
  151 + wterl_conn:close(ConnRef);
  152 +stop(#state{cursors=Cursors}=State) ->
  153 + ets:foldl(fun({_Table, Cursor}, _) ->
  154 + ok = wterl:cursor_close(Cursor)
  155 + end, true, Cursors),
  156 + ets:delete(Cursors),
  157 + stop(State#state{cursors=undefined}).
151 158
152 159 %% @doc Retrieve an object from the wterl backend
153 160 -spec get(riak_object:bucket(), riak_object:key(), state()) ->
154 161 {ok, any(), state()} |
155 162 {ok, not_found, state()} |
156 163 {error, term(), state()}.
157   -get(Bucket, Key, #state{table=Table, session=SRef}=State) ->
  164 +get(Bucket, Key, #state{session=SRef, table=Table}=State) ->
158 165 WTKey = to_object_key(Bucket, Key),
159   - case wterl:session_get(SRef, Table, WTKey) of
  166 + case wterl:cursor_search(shared_cursor(SRef, Table, State), WTKey) of
160 167 {ok, Value} ->
161 168 {ok, Value, State};
162 169 not_found ->
@@ -175,7 +182,7 @@ get(Bucket, Key, #state{table=Table, session=SRef}=State) ->
175 182 {error, term(), state()}.
176 183 put(Bucket, PrimaryKey, _IndexSpecs, Val, #state{table=Table, session=SRef}=State) ->
177 184 WTKey = to_object_key(Bucket, PrimaryKey),
178   - case wterl:session_put(SRef, Table, WTKey, Val) of
  185 + case wterl:cursor_insert(shared_cursor(SRef, Table, State), WTKey, Val) of
179 186 ok ->
180 187 {ok, State};
181 188 {error, Reason} ->
@@ -191,7 +198,7 @@ put(Bucket, PrimaryKey, _IndexSpecs, Val, #state{table=Table, session=SRef}=Stat
191 198 {error, term(), state()}.
192 199 delete(Bucket, Key, _IndexSpecs, #state{table=Table, session=SRef}=State) ->
193 200 WTKey = to_object_key(Bucket, Key),
194   - case wterl:session_delete(SRef, Table, WTKey) of
  201 + case wterl:cursor_remove(shared_cursor(SRef, Table, State), WTKey) of
195 202 ok ->
196 203 {ok, State};
197 204 {error, Reason} ->
@@ -340,6 +347,20 @@ callback(_Ref, _Msg, State) ->
340 347 %% Internal functions
341 348 %% ===================================================================
342 349
  350 +shared_cursor(SRef, Table, #state{cursors=undefined}=State) ->
  351 + Cursors = ets:new(?MODULE, []),
  352 + shared_cursor(SRef, Table, State#state{cursors=Cursors});
  353 +shared_cursor(SRef, Table, #state{cursors=Cursors}=State) ->
  354 + case ets:lookup(Cursors, Table) of
  355 + [{Table, Cursor}] ->
  356 + {Cursor, State};
  357 + _ ->
  358 + Cursor = wterl:cursor_open(SRef, Table),
  359 + ets:insert(Cursors, {Table, Cursor}),
  360 + {Cursor, State}
  361 + end.
  362 +
  363 +
343 364 %% @private
344 365 %% Return a function to fold over the buckets on this backend
345 366 fold_buckets_fun(FoldBucketsFun) ->

0 comments on commit e4b3acb

Please sign in to comment.
Something went wrong with that request. Please try again.