package org.mitallast.queue.raft.log;

import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import org.mitallast.queue.common.component.AbstractComponent;
import org.mitallast.queue.common.settings.Settings;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SegmentFileService extends AbstractComponent {

    private final static Pattern descriptorPattern = Pattern.compile("^log-(\\d+)-(\\d+)\\.[a-z]+");
    private final File directory;

    @Inject
    public SegmentFileService(Settings settings) {
        super(settings);

        File workDir = new File(this.settings.get("work_dir", "data"));
        directory = new File(workDir, componentSettings.get("log_dir", "log"));
    }

    public ImmutableList<SegmentFile> listDescriptorFiles() {
        return listFiles(".descriptor");
    }

    public ImmutableList<SegmentFile> listFiles(String extension) {
        ImmutableList.Builder<SegmentFile> builder = ImmutableList.builder();
        for (File file : directory.listFiles(File::isFile)) {
            if (file.getName().endsWith(extension)) {
                Matcher matcher = descriptorPattern.matcher(file.getName());
                if (matcher.matches()) {
                    long id = Long.parseLong(matcher.group(1), 10);
                    long version = Long.parseLong(matcher.group(2), 10);
                    builder.add(new SegmentFile(file, id, version));
                }
            }
        }
        return builder.build();
    }

    public File createSegmentFile(SegmentDescriptor descriptor) {
        return new File(directory, String.format("log-%d-%d.log", descriptor.id(), descriptor.version()));
    }

    public File createIndexFile(SegmentDescriptor descriptor) {
        return new File(directory, String.format("log-%d-%d.index", descriptor.id(), descriptor.version()));
    }

    public File createDescriptorFile(SegmentDescriptor descriptor) {
        return new File(directory, String.format("log-%d-%d.info", descriptor.id(), descriptor.version()));
    }
}

@@ -251,16 +251,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -311,16 +311,16 @@
 atcher;%0A
-
 import j
@@ -601,16 +601,35 @@
 ettings)
+ throws IOException
  %7B%0A     
@@ -717,24 +717,24 @@
 , %22data%22));%0A
-
         dire
@@ -797,24 +797,200 @@
 %22, %22log%22));%0A
+        if (!directory.exists()) %7B%0A            if (!directory.mkdir()) %7B%0A                throw new IOException(%22Error create directory: %22 + directory);%0A            %7D%0A        %7D%0A
     %7D%0A%0A    p

fix segment file service