Example usage for java.util BitSet set

List of usage examples for java.util BitSet set

Introduction

In this page you can find the example usage for java.util BitSet set.

Prototype

public void set(int bitIndex) 

Source Link

Document

Sets the bit at the specified index to true .

Usage

From source file:org.apache.openjpa.kernel.StateManagerImpl.java

/**
 * Internal version of {@link OpenJPAStateManager#getUnloaded} that avoids
 * creating an empty bit set by returning null when there are no unloaded
 * fields./*from  ww w  .ja v  a  2 s . com*/
 */
private BitSet getUnloadedInternal(FetchConfiguration fetch, int mode, BitSet exclude) {
    if (exclude == StoreContext.EXCLUDE_ALL)
        return null;

    BitSet fields = null;
    FieldMetaData[] fmds = _meta.getFields();
    boolean load;
    for (int i = 0; i < fmds.length; i++) {
        if (_loaded.get(i) || (exclude != null && exclude.get(i)))
            continue;

        switch (mode) {
        case LOAD_SERIALIZE:
            load = !fmds[i].isTransient();
            break;
        case LOAD_FGS:
            load = fetch == null || fetch.requiresFetch(fmds[i]) != FetchConfiguration.FETCH_NONE;
            break;
        default: // LOAD_ALL
            load = true;
        }

        if (load) {
            if (fields == null)
                fields = new BitSet(fmds.length);
            fields.set(i);
        }
    }
    return fields;
}

From source file:org.apache.hadoop.hive.metastore.txn.TxnHandler.java

@Override
@RetrySemantics.ReadOnly//from   w  ww.  jav a 2s.  co m
public GetOpenTxnsResponse getOpenTxns() throws MetaException {
    try {
        // We need to figure out the current transaction number and the list of
        // open transactions.  To avoid needing a transaction on the underlying
        // database we'll look at the current transaction number first.  If it
        // subsequently shows up in the open list that's ok.
        Connection dbConn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            /**
             * This runs at READ_COMMITTED for exactly the same reason as {@link #getOpenTxnsInfo()}
             */
            dbConn = getDbConn(Connection.TRANSACTION_READ_COMMITTED);
            stmt = dbConn.createStatement();
            String s = "select ntxn_next - 1 from NEXT_TXN_ID";
            LOG.debug("Going to execute query <" + s + ">");
            rs = stmt.executeQuery(s);
            if (!rs.next()) {
                throw new MetaException(
                        "Transaction tables not properly " + "initialized, no record found in next_txn_id");
            }
            long hwm = rs.getLong(1);
            if (rs.wasNull()) {
                throw new MetaException(
                        "Transaction tables not properly " + "initialized, null record found in next_txn_id");
            }
            close(rs);
            List<Long> openList = new ArrayList<Long>();
            //need the WHERE clause below to ensure consistent results with READ_COMMITTED
            s = "select txn_id, txn_state from TXNS where txn_id <= " + hwm + " order by txn_id";
            LOG.debug("Going to execute query<" + s + ">");
            rs = stmt.executeQuery(s);
            long minOpenTxn = Long.MAX_VALUE;
            BitSet abortedBits = new BitSet();
            while (rs.next()) {
                long txnId = rs.getLong(1);
                openList.add(txnId);
                char c = rs.getString(2).charAt(0);
                if (c == TXN_OPEN) {
                    minOpenTxn = Math.min(minOpenTxn, txnId);
                } else if (c == TXN_ABORTED) {
                    abortedBits.set(openList.size() - 1);
                }
            }
            LOG.debug("Going to rollback");
            dbConn.rollback();
            ByteBuffer byteBuffer = ByteBuffer.wrap(abortedBits.toByteArray());
            GetOpenTxnsResponse otr = new GetOpenTxnsResponse(hwm, openList, byteBuffer);
            if (minOpenTxn < Long.MAX_VALUE) {
                otr.setMin_open_txn(minOpenTxn);
            }
            return otr;
        } catch (SQLException e) {
            LOG.debug("Going to rollback");
            rollbackDBConn(dbConn);
            checkRetryable(dbConn, e, "getOpenTxns");
            throw new MetaException(
                    "Unable to select from transaction database, " + StringUtils.stringifyException(e));
        } finally {
            close(rs, stmt, dbConn);
        }
    } catch (RetryException e) {
        return getOpenTxns();
    }
}

From source file:edu.uci.ics.asterix.optimizer.rules.am.BTreeAccessMethod.java

