Example usage for org.apache.commons.collections IteratorUtils singletonIterator

List of usage examples for org.apache.commons.collections IteratorUtils singletonIterator

Introduction

In this page you can find the example usage for org.apache.commons.collections IteratorUtils singletonIterator.

Prototype

public static ResettableIterator singletonIterator(Object object) 

Source Link

Document

Gets a singleton iterator.

Usage

From source file:com.bigdata.dastor.db.filter.SliceQueryFilter.java

public SuperColumn filterSuperColumn(SuperColumn superColumn, int gcBefore) {
    // we clone shallow, then add, under the theory that generally we're interested in a relatively small number of subcolumns.
    // this may be a poor assumption.
    SuperColumn scFiltered = superColumn.cloneMeShallow();
    Iterator<IColumn> subcolumns;
    if (reversed) {
        List<IColumn> columnsAsList = new ArrayList<IColumn>(superColumn.getSubColumns());
        subcolumns = new ReverseListIterator(columnsAsList);
    } else {//from   w w w.  jav  a2 s . com
        subcolumns = superColumn.getSubColumns().iterator();
    }

    // iterate until we get to the "real" start column
    Comparator<byte[]> comparator = reversed ? superColumn.getComparator().getReverseComparator()
            : superColumn.getComparator();
    while (subcolumns.hasNext()) {
        IColumn column = subcolumns.next();
        if (comparator.compare(column.name(), start) >= 0) {
            subcolumns = IteratorUtils.chainedIterator(IteratorUtils.singletonIterator(column), subcolumns);
            break;
        }
    }
    // subcolumns is either empty now, or has been redefined in the loop above.  either is ok.
    collectReducedColumns(scFiltered, subcolumns, gcBefore);
    return scFiltered;
}

From source file:org.apache.cassandra.db.filter.SliceQueryFilter.java

public SuperColumn filterSuperColumn(SuperColumn superColumn, int gcBefore) {
    // we clone shallow, then add, under the theory that generally we're interested in a relatively small number of subcolumns.
    // this may be a poor assumption.
    SuperColumn scFiltered = superColumn.cloneMeShallow();
    Iterator<IColumn> subcolumns;
    if (reversed) {
        List<IColumn> columnsAsList = new ArrayList<IColumn>(superColumn.getSubColumns());
        subcolumns = new ReverseListIterator(columnsAsList);
    } else {/* www.  j  a v a2s  .  c om*/
        subcolumns = superColumn.getSubColumns().iterator();
    }

    // iterate until we get to the "real" start column
    Comparator<ByteBuffer> comparator = reversed ? superColumn.getComparator().reverseComparator
            : superColumn.getComparator();
    while (subcolumns.hasNext()) {
        IColumn column = subcolumns.next();
        if (comparator.compare(column.name(), start) >= 0) {
            subcolumns = IteratorUtils.chainedIterator(IteratorUtils.singletonIterator(column), subcolumns);
            break;
        }
    }
    // subcolumns is either empty now, or has been redefined in the loop above.  either is ok.
    collectReducedColumns(scFiltered, subcolumns, gcBefore);
    return scFiltered;
}

From source file:org.apache.cayenne.access.jdbc.SQLTemplateAction.java

private void runWithNamedParametersBatch(Connection connection, OperationObserver callback, String template,
        Collection<Number> counts, boolean loggable) throws Exception {

    int size = query.parametersSize();

    // zero size indicates a one-shot query with no parameters
    // so fake a single entry batch...
    int batchSize = (size > 0) ? size : 1;

    // for now supporting deprecated batch parameters...
    @SuppressWarnings("unchecked")
    Iterator<Map<String, ?>> it = (size > 0) ? query.parametersIterator()
            : IteratorUtils.singletonIterator(Collections.emptyMap());
    for (int i = 0; i < batchSize; i++) {
        Map<String, ?> nextParameters = it.next();

        SQLStatement compiled = dataNode.getSqlTemplateProcessor().processTemplate(template, nextParameters);

        if (loggable) {
            dataNode.getJdbcEventLogger().logQuery(compiled.getSql(), compiled.getBindings(), 0);
        }//  w  w w .j a  v a  2s .com

        execute(connection, callback, compiled, counts);
    }

}

From source file:org.apache.cayenne.map.ObjAttribute.java

@SuppressWarnings("unchecked")
public Iterator<CayenneMapEntry> getDbPathIterator(ObjEntity entity) {
    if (dbAttributePath == null) {
        return IteratorUtils.EMPTY_ITERATOR;
    }/*from   w  ww .  ja  v  a2  s  .  co  m*/

    if (entity == null) {
        return IteratorUtils.EMPTY_ITERATOR;
    }

    DbEntity dbEnt = entity.getDbEntity();
    if (dbEnt == null) {
        return IteratorUtils.EMPTY_ITERATOR;
    }

    int lastPartStart = dbAttributePath.lastIndexOf('.');
    if (lastPartStart < 0) {
        DbAttribute attribute = dbEnt.getAttribute(dbAttributePath);
        if (attribute == null) {
            return IteratorUtils.EMPTY_ITERATOR;
        }
        return IteratorUtils.singletonIterator(attribute);
    }

    return dbEnt.resolvePathComponents(dbAttributePath);
}

