Example usage for org.apache.commons.collections.iterators FilterIterator FilterIterator

List of usage examples for org.apache.commons.collections.iterators FilterIterator FilterIterator

Introduction

In this page you can find the example usage for org.apache.commons.collections.iterators FilterIterator FilterIterator.

Prototype

public FilterIterator(Iterator iterator, Predicate predicate) 

Source Link

Document

Constructs a new FilterIterator that will use the given iterator and predicate.

Usage

From source file:com.bigdata.dastor.db.CompactionManager.java

/**
 * Performs a readonly "compaction" of all sstables in order to validate complete rows,
 * but without writing the merge result//w  w  w. jav  a  2s. c o m
 */
private void doValidationCompaction(ColumnFamilyStore cfs, AntiEntropyService.Validator validator)
        throws IOException {
    Collection<SSTableReader> sstables = cfs.getSSTables();
    CompactionIterator ci = new CompactionIterator(cfs, sstables, getDefaultGCBefore(), true);
    getExecutor(cfs).beginCompaction(cfs, ci);
    try {
        Iterator<CompactionIterator.CompactedRow> nni = new FilterIterator(ci,
                PredicateUtils.notNullPredicate());

        // validate the CF as we iterate over it
        validator.prepare(cfs);
        while (nni.hasNext()) {
            CompactionIterator.CompactedRow row = nni.next();
            validator.add(row);
        }
        validator.complete();
    } finally {
        ci.close();
    }
}

From source file:flex2.compiler.mxml.rep.Model.java

/**
 *
 *//*from  www .  j  av a 2  s .co m*/
@SuppressWarnings("unchecked")
protected Iterator<Initializer> bindingFilter(Iterator<? extends Initializer> iter, final boolean include) {
    return new FilterIterator(iter, new Predicate() {
        public boolean evaluate(Object object) {
            return (((Initializer) object).isBinding()) == include;
        }
    });
}

From source file:flex2.compiler.mxml.rep.MxmlDocument.java

/**
 *
 *///from  ww  w .  j a  v  a  2  s.  com
public Iterator getInheritingStyleNameIterator() {
    final Styles styles = typeTable.getStyles();
    return new FilterIterator(styles.getStyleNames(), new Predicate() {
        public boolean evaluate(Object obj) {
            return styles.isInheritingStyle((String) obj);
        }
    });
}

From source file:org.amplafi.tapestry4.ProgrammableTapestryGlobals.java

@SuppressWarnings("unchecked")
public Iterator<Object> mocksIterator() {
    return new FilterIterator(requestObjects().iterator(), new Predicate() {
        @Override//from   w  w  w.  j a  va 2  s.  c om
        public boolean evaluate(Object object) {
            return isMockObject(object);
        }
    });
}

From source file:org.apache.cassandra.db.compaction.CompactionManager.java

/**
 * For internal use and testing only.  The rest of the system should go through the submit* methods,
 * which are properly serialized.//from   w  w w .  java2 s  . c om
 */
int doCompactionWithoutSizeEstimation(ColumnFamilyStore cfs, Collection<SSTableReader> sstables, int gcBefore,
        String compactionFileLocation, boolean forceDeserialize) throws IOException {
    // The collection of sstables passed may be empty (but not null); even if
    // it is not empty, it may compact down to nothing if all rows are deleted.
    assert sstables != null;

    Table table = cfs.table;
    if (DatabaseDescriptor.isSnapshotBeforeCompaction())
        table.snapshot(System.currentTimeMillis() + "-" + "compact-" + cfs.columnFamily);

    // sanity check: all sstables must belong to the same cfs
    for (SSTableReader sstable : sstables)
        assert sstable.descriptor.cfname.equals(cfs.columnFamily);

    CompactionController controller = new CompactionController(cfs, sstables, gcBefore, forceDeserialize);
    // new sstables from flush can be added during a compaction, but only the compaction can remove them,
    // so in our single-threaded compaction world this is a valid way of determining if we're compacting
    // all the sstables (that existed when we started)
    CompactionType type = controller.isMajor() ? CompactionType.MAJOR : CompactionType.MINOR;
    logger.info("Compacting {}: {}", type, sstables);

    long startTime = System.currentTimeMillis();
    long totalkeysWritten = 0;

    // TODO the int cast here is potentially buggy
    int expectedBloomFilterSize = Math.max(DatabaseDescriptor.getIndexInterval(),
            (int) SSTableReader.getApproximateKeyCount(sstables));
    if (logger.isDebugEnabled())
        logger.debug("Expected bloom filter size : " + expectedBloomFilterSize);

    SSTableWriter writer;
    CompactionIterator ci = new CompactionIterator(type, sstables, controller); // retain a handle so we can call close()
    Iterator<AbstractCompactedRow> nni = new FilterIterator(ci, PredicateUtils.notNullPredicate());
    Map<DecoratedKey, Long> cachedKeys = new HashMap<DecoratedKey, Long>();

    executor.beginCompaction(ci);
    try {
        if (!nni.hasNext()) {
            // don't mark compacted in the finally block, since if there _is_ nondeleted data,
            // we need to sync it (via closeAndOpen) first, so there is no period during which
            // a crash could cause data loss.
            cfs.markCompacted(sstables);
            return 0;
        }

        writer = cfs.createCompactionWriter(expectedBloomFilterSize, compactionFileLocation, sstables);
        while (nni.hasNext()) {
            AbstractCompactedRow row = nni.next();
            if (row.isEmpty())
                continue;

            long position = writer.append(row);
            totalkeysWritten++;

            if (DatabaseDescriptor.getPreheatKeyCache()) {
                for (SSTableReader sstable : sstables) {
                    if (sstable.getCachedPosition(row.key) != null) {
                        cachedKeys.put(row.key, position);
                        break;
                    }
                }
            }
        }
    } finally {
        ci.close();
        executor.finishCompaction(ci);
    }

    SSTableReader ssTable = writer.closeAndOpenReader(getMaxDataAge(sstables));
    cfs.replaceCompactedSSTables(sstables, Arrays.asList(ssTable));
    for (Entry<DecoratedKey, Long> entry : cachedKeys.entrySet()) // empty if preheat is off
        ssTable.cacheKey(entry.getKey(), entry.getValue());
    submitMinorIfNeeded(cfs);

    long dTime = System.currentTimeMillis() - startTime;
    long startsize = SSTable.getTotalBytes(sstables);
    long endsize = ssTable.length();
    double ratio = (double) endsize / (double) startsize;
    logger.info(
            String.format("Compacted to %s.  %,d to %,d (~%d%% of original) bytes for %,d keys.  Time: %,dms.",
                    writer.getFilename(), startsize, endsize, (int) (ratio * 100), totalkeysWritten, dTime));
    return sstables.size();
}