private ILogicalOperator createSecondaryToPrimaryPlan(Mutable<ILogicalOperator> topOpRef,
        Mutable<ILogicalExpression> conditionRef, OptimizableOperatorSubTree indexSubTree,
        OptimizableOperatorSubTree probeSubTree, Index chosenIndex, AccessMethodAnalysisContext analysisCtx,
        boolean retainInput, boolean retainNull, boolean requiresBroadcast, IOptimizationContext context)
        throws AlgebricksException {
    Dataset dataset = indexSubTree.dataset;
    ARecordType recordType = indexSubTree.recordType;
    // we made sure indexSubTree has datasource scan
    AbstractDataSourceOperator dataSourceOp = (AbstractDataSourceOperator) indexSubTree.dataSourceRef
            .getValue();/* w w  w .  j a v a 2 s  .c  o  m*/
    List<Pair<Integer, Integer>> exprAndVarList = analysisCtx.indexExprsAndVars.get(chosenIndex);
    List<IOptimizableFuncExpr> matchedFuncExprs = analysisCtx.matchedFuncExprs;
    int numSecondaryKeys = analysisCtx.indexNumMatchedKeys.get(chosenIndex);
    // List of function expressions that will be replaced by the secondary-index search.
    // These func exprs will be removed from the select condition at the very end of this method.
    Set<ILogicalExpression> replacedFuncExprs = new HashSet<ILogicalExpression>();

    // Info on high and low keys for the BTree search predicate.
    ILogicalExpression[] lowKeyExprs = new ILogicalExpression[numSecondaryKeys];
    ILogicalExpression[] highKeyExprs = new ILogicalExpression[numSecondaryKeys];
    LimitType[] lowKeyLimits = new LimitType[numSecondaryKeys];
    LimitType[] highKeyLimits = new LimitType[numSecondaryKeys];
    boolean[] lowKeyInclusive = new boolean[numSecondaryKeys];
    boolean[] highKeyInclusive = new boolean[numSecondaryKeys];

    // TODO: For now we don't do any sophisticated analysis of the func exprs to come up with "the best" range predicate.
    // If we can't figure out how to integrate a certain funcExpr into the current predicate, we just bail by setting this flag.
    boolean couldntFigureOut = false;
    boolean doneWithExprs = false;
    boolean isEqCondition = false;
    // TODO: For now don't consider prefix searches.
    BitSet setLowKeys = new BitSet(numSecondaryKeys);
    BitSet setHighKeys = new BitSet(numSecondaryKeys);
    // Go through the func exprs listed as optimizable by the chosen index,
    // and formulate a range predicate on the secondary-index keys.

    // checks whether a type casting happened from a real (FLOAT, DOUBLE) value to an INT value
    // since we have a round issues when dealing with LT(<) OR GT(>) operator.
    boolean realTypeConvertedToIntegerType = false;

    for (Pair<Integer, Integer> exprIndex : exprAndVarList) {
        // Position of the field of matchedFuncExprs.get(exprIndex) in the chosen index's indexed exprs.
        IOptimizableFuncExpr optFuncExpr = matchedFuncExprs.get(exprIndex.first);
        int keyPos = indexOf(optFuncExpr.getFieldName(0), chosenIndex.getKeyFieldNames());
        if (keyPos < 0) {
            if (optFuncExpr.getNumLogicalVars() > 1) {
                // If we are optimizing a join, the matching field may be the second field name.
                keyPos = indexOf(optFuncExpr.getFieldName(1), chosenIndex.getKeyFieldNames());
            }
        }
        if (keyPos < 0) {
            throw new AlgebricksException(
                    "Could not match optimizable function expression to any index field name.");
        }
        Pair<ILogicalExpression, Boolean> returnedSearchKeyExpr = AccessMethodUtils
                .createSearchKeyExpr(optFuncExpr, indexSubTree, probeSubTree);
        ILogicalExpression searchKeyExpr = returnedSearchKeyExpr.first;
        realTypeConvertedToIntegerType = returnedSearchKeyExpr.second;

        LimitType limit = getLimitType(optFuncExpr, probeSubTree);

        // If a DOUBLE or FLOAT constant is converted to an INT type value,
        // we need to check a corner case where two real values are located between an INT value.
        // For example, for the following query,
        //
        // for $emp in dataset empDataset
        // where $emp.age > double("2.3") and $emp.age < double("3.3")
        // return $emp.id;
        //
        // It should generate a result if there is a tuple that satisfies the condition, which is 3,
        // however, it does not generate the desired result since finding candidates
        // fail after truncating the fraction part (there is no INT whose value is greater than 2 and less than 3.)
        //
        // Therefore, we convert LT(<) to LE(<=) and GT(>) to GE(>=) to find candidates.
        // This does not change the result of an actual comparison since this conversion is only applied
        // for finding candidates from an index.
        //
        if (realTypeConvertedToIntegerType) {
            if (limit == LimitType.HIGH_EXCLUSIVE) {
                limit = LimitType.HIGH_INCLUSIVE;
            } else if (limit == LimitType.LOW_EXCLUSIVE) {
                limit = LimitType.LOW_INCLUSIVE;
            }
        }

        switch (limit) {
        case EQUAL: {
            if (lowKeyLimits[keyPos] == null && highKeyLimits[keyPos] == null) {
                lowKeyLimits[keyPos] = highKeyLimits[keyPos] = limit;
                lowKeyInclusive[keyPos] = highKeyInclusive[keyPos] = true;
                lowKeyExprs[keyPos] = highKeyExprs[keyPos] = searchKeyExpr;
                setLowKeys.set(keyPos);
                setHighKeys.set(keyPos);
                isEqCondition = true;
            } else {
                // Has already been set to the identical values. When optimizing join we may encounter the same optimizable expression twice
                // (once from analyzing each side of the join)
                if (lowKeyLimits[keyPos] == limit && lowKeyInclusive[keyPos] == true
                        && lowKeyExprs[keyPos].equals(searchKeyExpr) && highKeyLimits[keyPos] == limit
                        && highKeyInclusive[keyPos] == true && highKeyExprs[keyPos].equals(searchKeyExpr)) {
                    isEqCondition = true;
                    break;
                }
                couldntFigureOut = true;
            }
            // TODO: For now don't consider prefix searches.
            // If high and low keys are set, we exit for now.
            if (setLowKeys.cardinality() == numSecondaryKeys && setHighKeys.cardinality() == numSecondaryKeys) {
                doneWithExprs = true;
            }
            break;
        }
        case HIGH_EXCLUSIVE: {
            if (highKeyLimits[keyPos] == null || (highKeyLimits[keyPos] != null && highKeyInclusive[keyPos])) {
                highKeyLimits[keyPos] = limit;
                highKeyExprs[keyPos] = searchKeyExpr;
                highKeyInclusive[keyPos] = false;
            } else {
                // Has already been set to the identical values. When optimizing join we may encounter the same optimizable expression twice
                // (once from analyzing each side of the join)
                if (highKeyLimits[keyPos] == limit && highKeyInclusive[keyPos] == false
                        && highKeyExprs[keyPos].equals(searchKeyExpr)) {
                    break;
                }
                couldntFigureOut = true;
                doneWithExprs = true;
            }
            break;
        }
        case HIGH_INCLUSIVE: {
            if (highKeyLimits[keyPos] == null) {
                highKeyLimits[keyPos] = limit;
                highKeyExprs[keyPos] = searchKeyExpr;
                highKeyInclusive[keyPos] = true;
            } else {
                // Has already been set to the identical values. When optimizing join we may encounter the same optimizable expression twice
                // (once from analyzing each side of the join)
                if (highKeyLimits[keyPos] == limit && highKeyInclusive[keyPos] == true
                        && highKeyExprs[keyPos].equals(searchKeyExpr)) {
                    break;
                }
                couldntFigureOut = true;
                doneWithExprs = true;
            }
            break;
        }
        case LOW_EXCLUSIVE: {
            if (lowKeyLimits[keyPos] == null || (lowKeyLimits[keyPos] != null && lowKeyInclusive[keyPos])) {
                lowKeyLimits[keyPos] = limit;
                lowKeyExprs[keyPos] = searchKeyExpr;
                lowKeyInclusive[keyPos] = false;
            } else {
                // Has already been set to the identical values. When optimizing join we may encounter the same optimizable expression twice
                // (once from analyzing each side of the join)
                if (lowKeyLimits[keyPos] == limit && lowKeyInclusive[keyPos] == false
                        && lowKeyExprs[keyPos].equals(searchKeyExpr)) {
                    break;
                }
                couldntFigureOut = true;
                doneWithExprs = true;
            }
            break;
        }
        case LOW_INCLUSIVE: {
            if (lowKeyLimits[keyPos] == null) {
                lowKeyLimits[keyPos] = limit;
                lowKeyExprs[keyPos] = searchKeyExpr;
                lowKeyInclusive[keyPos] = true;
            } else {
                // Has already been set to the identical values. When optimizing join we may encounter the same optimizable expression twice
                // (once from analyzing each side of the join)
                if (lowKeyLimits[keyPos] == limit && lowKeyInclusive[keyPos] == true
                        && lowKeyExprs[keyPos].equals(searchKeyExpr)) {
                    break;
                }
                couldntFigureOut = true;
                doneWithExprs = true;
            }
            break;
        }
        default: {
            throw new IllegalStateException();
        }
        }
        if (!couldntFigureOut) {
            // Remember to remove this funcExpr later.
            replacedFuncExprs.add(matchedFuncExprs.get(exprIndex.first).getFuncExpr());
        }
        if (doneWithExprs) {
            break;
        }
    }
    if (couldntFigureOut) {
        return null;
    }

    // If the select condition contains mixed open/closed intervals on multiple keys, then we make all intervals closed to obtain a superset of answers and leave the original selection in place.
    boolean primaryIndexPostProccessingIsNeeded = false;
    for (int i = 1; i < numSecondaryKeys; ++i) {
        if (lowKeyInclusive[i] != lowKeyInclusive[0]) {
            Arrays.fill(lowKeyInclusive, true);
            primaryIndexPostProccessingIsNeeded = true;
            break;
        }
    }
    for (int i = 1; i < numSecondaryKeys; ++i) {
        if (highKeyInclusive[i] != highKeyInclusive[0]) {
            Arrays.fill(highKeyInclusive, true);
            primaryIndexPostProccessingIsNeeded = true;
            break;
        }
    }

    // determine cases when prefix search could be applied
    for (int i = 1; i < lowKeyExprs.length; i++) {
        if (lowKeyLimits[0] == null && lowKeyLimits[i] != null
                || lowKeyLimits[0] != null && lowKeyLimits[i] == null
                || highKeyLimits[0] == null && highKeyLimits[i] != null
                || highKeyLimits[0] != null && highKeyLimits[i] == null) {
            numSecondaryKeys--;
            primaryIndexPostProccessingIsNeeded = true;
        }
    }
    if (lowKeyLimits[0] == null) {
        lowKeyInclusive[0] = true;
    }
    if (highKeyLimits[0] == null) {
        highKeyInclusive[0] = true;
    }

    // Here we generate vars and funcs for assigning the secondary-index keys to be fed into the secondary-index search.
    // List of variables for the assign.
    ArrayList<LogicalVariable> keyVarList = new ArrayList<LogicalVariable>();
    // List of variables and expressions for the assign.
    ArrayList<LogicalVariable> assignKeyVarList = new ArrayList<LogicalVariable>();
    ArrayList<Mutable<ILogicalExpression>> assignKeyExprList = new ArrayList<Mutable<ILogicalExpression>>();
    int numLowKeys = createKeyVarsAndExprs(numSecondaryKeys, lowKeyLimits, lowKeyExprs, assignKeyVarList,
            assignKeyExprList, keyVarList, context);
    int numHighKeys = createKeyVarsAndExprs(numSecondaryKeys, highKeyLimits, highKeyExprs, assignKeyVarList,
            assignKeyExprList, keyVarList, context);

    BTreeJobGenParams jobGenParams = new BTreeJobGenParams(chosenIndex.getIndexName(), IndexType.BTREE,
            dataset.getDataverseName(), dataset.getDatasetName(), retainInput, retainNull, requiresBroadcast);
    jobGenParams.setLowKeyInclusive(lowKeyInclusive[0]);
    jobGenParams.setHighKeyInclusive(highKeyInclusive[0]);
    jobGenParams.setIsEqCondition(isEqCondition);
    jobGenParams.setLowKeyVarList(keyVarList, 0, numLowKeys);
    jobGenParams.setHighKeyVarList(keyVarList, numLowKeys, numHighKeys);

    ILogicalOperator inputOp = null;
    if (!assignKeyVarList.isEmpty()) {
        // Assign operator that sets the constant secondary-index search-key fields if necessary.
        AssignOperator assignConstantSearchKeys = new AssignOperator(assignKeyVarList, assignKeyExprList);
        // Input to this assign is the EmptyTupleSource (which the dataSourceScan also must have had as input).
        assignConstantSearchKeys.getInputs().add(dataSourceOp.getInputs().get(0));
        assignConstantSearchKeys.setExecutionMode(dataSourceOp.getExecutionMode());
        inputOp = assignConstantSearchKeys;
    } else {
        // All index search keys are variables.
        inputOp = probeSubTree.root;
    }

    UnnestMapOperator secondaryIndexUnnestOp = AccessMethodUtils.createSecondaryIndexUnnestMap(dataset,
            recordType, chosenIndex, inputOp, jobGenParams, context, false, retainInput);

    // Generate the rest of the upstream plan which feeds the search results into the primary index.
    UnnestMapOperator primaryIndexUnnestOp = null;
    boolean isPrimaryIndex = chosenIndex.isPrimaryIndex();
    if (dataset.getDatasetType() == DatasetType.EXTERNAL) {
        // External dataset
        ExternalDataLookupOperator externalDataAccessOp = AccessMethodUtils.createExternalDataLookupUnnestMap(
                dataSourceOp, dataset, recordType, secondaryIndexUnnestOp, context, chosenIndex, retainInput,
                retainNull);
        indexSubTree.dataSourceRef.setValue(externalDataAccessOp);
        return externalDataAccessOp;
    } else if (!isPrimaryIndex) {
        primaryIndexUnnestOp = AccessMethodUtils.createPrimaryIndexUnnestMap(dataSourceOp, dataset, recordType,
                secondaryIndexUnnestOp, context, true, retainInput, retainNull, false);

        // Replace the datasource scan with the new plan rooted at
        // primaryIndexUnnestMap.
        indexSubTree.dataSourceRef.setValue(primaryIndexUnnestOp);
    } else {
        List<Object> primaryIndexOutputTypes = new ArrayList<Object>();
        try {
            AccessMethodUtils.appendPrimaryIndexTypes(dataset, recordType, primaryIndexOutputTypes);
        } catch (IOException e) {
            throw new AlgebricksException(e);
        }
        List<LogicalVariable> scanVariables = dataSourceOp.getVariables();
        primaryIndexUnnestOp = new UnnestMapOperator(scanVariables, secondaryIndexUnnestOp.getExpressionRef(),
                primaryIndexOutputTypes, retainInput);
        primaryIndexUnnestOp.getInputs().add(new MutableObject<ILogicalOperator>(inputOp));

        if (!primaryIndexPostProccessingIsNeeded) {
            List<Mutable<ILogicalExpression>> remainingFuncExprs = new ArrayList<Mutable<ILogicalExpression>>();
            getNewConditionExprs(conditionRef, replacedFuncExprs, remainingFuncExprs);
            // Generate new condition.
            if (!remainingFuncExprs.isEmpty()) {
                ILogicalExpression pulledCond = createSelectCondition(remainingFuncExprs);
                conditionRef.setValue(pulledCond);
            } else {
                conditionRef.setValue(null);
            }
        }

        // Adds equivalence classes --- one equivalent class between a primary key
        // variable and a record field-access expression.
        EquivalenceClassUtils.addEquivalenceClassesForPrimaryIndexAccess(primaryIndexUnnestOp, scanVariables,
                recordType, dataset, context);
    }

    return primaryIndexUnnestOp;
}

