aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/morling/onebrc/CalculateAverage_mattiz.java
blob: 52c31ba395a7b39e7bd0bc3881697e9e70057199 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 *  Copyright 2023 The original authors
 *
 *  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.
 */
package dev.morling.onebrc;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
import static java.nio.channels.FileChannel.MapMode.READ_ONLY;

public class CalculateAverage_mattiz {
    private static final int TWO_BYTE_TO_INT = 480 + 48; // 48 is the ASCII code for '0'
    private static final int THREE_BYTE_TO_INT = 4800 + 480 + 48;
    private static final String FILE = "./measurements.txt";
    public static final int PARTS = 8;

    public static void main(String[] args) throws Exception {
        var result = new CalculateAverage_mattiz().calculate(FILE, PARTS);
        System.out.println(result);
    }

    StationList calculate(String file, int numParts) throws Exception {
        var buffers = createBuffers(Paths.get(file), numParts);

        return buffers
                .parallelStream()
                .map(this::aggregate)
                .reduce(StationList::merge)
                .orElseThrow();
    }

    record BufferAndSize(ByteBuffer buffer, long size) {
    }

    List<ByteBuffer> createBuffers(Path file, int numParts) throws IOException {
        FileChannel fileChannel = FileChannel.open(file, StandardOpenOption.READ);

        var fileSize = fileChannel.size();

        if (fileSize < (1024 * 1024)) { // Only one core for small files
            numParts = 1;
        }

        var chunkSize = fileSize / numParts;
        var buffers = new ArrayList<ByteBuffer>();
        long filePointer = 0;

        for (int i = 0; i < numParts; i++) {
            if (i != numParts - 1) { // not last element
                var adjustedChunkSize = getBuffer(fileChannel, filePointer, chunkSize, true);
                buffers.add(adjustedChunkSize.buffer());
                filePointer += adjustedChunkSize.size();
            }
            else {
                var adjustedChunkSize = getBuffer(fileChannel, filePointer, fileSize - filePointer, false);
                buffers.add(adjustedChunkSize.buffer());
            }
        }

        return buffers;
    }

    BufferAndSize getBuffer(FileChannel fileChannel, long start, long size, boolean adjust) throws IOException {
        MappedByteBuffer buffer = fileChannel.map(READ_ONLY, start, size);

        var actualSize = ((int) size);

        if (adjust) {
            while (buffer.get(actualSize - 1) != '\n') {
                actualSize--;
            }
        }

        buffer.limit(actualSize);

        return new BufferAndSize(buffer, actualSize);
    }

    private StationList aggregate(ByteBuffer buffer) {
        var measurements = new StationList();

        while (buffer.hasRemaining()) {
            int startPos = buffer.position();

            byte b;
            int hash = 0;
            while ((b = buffer.get()) != ';') {
                hash = ((hash << 5) - hash) + b;
            }

            if (hash < 0) {
                hash = -hash;
            }

            int length = buffer.position() - startPos - 1;
            byte[] station = new byte[length];
            buffer.get(startPos, station);

            int value = readValue(buffer);

            measurements.update(station, length, hash, value);
        }

        return measurements;
    }

    /*
     * Read decimal number from ascii characters (copied from arjenw)
     *
     * Example:
     * If you have the decimal number 1.4,
     * then byte 1 contain 49 (ascii code for '1')
     * and byte 3 contain 52 (ascii code for '4')
     * Subtract 480 + 48 (48 is the ASCII code for '0')
     * to move number from ascii number to int
     *
     * 49 * 10 + 52 - 528 = 14
     */
    private static int readValue(ByteBuffer buffer) {
        int value;
        byte b1 = buffer.get();
        byte b2 = buffer.get();
        byte b3 = buffer.get();
        byte b4 = buffer.get();

        if (b2 == '.') {// value is n.n
            value = (b1 * 10 + b3 - TWO_BYTE_TO_INT);
        }
        else {
            if (b4 == '.') { // value is -nn.n
                value = -(b2 * 100 + b3 * 10 + buffer.get() - THREE_BYTE_TO_INT);
            }
            else if (b1 == '-') { // value is -n.n
                value = -(b2 * 10 + b4 - TWO_BYTE_TO_INT);
            }
            else { // value is nn.n
                value = (b1 * 100 + b2 * 10 + b4 - THREE_BYTE_TO_INT);
            }
            buffer.get(); // new line
        }
        return value;
    }
}

