1
%% ``The contents of this file are subject to the Erlang Public License,
2
%% Version 1.1, (the "License"); you may not use this file except in
3
%% compliance with the License. You should have received a copy of the
4
%% Erlang Public License along with this software. If not, it can be
5
%% retrieved via the world wide web at http://www.erlang.org/.
7
%% Software distributed under the License is distributed on an "AS IS"
8
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
%% the License for the specific language governing rights and limitations
12
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
%% AB. All Rights Reserved.''
16
%% $Id: mnesia_frag_old_hash.erl,v 1.1 2008/12/17 09:53:38 mikpe Exp $
18
%%%----------------------------------------------------------------------
19
%%% Purpose : Implements hashing functionality for fragmented tables
20
%%%----------------------------------------------------------------------
22
-module(mnesia_frag_old_hash).
23
-behaviour(mnesia_frag_hash).
25
%% Hashing callback functions
31
match_spec_to_frag_numbers/2
34
-record(old_hash_state,
39
%% Old style. Kept for backwards compatibility.
46
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48
init_state(_Tab, InitialState) when InitialState == undefined ->
49
#old_hash_state{n_fragments = 1,
52
init_state(_Tab, FH) when record(FH, frag_hash) ->
53
%% Old style. Kept for backwards compatibility.
54
#old_hash_state{n_fragments = FH#frag_hash.n_fragments,
55
next_n_to_split = FH#frag_hash.next_n_to_split,
56
n_doubles = FH#frag_hash.n_doubles}.
58
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60
add_frag(State) when record(State, old_hash_state) ->
61
SplitN = State#old_hash_state.next_n_to_split,
63
L = State#old_hash_state.n_doubles,
64
NewN = State#old_hash_state.n_fragments + 1,
65
State2 = case trunc(math:pow(2, L)) + 1 of
67
State#old_hash_state{n_fragments = NewN,
71
State#old_hash_state{n_fragments = NewN,
74
{State2, [SplitN], [NewN]}.
76
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78
del_frag(State) when record(State, old_hash_state) ->
79
P = State#old_hash_state.next_n_to_split - 1,
80
L = State#old_hash_state.n_doubles,
81
N = State#old_hash_state.n_fragments,
85
MergeN = trunc(math:pow(2, L2)),
86
State2 = State#old_hash_state{n_fragments = N - 1,
87
next_n_to_split = MergeN,
89
{State2, [N], [MergeN]};
92
State2 = State#old_hash_state{n_fragments = N - 1,
93
next_n_to_split = MergeN},
94
{State2, [N], [MergeN]}
97
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99
key_to_frag_number(State, Key) when record(State, old_hash_state) ->
100
L = State#old_hash_state.n_doubles,
101
A = erlang:hash(Key, trunc(math:pow(2, L))),
102
P = State#old_hash_state.next_n_to_split,
105
erlang:hash(Key, trunc(math:pow(2, L + 1)));
110
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112
match_spec_to_frag_numbers(State, MatchSpec) when record(State, old_hash_state) ->
114
[{HeadPat, _, _}] when tuple(HeadPat), size(HeadPat) > 2 ->
115
KeyPat = element(2, HeadPat),
116
case has_var(KeyPat) of
118
[key_to_frag_number(State, KeyPat)];
120
lists:seq(1, State#old_hash_state.n_fragments)
123
lists:seq(1, State#old_hash_state.n_fragments)