Search in sources :

Example 1 with OptionDescriber

use of org.apache.accumulo.core.iterators.OptionDescriber in project accumulo by apache.

the class SetIterCommand method setUpOptions.

private static String setUpOptions(ClassLoader classloader, final ConsoleReader reader, final String className, final Map<String, String> options) throws IOException, ShellCommandException {
    String input;
    @SuppressWarnings("rawtypes") SortedKeyValueIterator untypedInstance;
    @SuppressWarnings("rawtypes") Class<? extends SortedKeyValueIterator> clazz;
    try {
        clazz = classloader.loadClass(className).asSubclass(SortedKeyValueIterator.class);
        untypedInstance = clazz.newInstance();
    } catch (ClassNotFoundException e) {
        StringBuilder msg = new StringBuilder("Unable to load ").append(className);
        if (className.indexOf('.') < 0) {
            msg.append("; did you use a fully qualified package name?");
        } else {
            msg.append("; class not found.");
        }
        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, msg.toString());
    } catch (InstantiationException | IllegalAccessException e) {
        throw new IllegalArgumentException(e.getMessage());
    } catch (ClassCastException e) {
        StringBuilder msg = new StringBuilder(50);
        msg.append(className).append(" loaded successfully but does not implement SortedKeyValueIterator.");
        msg.append(" This class cannot be used with this command.");
        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, msg.toString());
    }
    @SuppressWarnings("unchecked") SortedKeyValueIterator<Key, Value> skvi = untypedInstance;
    OptionDescriber iterOptions = null;
    if (OptionDescriber.class.isAssignableFrom(skvi.getClass())) {
        iterOptions = (OptionDescriber) skvi;
    }
    String iteratorName;
    if (null != iterOptions) {
        final IteratorOptions itopts = iterOptions.describeOptions();
        iteratorName = itopts.getName();
        if (iteratorName == null) {
            throw new IllegalArgumentException(className + " described its default distinguishing name as null");
        }
        String shortClassName = className;
        if (className.contains(".")) {
            shortClassName = className.substring(className.lastIndexOf('.') + 1);
        }
        final Map<String, String> localOptions = new HashMap<>();
        do {
            // clean up the overall options that caused things to fail
            for (String key : localOptions.keySet()) {
                options.remove(key);
            }
            localOptions.clear();
            reader.println(itopts.getDescription());
            String prompt;
            if (itopts.getNamedOptions() != null) {
                for (Entry<String, String> e : itopts.getNamedOptions().entrySet()) {
                    prompt = Shell.repeat("-", 10) + "> set " + shortClassName + " parameter " + e.getKey() + ", " + e.getValue() + ": ";
                    reader.flush();
                    input = reader.readLine(prompt);
                    if (input == null) {
                        reader.println();
                        throw new IOException("Input stream closed");
                    }
                    // Places all Parameters and Values into the LocalOptions, even if the value is "".
                    // This allows us to check for "" values when setting the iterators and allows us to remove
                    // the parameter and value from the table property.
                    localOptions.put(e.getKey(), input);
                }
            }
            if (itopts.getUnnamedOptionDescriptions() != null) {
                for (String desc : itopts.getUnnamedOptionDescriptions()) {
                    reader.println(Shell.repeat("-", 10) + "> entering options: " + desc);
                    input = "start";
                    prompt = Shell.repeat("-", 10) + "> set " + shortClassName + " option (<name> <value>, hit enter to skip): ";
                    while (true) {
                        reader.flush();
                        input = reader.readLine(prompt);
                        if (input == null) {
                            reader.println();
                            throw new IOException("Input stream closed");
                        } else {
                            input = new String(input);
                        }
                        if (input.length() == 0)
                            break;
                        String[] sa = input.split(" ", 2);
                        localOptions.put(sa[0], sa[1]);
                    }
                }
            }
            options.putAll(localOptions);
            if (!iterOptions.validateOptions(options))
                reader.println("invalid options for " + clazz.getName());
        } while (!iterOptions.validateOptions(options));
    } else {
        reader.flush();
        reader.println("The iterator class does not implement OptionDescriber. Consider this for better iterator configuration using this setiter command.");
        iteratorName = reader.readLine("Name for iterator (enter to skip): ");
        if (null == iteratorName) {
            reader.println();
            throw new IOException("Input stream closed");
        } else if (StringUtils.isWhitespace(iteratorName)) {
            // Treat whitespace or empty string as no name provided
            iteratorName = null;
        }
        reader.flush();
        reader.println("Optional, configure name-value options for iterator:");
        String prompt = Shell.repeat("-", 10) + "> set option (<name> <value>, hit enter to skip): ";
        final HashMap<String, String> localOptions = new HashMap<>();
        while (true) {
            reader.flush();
            input = reader.readLine(prompt);
            if (input == null) {
                reader.println();
                throw new IOException("Input stream closed");
            } else if (StringUtils.isWhitespace(input)) {
                break;
            }
            String[] sa = input.split(" ", 2);
            localOptions.put(sa[0], sa[1]);
        }
        options.putAll(localOptions);
    }
    return iteratorName;
}
Also used : HashMap(java.util.HashMap) ShellCommandException(org.apache.accumulo.shell.ShellCommandException) SortedKeyValueIterator(org.apache.accumulo.core.iterators.SortedKeyValueIterator) IOException(java.io.IOException) IteratorOptions(org.apache.accumulo.core.iterators.OptionDescriber.IteratorOptions) Value(org.apache.accumulo.core.data.Value) OptionDescriber(org.apache.accumulo.core.iterators.OptionDescriber) Key(org.apache.accumulo.core.data.Key)

Aggregations

IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Key (org.apache.accumulo.core.data.Key)1 Value (org.apache.accumulo.core.data.Value)1 OptionDescriber (org.apache.accumulo.core.iterators.OptionDescriber)1 IteratorOptions (org.apache.accumulo.core.iterators.OptionDescriber.IteratorOptions)1 SortedKeyValueIterator (org.apache.accumulo.core.iterators.SortedKeyValueIterator)1 ShellCommandException (org.apache.accumulo.shell.ShellCommandException)1