aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mnesia/src/mnesia_frag_old_hash.erl
blob: b246c762361de4a457a60a37204858c588a8498c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2002-2016. All Rights Reserved.
%% 
%% 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.
%% 
%% %CopyrightEnd%
%%

%%
%%%----------------------------------------------------------------------
%%% Purpose : Implements hashing functionality for fragmented tables
%%%----------------------------------------------------------------------

-module(mnesia_frag_old_hash).
%%-behaviour(mnesia_frag_hash).

-compile({nowarn_deprecated_function, {erlang,hash,2}}).

%% Hashing callback functions
-export([
	 init_state/2,
	 add_frag/1,
	 del_frag/1,
	 key_to_frag_number/2,
	 match_spec_to_frag_numbers/2
	]).

-record(old_hash_state,
	{n_fragments,
	 next_n_to_split,
	 n_doubles}).

%% Old style. Kept for backwards compatibility.
-record(frag_hash,
	{foreign_key,
	 n_fragments,
	 next_n_to_split,
	 n_doubles}).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

init_state(_Tab, InitialState) when InitialState == undefined ->
    #old_hash_state{n_fragments     = 1,
		    next_n_to_split = 1,
		    n_doubles       = 0};
init_state(_Tab, FH) when is_record(FH, frag_hash) ->
    %% Old style. Kept for backwards compatibility.
    #old_hash_state{n_fragments     = FH#frag_hash.n_fragments,
		    next_n_to_split = FH#frag_hash.next_n_to_split,
		    n_doubles       = FH#frag_hash.n_doubles}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

add_frag(State) when is_record(State, old_hash_state) ->
    SplitN = State#old_hash_state.next_n_to_split,
    P = SplitN + 1,
    L = State#old_hash_state.n_doubles,
    NewN = State#old_hash_state.n_fragments + 1,
    State2 = case trunc(math:pow(2, L)) + 1 of
		 P2 when P2 == P ->
		     State#old_hash_state{n_fragments = NewN,
					  next_n_to_split = 1,
					  n_doubles = L + 1};
		 _ ->
		     State#old_hash_state{n_fragments = NewN,
					  next_n_to_split = P}
	     end,
    {State2, [SplitN], [NewN]}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

del_frag(State) when is_record(State, old_hash_state) ->
    P = State#old_hash_state.next_n_to_split - 1,
    L = State#old_hash_state.n_doubles,
    N = State#old_hash_state.n_fragments,
    if
	P < 1 ->
	    L2 = L - 1,
	    MergeN = trunc(math:pow(2, L2)),
	    State2 = State#old_hash_state{n_fragments = N - 1,
					  next_n_to_split = MergeN,
					  n_doubles = L2},
	    {State2, [N], [MergeN]};
	true ->
	    MergeN = P,
	    State2 = State#old_hash_state{n_fragments = N - 1,
					  next_n_to_split = MergeN},
	    {State2, [N], [MergeN]}
	end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

key_to_frag_number(State, Key) when is_record(State, old_hash_state) ->
    L = State#old_hash_state.n_doubles,
    A = erlang:hash(Key, trunc(math:pow(2, L))),
    P = State#old_hash_state.next_n_to_split,
    if
	A < P ->
	    erlang:hash(Key, trunc(math:pow(2, L + 1)));
	true ->
	    A
    end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

match_spec_to_frag_numbers(State, MatchSpec) when is_record(State, old_hash_state) ->
    case MatchSpec of
	[{HeadPat, _, _}] when is_tuple(HeadPat), tuple_size(HeadPat) > 2 ->
	    KeyPat = element(2, HeadPat),
	    case has_var(KeyPat) of
		false ->
		    [key_to_frag_number(State, KeyPat)];
		true ->
		    lists:seq(1, State#old_hash_state.n_fragments)
	    end;
	_ -> 
	    lists:seq(1, State#old_hash_state.n_fragments)
    end.

has_var(Pat) ->
    mnesia:has_var(Pat).