From source file:org.apache.cassandra.db.compaction.CompactionManager.java

/**
 * Performs a readonly "compaction" of all sstables in order to validate complete rows,
 * but without writing the merge result//from   ww w.  j a  v  a 2 s. co  m
 */
private void doValidationCompaction(ColumnFamilyStore cfs, AntiEntropyService.Validator validator)
        throws IOException {
    // flush first so everyone is validating data that is as similar as possible
    try {
        StorageService.instance.forceTableFlush(cfs.table.name, cfs.getColumnFamilyName());
    } catch (ExecutionException e) {
        throw new IOException(e);
    } catch (InterruptedException e) {
        throw new AssertionError(e);
    }

    CompactionIterator ci = new ValidationCompactionIterator(cfs, validator.request.range);
    validationExecutor.beginCompaction(ci);
    try {
        Iterator<AbstractCompactedRow> nni = new FilterIterator(ci, PredicateUtils.notNullPredicate());

        // validate the CF as we iterate over it
        validator.prepare(cfs);
        while (nni.hasNext()) {
            AbstractCompactedRow row = nni.next();
            validator.add(row);
        }
        validator.complete();
    } finally {
        ci.close();
        validationExecutor.finishCompaction(ci);
    }
}

From source file:org.apache.cassandra.db.CompactionManager.java

/**
 * For internal use and testing only.  The rest of the system should go through the submit* methods,
 * which are properly serialized.//from   w w  w . java2 s .  c  o m
 */
