// Path: src/main/java/io/dashbase/clue/LuceneContext.java
// public class LuceneContext extends ClueContext {
//     private final IndexReaderFactory readerFactory;
// 
//     private IndexWriter writer;
//     private final Directory directory;
//     private final IndexWriterConfig writerConfig;
//     private final QueryBuilder queryBuilder;
//     private final AnalyzerFactory analyzerFactory;
//     private final BytesRefDisplay termBytesRefDisplay;
//     private final BytesRefDisplay payloadBytesRefDisplay;
// 
//     public LuceneContext(String dir, ClueAppConfiguration config, boolean interactiveMode)
//             throws Exception {
//         super(config.commandRegistrar, interactiveMode);
//         this.directory = config.dirBuilder.build(dir);
//         this.analyzerFactory = config.analyzerFactory;
//         this.readerFactory = config.indexReaderFactory;
//         this.readerFactory.initialize(directory);
//         this.queryBuilder = config.queryBuilder;
//         this.queryBuilder.initialize("contents", analyzerFactory.forQuery());
//         this.writerConfig = new IndexWriterConfig(new StandardAnalyzer());
//         this.termBytesRefDisplay = new StringBytesRefDisplay();
//         this.payloadBytesRefDisplay = new StringBytesRefDisplay();
//         this.writer = null;
//     }
// 
// 
//     public IndexReader getIndexReader(){
//         return readerFactory.getIndexReader();
//     }
// 
//     public IndexSearcher getIndexSearcher() {
//         return new IndexSearcher(getIndexReader());
//     }
// 
//     public IndexWriter getIndexWriter(){
//         if (registry.isReadonly()) return null;
//         if (writer == null) {
//             try {
//                 writer = new IndexWriter(directory, writerConfig);
//             } catch (IOException e) {
//                 e.printStackTrace();
//             }
//         }
//         return writer;
//     }
// 
//     Collection<String> fieldNames() {
//         LinkedList<String> fieldNames = new LinkedList<>();
//         for (LeafReaderContext context : getIndexReader().leaves()) {
//             LeafReader reader = context.reader();
//             for(FieldInfo info : reader.getFieldInfos()) {
//                 fieldNames.add(info.name);
//             }
//         }
//         return fieldNames;
//     }
// 
//     public QueryBuilder getQueryBuilder() {
//         return queryBuilder;
//     }
// 
//     public Analyzer getAnalyzerQuery() {
//         return analyzerFactory.forQuery();
//     }
// 
//     public BytesRefDisplay getTermBytesRefDisplay() {
//         return termBytesRefDisplay;
//     }
// 
//     public BytesRefDisplay getPayloadBytesRefDisplay() {
//         return payloadBytesRefDisplay;
//     }
// 
//     public Directory getDirectory() {
//         return directory;
//     }
// 
//     public void setReadOnlyMode(boolean readOnlyMode) {
//         this.registry.setReadonly(readOnlyMode);
//         if (writer != null) {
//             try {
//                 writer.close();
//             } catch (IOException e) {
//                 e.printStackTrace();
//             }
//             writer = null;
//         }
//     }
// 
//     public void refreshReader() throws Exception {
//         readerFactory.refreshReader();
//     }
// 
//     @Override
//     public void shutdown() throws Exception{
//         try {
//             readerFactory.shutdown();
//         }
//         finally{
//             directory.close();
//             if (writer != null) {
//                 writer.close();
//             }
//         }
//     }
// }
// 
// Path: src/main/java/io/dashbase/clue/client/CmdlineHelper.java
// public class CmdlineHelper {
//     private final ConsoleReader consoleReader;
// 
//     public CmdlineHelper(Supplier<Collection<String>> commandNameSupplier,
//                          Supplier<Collection<String>> fieldNameSupplier) throws IOException {
//         consoleReader = new ConsoleReader();
//         consoleReader.setBellEnabled(false);
// 
//         Collection<String> commands = commandNameSupplier != null
//                 ? commandNameSupplier.get() : Collections.emptyList();
// 
//         Collection<String> fields = fieldNameSupplier != null
//                 ? fieldNameSupplier.get() : Collections.emptyList();
// 
//         LinkedList<Completer> completors = new LinkedList<Completer>();
//         completors.add(new StringsCompleter(commands));
//         completors.add(new StringsCompleter(fields));
//         completors.add(new FileNameCompleter());
//         consoleReader.addCompleter(new ArgumentCompleter(completors));
//     }
// 
//     public String readCommand() {
//         try {
//             return consoleReader.readLine("> ");
//         } catch (IOException e) {
//             System.err.println("Error! Clue is unable to read line from stdin: " + e.getMessage());
//             throw new IllegalStateException("Unable to read command line!", e);
//         }
//     }
// 
//     public static String toString(List<String> list) {
//         StringBuilder buf = new StringBuilder();
//         for (String s : list) {
//             buf.append(s).append(" ");
//         }
//         return buf.toString().trim();
//     }
// 
//     public static Query toQuery(List<String> list, QueryBuilder queryBuilder) throws Exception {
//         String qstring = toString(list);
//         Query q = null;
//         if (qstring == null || qstring.isEmpty() || qstring.equals("*")){
//             q = new MatchAllDocsQuery();
//         }
//         else{
//             q = queryBuilder.build(qstring);
//         }
//         return q;
//     }
// }