From source file:org.unitime.timetable.solver.TimetableDatabaseLoader.java

public void loadInstructorAvailability(RoomAvailabilityInterface availability, Date[] startEnd) {
    iProgress.setPhase("Loading instructor availability...", getModel().getInstructorConstraints().size());
    int firstDOY = iSession.getDayOfYear(1, iSession.getPatternStartMonth());
    int lastDOY = iSession.getDayOfYear(0, iSession.getPatternEndMonth() + 1);
    int size = lastDOY - firstDOY;
    Calendar c = Calendar.getInstance(Locale.US);
    Formats.Format<Date> df = Formats.getDateFormat(Formats.Pattern.DATE_PATTERN);
    int sessionYear = iSession.getSessionStartYear();
    for (InstructorConstraint instructor : getModel().getInstructorConstraints()) {
        iProgress.incProgress();//  w  ww.j  a  va 2  s  .c  o  m
        Collection<TimeBlock> times = getInstructorAvailability(availability, instructor, startEnd[0],
                startEnd[1]);
        if (times == null)
            continue;
        for (TimeBlock time : times) {
            iProgress.debug(instructor.getName() + " not available due to " + time);
            int dayCode = 0;
            c.setTime(time.getStartTime());
            int m = c.get(Calendar.MONTH);
            int d = c.get(Calendar.DAY_OF_MONTH);
            if (c.get(Calendar.YEAR) < sessionYear)
                m -= (12 * (sessionYear - c.get(Calendar.YEAR)));
            if (c.get(Calendar.YEAR) > sessionYear)
                m += (12 * (c.get(Calendar.YEAR) - sessionYear));
            BitSet weekCode = new BitSet(size);
            int offset = iSession.getDayOfYear(d, m) - firstDOY;
            if (offset < 0 || offset >= size)
                continue;
            weekCode.set(offset);
            switch (c.get(Calendar.DAY_OF_WEEK)) {
            case Calendar.MONDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_MON];
                break;
            case Calendar.TUESDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_TUE];
                break;
            case Calendar.WEDNESDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_WED];
                break;
            case Calendar.THURSDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_THU];
                break;
            case Calendar.FRIDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_FRI];
                break;
            case Calendar.SATURDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_SAT];
                break;
            case Calendar.SUNDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_SUN];
                break;
            }
            int startSlot = (c.get(Calendar.HOUR_OF_DAY) * 60 + c.get(Calendar.MINUTE)
                    - Constants.FIRST_SLOT_TIME_MIN) / Constants.SLOT_LENGTH_MIN;
            c.setTime(time.getEndTime());
            int endSlot = (c.get(Calendar.HOUR_OF_DAY) * 60 + c.get(Calendar.MINUTE)
                    - Constants.FIRST_SLOT_TIME_MIN) / Constants.SLOT_LENGTH_MIN;
            if (endSlot == 0 && c.get(Calendar.DAY_OF_MONTH) != d)
                endSlot = 288; // next day midnight
            int length = endSlot - startSlot;
            if (length <= 0)
                continue;
            TimeLocation timeLocation = new TimeLocation(dayCode, startSlot, length, 0, 0, null,
                    df.format(time.getStartTime()), weekCode, 0);
            List<TimeLocation> timeLocations = new ArrayList<TimeLocation>(1);
            timeLocations.add(timeLocation);
            Lecture lecture = new Lecture(new Long(--iFakeLectureId), null, null, time.getEventName(),
                    timeLocations, new ArrayList<RoomLocation>(), 0,
                    new Placement(null, timeLocation, (RoomLocation) null), 0, 0, 1.0);
            lecture.setNote(time.getEventType());
            Placement p = (Placement) lecture.getInitialAssignment();
            lecture.setBestAssignment(p, 0);
            lecture.setCommitted(true);
            instructor.setNotAvailable(p);
            getModel().addVariable(p.variable());
        }
    }
}