class CustomMap {
    private static final int SIZE = 1024 * 64;
    private final Station[] stationList = new Station[SIZE];

    public void addOrUpdate(byte[] stationName, int length, int hash, int value) {
        int slot = hash & (SIZE - 1);
        var station = stationList[slot];

        while (station != null
                && station.getHash() != hash
                && !Arrays.equals(
                        station.getName(), 0, station.getName().length,
                        stationName, 0, length)) {

            slot = (slot + 1) & (SIZE - 1);
            station = stationList[slot];
        }

        if (station == null) {
            stationList[slot] = new Station(stationName, hash);
        }

        stationList[slot].add(value);
    }

    public Station get(byte[] stationName) {
        return stationList[findSlot(stationName)];
    }

    public void put(byte[] stationName, Station newStation) {
        stationList[findSlot(stationName)] = newStation;
    }

    private int findSlot(byte[] stationName) {
        int hash = getHash(stationName);
        int slot = hash & (SIZE - 1);
        var station = stationList[slot];

        while (station != null
                && station.getHash() != hash
                && !Arrays.equals(station.getName(), stationName)) {

            slot = (slot + 1) & (SIZE - 1);
            station = stationList[slot];
        }

        return slot;
    }

    private int getHash(byte[] key) {
        int hash = 0;

        for (byte b : key) {
            hash = hash * 31 + b;
        }

        if (hash < 0) {
            hash = -hash;
        }

        return hash;
    }

    public Set<Map.Entry<byte[], Station>> entrySet() {
        var sorted = new HashMap<byte[], Station>();

        for (var s : stationList) {
            if (s != null) {
                sorted.put(s.getName(), s);
            }
        }

        return sorted.entrySet();
    }

    public Map<String, Station> sorted() {
        var sorted = new TreeMap<String, Station>();

        for (var s : stationList) {
            if (s != null) {
                sorted.put(new String(s.getName(), StandardCharsets.UTF_8), s);
            }
        }

        return sorted;
    }
}

class StationList {
    private final CustomMap stations = new CustomMap();

    public void update(byte[] stationName, int length, int hash, int value) {
        stations.addOrUpdate(stationName, length, hash, value);
    }

    public StationList merge(StationList other) {
        for (var aggregator : other.stations.entrySet()) {
            var agg = stations.get(aggregator.getKey());

            if (agg == null) {
                stations.put(aggregator.getKey(), aggregator.getValue());
            }
            else {
                agg.merge(aggregator.getValue());
            }
        }

        return this;
    }

    @Override
    public String toString() {
        return stations.sorted().toString();
    }
}

class Station {
    private final byte[] name;
    private final int hash;
    private int min = Integer.MAX_VALUE;
    private int max = Integer.MIN_VALUE;
    private int sum;
    private int count;

    public Station(byte[] name, int hash) {
        this.name = name;
        this.hash = hash;
    }

    public void add(int max, int min, int sum, int count) {
        this.max = Math.max(this.max, max);
        this.min = Math.min(this.min, min);
        this.sum += sum;
        this.count += count;
    }

    public void add(int value) {
        this.max = Math.max(this.max, value);
        this.min = Math.min(this.min, value);
        this.sum += value;
        this.count++;
    }

    public void merge(Station other) {
        this.max = Math.max(this.max, other.max);
        this.min = Math.min(this.min, other.min);
        this.sum += other.sum;
        this.count += other.count;
    }

    public String toString() {
        return (min / 10.0) + "/" + (Math.round(((double) sum) / count)) / 10.0 + "/" + (max / 10.0);
    }

    public byte[] getName() {
        return name;
    }

    public int getHash() {
        return hash;
    }
}