aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/morling/onebrc/CalculateAverage_jgrateron.java
blob: 251f26d7537806b5f8b48ab04364605014561d41 (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
/*
 *  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.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Queue;
import java.util.Map.Entry;
import java.util.stream.Collectors;

public class CalculateAverage_jgrateron {
    private static final String FILE = "./measurements.txt";
    private static final int MAX_LENGTH_LINE = 115;
    private static final int MAX_BUFFER = 1024 * 8;
    private static boolean DEBUG = false;

    public record Particion(long offset, long size) {
    }

    public record Tupla(String str, double num) {
    }

    /*
     * 
     */
    public static List<Particion> dividirArchivo(File archivo) throws IOException {
        var particiones = new ArrayList<Particion>();
        var buffer = new byte[MAX_LENGTH_LINE];
        var length = archivo.length();
        int cores = Runtime.getRuntime().availableProcessors();
        var sizeParticion = length / cores;
        if (sizeParticion > MAX_BUFFER) {
            var ini = 0l;
            try (var rfile = new RandomAccessFile(archivo, "r")) {
                for (;;) {
                    var size = sizeParticion;
                    var pos = ini + size;
                    if (pos > length) {
                        pos = length - 1;
                        size = length - ini;
                    }
                    rfile.seek(pos);
                    int count = rfile.read(buffer);
                    if (count == -1) {
                        break;
                    }
                    for (int i = 0; i < count; i++) {
                        size++;
                        if (buffer[i] == '\n' || buffer[i] == '\r') {
                            break;
                        }
                    }
                    var particion = new Particion(ini, size);
                    particiones.add(particion);
                    if (count != buffer.length) {
                        break;
                    }
                    ini += size;
                }
            }
        }
        else {
            particiones.add(new Particion(0, length));
        }
        return particiones;
    }

    public static void main(String[] args) throws InterruptedException, IOException {
        Locale.setDefault(Locale.US);
        var startTime = System.nanoTime();
        var archivo = new File(FILE);
        var totalMediciones = new HashMap<Integer, Medicion>();
        var tareas = new ArrayList<Thread>();
        var particiones = dividirArchivo(archivo);

        for (var p : particiones) {
            var hilo = Thread.ofVirtual().start(() -> {
                var mediciones = new HashMap<Integer, Medicion>();
                try (var miArchivo = new MiArchivo(archivo)) {
                    miArchivo.seek(p);
                    for (;;) {
                        var tuples = miArchivo.readTuples();
                        if (tuples.isEmpty()) {
                            break;
                        }
                        for (;;) {
                            var tuple = tuples.poll();
                            if (tuple == null) {
                                break;
                            }
                            var estacion = tuple.str;
                            var temp = tuple.num;
                            var hashCode = estacion.hashCode();
                            var medicion = mediciones.get(hashCode);
                            if (medicion == null) {
                                medicion = new Medicion(estacion, 1, temp, temp, temp);
                                mediciones.put(hashCode, medicion);
                            }
                            else {
                                medicion.update(1, temp, temp, temp);
                            }
                        }
                    }
                }
                catch (IOException e) {
                    System.exit(-1);
                }
                synchronized (totalMediciones) {
                    for (var entry : mediciones.entrySet()) {
                        var medicion = totalMediciones.get(entry.getKey());
                        if (medicion == null) {
                            totalMediciones.put(entry.getKey(), entry.getValue());
                        }
                        else {
                            var otraMed = entry.getValue();
                            medicion.update(otraMed.count, otraMed.tempMin, otraMed.tempMax, otraMed.tempSum);
                        }
                    }
                }
            });
            tareas.add(hilo);
        }
        for (var hilo : tareas) {
            hilo.join();
        }

        Comparator<Entry<Integer, Medicion>> comparar = (a, b) -> {
            return a.getValue().estacion.compareTo(b.getValue().estacion);
        };

        var result = totalMediciones.entrySet().stream()//
                .sorted(comparar)//
                .map(e -> e.getValue().toString())//
                .collect(Collectors.joining(", "));

        System.out.println("{" + result + "}");
        if (DEBUG) {
            System.out.println("Total: " + (System.nanoTime() - startTime) / 1000000 + "ms");
        }

    }

    /*
     * 
     */
    public static double strToDouble(byte linea[], int posSeparator, int len) {
        double number[] = { 0, 0 };
        int pos = 0;
        boolean esNegativo = false;
        for (int i = posSeparator + 1; i < len; i++) {
            switch (linea[i]) {
                case '0', '1', '2', '3', '4':
                case '5', '6', '7', '8', '9':
                    number[pos] = number[pos] * 10;
                    number[pos] = number[pos] + (linea[i] - 48);
                    break;
                case '-':
                    esNegativo = true;
                    break;
                case '.':
                    pos = 1;
                    break;
            }
        }
        double num = number[0];
        if (number[1] > 0) {
            num += (number[1] / 10);
        }
        if (esNegativo) {
            num = num * -1;
        }
        return num;
    }

    /*
     * 
     */
    static class MiArchivo implements AutoCloseable {
        private final RandomAccessFile rFile;
        private final byte buffer[] = new byte[MAX_BUFFER];
        private final byte line[] = new byte[MAX_LENGTH_LINE];
        private final byte rest[] = new byte[MAX_LENGTH_LINE];
        private int lenRest = 0;
        private long maxRead = 0;
        private long totalRead = 0;
        private Queue<Tupla> tuples = new LinkedList<Tupla>();

        public MiArchivo(File file) throws IOException {
            rFile = new RandomAccessFile(file, "r");
        }

        public void seek(Particion particion) throws IOException {
            maxRead = particion.size;
            rFile.seek(particion.offset);
        }

        @Override
        public void close() throws IOException {
            rFile.close();
        }

        public Queue<Tupla> readTuples() throws IOException {
            if (totalRead == maxRead) {
                return tuples;
            }
            long numBytes = rFile.read(buffer);
            if (numBytes == -1) {
                return tuples;
            }
            var totalLeidos = totalRead + numBytes;
            if (totalLeidos > maxRead) {
                numBytes = maxRead - totalRead;
            }
            totalRead += numBytes;
            int pos = 0;
            int len = 0;
            int idx = 0;
            while (pos < numBytes) {
                if (buffer[pos] == '\n' || buffer[pos] == '\r') {
                    if (lenRest > 0) {
                        System.arraycopy(rest, 0, line, 0, lenRest);
                        System.arraycopy(buffer, idx, line, lenRest, len);
                        len += lenRest;
                        lenRest = 0;
                    }
                    else {
                        System.arraycopy(buffer, idx, line, 0, len);
                    }
                    int semicolon = 0;
                    for (int i = 0; i < len; i++) {
                        if (line[i] == ';') {
                            semicolon = i;
                            break;
                        }
                    }
                    var temperatura = strToDouble(line, semicolon, len);
                    var tupla = new Tupla(new String(line, 0, semicolon), temperatura);
                    tuples.add(tupla);
                    idx = pos + 1;
                    len = 0;
                }
                else {
                    len++;
                }
                pos++;
            }
            if (len > 0) {
                System.arraycopy(buffer, idx, rest, 0, len);
                lenRest = len;
            }
            return tuples;
        }
    }

    /*
     * 
     */
    static class Medicion {
        private String estacion;
        private int count;
        private double tempMin;
        private double tempMax;
        private double tempSum;

        public Medicion(String estacion, int count, double tempMin, double tempMax, double tempSum) {
            super();
            this.estacion = estacion;
            this.count = count;
            this.tempMin = tempMin;
            this.tempMax = tempMax;
            this.tempSum = tempSum;
        }

        public void update(int count, double tempMin, double tempMax, double tempSum) {
            this.count += count;
            if (tempMin < this.tempMin) {
                this.tempMin = tempMin;
            }
            if (tempMax > this.tempMax) {
                this.tempMax = tempMax;
            }
            this.tempSum += tempSum;
        }

        @Override
        public String toString() {
            double tempPro = tempSum / count;
            return "%s=%.1f/%.1f/%.1f".formatted(estacion, tempMin, tempPro, tempMax);
        }
    }
}