-
Notifications
You must be signed in to change notification settings - Fork 478
Expand file tree
/
Copy pathHashSplitting.java
More file actions
118 lines (92 loc) · 3.1 KB
/
Copy pathHashSplitting.java
File metadata and controls
118 lines (92 loc) · 3.1 KB
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
/*
* Copyright 2013-2026 chronicle.software; SPDX-License-Identifier: Apache-2.0
*/
package net.openhft.chronicle.hash.impl;
import net.openhft.chronicle.core.Maths;
import net.openhft.chronicle.hash.serialization.impl.EnumMarshallable;
import net.openhft.chronicle.wire.Marshallable;
import net.openhft.chronicle.wire.WireIn;
import net.openhft.chronicle.wire.WireOut;
import org.jetbrains.annotations.NotNull;
import static net.openhft.chronicle.core.Maths.isPowerOf2;
public interface HashSplitting extends Marshallable {
static HashSplitting forSegments(int segments) {
assert segments > 0;
if (segments == 1)
return ForSingleSegment.INSTANCE;
if (isPowerOf2(segments))
return new ForPowerOf2Segments(segments);
return new ForNonPowerOf2Segments(segments);
}
int segmentIndex(long hash);
long segmentHash(long hash);
final class ForSingleSegment implements HashSplitting, EnumMarshallable<ForSingleSegment> {
public static final ForSingleSegment INSTANCE = new ForSingleSegment();
private ForSingleSegment() {
}
@Override
public int segmentIndex(long hash) {
return 0;
}
@Override
public long segmentHash(long hash) {
return hash;
}
@NotNull
@Override
public ForSingleSegment readResolve() {
return INSTANCE;
}
}
class ForPowerOf2Segments implements HashSplitting {
private int bits;
private transient int mask;
ForPowerOf2Segments(int segments) {
assert Maths.isPowerOf2(segments);
bits = Integer.numberOfTrailingZeros(segments);
mask = segments - 1;
}
@Override
public int segmentIndex(long hash) {
return ((int) hash) & mask;
}
@Override
public long segmentHash(long hash) {
return hash >>> bits;
}
@Override
public void readMarshallable(@NotNull WireIn wire) {
bits = wire.read(() -> "bits").int32();
mask = (1 << bits) - 1;
}
@Override
public void writeMarshallable(@NotNull WireOut wire) {
wire.write(() -> "bits").int32(bits);
}
}
//TODO optimize?
class ForNonPowerOf2Segments implements HashSplitting {
private static final int MASK = Integer.MAX_VALUE;
private static final int BITS = 31;
private int segments;
public ForNonPowerOf2Segments(int segments) {
this.segments = segments;
}
@Override
public int segmentIndex(long hash) {
return (((int) hash) & MASK) % segments;
}
@Override
public long segmentHash(long hash) {
return hash >>> BITS;
}
@Override
public void readMarshallable(@NotNull WireIn wire) {
segments = wire.read(() -> "segments").int32();
}
@Override
public void writeMarshallable(@NotNull WireOut wire) {
wire.write(() -> "segments").int32(segments);
}
}
}