From source file:org.unitime.timetable.solver.TimetableDatabaseLoader.java

public void loadRoomAvailability(RoomAvailabilityInterface availability, Date[] startEnd) {
    iProgress.setPhase("Loading room availability...", iRooms.size());
    int firstDOY = iSession.getDayOfYear(1, iSession.getPatternStartMonth());
    int lastDOY = iSession.getDayOfYear(0, iSession.getPatternEndMonth() + 1);
    int size = lastDOY - firstDOY;
    Calendar c = Calendar.getInstance(Locale.US);
    Formats.Format<Date> df = Formats.getDateFormat(Formats.Pattern.DATE_PATTERN);
    int sessionYear = iSession.getSessionStartYear();
    for (Enumeration e = iRooms.elements(); e.hasMoreElements();) {
        RoomConstraint room = (RoomConstraint) e.nextElement();
        iProgress.incProgress();//from   w  w  w  .j a  v  a2 s  .c  om
        if (!room.getConstraint())
            continue;
        Collection<TimeBlock> times = getRoomAvailability(availability, room, startEnd[0], startEnd[1]);
        if (times == null)
            continue;
        for (TimeBlock time : times) {
            iProgress.debug(room.getName() + " not available due to " + time);
            int dayCode = 0;
            c.setTime(time.getStartTime());
            int m = c.get(Calendar.MONTH);
            int d = c.get(Calendar.DAY_OF_MONTH);
            if (c.get(Calendar.YEAR) < sessionYear)
                m -= (12 * (sessionYear - c.get(Calendar.YEAR)));
            if (c.get(Calendar.YEAR) > sessionYear)
                m += (12 * (c.get(Calendar.YEAR) - sessionYear));
            BitSet weekCode = new BitSet(size);
            int offset = iSession.getDayOfYear(d, m) - firstDOY;
            if (offset < 0 || offset >= size)
                continue;
            weekCode.set(offset);
            switch (c.get(Calendar.DAY_OF_WEEK)) {
            case Calendar.MONDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_MON];
                break;
            case Calendar.TUESDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_TUE];
                break;
            case Calendar.WEDNESDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_WED];
                break;
            case Calendar.THURSDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_THU];
                break;
            case Calendar.FRIDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_FRI];
                break;
            case Calendar.SATURDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_SAT];
                break;
            case Calendar.SUNDAY:
                dayCode = Constants.DAY_CODES[Constants.DAY_SUN];
                break;
            }
            int startSlot = (c.get(Calendar.HOUR_OF_DAY) * 60 + c.get(Calendar.MINUTE)
                    - Constants.FIRST_SLOT_TIME_MIN) / Constants.SLOT_LENGTH_MIN;
            c.setTime(time.getEndTime());
            int endSlot = (c.get(Calendar.HOUR_OF_DAY) * 60 + c.get(Calendar.MINUTE)
                    - Constants.FIRST_SLOT_TIME_MIN) / Constants.SLOT_LENGTH_MIN;
            if (endSlot == 0 && c.get(Calendar.DAY_OF_MONTH) != d)
                endSlot = 288; // next day midnight
            int length = endSlot - startSlot;
            if (length <= 0)
                continue;
            TimeLocation timeLocation = new TimeLocation(dayCode, startSlot, length, 0, 0, null,
                    df.format(time.getStartTime()), weekCode, 0);
            List<TimeLocation> timeLocations = new ArrayList<TimeLocation>(1);
            timeLocations.add(timeLocation);
            RoomLocation roomLocation = new RoomLocation(room.getResourceId(), room.getName(),
                    room.getBuildingId(), 0, room.getCapacity(), room.getPosX(), room.getPosY(),
                    room.getIgnoreTooFar(), room);
            List<RoomLocation> roomLocations = new ArrayList<RoomLocation>(1);
            roomLocations.add(roomLocation);
            Lecture lecture = new Lecture(new Long(--iFakeLectureId), null, null, time.getEventName(),
                    timeLocations, roomLocations, 1, new Placement(null, timeLocation, roomLocations), 0, 0,
                    1.0);
            lecture.setNote(time.getEventType());
            Placement p = (Placement) lecture.getInitialAssignment();
            lecture.setBestAssignment(p, 0);
            lecture.setCommitted(true);
            room.setNotAvailable(p);
            getModel().addVariable(p.variable());
        }
    }
}

From source file:org.apache.asterix.optimizer.rules.am.BTreeAccessMethod.java

