Search in sources :

Example 1 with StringSplitter

use of com.cinchapi.common.base.StringSplitter in project concourse by cinchapi.

the class DelimitedLineImporter method parseObject.

/**
 * Transform the delimited line into an object where each field is mapped as
 * a value from the analogous key in the {@link #header}. Each key/value
 * mapping is subject to processing by the {@link #transformer}.
 *
 * @param line
 * @return the parsed object
 */
protected Multimap<String, Object> parseObject(String line) {
    StringSplitter it = new QuoteAwareStringSplitter(line, delimiter, SplitOption.TRIM_WHITESPACE);
    Multimap<String, Object> object = LinkedHashMultimap.create();
    Strainer strainer = new Strainer((key, value) -> object.put(key, value));
    int col = 0;
    while (it.hasNext()) {
        String key = header.get(col++);
        String value = it.next();
        Map<String, Object> transformed = transformer.transform(key, (Object) value);
        if (transformed != null) {
            strainer.process(transformed);
        } else {
            strainer.process(key, value);
        }
    }
    return object;
}
Also used : QuoteAwareStringSplitter(com.cinchapi.common.base.QuoteAwareStringSplitter) StringSplitter(com.cinchapi.common.base.StringSplitter) Strainer(com.cinchapi.concourse.etl.Strainer) QuoteAwareStringSplitter(com.cinchapi.common.base.QuoteAwareStringSplitter)

Example 2 with StringSplitter

use of com.cinchapi.common.base.StringSplitter in project concourse by cinchapi.

the class RoutingKey method forName.

/**
 * Parse a {@link RoutingKey} from a name. Fully qualified names
 * should be in the form of
 * (com|org|net).(company).(concourse?).(module).(...).ClassName to produce
 * a PluginId with the following properties:
 * <ul>
 * <li><strong>group-id:</strong> (com|org|net).company</li>
 * <li><strong>module:</strong> module</li>
 * <li><strong>cls:</strong> ClassName</li>
 * </ul>
 *
 * @param name the name of the class for which the {@link RoutingKey} should
 *            be generated
 * @return a {@link RoutingKey} for the class {@code name}
 */
public static RoutingKey forName(String name) {
    String group = null;
    String module = null;
    String cls = null;
    StringSplitter it = new StringSplitter(name, '.');
    String previous = null;
    String next = null;
    while (it.hasNext()) {
        previous = next;
        next = it.next();
        if (group == null) {
            group = next;
        } else if (group.equals("com") || group.equals("org") || group.equals("net")) {
            group += '.' + next;
        } else if (module == null) {
            if (next != null && (next.equals("concourse") || (next.equals("router") && previous != null && previous.equals("concourse")))) {
                continue;
            } else {
                module = next;
            }
        } else {
            cls = next;
        }
    }
    return new RoutingKey(group, module, cls);
}
Also used : StringSplitter(com.cinchapi.common.base.StringSplitter)

Example 3 with StringSplitter

use of com.cinchapi.common.base.StringSplitter in project concourse by cinchapi.

the class Operations method navigateKeyRecordAtomic.

/**
 * Do the work to atomically to navigate all the values for a key in a
 * record and if its of type {@value Type.LINK}, iterate until the key is
 * not {@value Type.LINK} at timestamp and return a {@code TObject} using
 * the provided atomic {@code operation}.
 *
 * @param key
 * @param record
 * @param timestamp
 * @param atomic
 * @return a mapping from each record at the end of the navigation chain to
 *         the
 * @deprecated use
 *             {@link #traverseKeyRecordOptionalAtomic(String, long, long, Store)}
 *             instead
 */
@Deprecated
public static Map<Long, Set<TObject>> navigateKeyRecordAtomic(String key, long record, long timestamp, AtomicOperation atomic) {
    StringSplitter it = new StringSplitter(key, '.');
    Set<Long> records = Sets.newHashSet(record);
    Map<Long, Set<TObject>> result = Maps.newLinkedHashMap();
    while (it.hasNext()) {
        key = it.next();
        Set<Long> nextRecords = Sets.newLinkedHashSet();
        for (long rec : records) {
            Set<TObject> values = timestamp == Time.NONE ? atomic.select(key, rec) : atomic.select(key, rec, timestamp);
            if (!it.hasNext() && !values.isEmpty()) {
                result.put(rec, values);
            } else {
                values.forEach((value) -> {
                    if (value.type == Type.LINK) {
                        nextRecords.add(((Link) Convert.thriftToJava(value)).longValue());
                    }
                });
            }
        }
        records = nextRecords;
    }
    return result;
}
Also used : TObject(com.cinchapi.concourse.thrift.TObject) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) StringSplitter(com.cinchapi.common.base.StringSplitter)

Example 4 with StringSplitter

use of com.cinchapi.common.base.StringSplitter in project concourse by cinchapi.

the class Commands method runCommand.

/**
 * Run a command as a separate process and handle the results (or error)
 * gracefully.
 *
 * @param command
 * @return the results of the command
 */
private static String runCommand(String command) {
    try {
        String[] parts = new StringSplitter(command, ' ', SplitOption.TRIM_WHITESPACE).toArray();
        ProcessBuilder pb = new ProcessBuilder(parts);
        Process p = pb.start();
        p.waitFor();
        if (p.exitValue() == 0) {
            return IOUtils.toString(p.getInputStream());
        } else {
            throw new RuntimeException(IOUtils.toString(p.getErrorStream()));
        }
    } catch (IOException | InterruptedException e) {
        throw CheckedExceptions.wrapAsRuntimeException(e);
    }
}
Also used : StringSplitter(com.cinchapi.common.base.StringSplitter) IOException(java.io.IOException)

Example 5 with StringSplitter

use of com.cinchapi.common.base.StringSplitter in project concourse by cinchapi.

the class TStrings method stripStopWordsAndTokenize.

/**
 * Tokenize the {@code string} and return an array of tokens where all the
 * stopwords are removed.
 *
 * @param string
 * @return the tokens without stopwords
 */
public static String[] stripStopWordsAndTokenize(String string) {
    ArrayBuilder<String> toks = ArrayBuilder.builder();
    StringSplitter it = new StringSplitter(string, ' ');
    while (it.hasNext()) {
        String next = it.next();
        if (!StringUtils.isBlank(next) && !GlobalState.STOPWORDS.contains(next)) {
            toks.add(next);
        }
    }
    return toks.length() > 0 ? toks.build() : Array.containing();
}
Also used : StringSplitter(com.cinchapi.common.base.StringSplitter)

Aggregations

StringSplitter (com.cinchapi.common.base.StringSplitter)5 QuoteAwareStringSplitter (com.cinchapi.common.base.QuoteAwareStringSplitter)1 Strainer (com.cinchapi.concourse.etl.Strainer)1 TObject (com.cinchapi.concourse.thrift.TObject)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 IOException (java.io.IOException)1 Set (java.util.Set)1