aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/morling/onebrc/CalculateAverage_C5H12O5.java
blob: 0764b65e3815ffaa4c114d42636b09f9dddd7129 (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
/*
 *  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.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Calculates the average using AIO and multiple threads.
 *
 * @author Xylitol
 */
public class CalculateAverage_C5H12O5 {
    private static final int BUFFER_CAPACITY = 1024 * 1024;
    private static final int MAP_CAPACITY = 10000;
    private static final int QUEUE_CAPACITY = 2;

    public static void main(String[] args) throws Exception {
        // Files.list(Paths.get("./src/test/resources/samples"))
        // .filter(file -> file.toString().endsWith(".txt"))
        // .forEach(file -> {
        // try {
        // String actual = calc(file);
        // String expected = Files.readAllLines(Paths.get(file.toString().replace(".txt", ".out"))).get(0);
        // System.out.println(file.getFileName() + ": " + expected.equals(actual));
        // } catch (Exception e) {
        // System.out.println(file.getFileName() + ": " + false);
        // e.printStackTrace();
        // }
        // });
        // long start = System.currentTimeMillis();
        System.out.println(calc(Paths.get("./measurements.txt")));
        // System.out.println("Time: " + (System.currentTimeMillis() - start) + "ms");
    }

    /**
     * Calculate the average.
     */
    public static String calc(Path file) throws IOException, ExecutionException, InterruptedException {
        long[] positions = fragment(file, Runtime.getRuntime().availableProcessors());
        FutureTask<Map<MeasurementName, MeasurementData>>[] tasks = new FutureTask[positions.length];
        for (int i = 0; i < positions.length; i++) {
            tasks[i] = new FutureTask<>(new Task(file, (i == 0 ? 0 : positions[i - 1] + 1), positions[i]));
            new Thread(tasks[i]).start();
        }
        Map<String, MeasurementData> result = HashMap.newHashMap(MAP_CAPACITY);
        for (FutureTask<Map<MeasurementName, MeasurementData>> task : tasks) {
            task.get().forEach((k, v) -> result.merge(k.toString(), v, MeasurementData::merge));
        }
        return new TreeMap<>(result).toString();
    }

    /**
     * Fragment the file into chunks.
     */
    private static long[] fragment(Path filePath, int chunkNum) throws IOException {
        long fileSize = Files.size(filePath);
        long chunkSize = fileSize / chunkNum;
        long[] positions = new long[chunkNum];
        try (RandomAccessFile file = new RandomAccessFile(filePath.toFile(), "r")) {
            long position = chunkSize;
            for (int i = 0; i < chunkNum - 1; i++) {
                if (position >= fileSize) {
                    break;
                }
                file.seek(position);
                while (file.read() != '\n') {
                    position++;
                }
                positions[i] = position;
                position += chunkSize;
            }
        }
        positions[chunkNum - 1] = fileSize;
        return Arrays.stream(positions).filter(value -> value != 0).toArray();
    }

    /**
     * The measurement name.
     */
    private record MeasurementName(byte[] bytes) {

        @Override
        public boolean equals(Object other) {
            if (!(other instanceof MeasurementName)) {
                return false;
            }
            return Arrays.equals(bytes, ((MeasurementName) other).bytes);
        }

        @Override
        public int hashCode() {
            return Arrays.hashCode(bytes);
        }

        @Override
        public String toString() {
            return new String(bytes, StandardCharsets.UTF_8);
        }
    }

    /**
     * The measurement data.
     */
    private static class MeasurementData {
        private int min;
        private int max;
        private int sum;
        private int count;

        public MeasurementData(int value) {
            this.min = value;
            this.max = value;
            this.sum = value;
            this.count = 1;
        }

        public MeasurementData merge(MeasurementData data) {
            return merge(data.min, data.max, data.sum, data.count);
        }

        public MeasurementData merge(int min, int max, int sum, int count) {
            this.min = Math.min(this.min, min);
            this.max = Math.max(this.max, max);
            this.sum += sum;
            this.count += count;
            return this;
        }

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

    /**
     * The task to read and calculate.
     */
    private static class Task implements Callable<Map<MeasurementName, MeasurementData>> {
        private final Path file;
        private long readPosition;
        private long calcPosition;
        private final long limitSize;
        private final BlockingQueue<byte[]> bytesQueue = new LinkedBlockingQueue<>(QUEUE_CAPACITY);

        public Task(Path file, long position, long limitSize) {
            this.file = file;
            this.readPosition = position;
            this.calcPosition = position;
            this.limitSize = limitSize;
        }

        @Override
        public Map<MeasurementName, MeasurementData> call() throws IOException {
            // read and offer to queue
            AsynchronousFileChannel channel = AsynchronousFileChannel.open(
                    file, Set.of(StandardOpenOption.READ), Executors.newVirtualThreadPerTaskExecutor());
            ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_CAPACITY);
            channel.read(buffer, readPosition, buffer, new CompletionHandler<>() {
                @Override
                public void completed(Integer bytesRead, ByteBuffer buffer) {
                    if (bytesRead > 0 && readPosition < limitSize) {
                        try {
                            buffer.flip();
                            byte[] bytes = new byte[buffer.remaining()];
                            buffer.get(bytes);
                            readPosition += bytesRead;
                            if (readPosition > limitSize) {
                                int diff = (int) (readPosition - limitSize);
                                byte[] newBytes = new byte[bytes.length - diff];
                                System.arraycopy(bytes, 0, newBytes, 0, newBytes.length);
                                bytesQueue.put(newBytes);
                            }
                            else {
                                bytesQueue.put(bytes);
                                buffer.clear();
                                channel.read(buffer, readPosition, buffer, this);
                            }
                        }
                        catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                }

                @Override
                public void failed(Throwable exc, ByteBuffer buffer) {
                    // ignore
                }
            });

            // poll from queue and calculate
            Map<MeasurementName, MeasurementData> result = HashMap.newHashMap(MAP_CAPACITY);
            byte[] readBytes = null;
            byte[] remaining = null;
            while (calcPosition < limitSize) {
                readBytes = bytesQueue.poll();
                if (readBytes != null) {
                    List<byte[]> lines = split(readBytes, (byte) '\n');
                    for (int i = 0; i < lines.size(); i++) {
                        byte[] lineBytes = lines.get(i);
                        if (i == 0 && remaining != null) {
                            byte[] newBytes = new byte[remaining.length + lineBytes.length];
                            System.arraycopy(remaining, 0, newBytes, 0, remaining.length);
                            System.arraycopy(lineBytes, 0, newBytes, remaining.length, lineBytes.length);
                            lineBytes = newBytes;
                        }
                        if (i == lines.size() - 1) {
                            remaining = lineBytes;
                            break;
                        }
                        agg(result, lineBytes);
                    }
                    calcPosition += readBytes.length;
                }
            }
            if (remaining != null && remaining.length > 0) {
                agg(result, remaining);
            }
            channel.close();
            return result;
        }

        /**
         * Aggregate the measurement data.
         */
        private static void agg(Map<MeasurementName, MeasurementData> result, byte[] bytes) {
            List<byte[]> parts = split(bytes, (byte) ';');
            MeasurementName station = new MeasurementName(parts.getFirst());
            int value = toInt(parts.getLast());
            MeasurementData data = result.get(station);
            if (data != null) {
                data.merge(value, value, value, 1);
            }
            else {
                result.put(station, new MeasurementData(value));
            }
        }

        /**
         * Convert the byte array to int.
         */
        private static int toInt(byte[] bytes) {
            boolean negative = false;
            int result = 0;
            for (byte b : bytes) {
                if (b == '-') {
                    negative = true;
                    continue;
                }
                if (b != '.') {
                    result = result * 10 + (b - '0');
                }
            }
            return negative ? -result : result;
        }

        /**
         * Split the byte array by given byte.
         */
        private static List<byte[]> split(byte[] bytes, byte separator) {
            List<byte[]> result = new ArrayList<>();
            int start = 0;
            for (int end = 0; end < bytes.length; end++) {
                if (bytes[end] == separator) {
                    byte[] newBytes = new byte[end - start];
                    System.arraycopy(bytes, start, newBytes, 0, newBytes.length);
                    result.add(newBytes);
                    start = end + 1;
                }
            }
            if (start <= bytes.length) {
                byte[] newBytes = new byte[bytes.length - start];
                System.arraycopy(bytes, start, newBytes, 0, newBytes.length);
                result.add(newBytes);
            }
            return result;
        }
    }
}