|
1 | 1 | package de.komoot.photon;
|
2 | 2 |
|
3 |
| -import org.apache.commons.lang3.NotImplementedException; |
| 3 | +import com.fasterxml.jackson.core.JsonEncoding; |
| 4 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 5 | +import com.fasterxml.jackson.core.Version; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 8 | +import de.komoot.photon.opensearch.PhotonDocSerializer; |
| 9 | +import org.slf4j.Logger; |
4 | 10 |
|
5 |
| -import java.io.FileNotFoundException; |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
6 | 13 |
|
7 | 14 | public class JsonDumper implements Importer {
|
| 15 | + private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(JsonDumper.class); |
8 | 16 |
|
9 |
| - public JsonDumper(String filename, String[] languages, String[] extraTags) throws FileNotFoundException { |
10 |
| - throw new NotImplementedException(); |
| 17 | + final JsonGenerator generator; |
| 18 | + |
| 19 | + public JsonDumper(String filename, String[] languages, String[] extraTags) throws IOException { |
| 20 | + final var module = new SimpleModule("PhotonDocSerializer", |
| 21 | + new Version(1, 0, 0, null, null, null)); |
| 22 | + module.addSerializer(PhotonDoc.class, new PhotonDocSerializer(languages, extraTags)); |
| 23 | + |
| 24 | + final ObjectMapper mapper = new ObjectMapper(); |
| 25 | + mapper.registerModule(module); |
| 26 | + |
| 27 | + if ("-".equals(filename)) { |
| 28 | + generator = mapper.getFactory().createGenerator(System.out, JsonEncoding.UTF8); |
| 29 | + } else { |
| 30 | + generator = mapper.getFactory().createGenerator(new File(filename), JsonEncoding.UTF8); |
| 31 | + } |
| 32 | + generator.writeStartObject(); |
| 33 | + generator.writeObjectField("id", "Photon Dump Header"); |
| 34 | + generator.writeObjectField("version", PhotonDocSerializer.FORMAT_VERSION); |
| 35 | + generator.writeEndObject(); |
11 | 36 | }
|
12 | 37 |
|
13 | 38 | @Override
|
14 | 39 | public void add(PhotonDoc doc, int objectId) {
|
15 |
| - throw new NotImplementedException(); |
| 40 | + try { |
| 41 | + generator.writeStartObject(); |
| 42 | + generator.writeObjectField("id", doc.getUid(objectId)); |
| 43 | + generator.writeObjectField("document", doc); |
| 44 | + generator.writeEndObject(); |
| 45 | + } catch (IOException e) { |
| 46 | + LOGGER.error("Error writing json file", e); |
| 47 | + } |
| 48 | + |
16 | 49 | }
|
17 | 50 |
|
18 | 51 | @Override
|
19 | 52 | public void finish() {
|
20 |
| - throw new NotImplementedException(); |
| 53 | + try { |
| 54 | + generator.close(); |
| 55 | + } catch (IOException e) { |
| 56 | + LOGGER.warn("Error while closing output file", e); |
| 57 | + } |
21 | 58 | }
|
22 | 59 | }
|
0 commit comments