From source file:org.objectstyle.cayenne.access.jdbc.SQLTemplateExecutionPlan.java

/**
 * Runs a SQLTemplate query as an update.
 *//* ww  w. j  a v  a2 s  .  com*/
public void execute(Connection connection, SQLTemplate query, OperationObserver observer)
        throws SQLException, Exception {

    SQLTemplateProcessor templateProcessor = new SQLTemplateProcessor();
    String template = query.getTemplate(adapter.getClass().getName());

    boolean loggable = QueryLogger.isLoggable(query.getLoggingLevel());
    int size = query.parametersSize();

    // zero size indicates a one-shot query with no parameters
    // so fake a single entry batch...
    int[] counts = new int[size > 0 ? size : 1];
    Iterator it = (size > 0) ? query.parametersIterator()
            : IteratorUtils.singletonIterator(Collections.EMPTY_MAP);
    for (int i = 0; i < counts.length; i++) {
        Map nextParameters = (Map) it.next();

        SQLStatement compiled = templateProcessor.processTemplate(template, nextParameters);

        if (loggable) {
            QueryLogger.logQuery(query.getLoggingLevel(), compiled.getSql(),
                    Arrays.asList(compiled.getBindings()));
        }

        // TODO: we may cache prep statements for this loop, using merged string as a key
        // since it is very likely that it will be the same for multiple parameter sets...
        PreparedStatement statement = connection.prepareStatement(compiled.getSql());
        try {
            bind(statement, compiled.getBindings());
            counts[i] = statement.executeUpdate();
            QueryLogger.logUpdateCount(query.getLoggingLevel(), counts[i]);
        } finally {
            statement.close();
        }
    }

    observer.nextBatchCount(query, counts);
}

From source file:org.objectstyle.cayenne.access.jdbc.SQLTemplateSelectExecutionPlan.java

/**
 * Runs a SQLTemplate query as a select.
 *//*w  ww . j a  v a 2  s .co  m*/
public void execute(Connection connection, SQLTemplate query, OperationObserver observer)
        throws SQLException, Exception {

    SQLTemplateProcessor templateProcessor = new SQLTemplateProcessor();
    String template = query.getTemplate(adapter.getClass().getName());
    boolean loggable = QueryLogger.isLoggable(query.getLoggingLevel());

    int size = query.parametersSize();

    // zero size indicates a one-shot query with no parameters
    // so fake a single entry batch...
    Iterator it = (size > 0) ? query.parametersIterator()
            : IteratorUtils.singletonIterator(Collections.EMPTY_MAP);
    while (it.hasNext()) {
        Map nextParameters = (Map) it.next();

        SQLSelectStatement compiled = templateProcessor.processSelectTemplate(template, nextParameters);

        if (loggable) {
            QueryLogger.logQuery(query.getLoggingLevel(), compiled.getSql(),
                    Arrays.asList(compiled.getBindings()));
        }

        long t1 = System.currentTimeMillis();

        // TODO: we may cache prep statements for this loop, using merged string as a key
        // since it is very likely that it will be the same for multiple parameter sets...
        PreparedStatement statement = connection.prepareStatement(compiled.getSql());

        bind(statement, compiled.getBindings());
        ResultSet rs = statement.executeQuery();

        ResultDescriptor descriptor = (compiled.getResultColumns().length > 0)
                ? ResultDescriptor.createDescriptor(compiled.getResultColumns(), adapter.getExtendedTypes())
                : ResultDescriptor.createDescriptor(rs, adapter.getExtendedTypes());

        DefaultResultIterator result = new DefaultResultIterator(connection, statement, rs, descriptor,
                query.getFetchLimit());

        if (!observer.isIteratedResult()) {
            // note that we don't need to close ResultIterator
            // since "dataRows" will do it internally
            List resultRows = result.dataRows(true);
            QueryLogger.logSelectCount(query.getLoggingLevel(), resultRows.size(),
                    System.currentTimeMillis() - t1);

            observer.nextDataRows(query, resultRows);
        } else {
            try {
                result.setClosingConnection(true);
                observer.nextDataRows(query, result);
            } catch (Exception ex) {
                result.close();
                throw ex;
            }
        }
    }
}

From source file:org.objectstyle.cayenne.map.ObjAttribute.java

public Iterator getDbPathIterator() {
    if (dbAttributePath == null) {
        return IteratorUtils.EMPTY_ITERATOR;
    }/* w w w. ja  v a 2  s  .  co  m*/

    ObjEntity ent = (ObjEntity) getEntity();
    if (ent == null) {
        return IteratorUtils.EMPTY_ITERATOR;
    }

    DbEntity dbEnt = ent.getDbEntity();
    if (dbEnt == null) {
        return IteratorUtils.EMPTY_ITERATOR;
    }

    int lastPartStart = dbAttributePath.lastIndexOf('.');
    if (lastPartStart < 0) {
        Attribute attribute = dbEnt.getAttribute(dbAttributePath);
        if (attribute == null) {
            return IteratorUtils.EMPTY_ITERATOR;
        }
        return IteratorUtils.singletonIterator(attribute);
    }

    return dbEnt.resolvePathComponents(dbAttributePath);
}