com.googlecode.concurrenttrees.suffix.SuffixTree Java Examples

The following examples show how to use com.googlecode.concurrenttrees.suffix.SuffixTree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SuffixTreeIndex.java    From cqengine with Apache License 2.0 6 votes vote down vote up
protected ResultSet<O> retrieveIn(final In<O, A> in, final QueryOptions queryOptions, final SuffixTree<StoredResultSet<O>> tree) {
    // Process the IN query as the union of the EQUAL queries for the values specified by the IN query.
    final Iterable<? extends ResultSet<O>> results = new Iterable<ResultSet<O>>() {
        @Override
        public Iterator<ResultSet<O>> iterator() {
            return new LazyIterator<ResultSet<O>>() {
                final Iterator<A> values = in.getValues().iterator();
                @Override
                protected ResultSet<O> computeNext() {
                    if (values.hasNext()){
                        return retrieveEqual(new Equal<O, A>(in.getAttribute(), values.next()), queryOptions, tree);
                    }else{
                        return endOfData();
                    }
                }
            };
        }
    };
    return deduplicateIfNecessary(results, in, getAttribute(), queryOptions, INDEX_RETRIEVAL_COST);
}
 
Example #2
Source File: SuffixTreeIndex.java    From cqengine with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean removeAll(ObjectSet<O> objectSet, QueryOptions queryOptions) {
    try {
        boolean modified = false;
        final SuffixTree<StoredResultSet<O>> tree = this.tree;
        for (O object : objectSet) {
            Iterable<A> attributeValues = getAttribute().getValues(object, queryOptions);
            for (A attributeValue : attributeValues) {
                StoredResultSet<O> valueSet = tree.getValueForExactKey(attributeValue);
                if (valueSet == null) {
                    continue;
                }
                modified |= valueSet.remove(object);
                if (valueSet.isEmpty()) {
                    tree.remove(attributeValue);
                }
            }
        }
        return modified;
    }
    finally {
        objectSet.close();
    }
}
 
Example #3
Source File: SuffixTreeUsage.java    From concurrent-trees with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    System.out.println("Suffixes for 'TEST': " + Iterables.toString(CharSequences.generateSuffixes("TEST")));
    System.out.println("Suffixes for 'TOAST': " + Iterables.toString(CharSequences.generateSuffixes("TOAST")));
    System.out.println("Suffixes for 'TEAM': " + Iterables.toString(CharSequences.generateSuffixes("TEAM")));

    SuffixTree<Integer> tree = new ConcurrentSuffixTree<Integer>(new DefaultCharArrayNodeFactory());

    tree.put("TEST", 1);
    tree.put("TOAST", 2);
    tree.put("TEAM", 3);

    System.out.println();
    System.out.println("Tree structure:");
    // PrettyPrintable is a non-public API for testing, prints semi-graphical representations of trees...
    PrettyPrinter.prettyPrint((PrettyPrintable) tree, System.out);

    System.out.println();
    System.out.println("Value for 'TEST' (exact match): " + tree.getValueForExactKey("TEST"));
    System.out.println("Value for 'TOAST' (exact match): " + tree.getValueForExactKey("TOAST"));
    System.out.println();
    System.out.println("Keys ending with 'ST': " + Iterables.toString(tree.getKeysEndingWith("ST")));
    System.out.println("Keys ending with 'M': " + Iterables.toString(tree.getKeysEndingWith("M")));
    System.out.println("Values for keys ending with 'ST': " + Iterables.toString(tree.getValuesForKeysEndingWith("ST")));
    System.out.println("Key-Value pairs for keys ending with 'ST': " + Iterables.toString(tree.getKeyValuePairsForKeysEndingWith("ST")));
    System.out.println();
    System.out.println("Keys containing 'TE': " + Iterables.toString(tree.getKeysContaining("TE")));
    System.out.println("Keys containing 'A': " + Iterables.toString(tree.getKeysContaining("A")));
    System.out.println("Values for keys containing 'A': " + Iterables.toString(tree.getValuesForKeysContaining("A")));
    System.out.println("Key-Value pairs for keys containing 'A': " + Iterables.toString(tree.getKeyValuePairsForKeysContaining("A")));
}
 
Example #4
Source File: TypeUtils.java    From baleen with Apache License 2.0 5 votes vote down vote up
private static Class<AnnotationBase> getClassFromType(
    String typeName, SuffixTree<Class<AnnotationBase>> types) {
  Iterator<CharSequence> itMatchedTypes = types.getKeysEndingWith(typeName).iterator();
  if (!itMatchedTypes.hasNext()) {
    return null; // No match found
  }

  Class<AnnotationBase> ret = types.getValueForExactKey(itMatchedTypes.next());

  if (itMatchedTypes.hasNext()) {
    return null; // More than one match found
  }

  return ret;
}
 
Example #5
Source File: SuffixTreeIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean addAll(ObjectSet<O> objectSet, QueryOptions queryOptions) {
    try {
        boolean modified = false;
        final SuffixTree<StoredResultSet<O>> tree = this.tree;
        for (O object : objectSet) {
            Iterable<A> attributeValues = getAttribute().getValues(object, queryOptions);
            for (A attributeValue : attributeValues) {

                // Look up StoredResultSet for the value...
                StoredResultSet<O> valueSet = tree.getValueForExactKey(attributeValue);
                if (valueSet == null) {
                    // No StoredResultSet, create and add one...
                    valueSet = createValueSet();
                    StoredResultSet<O> existingValueSet = tree.putIfAbsent(attributeValue, valueSet);
                    if (existingValueSet != null) {
                        // Another thread won race to add new value set, use that one...
                        valueSet = existingValueSet;
                    }
                }
                // Add the object to the StoredResultSet for this value...
                modified |= valueSet.add(object);
            }
        }
        return modified;
    }
    finally {
        objectSet.close();
    }
}
 
Example #6
Source File: TypeUtils.java    From baleen with Apache License 2.0 4 votes vote down vote up
/**
 * For a given type name, look through the type system and return the matching class. If two types
 * of the same name (but different packages) exist, then null will be returned and the package
 * will need to be included in the typeName.
 *
 * @param typeName The name of the type, optionally including the package
 * @param typeSystem The type system to search
 * @return The class associated with that type
 */
@SuppressWarnings("unchecked")
public static Class<AnnotationBase> getType(String typeName, TypeSystem typeSystem) {
  SuffixTree<Class<AnnotationBase>> types =
      new ConcurrentSuffixTree<>(new DefaultByteArrayNodeFactory());

  Iterator<Type> itTypes = typeSystem.getTypeIterator();
  while (itTypes.hasNext()) {
    Type t = itTypes.next();

    Class<AnnotationBase> c;
    try {
      String clazz = t.getName();
      if (clazz.startsWith("uima.")) {
        continue;
      } else if (clazz.endsWith("[]")) {
        clazz = clazz.substring(0, clazz.length() - 2);
      }

      Class<?> unchecked = Class.forName(clazz);

      if (AnnotationBase.class.isAssignableFrom(unchecked)) {
        c = (Class<AnnotationBase>) unchecked;
        types.put(t.getName(), c);
      } else {
        LOGGER.debug("Skipping class {} that doesn't inherit from AnnotationBase", clazz);
      }
    } catch (ClassNotFoundException e) {
      LOGGER.warn("Unable to load class {} from type system", t.getName(), e);
    }
  }

  Class<AnnotationBase> ret = getClassFromType("." + typeName, types);
  if (ret == null) {
    ret = getClassFromType(typeName, types);
  }

  if (ret == null) {
    LOGGER.warn("No uniquely matching class found for type {}", typeName);
  }

  return ret;
}