import java.io.PrintStream;
import java.util.List;
import io.dashbase.clue.LuceneContext;
import io.dashbase.clue.client.CmdlineHelper;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.Namespace;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
package io.dashbase.clue.commands;



@Readonly
public class CountCommand extends ClueCommand {

	private final LuceneContext ctx;

	public CountCommand(LuceneContext ctx) {
		super(ctx);
		this.ctx = ctx;
	}

	@Override
	public String getName() {
		return "count";
	}

	@Override
	public String help() {
		return "shows how many documents in index match the given query";
	}

	@Override
	protected ArgumentParser buildParser(ArgumentParser parser) {
		parser.addArgument("-q", "--query").nargs("*").setDefault(new String[]{"*"});
		return parser;
	}

	@Override
	public void execute(Namespace args, PrintStream out) throws Exception {
		IndexSearcher searcher = ctx.getIndexSearcher();

		List<String> qlist = args.getList("query");
		Query q;
		try{
// Path: src/main/java/io/dashbase/clue/LuceneContext.java
// public class LuceneContext extends ClueContext {
//     private final IndexReaderFactory readerFactory;
// 
//     private IndexWriter writer;
//     private final Directory directory;
//     private final IndexWriterConfig writerConfig;
//     private final QueryBuilder queryBuilder;
//     private final AnalyzerFactory analyzerFactory;
//     private final BytesRefDisplay termBytesRefDisplay;
//     private final BytesRefDisplay payloadBytesRefDisplay;
// 
//     public LuceneContext(String dir, ClueAppConfiguration config, boolean interactiveMode)
//             throws Exception {
//         super(config.commandRegistrar, interactiveMode);
//         this.directory = config.dirBuilder.build(dir);
//         this.analyzerFactory = config.analyzerFactory;
//         this.readerFactory = config.indexReaderFactory;
//         this.readerFactory.initialize(directory);
//         this.queryBuilder = config.queryBuilder;
//         this.queryBuilder.initialize("contents", analyzerFactory.forQuery());
//         this.writerConfig = new IndexWriterConfig(new StandardAnalyzer());
//         this.termBytesRefDisplay = new StringBytesRefDisplay();
//         this.payloadBytesRefDisplay = new StringBytesRefDisplay();
//         this.writer = null;
//     }
// 
// 
//     public IndexReader getIndexReader(){
//         return readerFactory.getIndexReader();
//     }
// 
//     public IndexSearcher getIndexSearcher() {
//         return new IndexSearcher(getIndexReader());
//     }
// 
//     public IndexWriter getIndexWriter(){
//         if (registry.isReadonly()) return null;
//         if (writer == null) {
//             try {
//                 writer = new IndexWriter(directory, writerConfig);
//             } catch (IOException e) {
//                 e.printStackTrace();
//             }
//         }
//         return writer;
//     }
// 
//     Collection<String> fieldNames() {
//         LinkedList<String> fieldNames = new LinkedList<>();
//         for (LeafReaderContext context : getIndexReader().leaves()) {
//             LeafReader reader = context.reader();
//             for(FieldInfo info : reader.getFieldInfos()) {
//                 fieldNames.add(info.name);
//             }
//         }
//         return fieldNames;
//     }
// 
//     public QueryBuilder getQueryBuilder() {
//         return queryBuilder;
//     }
// 
//     public Analyzer getAnalyzerQuery() {
//         return analyzerFactory.forQuery();
//     }
// 
//     public BytesRefDisplay getTermBytesRefDisplay() {
//         return termBytesRefDisplay;
//     }
// 
//     public BytesRefDisplay getPayloadBytesRefDisplay() {
//         return payloadBytesRefDisplay;
//     }
// 
//     public Directory getDirectory() {
//         return directory;
//     }
// 
//     public void setReadOnlyMode(boolean readOnlyMode) {
//         this.registry.setReadonly(readOnlyMode);
//         if (writer != null) {
//             try {
//                 writer.close();
//             } catch (IOException e) {
//                 e.printStackTrace();
//             }
//             writer = null;
//         }
//     }
// 
//     public void refreshReader() throws Exception {
//         readerFactory.refreshReader();
//     }
// 
//     @Override
//     public void shutdown() throws Exception{
//         try {
//             readerFactory.shutdown();
//         }
//         finally{
//             directory.close();
//             if (writer != null) {
//                 writer.close();
//             }
//         }
//     }
// }
// 
// Path: src/main/java/io/dashbase/clue/client/CmdlineHelper.java
// public class CmdlineHelper {
//     private final ConsoleReader consoleReader;
// 
//     public CmdlineHelper(Supplier<Collection<String>> commandNameSupplier,
//                          Supplier<Collection<String>> fieldNameSupplier) throws IOException {
//         consoleReader = new ConsoleReader();
//         consoleReader.setBellEnabled(false);
// 
//         Collection<String> commands = commandNameSupplier != null
//                 ? commandNameSupplier.get() : Collections.emptyList();
// 
//         Collection<String> fields = fieldNameSupplier != null
//                 ? fieldNameSupplier.get() : Collections.emptyList();
// 
//         LinkedList<Completer> completors = new LinkedList<Completer>();
//         completors.add(new StringsCompleter(commands));
//         completors.add(new StringsCompleter(fields));
//         completors.add(new FileNameCompleter());
//         consoleReader.addCompleter(new ArgumentCompleter(completors));
//     }
// 
//     public String readCommand() {
//         try {
//             return consoleReader.readLine("> ");
//         } catch (IOException e) {
//             System.err.println("Error! Clue is unable to read line from stdin: " + e.getMessage());
//             throw new IllegalStateException("Unable to read command line!", e);
//         }
//     }
// 
//     public static String toString(List<String> list) {
//         StringBuilder buf = new StringBuilder();
//         for (String s : list) {
//             buf.append(s).append(" ");
//         }
//         return buf.toString().trim();
//     }
// 
//     public static Query toQuery(List<String> list, QueryBuilder queryBuilder) throws Exception {
//         String qstring = toString(list);
//         Query q = null;
//         if (qstring == null || qstring.isEmpty() || qstring.equals("*")){
//             q = new MatchAllDocsQuery();
//         }
//         else{
//             q = queryBuilder.build(qstring);
//         }
//         return q;
//     }
// }


// Path: src/main/java/io/dashbase/clue/commands/CountCommand.java
import java.io.PrintStream;
import java.util.List;
import io.dashbase.clue.LuceneContext;
import io.dashbase.clue.client.CmdlineHelper;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.Namespace;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;

package io.dashbase.clue.commands;



@Readonly
public class CountCommand extends ClueCommand {

	private final LuceneContext ctx;

	public CountCommand(LuceneContext ctx) {
		super(ctx);
		this.ctx = ctx;
	}

	@Override
	public String getName() {
		return "count";
	}

	@Override
	public String help() {
		return "shows how many documents in index match the given query";
	}

	@Override
	protected ArgumentParser buildParser(ArgumentParser parser) {
		parser.addArgument("-q", "--query").nargs("*").setDefault(new String[]{"*"});
		return parser;
	}

	@Override
	public void execute(Namespace args, PrintStream out) throws Exception {
		IndexSearcher searcher = ctx.getIndexSearcher();

		List<String> qlist = args.getList("query");
		Query q;
		try{
			q = CmdlineHelper.toQuery(qlist, ctx.getQueryBuilder());