|
16 | 16 |
|
17 | 17 | package org.metafacture.metafix;
|
18 | 18 |
|
| 19 | +import org.metafacture.framework.StandardEventNames; |
| 20 | +import org.metafacture.io.ObjectWriter; |
19 | 21 | import org.metafacture.metafix.api.FixFunction;
|
20 | 22 | import org.metafacture.metamorph.api.Maps;
|
21 | 23 | import org.metafacture.metamorph.functions.ISBN;
|
22 | 24 | import org.metafacture.metamorph.functions.Timestamp;
|
23 | 25 | import org.metafacture.metamorph.maps.FileMap;
|
24 | 26 |
|
25 | 27 | import java.io.File;
|
| 28 | +import java.io.IOException; |
26 | 29 | import java.util.Arrays;
|
27 | 30 | import java.util.Collections;
|
28 | 31 | import java.util.Comparator;
|
| 32 | +import java.util.HashMap; |
29 | 33 | import java.util.List;
|
30 | 34 | import java.util.Map;
|
31 | 35 | import java.util.Random;
|
| 36 | +import java.util.concurrent.atomic.LongAdder; |
32 | 37 | import java.util.function.Function;
|
33 | 38 | import java.util.function.Predicate;
|
34 | 39 | import java.util.function.UnaryOperator;
|
@@ -126,6 +131,53 @@ public void apply(final Metafix metafix, final Record record, final List<String>
|
126 | 131 | }));
|
127 | 132 | }
|
128 | 133 | },
|
| 134 | + debug_record { |
| 135 | + private final Map<Metafix, LongAdder> scopedCounter = new HashMap<>(); |
| 136 | + |
| 137 | + @Override |
| 138 | + public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) { |
| 139 | + final String destination = options.getOrDefault("destination", ObjectWriter.STDOUT); |
| 140 | + final Value idValue = record.get(options.getOrDefault("id", StandardEventNames.ID)); |
| 141 | + |
| 142 | + final boolean json = getBoolean(options, "json"); |
| 143 | + final boolean pretty = getBoolean(options, "pretty"); |
| 144 | + |
| 145 | + final String id = Value.isNull(idValue) ? "" : idValue.toString(); |
| 146 | + final String prefix = (id.isEmpty() ? "" : "[" + id + "] ") + (params.isEmpty() ? "" : params.get(0) + ": "); |
| 147 | + |
| 148 | + final LongAdder counter = scopedCounter.computeIfAbsent(metafix, k -> new LongAdder()); |
| 149 | + counter.increment(); |
| 150 | + |
| 151 | + final ObjectWriter<String> writer = new ObjectWriter<>(String.format(destination, counter.sum(), id)); |
| 152 | + |
| 153 | + withOption(options, "compression", writer::setCompression); |
| 154 | + withOption(options, "encoding", writer::setEncoding); |
| 155 | + withOption(options, "footer", writer::setFooter); |
| 156 | + withOption(options, "header", writer::setHeader); |
| 157 | + |
| 158 | + boolean written = false; |
| 159 | + |
| 160 | + if (json) { |
| 161 | + try { |
| 162 | + writer.process(prefix + record.toJson(pretty)); |
| 163 | + written = true; |
| 164 | + } |
| 165 | + catch (final IOException e) { |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if (!written) { |
| 170 | + if (pretty) { |
| 171 | + record.forEach((f, v) -> writer.process(prefix + f + "=" + v)); |
| 172 | + } |
| 173 | + else { |
| 174 | + writer.process(prefix + record); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + writer.closeStream(); |
| 179 | + } |
| 180 | + }, |
129 | 181 | format {
|
130 | 182 | @Override
|
131 | 183 | public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
|
|
0 commit comments