<commit_before>
/*
 * Copyright (C) 2015 <NAME>. All rights reserved.
 */

package org.jsimpledb.cli.cmd;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Map;

import org.dellroad.stuff.io.AtomicUpdateFileOutputStream;
import org.jsimpledb.SessionMode;
import org.jsimpledb.cli.CliSession;
import org.jsimpledb.kv.util.XMLSerializer;
import org.jsimpledb.parse.Parser;
import org.jsimpledb.util.ParseContext;

public class KVSaveCommand extends AbstractCommand {

    public KVSaveCommand() {
        super("kvsave -i:indent file.xml:file minKey? maxKey? limit?");
    }

    @Override
    public String getHelpSummary() {
        return "Exports key/value pairs to an XML file";
    }

    @Override
    public String getHelpDetail() {
        return "Writes all key/value pairs to the specified XML file. Data can be read back in later via `kvload'."
          + "\n\nIf `minKey', `maxKey', and/or `limit' are specified, the keys are restricted to the specified range"
          + " and/or count limit. `minKey' and `maxKey' may be given as hexadecimal strings or C-style doubly-quoted strings."
          + " The `-i' flag causes the output XML to be indented.";
    }

    @Override
    public EnumSet<SessionMode> getSessionModes() {
        return EnumSet.allOf(SessionMode.class);
    }

    @Override
    protected Parser<?> getParser(String typeName) {
        return "file".equals(typeName) ? new OutputFileParser() : super.getParser(typeName);
    }

    @Override
    public CliSession.Action getAction(CliSession session, ParseContext ctx, boolean complete, Map<String, Object> params) {

        // Parse parameters
        final File file = (File)params.get("file.xml");
        final boolean indent = params.containsKey("indent");
        final byte[] key = (byte[])params.get("key");
        final byte[] maxKey = (byte[])params.get("maxKey");
        final Integer limit = (Integer)params.get("limit");

        // Return action
        return new CliSession.TransactionalAction() {
            @Override
            public void run(CliSession session) throws Exception {
                final FileOutputStream updateOutput = !this.isWindows() ?
                  new AtomicUpdateFileOutputStream(file) : new FileOutputStream(file);
                final BufferedOutputStream output = new BufferedOutputStream(updateOutput);
                boolean success = false;
                final int count;
                try {
                    final XMLSerializer serializer = new XMLSerializer(session.getKVTransaction());
                    count = serializer.write(output, indent);
                    output.flush();
                    success = true;
                } finally {
                    if (success) {
                        try {
                            output.close();
                        } catch (IOException e) {
                            // ignore
                        }
                    } else if (updateOutput instanceof AtomicUpdateFileOutputStream)
                        ((AtomicUpdateFileOutputStream)updateOutput).cancel();
                }
                session.getWriter().println("Wrote " + count + " key/value pairs to `" + file + "'");
            }
<commit_msg>Fix bugs in 'kvsave': implement 'minKey'/'maxKey' and remove 'limit'.<commit_after>
/*
 * Copyright (C) 2015 <NAME>. All rights reserved.
 */

package org.jsimpledb.cli.cmd;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Map;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

import org.dellroad.stuff.io.AtomicUpdateFileOutputStream;
import org.dellroad.stuff.xml.IndentXMLStreamWriter;
import org.jsimpledb.SessionMode;
import org.jsimpledb.cli.CliSession;
import org.jsimpledb.kv.util.XMLSerializer;
import org.jsimpledb.parse.Parser;
import org.jsimpledb.util.ParseContext;

public class KVSaveCommand extends AbstractCommand {

    public KVSaveCommand() {
        super("kvsave -i:indent file.xml:file minKey? maxKey?");
    }

    @Override
    public String getHelpSummary() {
        return "Exports key/value pairs to an XML file";
    }

    @Override
    public String getHelpDetail() {
        return "Writes all key/value pairs to the specified XML file. Data can be read back in later via `kvload'."
          + "\n\nIf `minKey' and/or `maxKey' are specified, the keys are restricted to the specified range."
          + " `minKey' and `maxKey' may be given as hexadecimal strings or C-style doubly-quoted strings."
          + " The `-i' flag causes the output XML to be indented.";
    }

    @Override
    public EnumSet<SessionMode> getSessionModes() {
        return EnumSet.allOf(SessionMode.class);
    }

    @Override
    protected Parser<?> getParser(String typeName) {
        return "file".equals(typeName) ? new OutputFileParser() : super.getParser(typeName);
    }

    @Override
    public CliSession.Action getAction(CliSession session, ParseContext ctx, boolean complete, Map<String, Object> params) {

        // Parse parameters
        final File file = (File)params.get("file.xml");
        final boolean indent = params.containsKey("indent");
        final byte[] minKey = (byte[])params.get("minKey");
        final byte[] maxKey = (byte[])params.get("maxKey");

        // Return action
        return new CliSession.TransactionalAction() {
            @Override
            public void run(CliSession session) throws Exception {
                final FileOutputStream updateOutput = !this.isWindows() ?
                  new AtomicUpdateFileOutputStream(file) : new FileOutputStream(file);
                final BufferedOutputStream output = new BufferedOutputStream(updateOutput);
                boolean success = false;
                final int count;
                try {
                    XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(output, "UTF-8");
                    if (indent)
                        writer = new IndentXMLStreamWriter(writer);
                    writer.writeStartDocument("UTF-8", "1.0");
                    final XMLSerializer serializer = new XMLSerializer(session.getKVTransaction());
                    count = serializer.write(writer, minKey, maxKey);
                    output.flush();
                    success = true;
                } finally {
                    if (success) {
                        try {
                            output.close();
                        } catch (IOException e) {
                            // ignore
                        }
                    } else if (updateOutput instanceof AtomicUpdateFileOutputStream)
                        ((AtomicUpdateFileOutputStream)updateOutput).cancel();
                }
                session.getWriter().println("Wrote " + count + " key/value pairs to `" + file + "'");
            }

{'fraction_non_alphanumeric': 0.09904735219949566, 'fraction_numerical': 0.0016811431773606051, 'mean_word_length': 2.2361740707162285, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '139431', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1812, 'n_words': 495}