package de.jeha.kame.crawler.service.ds;

import com.codahale.metrics.health.HealthCheck;
import de.jeha.kame.crawler.core.types.CrawlResult;
import de.jeha.kame.crawler.service.ds.util.FileNameUtils;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.time.ZonedDateTime;
import java.util.zip.GZIPOutputStream;

/**
 * @author jenshadlich@googlemail.com
 */
public class FileDocumentStore implements DocumentStore {

    private static final Logger LOG = LoggerFactory.getLogger(FileDocumentStore.class);

    private final String path;

    public FileDocumentStore(String path) {
        this.path = path;

        try {
            File root = new File(path);
            if (!root.exists()) {
                FileUtils.forceMkdir(root);
            }
        } catch (IOException e) {
            LOG.error("Could not create root directory", e);
        }
    }

    @Override
    public void save(String crawlId, ZonedDateTime timestamp, CrawlResult result) throws IOException {
        final String filename = String.format("%s-%d-%s.gz", crawlId, timestamp.toEpochSecond(), FileNameUtils.stripToFilename(result.getUrl()));

        try (FileOutputStream output = new FileOutputStream(path + "/" + filename)) {
            try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(output), "UTF-8")) {
                writer.write(result.getContent());
                LOG.debug("file '{}' written", filename);
            }
        }
    }

    @Override
    public HealthCheck.Result checkHealth() {
        File root = new File(path);
        return root.exists()
                ? HealthCheck.Result.healthy()
                : HealthCheck.Result.unhealthy("root path does not exist");
    }

}

@@ -226,32 +226,83 @@
 s.io.FileUtils;%0A
+import org.apache.commons.lang3.StringEscapeUtils;%0A
 import org.slf4j
@@ -1134,16 +1134,22 @@
 filename
+Prefix
  = Strin
@@ -1161,31 +1161,77 @@
 mat(
-%22%25s-%25d-%25s.gz%22, crawlId,
+%0A                %22%25s-%25d-%25s%22,%0A                crawlId,%0A               
  tim
@@ -1253,16 +1253,32 @@
 econd(),
+%0A               
  FileNam
@@ -1316,20 +1316,301 @@
 etUrl())
+%0A        
 );%0A%0A
+        saveDocument(filenamePrefix, result);%0A        saveMetaRecord(filenamePrefix, result);%0A    %7D%0A%0A    private void saveDocument(String filenamePrefix, CrawlResult result) throws IOException %7B%0A        final String documentFilename = path + %22/%22 + filenamePrefix + %22.gz%22;%0A
         
@@ -1661,30 +1661,25 @@
 tStream(
-path + %22/%22 + f
+documentF
 ilename)
@@ -1882,42 +1882,478 @@
 n%22, 
-filename);%0A            %7D%0A        %7D
+documentFilename);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void saveMetaRecord(String filenamePrefix, CrawlResult result) throws IOException %7B%0A        final String metaFilename = path + %22/%22 + filenamePrefix + %22.meta.json%22;%0A        final String metaRecord = String.format(%22%7B%5Cn  url: %25s%5Cn%7D%5Cn%22, StringEscapeUtils.escapeJson(result.getUrl()));%0A%0A        FileUtils.writeStringToFile(new File(metaFilename), metaRecord);%0A        LOG.debug(%22file '%7B%7D' written%22, metaFilename);
 %0A   

store meta record