Skip to content

Commit 1f5e47d

Browse files
committed
Add debug_record() Fix function.
1 parent b72c4e2 commit 1f5e47d

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,37 @@ Copies (or appends to) a field from an existing field.
242242
copy_field("<sourceField>", "<targetField>")
243243
```
244244

245+
##### `debug_record`
246+
247+
Prints the current record either to standard output or to a file.
248+
249+
Parameters:
250+
251+
- `prefix` (optional): Prefix to print before the record. (Default: Empty string)
252+
253+
Options:
254+
255+
- `compression` (file output only): Compression mode. (Default: `auto`)
256+
- `destination`: Destination to write the record to; may include [format directives](https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax) for counter and record ID (in that order). (Default: `stdout`)
257+
- `encoding` (file output only): Encoding used by the underlying writer. (Default: `UTF-8`)
258+
- `footer`: Footer which is output after the record. (Default: `\n`)
259+
- `header`: Header which is output before the record. (Default: Empty string)
260+
- `id`: Field name which contains the record ID; if found, will be included before the prefix. (Default: `_id`)
261+
- `json`: Whether to encode the record as JSON. (Default: `false`)
262+
- `pretty`: Whether to use pretty printing. (Default: `false`)
263+
264+
```perl
265+
debug_record(["<prefix>"][, <options>...])
266+
```
267+
268+
E.g.:
269+
270+
```perl
271+
debug_record("Before transformation")
272+
debug_record(destination: "record-%03d.gz", header: "After transformation: ")
273+
debug_record(destination: "record-%2$s.json", id: "001", json: "true", pretty: "true")
274+
```
275+
245276
##### `format`
246277

247278
Replaces the value with a formatted (`sprintf`-like) version.
@@ -369,8 +400,8 @@ Creates (or replaces) a field with the current timestamp.
369400

370401
Options:
371402

372-
- `format`: Date and time pattern as in [java.text.SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html). (Default: Unix timestamp)
373-
- `timezone`: Time zone as in [java.util.TimeZone](https://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html). (Default: UTC)
403+
- `format`: Date and time pattern as in [java.text.SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html). (Default: `timestamp`)
404+
- `timezone`: Time zone as in [java.util.TimeZone](https://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html). (Default: `UTC`)
374405
- `language`: Language tag as in [java.util.Locale](https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html). (Default: The locale of the host system)
375406

376407
```perl

metafix/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ dependencies {
2626
runtimeOnly "org.slf4j:slf4j-simple:${versions.slf4j}"
2727

2828
implementation "org.metafacture:metafacture-commons:${versions.metafacture}"
29-
implementation "org.metafacture:metafacture-mangling:${versions.metafacture}"
30-
implementation "org.metafacture:metafacture-javaintegration:${versions.metafacture}"
3129
implementation "org.metafacture:metafacture-flowcontrol:${versions.metafacture}"
30+
implementation "org.metafacture:metafacture-framework:${versions.metafacture}"
31+
implementation "org.metafacture:metafacture-io:${versions.metafacture}"
32+
implementation "org.metafacture:metafacture-javaintegration:${versions.metafacture}"
33+
implementation "org.metafacture:metafacture-mangling:${versions.metafacture}"
3234
implementation "org.metafacture:metamorph:${versions.metafacture}"
3335

3436
testImplementation "nl.jqno.equalsverifier:equalsverifier:${versions.equalsverifier}"

metafix/src/main/java/org/metafacture/metafix/FixMethod.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@
1616

1717
package org.metafacture.metafix;
1818

19+
import org.metafacture.framework.StandardEventNames;
20+
import org.metafacture.io.ObjectWriter;
1921
import org.metafacture.metafix.api.FixFunction;
2022
import org.metafacture.metamorph.api.Maps;
2123
import org.metafacture.metamorph.functions.ISBN;
2224
import org.metafacture.metamorph.functions.Timestamp;
2325
import org.metafacture.metamorph.maps.FileMap;
2426

2527
import java.io.File;
28+
import java.io.IOException;
2629
import java.util.Arrays;
2730
import java.util.Collections;
2831
import java.util.Comparator;
32+
import java.util.HashMap;
2933
import java.util.List;
3034
import java.util.Map;
3135
import java.util.Random;
36+
import java.util.concurrent.atomic.LongAdder;
3237
import java.util.function.Function;
3338
import java.util.function.Predicate;
3439
import java.util.function.UnaryOperator;
@@ -126,6 +131,53 @@ public void apply(final Metafix metafix, final Record record, final List<String>
126131
}));
127132
}
128133
},
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+
},
129181
format {
130182
@Override
131183
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {

0 commit comments

Comments
 (0)