<commit_before>/*
 *  Copyright 2007-2011 Zuse Institute Berlin
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package de.zib.scalaris;

import java.util.ArrayList;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 * Class to test basic functionality of the package and to use scalaris 
 * from command line.
 * 
 * @author <NAME>, <EMAIL>
 * @version 2.0
 * @since 2.0
 */
public class Main {
	/**
	 * Queries the command line options for an action to perform.
	 * 
	 * <pre>
	 * <code>
	 * > java -jar scalaris.jar -help
	 * usage: scalaris [Options]
	 *  -h,--help                        print this message
	 *  -v,--verbose                     print verbose information, e.g. the
	 *                                   properties read
	 *  -lh,--localhost                  gets the local host's name as known to
	 *                                   Java (for debugging purposes)
	 *  -b,--minibench                   run mini benchmark
	 *  -r,--read <key>                  read an item
	 *  -w,--write <key> <value>         write an item
	 *  -d,--delete <key> <[timeout]>    delete an item (default timeout: 2000ms)
	 *                                   WARNING: This function can lead to
	 *                                   inconsistent data (e.g. deleted items
	 *                                   can re-appear). Also when re-creating an
	 *                                   item the version before the delete can
	 *                                   re-appear.
	 *  -p,--publish <topic> <message>   publish a new message for the given
	 *                                   topic
	 *  -s,--subscribe <topic> <url>     subscribe to a topic
	 *  -g,--getsubscribers <topic>      get subscribers of a topic
	 *  -u,--unsubscribe <topic> <url>   unsubscribe from a topic
	 * </code>
	 * </pre>
	 * 
	 * In order to override node and cookie to use for a connection, specify
	 * the <tt>SCALARIS_JAPI_NODE</tt> or <tt>SCALARIS_JAPI_COOKIE</tt>
	 * environment variables. Their values will be used instead of the values
	 * defined in the config file!
	 * 
	 * @param args
	 *            command line arguments
	 */
	public static void main(String[] args) {
		boolean verbose = false;
		CommandLineParser parser = new GnuParser();
		CommandLine line = null;
		Options options = getOptions();
		try {
			line = parser.parse(options, args);
		} catch (ParseException e) {
			printException("Parsing failed", e, false);
		}

		if (line.hasOption("verbose")) {
	        verbose = true;
	        ConnectionFactory.getInstance().printProperties();
		}
		
		if (line.hasOption("minibench")) {
			String[] optionValues = line.getOptionValues("minibench");
			int testruns = 100;
			int benchmarks = -1;
			if (optionValues != null) {
				checkArguments(optionValues, 2, options, "b");
				testruns = Integer.parseInt(optionValues[0]);
				String benchmarks_str = optionValues[1];
				benchmarks = benchmarks_str.equals("all") ? -1 : Integer.parseInt(benchmarks_str);
			}
	        Benchmark.minibench(testruns, benchmarks);
		} else if (line.hasOption("r")) { // read
			String key = line.getOptionValue("read");
			checkArguments(key, options, "r");
			try {
				Scalaris sc = new Scalaris();
				String value = sc.read(key);
				System.out.println("read(" + key + ") == " + value);
			} catch (ConnectionException e) {
				printException("read failed with connection error", e, verbose);
			} catch (TimeoutException e) {
				printException("read failed with timeout", e, verbose);
			} catch (NotFoundException e) {
				printException("read failed with not found", e, verbose);
			} catch (UnknownException e) {
				printException("read failed with unknown", e, verbose);
			}
		} else if (line.hasOption("w")) { // write
			String[] optionValues = line.getOptionValues("write");
			checkArguments(optionValues, 2, options, "w");
			String key = optionValues[0];
			String value = optionValues[1];
			try {
				Scalaris sc = new Scalaris();
				sc.write(key, value);
				System.out.println("write(" + key + ", " + value + "): ok");
			} catch (ConnectionException e) {
				printException("write failed with connection error", e, verbose);
			} catch (TimeoutException e) {
				printException("write failed with timeout", e, verbose);
            } catch (AbortException e) {
                printException("write failed with abort", e, verbose);
			} catch (UnknownException e) {
				printException("write failed with unknown", e, verbose);
            }
		} else if (line.hasOption("p")) { // publish
			String[] optionValues = line.getOptionValues("publish");
			checkArguments(optionValues, 2, options, "p");
			String topic = optionValues[0];
			String content = optionValues[1];
			if (content == null) {
				// parsing methods of commons.cli only checks the first argument :(
				printException("Parsing failed", new ParseException("missing content for option p"), verbose);
			}
			try {
			    PubSub sc = new PubSub();
				sc.publish(topic, content);
				System.out.println("publish(" + topic + ", " + content + "): ok");
			} catch (ConnectionException e) {
				printException("publish failed with connection error", e, verbose);
			}
		} else if (line.hasOption("s")) { // subscribe
			String[] optionValues = line.getOptionValues("subscribe");
			checkArguments(optionValues, 2, options, "s");
			String topic = optionValues[0];
			String url = optionValues[1];
			try {
			    PubSub sc = new PubSub();
				sc.subscribe(topic, url);
				System.out.println("subscribe(" + topic + ", " + url + "): ok");
			} catch (ConnectionException e) {
				printException("subscribe failed with connection error", e, verbose);
			} catch (TimeoutException e) {
				printException("subscribe failed with timeout", e, verbose);
			} catch (UnknownException e) {
				printException("subscribe failed with unknown", e, verbose);
			}
		} else if (line.hasOption("u")) { // unsubscribe
			String[] optionValues = line.getOptionValues("unsubscribe");
			checkArguments(optionValues, 2, options, "u");
			String topic = optionValues[0];
			String url = optionValues[1];
			try {
			    PubSub sc = new PubSub();
				sc.unsubscribe(topic, url);
				System.out.println("unsubscribe(" + topic + ", " + url + "): ok");
			} catch (ConnectionException e) {
				printException("unsubscribe failed with connection error", e, verbose);
			} catch (TimeoutException e) {
				printException("unsubscribe failed with timeout", e, verbose);
			} catch (NotFoundException e) {
				printException("unsubscribe failed with not found", e, verbose);
			} catch (UnknownException e) {
				printException("unsubscribe failed with unknown", e, verbose);
			}
		} else if (line.hasOption("g")) { // getsubscribers
			String topic = line.getOptionValue("getsubscribers");
			checkArguments(topic, options, "g");<commit_msg>- fixed Java-API build<commit_after>/*
 *  Copyright 2007-2011 Zuse Institute Berlin
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package de.zib.scalaris;

import java.util.ArrayList;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 * Class to test basic functionality of the package and to use scalaris 
 * from command line.
 * 
 * @author <NAME>, <EMAIL>
 * @version 2.0
 * @since 2.0
 */
public class Main {
	/**
	 * Queries the command line options for an action to perform.
	 * 
	 * <pre>
	 * <code>
	 * > java -jar scalaris.jar -help
	 * usage: scalaris [Options]
	 *  -h,--help                        print this message
	 *  -v,--verbose                     print verbose information, e.g. the
	 *                                   properties read
	 *  -lh,--localhost                  gets the local host's name as known to
	 *                                   Java (for debugging purposes)
	 *  -b,--minibench                   run mini benchmark
	 *  -r,--read <key>                  read an item
	 *  -w,--write <key> <value>         write an item
	 *  -d,--delete <key> <[timeout]>    delete an item (default timeout: 2000ms)
	 *                                   WARNING: This function can lead to
	 *                                   inconsistent data (e.g. deleted items
	 *                                   can re-appear). Also when re-creating an
	 *                                   item the version before the delete can
	 *                                   re-appear.
	 *  -p,--publish <topic> <message>   publish a new message for the given
	 *                                   topic
	 *  -s,--subscribe <topic> <url>     subscribe to a topic
	 *  -g,--getsubscribers <topic>      get subscribers of a topic
	 *  -u,--unsubscribe <topic> <url>   unsubscribe from a topic
	 * </code>
	 * </pre>
	 * 
	 * In order to override node and cookie to use for a connection, specify
	 * the <tt>SCALARIS_JAPI_NODE</tt> or <tt>SCALARIS_JAPI_COOKIE</tt>
	 * environment variables. Their values will be used instead of the values
	 * defined in the config file!
	 * 
	 * @param args
	 *            command line arguments
	 */
	public static void main(String[] args) {
		boolean verbose = false;
		CommandLineParser parser = new GnuParser();
		CommandLine line = null;
		Options options = getOptions();
		try {
			line = parser.parse(options, args);
		} catch (ParseException e) {
			printException("Parsing failed", e, false);
		}

		if (line.hasOption("verbose")) {
	        verbose = true;
	        ConnectionFactory.getInstance().printProperties();
		}
		
		if (line.hasOption("minibench")) {
			String[] optionValues = line.getOptionValues("minibench");
			int testruns = 100;
			int benchmarks = -1;
			if (optionValues != null) {
				checkArguments(optionValues, 2, options, "b");
				testruns = Integer.parseInt(optionValues[0]);
				String benchmarks_str = optionValues[1];
				benchmarks = benchmarks_str.equals("all") ? -1 : Integer.parseInt(benchmarks_str);
			}
	        Benchmark.minibench(testruns, benchmarks);
		} else if (line.hasOption("r")) { // read
			String key = line.getOptionValue("read");
			checkArguments(key, options, "r");
			try {
				Scalaris sc = new Scalaris();
				String value = sc.read(key);
				System.out.println("read(" + key + ") == " + value);
			} catch (ConnectionException e) {
				printException("read failed with connection error", e, verbose);
			} catch (TimeoutException e) {
				printException("read failed with timeout", e, verbose);
			} catch (NotFoundException e) {
				printException("read failed with not found", e, verbose);
			} catch (UnknownException e) {
				printException("read failed with unknown", e, verbose);
			}
		} else if (line.hasOption("w")) { // write
			String[] optionValues = line.getOptionValues("write");
			checkArguments(optionValues, 2, options, "w");
			String key = optionValues[0];
			String value = optionValues[1];
			try {
				Scalaris sc = new Scalaris();
				sc.write(key, value);
				System.out.println("write(" + key + ", " + value + "): ok");
			} catch (ConnectionException e) {
				printException("write failed with connection error", e, verbose);
			} catch (TimeoutException e) {
				printException("write failed with timeout", e, verbose);
            } catch (AbortException e) {
                printException("write failed with abort", e, verbose);
			} catch (UnknownException e) {
				printException("write failed with unknown", e, verbose);
            }
		} else if (line.hasOption("p")) { // publish
			String[] optionValues = line.getOptionValues("publish");
			checkArguments(optionValues, 2, options, "p");
			String topic = optionValues[0];
			String content = optionValues[1];
			if (content == null) {
				// parsing methods of commons.cli only checks the first argument :(
				printException("Parsing failed", new ParseException("missing content for option p"), verbose);
			}
			try {
			    PubSub sc = new PubSub();
				sc.publish(topic, content);
				System.out.println("publish(" + topic + ", " + content + "): ok");
			} catch (ConnectionException e) {
				printException("publish failed with connection error", e, verbose);
			}
		} else if (line.hasOption("s")) { // subscribe
			String[] optionValues = line.getOptionValues("subscribe");
			checkArguments(optionValues, 2, options, "s");
			String topic = optionValues[0];
			String url = optionValues[1];
			try {
			    PubSub sc = new PubSub();
				sc.subscribe(topic, url);
				System.out.println("subscribe(" + topic + ", " + url + "): ok");
			} catch (ConnectionException e) {
				printException("subscribe failed with connection error", e, verbose);
			} catch (TimeoutException e) {
				printException("subscribe failed with timeout", e, verbose);
            } catch (AbortException e) {
                printException("write failed with abort", e, verbose);
			} catch (UnknownException e) {
				printException("subscribe failed with unknown", e, verbose);
			}
		} else if (line.hasOption("u")) { // unsubscribe
			String[] optionValues = line.getOptionValues("unsubscribe");
			checkArguments(optionValues, 2, options, "u");
			String topic = optionValues[0];
			String url = optionValues[1];
			try {
			    PubSub sc = new PubSub();
				sc.unsubscribe(topic, url);
				System.out.println("unsubscribe(" + topic + ", " + url + "): ok");
			} catch (ConnectionException e) {
				printException("unsubscribe failed with connection error", e, verbose);
			} catch (TimeoutException e) {
				printException("unsubscribe failed with timeout", e, verbose);
			} catch (NotFoundException e) {
				printException("unsubscribe failed with not found", e, verbose);
            } catch (AbortException e) {
                printException("write failed with abort", e, verbose);
			} catch (UnknownException e) {
				printException("unsubscribe failed with unknown", e, verbose);
			}
		} else if (line.hasOption("g")) { // getsubscribers
			String topic = line.getOptionValue("getsubscribers");
			checkArguments(topic, options, "g");
{'fraction_non_alphanumeric': 0.12200632625395391, 'fraction_numerical': 0.005164288941966303, 'mean_word_length': 2.806388206388206, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2873648', 'n_tokens_mistral': 4946, 'n_tokens_neox': 4222, 'n_words': 1364}