int doCompactionWithoutSizeEstimation(ColumnFamilyStore cfs, Collection<SSTableReader> sstables, int gcBefore,
        String compactionFileLocation) throws IOException {
    // The collection of sstables passed may be empty (but not null); even if
    // it is not empty, it may compact down to nothing if all rows are deleted.
    assert sstables != null;

    Table table = cfs.table;
    if (DatabaseDescriptor.isSnapshotBeforeCompaction())
        table.snapshot(System.currentTimeMillis() + "-" + "compact-" + cfs.columnFamily);

    // sanity check: all sstables must belong to the same cfs
    for (SSTableReader sstable : sstables)
        assert sstable.descriptor.cfname.equals(cfs.columnFamily);

    // new sstables from flush can be added during a compaction, but only the compaction can remove them,
    // so in our single-threaded compaction world this is a valid way of determining if we're compacting
    // all the sstables (that existed when we started)
    boolean major = cfs.isCompleteSSTables(sstables);
    CompactionType type = major ? CompactionType.MAJOR : CompactionType.MINOR;
    logger.info("Compacting {}: {}", type, sstables);

    long startTime = System.currentTimeMillis();
    long totalkeysWritten = 0;

    // TODO the int cast here is potentially buggy
    int expectedBloomFilterSize = Math.max(DatabaseDescriptor.getIndexInterval(),
            (int) SSTableReader.getApproximateKeyCount(sstables));
    if (logger.isDebugEnabled())
        logger.debug("Expected bloom filter size : " + expectedBloomFilterSize);

    SSTableWriter writer;
    CompactionController controller = new CompactionController(cfs, sstables, major, gcBefore, false);
    CompactionIterator ci = new CompactionIterator(type, sstables, controller); // retain a handle so we can call close()
    Iterator<AbstractCompactedRow> nni = new FilterIterator(ci, PredicateUtils.notNullPredicate());
    Map<DecoratedKey, Long> cachedKeys = new HashMap<DecoratedKey, Long>();

    executor.beginCompaction(ci);
    try {
        if (!nni.hasNext()) {
            // don't mark compacted in the finally block, since if there _is_ nondeleted data,
            // we need to sync it (via closeAndOpen) first, so there is no period during which
            // a crash could cause data loss.
            cfs.markCompacted(sstables);
            return 0;
        }

        writer = cfs.createCompactionWriter(expectedBloomFilterSize, compactionFileLocation, sstables);
        while (nni.hasNext()) {
            AbstractCompactedRow row = nni.next();
            long position = writer.append(row);
            totalkeysWritten++;

            if (DatabaseDescriptor.getPreheatKeyCache()) {
                for (SSTableReader sstable : sstables) {
                    if (sstable.getCachedPosition(row.key) != null) {
                        cachedKeys.put(row.key, position);
                        break;
                    }
                }
            }
        }
    } finally {
        ci.close();
        executor.finishCompaction(ci);
    }

    SSTableReader ssTable = writer.closeAndOpenReader(getMaxDataAge(sstables));
    cfs.replaceCompactedSSTables(sstables, Arrays.asList(ssTable));
    for (Entry<DecoratedKey, Long> entry : cachedKeys.entrySet()) // empty if preheat is off
        ssTable.cacheKey(entry.getKey(), entry.getValue());
    submitMinorIfNeeded(cfs);

    long dTime = System.currentTimeMillis() - startTime;
    long startsize = SSTable.getTotalBytes(sstables);
    long endsize = ssTable.length();
    double ratio = (double) endsize / (double) startsize;
    logger.info(
            String.format("Compacted to %s.  %,d to %,d (~%d%% of original) bytes for %,d keys.  Time: %,dms.",
                    writer.getFilename(), startsize, endsize, (int) (ratio * 100), totalkeysWritten, dTime));
    return sstables.size();
}

From source file:org.apache.cassandra.db.CompactionManager.java

/**
 * Performs a readonly "compaction" of all sstables in order to validate complete rows,
 * but without writing the merge result//w ww . j a va2 s  . co m
 */
private void doValidationCompaction(ColumnFamilyStore cfs, AntiEntropyService.Validator validator)
        throws IOException {
    // flush first so everyone is validating data that is as similar as possible
    try {
        StorageService.instance.forceTableFlush(cfs.table.name, cfs.getColumnFamilyName());
    } catch (ExecutionException e) {
        throw new IOException(e);
    } catch (InterruptedException e) {
        throw new AssertionError(e);
    }

    CompactionIterator ci = new ValidationCompactionIterator(cfs, validator.request.range);
    executor.beginCompaction(ci);
    try {
        Iterator<AbstractCompactedRow> nni = new FilterIterator(ci, PredicateUtils.notNullPredicate());

        // validate the CF as we iterate over it
        validator.prepare(cfs);
        while (nni.hasNext()) {
            AbstractCompactedRow row = nni.next();
            validator.add(row);
        }
        validator.complete();
    } finally {
        ci.close();
        executor.finishCompaction(ci);
    }
}

From source file:org.apache.jackrabbit.core.state.XAItemStateManager.java

/**
 * Takes an iterator over {@link ItemState}s and returns a new iterator that
 * filters out all but REFERENCE {@link PropertyState}s.
 *
 * @param itemStates item state source iterator.
 * @return iterator over reference property states.
 *//*from   w w  w .  j  ava2s . c  om*/
private Iterable<PropertyState> filterReferenceProperties(final Iterable<ItemState> itemStates) {
    return new Iterable<PropertyState>() {
        @SuppressWarnings("unchecked")
        public Iterator<PropertyState> iterator() {
            return (Iterator<PropertyState>) new FilterIterator(itemStates.iterator(), new Predicate() {
                public boolean evaluate(Object object) {
                    ItemState state = (ItemState) object;
                    if (!state.isNode()) {
                        PropertyState prop = (PropertyState) state;
                        return prop.getType() == PropertyType.REFERENCE;
                    }
                    return false;
                }
            });
        }
    };
}

From source file:org.apache.jackrabbit.spi.commons.iterator.Iterators.java

/**
 * Returns an iterator with elements from an original <code>iterator</code> where the
 * given <code>predicate</code> matches removed.
 *
 * @param <T>/*  w w w. j av a2s  . c o  m*/
 * @param iterator
 * @param predicate
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> Iterator<T> filterIterator(Iterator<? extends T> iterator,
        final Predicate<? super T> predicate) {

    return new FilterIterator(iterator, new org.apache.commons.collections.Predicate() {
        public boolean evaluate(Object object) {
            return predicate.evaluate((T) object);
        }
    });
}