@Override
public ILogicalOperator createSecondaryToPrimaryPlan(Mutable<ILogicalExpression> conditionRef,
        OptimizableOperatorSubTree indexSubTree, OptimizableOperatorSubTree probeSubTree, Index chosenIndex,
        AccessMethodAnalysisContext analysisCtx, boolean retainInput, boolean retainNull,
        boolean requiresBroadcast, IOptimizationContext context) throws AlgebricksException {
    Dataset dataset = indexSubTree.getDataset();
    ARecordType recordType = indexSubTree.getRecordType();
    ARecordType metaRecordType = indexSubTree.getMetaRecordType();
    // we made sure indexSubTree has datasource scan
    AbstractDataSourceOperator dataSourceOp = (AbstractDataSourceOperator) indexSubTree.getDataSourceRef()
            .getValue();//from   w w w.  j  a v a 2 s  .  co m
    List<Pair<Integer, Integer>> exprAndVarList = analysisCtx.indexExprsAndVars.get(chosenIndex);
    List<IOptimizableFuncExpr> matchedFuncExprs = analysisCtx.matchedFuncExprs;
    int numSecondaryKeys = analysisCtx.indexNumMatchedKeys.get(chosenIndex);
    // List of function expressions that will be replaced by the secondary-index search.
    // These func exprs will be removed from the select condition at the very end of this method.
    Set<ILogicalExpression> replacedFuncExprs = new HashSet<>();

    // Info on high and low keys for the BTree search predicate.
    ILogicalExpression[] lowKeyExprs = new ILogicalExpression[numSecondaryKeys];
    ILogicalExpression[] highKeyExprs = new ILogicalExpression[numSecondaryKeys];
    LimitType[] lowKeyLimits = new LimitType[numSecondaryKeys];
    LimitType[] highKeyLimits = new LimitType[numSecondaryKeys];
    boolean[] lowKeyInclusive = new boolean[numSecondaryKeys];
    boolean[] highKeyInclusive = new boolean[numSecondaryKeys];
    ILogicalExpression[] constantAtRuntimeExpressions = new ILogicalExpression[numSecondaryKeys];
    LogicalVariable[] constAtRuntimeExprVars = new LogicalVariable[numSecondaryKeys];

    /* TODO: For now we don't do any sophisticated analysis of the func exprs to come up with "the best" range
     * predicate. If we can't figure out how to integrate a certain funcExpr into the current predicate,
     * we just bail by setting this flag.*/
    boolean couldntFigureOut = false;
    boolean doneWithExprs = false;
    boolean isEqCondition = false;
    BitSet setLowKeys = new BitSet(numSecondaryKeys);
    BitSet setHighKeys = new BitSet(numSecondaryKeys);
    // Go through the func exprs listed as optimizable by the chosen index,
    // and formulate a range predicate on the secondary-index keys.

    // checks whether a type casting happened from a real (FLOAT, DOUBLE) value to an INT value
    // since we have a round issues when dealing with LT(<) OR GT(>) operator.
    boolean realTypeConvertedToIntegerType;

    for (Pair<Integer, Integer> exprIndex : exprAndVarList) {
        // Position of the field of matchedFuncExprs.get(exprIndex) in the chosen index's indexed exprs.
        IOptimizableFuncExpr optFuncExpr = matchedFuncExprs.get(exprIndex.first);
        int keyPos = indexOf(optFuncExpr.getFieldName(0), chosenIndex.getKeyFieldNames());
        if (keyPos < 0 && optFuncExpr.getNumLogicalVars() > 1) {
            // If we are optimizing a join, the matching field may be the second field name.
            keyPos = indexOf(optFuncExpr.getFieldName(1), chosenIndex.getKeyFieldNames());
        }
        if (keyPos < 0) {
            throw new AlgebricksException(
                    "Could not match optimizable function expression to any index field name.");
        }
        Pair<ILogicalExpression, Boolean> returnedSearchKeyExpr = AccessMethodUtils
                .createSearchKeyExpr(optFuncExpr, indexSubTree, probeSubTree);
        ILogicalExpression searchKeyExpr = returnedSearchKeyExpr.first;
        if (searchKeyExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
            constantAtRuntimeExpressions[keyPos] = searchKeyExpr;
            constAtRuntimeExprVars[keyPos] = context.newVar();
            searchKeyExpr = new VariableReferenceExpression(constAtRuntimeExprVars[keyPos]);

        }
        realTypeConvertedToIntegerType = returnedSearchKeyExpr.second;

        LimitType limit = getLimitType(optFuncExpr, probeSubTree);

        // If a DOUBLE or FLOAT constant is converted to an INT type value,
        // we need to check a corner case where two real values are located between an INT value.
        // For example, for the following query,
        //
        // for $emp in dataset empDataset
        // where $emp.age > double("2.3") and $emp.age < double("3.3")
        // return $emp.id
        //
        // It should generate a result if there is a tuple that satisfies the condition, which is 3,
        // however, it does not generate the desired result since finding candidates
        // fail after truncating the fraction part (there is no INT whose value is greater than 2 and less than 3.)
        //
        // Therefore, we convert LT(<) to LE(<=) and GT(>) to GE(>=) to find candidates.
        // This does not change the result of an actual comparison since this conversion is only applied
        // for finding candidates from an index.
        //
        if (realTypeConvertedToIntegerType) {
            if (limit == LimitType.HIGH_EXCLUSIVE) {
                limit = LimitType.HIGH_INCLUSIVE;
            } else if (limit == LimitType.LOW_EXCLUSIVE) {
                limit = LimitType.LOW_INCLUSIVE;
            }
        }

        switch (limit) {
        case EQUAL: {
            if (lowKeyLimits[keyPos] == null && highKeyLimits[keyPos] == null) {
                lowKeyLimits[keyPos] = highKeyLimits[keyPos] = limit;
                lowKeyInclusive[keyPos] = highKeyInclusive[keyPos] = true;
                lowKeyExprs[keyPos] = highKeyExprs[keyPos] = searchKeyExpr;
                setLowKeys.set(keyPos);
                setHighKeys.set(keyPos);
                isEqCondition = true;
            } else {
                // Has already been set to the identical values.
                // When optimizing join we may encounter the same optimizable expression twice
                // (once from analyzing each side of the join)
                if (lowKeyLimits[keyPos] == limit && lowKeyInclusive[keyPos] == true
                        && lowKeyExprs[keyPos].equals(searchKeyExpr) && highKeyLimits[keyPos] == limit
                        && highKeyInclusive[keyPos] == true && highKeyExprs[keyPos].equals(searchKeyExpr)) {
                    isEqCondition = true;
                    break;
                }
                couldntFigureOut = true;
            }
            // TODO: For now don't consider prefix searches.
            // If high and low keys are set, we exit for now.
            if (setLowKeys.cardinality() == numSecondaryKeys && setHighKeys.cardinality() == numSecondaryKeys) {
                doneWithExprs = true;
            }
            break;
        }
        case HIGH_EXCLUSIVE: {
            if (highKeyLimits[keyPos] == null || (highKeyLimits[keyPos] != null && highKeyInclusive[keyPos])) {
                highKeyLimits[keyPos] = limit;
                highKeyExprs[keyPos] = searchKeyExpr;
                highKeyInclusive[keyPos] = false;
            } else {
                // Has already been set to the identical values. When optimizing join we may encounter the
                // same optimizable expression twice
                // (once from analyzing each side of the join)
                if (highKeyLimits[keyPos] == limit && highKeyInclusive[keyPos] == false
                        && highKeyExprs[keyPos].equals(searchKeyExpr)) {
                    break;
                }
                couldntFigureOut = true;
                doneWithExprs = true;
            }
            break;
        }
        case HIGH_INCLUSIVE: {
            if (highKeyLimits[keyPos] == null) {
                highKeyLimits[keyPos] = limit;
                highKeyExprs[keyPos] = searchKeyExpr;
                highKeyInclusive[keyPos] = true;
            } else {
                // Has already been set to the identical values. When optimizing join we may encounter the
                // same optimizable expression twice
                // (once from analyzing each side of the join)
                if (highKeyLimits[keyPos] == limit && highKeyInclusive[keyPos] == true
                        && highKeyExprs[keyPos].equals(searchKeyExpr)) {
                    break;
                }
                couldntFigureOut = true;
                doneWithExprs = true;
            }
            break;
        }
        case LOW_EXCLUSIVE: {
            if (lowKeyLimits[keyPos] == null || (lowKeyLimits[keyPos] != null && lowKeyInclusive[keyPos])) {
                lowKeyLimits[keyPos] = limit;
                lowKeyExprs[keyPos] = searchKeyExpr;
                lowKeyInclusive[keyPos] = false;
            } else {
                // Has already been set to the identical values. When optimizing join we may encounter the
                // same optimizable expression twice
                // (once from analyzing each side of the join)
                if (lowKeyLimits[keyPos] == limit && lowKeyInclusive[keyPos] == false
                        && lowKeyExprs[keyPos].equals(searchKeyExpr)) {
                    break;
                }
                couldntFigureOut = true;
                doneWithExprs = true;
            }
            break;
        }
        case LOW_INCLUSIVE: {
            if (lowKeyLimits[keyPos] == null) {
                lowKeyLimits[keyPos] = limit;
                lowKeyExprs[keyPos] = searchKeyExpr;
                lowKeyInclusive[keyPos] = true;
            } else {
                // Has already been set to the identical values. When optimizing join we may encounter the
                // same optimizable expression twice
                // (once from analyzing each side of the join)
                if (lowKeyLimits[keyPos] == limit && lowKeyInclusive[keyPos] == true
                        && lowKeyExprs[keyPos].equals(searchKeyExpr)) {
                    break;
                }
                couldntFigureOut = true;
                doneWithExprs = true;
            }
            break;
        }
        default: {
            throw new IllegalStateException();
        }
        }
        if (!couldntFigureOut) {
            // Remember to remove this funcExpr later.
            replacedFuncExprs.add(matchedFuncExprs.get(exprIndex.first).getFuncExpr());
        }
        if (doneWithExprs) {
            break;
        }
    }
    if (couldntFigureOut) {
        return null;
    }

    // If the select condition contains mixed open/closed intervals on multiple keys, then we make all intervals
    // closed to obtain a superset of answers and leave the original selection in place.
    boolean primaryIndexPostProccessingIsNeeded = false;
    for (int i = 1; i < numSecondaryKeys; ++i) {
        if (lowKeyInclusive[i] != lowKeyInclusive[0]) {
            Arrays.fill(lowKeyInclusive, true);
            primaryIndexPostProccessingIsNeeded = true;
            break;
        }
    }
    for (int i = 1; i < numSecondaryKeys; ++i) {
        if (highKeyInclusive[i] != highKeyInclusive[0]) {
            Arrays.fill(highKeyInclusive, true);
            primaryIndexPostProccessingIsNeeded = true;
            break;
        }
    }

    // determine cases when prefix search could be applied
    for (int i = 1; i < lowKeyExprs.length; i++) {
        if (lowKeyLimits[0] == null && lowKeyLimits[i] != null
                || lowKeyLimits[0] != null && lowKeyLimits[i] == null
                || highKeyLimits[0] == null && highKeyLimits[i] != null
                || highKeyLimits[0] != null && highKeyLimits[i] == null) {
            numSecondaryKeys--;
            primaryIndexPostProccessingIsNeeded = true;
        }
    }
    if (lowKeyLimits[0] == null) {
        lowKeyInclusive[0] = true;
    }
    if (highKeyLimits[0] == null) {
        highKeyInclusive[0] = true;
    }

    // Here we generate vars and funcs for assigning the secondary-index keys to be fed into the secondary-index
    // search.
    // List of variables for the assign.
    ArrayList<LogicalVariable> keyVarList = new ArrayList<LogicalVariable>();
    // List of variables and expressions for the assign.
    ArrayList<LogicalVariable> assignKeyVarList = new ArrayList<LogicalVariable>();
    ArrayList<Mutable<ILogicalExpression>> assignKeyExprList = new ArrayList<Mutable<ILogicalExpression>>();
    int numLowKeys = createKeyVarsAndExprs(numSecondaryKeys, lowKeyLimits, lowKeyExprs, assignKeyVarList,
            assignKeyExprList, keyVarList, context, constantAtRuntimeExpressions, constAtRuntimeExprVars);
    int numHighKeys = createKeyVarsAndExprs(numSecondaryKeys, highKeyLimits, highKeyExprs, assignKeyVarList,
            assignKeyExprList, keyVarList, context, constantAtRuntimeExpressions, constAtRuntimeExprVars);

    BTreeJobGenParams jobGenParams = new BTreeJobGenParams(chosenIndex.getIndexName(), IndexType.BTREE,
            dataset.getDataverseName(), dataset.getDatasetName(), retainInput, requiresBroadcast);
    jobGenParams.setLowKeyInclusive(lowKeyInclusive[0]);
    jobGenParams.setHighKeyInclusive(highKeyInclusive[0]);
    jobGenParams.setIsEqCondition(isEqCondition);
    jobGenParams.setLowKeyVarList(keyVarList, 0, numLowKeys);
    jobGenParams.setHighKeyVarList(keyVarList, numLowKeys, numHighKeys);

    ILogicalOperator inputOp = null;
    if (!assignKeyVarList.isEmpty()) {
        // Assign operator that sets the constant secondary-index search-key fields if necessary.
        AssignOperator assignConstantSearchKeys = new AssignOperator(assignKeyVarList, assignKeyExprList);
        // Input to this assign is the EmptyTupleSource (which the dataSourceScan also must have had as input).
        assignConstantSearchKeys.getInputs().add(new MutableObject<ILogicalOperator>(
                OperatorManipulationUtil.deepCopy(dataSourceOp.getInputs().get(0).getValue())));
        assignConstantSearchKeys.setExecutionMode(dataSourceOp.getExecutionMode());
        inputOp = assignConstantSearchKeys;
    } else {
        // All index search keys are variables.
        inputOp = probeSubTree.getRoot();
    }

    ILogicalOperator secondaryIndexUnnestOp = AccessMethodUtils.createSecondaryIndexUnnestMap(dataset,
            recordType, metaRecordType, chosenIndex, inputOp, jobGenParams, context, false, retainInput,
            retainNull);

    // Generate the rest of the upstream plan which feeds the search results into the primary index.
    AbstractUnnestMapOperator primaryIndexUnnestOp = null;

    boolean isPrimaryIndex = chosenIndex.isPrimaryIndex();
    if (dataset.getDatasetType() == DatasetType.EXTERNAL) {
        // External dataset
        UnnestMapOperator externalDataAccessOp = AccessMethodUtils.createExternalDataLookupUnnestMap(
                dataSourceOp, dataset, recordType, secondaryIndexUnnestOp, context, chosenIndex, retainInput,
                retainNull);
        indexSubTree.getDataSourceRef().setValue(externalDataAccessOp);
        return externalDataAccessOp;
    } else if (!isPrimaryIndex) {
        primaryIndexUnnestOp = AccessMethodUtils.createPrimaryIndexUnnestMap(dataSourceOp, dataset, recordType,
                metaRecordType, secondaryIndexUnnestOp, context, true, retainInput, retainNull, false);

        // Adds equivalence classes --- one equivalent class between a primary key
        // variable and a record field-access expression.
        EquivalenceClassUtils.addEquivalenceClassesForPrimaryIndexAccess(primaryIndexUnnestOp,
                dataSourceOp.getVariables(), recordType, metaRecordType, dataset, context);
    } else {
        List<Object> primaryIndexOutputTypes = new ArrayList<Object>();
        AccessMethodUtils.appendPrimaryIndexTypes(dataset, recordType, metaRecordType, primaryIndexOutputTypes);
        List<LogicalVariable> scanVariables = dataSourceOp.getVariables();

        // Checks whether the primary index search can replace the given
        // SELECT condition.
        // If so, condition will be set to null and eventually the SELECT
        // operator will be removed.
        // If not, we create a new condition based on remaining ones.
        if (!primaryIndexPostProccessingIsNeeded) {
            List<Mutable<ILogicalExpression>> remainingFuncExprs = new ArrayList<Mutable<ILogicalExpression>>();
            getNewConditionExprs(conditionRef, replacedFuncExprs, remainingFuncExprs);
            // Generate new condition.
            if (!remainingFuncExprs.isEmpty()) {
                ILogicalExpression pulledCond = createSelectCondition(remainingFuncExprs);
                conditionRef.setValue(pulledCond);
            } else {
                conditionRef.setValue(null);
            }
        }

        // Checks whether LEFT_OUTER_UNNESTMAP operator is required.
        boolean leftOuterUnnestMapRequired = false;
        if (retainNull && retainInput) {
            leftOuterUnnestMapRequired = true;
        } else {
            leftOuterUnnestMapRequired = false;
        }

        if (conditionRef.getValue() != null) {
            // The job gen parameters are transferred to the actual job gen
            // via the UnnestMapOperator's function arguments.
            List<Mutable<ILogicalExpression>> primaryIndexFuncArgs = new ArrayList<Mutable<ILogicalExpression>>();
            jobGenParams.writeToFuncArgs(primaryIndexFuncArgs);
            // An index search is expressed as an unnest-map over an
            // index-search function.
            IFunctionInfo primaryIndexSearch = FunctionUtil
                    .getFunctionInfo(AsterixBuiltinFunctions.INDEX_SEARCH);
            UnnestingFunctionCallExpression primaryIndexSearchFunc = new UnnestingFunctionCallExpression(
                    primaryIndexSearch, primaryIndexFuncArgs);
            primaryIndexSearchFunc.setReturnsUniqueValues(true);
            if (!leftOuterUnnestMapRequired) {
                primaryIndexUnnestOp = new UnnestMapOperator(scanVariables,
                        new MutableObject<ILogicalExpression>(primaryIndexSearchFunc), primaryIndexOutputTypes,
                        retainInput);
            } else {
                primaryIndexUnnestOp = new LeftOuterUnnestMapOperator(scanVariables,
                        new MutableObject<ILogicalExpression>(primaryIndexSearchFunc), primaryIndexOutputTypes,
                        true);
            }
        } else {
            if (!leftOuterUnnestMapRequired) {
                primaryIndexUnnestOp = new UnnestMapOperator(scanVariables,
                        ((UnnestMapOperator) secondaryIndexUnnestOp).getExpressionRef(),
                        primaryIndexOutputTypes, retainInput);
            } else {
                primaryIndexUnnestOp = new LeftOuterUnnestMapOperator(scanVariables,
                        ((LeftOuterUnnestMapOperator) secondaryIndexUnnestOp).getExpressionRef(),
                        primaryIndexOutputTypes, true);
            }
        }

        primaryIndexUnnestOp.getInputs().add(new MutableObject<ILogicalOperator>(inputOp));

        // Adds equivalence classes --- one equivalent class between a primary key
        // variable and a record field-access expression.
        EquivalenceClassUtils.addEquivalenceClassesForPrimaryIndexAccess(primaryIndexUnnestOp, scanVariables,
                recordType, metaRecordType, dataset, context);
    }

    return primaryIndexUnnestOp;
}

