Skip to content
Permalink
Browse files
Replace subspace usage with macros
The subspace layer is really not super helpful in Erlang. In Pyhton the
subspace layer helps significantly by using magic functions and
inheritance. However, Erlang doesn't have similar syntactical niceties
so it ends up being more burdensome than just adding macros for tuple
mutations.

If someone has a better idea I'm all ears on this as the macros do take
a decent level of knowledge on tuple packing to make use of.
  • Loading branch information
davisp committed Feb 15, 2019
1 parent aedfcac commit 1cb7d6b62e90283be5525060c241676ea9b3e674
Showing 2 changed files with 47 additions and 11 deletions.
@@ -0,0 +1,33 @@
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.


-define(ERLFDB_ERROR(Reason), erlang:error({?MODULE, Reason})).


-define(ERLFDB_PACK(Tuple), erlfdb_tuple:pack(Tuple)).
-define(ERLFDB_PACK(Tuple, Prefix), erlfdb_tuple:pack(Tuple, Prefix)).

-define(ERLFDB_RANGE(Prefix), erlfdb_tuple:range({}, Prefix)).
-define(ERLFDB_RANGE(Prefix, Term), erlfdb_tuple:range({Term}, Prefix)).

-define(ERLFDB_EXTEND(Prefix, Term), erlfdb_tuple:pack({Term}, Prefix)).

-define(ERLFDB_EXTRACT(Prefix, Packed), begin
__PrefixLen = size(Prefix),
<<Prefix:__PrefixLen/binary, __Tail/binary>> = Packed,
erlfdb_tuple:unpack(__Tail)
end).




@@ -23,16 +23,19 @@
]).


-include("erlfdb.hrl").


-record(erlfdb_hca, {
counters,
recent
}).


create(Subspace) ->
create(Prefix) ->
#erlfdb_hca{
counters = erlfdb_subspace:add(Subspace, 0),
recent = erlfdb_subspace:add(Subspace, 1)
counters = ?ERLFDB_EXTEND(Prefix, 0),
recent = ?ERLFDB_EXTEND(Prefix, 1)
}.


@@ -52,7 +55,7 @@ current_start(HCA, Tx) ->
#erlfdb_hca{
counters = Counters
} = HCA,
{CRangeStart, CRangeEnd} = erlfdb_subspace:range(Counters),
{CRangeStart, CRangeEnd} = ?ERLFDB_RANGE(Counters),
Options = [
{snapshot, true},
{reverse, true},
@@ -63,7 +66,7 @@ current_start(HCA, Tx) ->
[] ->
0;
[{CounterKey, _}] ->
{Start} = erlfdb_subspace:unpack(Counters, CounterKey),
{Start} = ?ERLFDB_EXTRACT(Counters, CounterKey),
Start
end.

@@ -77,7 +80,7 @@ range(HCA, Tx, Start, WindowAdvanced) ->
clear_previous_window(HCA, Tx, Start)
end,

CounterKey = erlfdb_subspace:pack(Counters, {Start}),
CounterKey = ?ERLFDB_EXTEND(Counters, Start),
erlfbd:add(Tx, CounterKey, 1),

Count = case erlfdb:wait(erlfdb:get_ss(Tx, CounterKey)) of
@@ -100,15 +103,15 @@ search_candidate(HCA, Tx, {Start, WindowSize}) ->

% -1 because random:uniform is 1 =< X $=< WindowSize
Candidate = Start + rand:uniform(WindowSize) - 1,
CandidateValueKey = erlfdb_subspace:pack(Recent, {Candidate}),
CandidateValueKey = ?ERLFDB_EXTEND(Recent, Candidate),

Options = [
{snapshot, true},
{reverse, true},
{streaming_mode, exact},
{limit, 1}
],
{CRangeStart, CRangeEnd} = erlfdb_subspace:range(Counters),
{CRangeStart, CRangeEnd} = ?ERLFDB_RANGE(Counters),

CFuture = erlfdb:get_range(Tx, CRangeStart, CRangeEnd, Options),
CVFuture = erlfdb:get(Tx, CandidateValueKey),
@@ -121,7 +124,7 @@ search_candidate(HCA, Tx, {Start, WindowSize}) ->
% Check that we're still in the same counter window
case LatestCounter of
[{CounterKey, _}] ->
{LStart} = erlfdb_subspace:unpack(Counters, CounterKey),
{LStart} = ?ERLFDB_EXTRACT(Counters, CounterKey),
if LStart == Start -> ok; true ->
throw(hca_restart)
end;
@@ -144,8 +147,8 @@ clear_previous_window(HCA, Tx, Start) ->
recent = Recent
} = HCA,

{CRangeStart, CRangeEnd} = erlfdb_subspace:range(Counters, {Start}),
{RRangeStart, RRangeEnd} = erlfdb_subspace:range(Recent, {Start}),
{CRangeStart, CRangeEnd} = ?ERLFDB_RANGE(Counters, Start),
{RRangeStart, RRangeEnd} = ?ERLFDB_RANGE(Recent, Start),

erlfdb:clear_range(Tx, CRangeStart, CRangeEnd),
erlfdb:set_option(Tx, next_write_no_conflict_range),

0 comments on commit 1cb7d6b

Please sign in to comment.