From source file:org.apache.openjpa.kernel.BrokerImpl.java

public boolean isCached(List<Object> oids) {
    BitSet loaded = new BitSet(oids.size());
    //check L1 cache first
    for (int i = 0; i < oids.size(); i++) {
        Object oid = oids.get(i);
        if (_cache.getById(oid, false) != null) {
            loaded.set(i);
        }//w  w w  . jav a  2 s  .  c  om
    }
    if (loaded.cardinality() == oids.size()) {
        return true;
    }
    return _store.isCached(oids, loaded);
}

From source file:org.apache.jackrabbit.core.query.lucene.JahiaLuceneQueryFactoryImpl.java

/**
 * Override LuceneQueryFactory.execute()
 *//*  w  w  w. jav a2s.com*/
@Override
public List<Row> execute(Map<String, PropertyValue> columns, Selector selector, Constraint constraint,
        Sort sort, boolean externalSort, long offsetIn, long limitIn) throws RepositoryException, IOException {
    final IndexReader reader = index.getIndexReader(true);
    final int offset = offsetIn < 0 ? 0 : (int) offsetIn;
    final int limit = limitIn < 0 ? Integer.MAX_VALUE : (int) limitIn;

    QueryHits hits = null;
    try {
        JackrabbitIndexSearcher searcher = new JackrabbitIndexSearcher(session, reader,
                index.getContext().getItemStateManager());
        searcher.setSimilarity(index.getSimilarity());

        Predicate filter = Predicate.TRUE;
        BooleanQuery query = new BooleanQuery();

        QueryPair qp = new QueryPair(query);

        query.add(create(selector), MUST);
        if (constraint != null) {
            String name = selector.getSelectorName();
            NodeType type = ntManager.getNodeType(selector.getNodeTypeName());
            filter = mapConstraintToQueryAndFilter(qp, constraint, Collections.singletonMap(name, type),
                    searcher, reader);
        }

        // Added by jahia
        Set<String> foundIds = new HashSet<String>();
        int hasFacets = FacetHandler.hasFacetFunctions(columns, session);
        CountHandler.CountType countType = CountHandler.hasCountFunction(columns, session);
        boolean isCount = countType != null;
        BitSet bitset = (hasFacets & FacetHandler.FACET_COLUMNS) == 0 ? null : new BitSet();
        // End

        List<Row> rowList = externalSort ? new LinkedList<Row>() : null;
        Map<String, Row> rows = externalSort ? null : new LinkedHashMap<String, Row>();
        hits = searcher.evaluate(qp.mainQuery, sort, offset + limit);
        int currentNode = 0;
        int addedNodes = 0;
        int resultCount = 0;
        int hitsSize = 0;

        ScoreNode node = hits.nextScoreNode();
        Map<String, Boolean> checkedAcls = new HashMap<String, Boolean>();

        while (node != null) {
            if (isCount && countType.isApproxCount()) {
                hitsSize++;
                if (hitsSize > countType.getApproxCountLimit()) {
                    if (hits.getSize() > 0) {
                        hitsSize = hits.getSize();
                        break;
                    } else {
                        node = hits.nextScoreNode();
                        continue;
                    }
                }
            }
            IndexedNodeInfo infos = getIndexedNodeInfo(node, reader, isCount && countType.isSkipChecks());
            if (foundIds.add(infos.getMainNodeUuid())) { // <-- Added by jahia
                if (isCount && countType.isSkipChecks()) {
                    resultCount++;
                } else {
                    try {
                        boolean canRead = true;
                        if (isAclUuidInIndex()) {
                            canRead = checkIndexedAcl(checkedAcls, infos);
                        }
                        boolean checkVisibility = "1".equals(infos.getCheckVisibility())
                                && Constants.LIVE_WORKSPACE.equals(session.getWorkspace().getName());

                        if (canRead && (!Constants.LIVE_WORKSPACE.equals(session.getWorkspace().getName())
                                || ((infos.getPublished() == null || "true".equals(infos.getPublished()))
                                        && (infos.getCheckInvalidLanguages() == null || getLocale() == null
                                                || !infos.getCheckInvalidLanguages()
                                                        .contains(getLocale().toString()))))) {
                            if (filter == Predicate.TRUE) { // <-- Added by jahia
                                if ((hasFacets & FacetHandler.ONLY_FACET_COLUMNS) == 0) {
                                    Row row = null;

                                    if (checkVisibility || !isAclUuidInIndex()) {
                                        NodeImpl objectNode = getNodeWithAclAndVisibilityCheck(node,
                                                checkVisibility);
                                        if (isCount) {
                                            resultCount++;
                                        } else {
                                            row = new LazySelectorRow(columns, evaluator,
                                                    selector.getSelectorName(), objectNode, node.getScore());
                                        }
                                    } else {
                                        if (isCount) {
                                            resultCount++;
                                        } else {
                                            row = new LazySelectorRow(columns, evaluator,
                                                    selector.getSelectorName(), node.getNodeId(),
                                                    node.getScore());
                                        }
                                    }

                                    if (row == null) {
                                        continue;
                                    }

                                    if (externalSort) {
                                        rowList.add(row);
                                    } else {
                                        // apply limit and offset rules locally
                                        if (currentNode >= offset && currentNode - offset < limit) {
                                            rows.put(node.getNodeId().toString(), row);
                                            addedNodes++;
                                        }
                                        currentNode++;
                                        // end the loop when going over the limit
                                        if (addedNodes == limit) {
                                            break;
                                        }
                                    }
                                }
                                if ((hasFacets & FacetHandler.FACET_COLUMNS) == FacetHandler.FACET_COLUMNS) {
                                    //Added by Jahia
                                    //can be added to bitset when ACL checked and not in live mode or no visibility rule to check
                                    if (isAclUuidInIndex() && !checkVisibility) {
                                        bitset.set(infos.getDocNumber());
                                    } else { //try to load nodeWrapper to check the visibility rules
                                        NodeImpl objectNode = getNodeWithAclAndVisibilityCheck(node,
                                                checkVisibility);
                                        bitset.set(infos.getDocNumber());
                                    }
                                    //!Added by Jahia
                                }
                            } else {
                                NodeImpl objectNode = session.getNodeById(node.getNodeId());
                                if (objectNode.isNodeType("jnt:translation")) {
                                    objectNode = (NodeImpl) objectNode.getParent();
                                }
                                if (isCount) {
                                    resultCount++;
                                } else {
                                    Row row = new SelectorRow(columns, evaluator, selector.getSelectorName(),
                                            objectNode, node.getScore());
                                    if (filter.evaluate(row)) {
                                        if ((hasFacets & FacetHandler.ONLY_FACET_COLUMNS) == 0) {
                                            if (externalSort) {
                                                rowList.add(row);
                                            } else {
                                                // apply limit and offset rules locally
                                                if (currentNode >= offset && currentNode - offset < limit) {
                                                    rows.put(node.getNodeId().toString(), row);
                                                    addedNodes++;
                                                }
                                                currentNode++;
                                                // end the loop when going over the limit
                                                if (addedNodes == limit) {
                                                    break;
                                                }
                                            }
                                        }
                                        if ((hasFacets
                                                & FacetHandler.FACET_COLUMNS) == FacetHandler.FACET_COLUMNS) {
                                            bitset.set(infos.getDocNumber()); // <-- Added by jahia
                                        }
                                    }
                                }
                            }
                        }
                    } catch (PathNotFoundException e) {
                    } catch (ItemNotFoundException e) {
                        // skip the node
                    }
                }
            } else {
                if (((hasFacets & FacetHandler.ONLY_FACET_COLUMNS) == 0) && !isCount && !externalSort
                        && !infos.getMainNodeUuid().equals(node.getNodeId().toString())
                        && rows.containsKey(infos.getMainNodeUuid())) {
                    // we've got the translation node -> adjusting the position of the original node in the result list
                    rows.put(infos.getMainNodeUuid(), rows.remove(infos.getMainNodeUuid()));
                }
            } // <-- Added by jahia
            node = hits.nextScoreNode();
        }

        if (rowList == null) {
            if (rows != null) {
                rowList = new LinkedList<Row>(rows.values());
            } else {
                rowList = new LinkedList<Row>();
            }
        }
        // Added by jahia
        if ((hasFacets & FacetHandler.FACET_COLUMNS) == FacetHandler.FACET_COLUMNS) {
            OpenBitSet docIdSet = new OpenBitSetDISI(new DocIdBitSet(bitset).iterator(), bitset.size());

            FacetHandler h = new FacetHandler(columns, selector, docIdSet, index, session, nsMappings);
            h.handleFacets(reader);
            rowList.add(0, h.getFacetsRow());
        } else if (isCount) {
            boolean wasApproxLimitReached = false;
            if (countType.isApproxCount() && hitsSize > countType.getApproxCountLimit()) {
                resultCount = hitsSize * resultCount / countType.getApproxCountLimit();
                resultCount = (int) Math.ceil(MathUtils.round(resultCount,
                        resultCount < 1000 ? -1 : (resultCount < 10000 ? -2 : -3), BigDecimal.ROUND_UP));
                wasApproxLimitReached = true;
            }
            rowList.add(0, CountHandler.createCountRow(resultCount, wasApproxLimitReached));
        }
        // End

        return rowList;
    } finally {
        if (hits != null) {
            hits.close();
        }
        Util.closeOrRelease(reader);
    }
}

From source file:gov.noaa.pfel.erddap.dataset.EDDTableFromNcFiles.java

/** one time(?) test for Bob */
public static void bobFindGtsppDuplicateCruises() throws Throwable {
    EDDTable eddTable = (EDDTable) oneFromDatasetsXml(null, "testErdGtsppBest");
    eddTable.makeNewFileForDapQuery(null, null,
            "cruise,type,org,platform&orderBy(\"cruise,type,org,platform\")&distinct()", "/temp/",
            "gtsppDuplicates", ".nc");
    Table table = new Table();
    table.readFlatNc("/temp/gtsppDuplicates.nc", null, 0);
    int n = table.nRows();
    BitSet keep = new BitSet(n); //all false
    PrimitiveArray cr = table.getColumn(0);
    PrimitiveArray ty = table.getColumn(1);
    PrimitiveArray or = table.getColumn(2);
    String2.log("nRows=" + n);
    for (int row = 1; row < n; row++) { //look back
        if (cr.getString(row - 1).equals(cr.getString(row)) && ty.getString(row - 1).equals(ty.getString(row))
                && or.getString(row - 1).equals(or.getString(row))) {
            keep.set(row - 1);
            keep.set(row);/*from   w  w w. j a v  a 2 s.c o m*/
        }
    }
    table.justKeep(keep);
    String2.log("nRows=" + table.nRows());
    String2.log(table.toString());
}