Example usage for com.google.common.collect Sets newHashSetWithExpectedSize

List of usage examples for com.google.common.collect Sets newHashSetWithExpectedSize

Introduction

In this page you can find the example usage for com.google.common.collect Sets newHashSetWithExpectedSize.

Prototype

public static <E> HashSet<E> newHashSetWithExpectedSize(int expectedSize) 

Source Link

Document

Creates a HashSet instance, with a high enough initial table size that it should hold expectedSize elements without resizing.

Usage

From source file:com.cinchapi.concourse.server.ConcourseServer.java

@Override
@ThrowsThriftExceptions/*from   w w w.  j  ava  2 s.co m*/
public Map<TObject, Map<Diff, Set<Long>>> diffKeyStartEnd(String key, long start, long end, AccessToken creds,
        TransactionToken transaction, String environment) throws TException {
    checkAccess(creds, transaction);
    AtomicSupport store = getStore(transaction, environment);
    AtomicOperation atomic = null;
    Map<TObject, Set<Long>> startData = null;
    Map<TObject, Set<Long>> endData = null;
    while (atomic == null || !atomic.commit()) {
        atomic = store.startAtomicOperation();
        try {
            startData = store.browse(key, start);
            endData = store.browse(key, end);
        } catch (AtomicStateException e) {
            atomic = null;
        }
    }
    Set<TObject> startValues = startData.keySet();
    Set<TObject> endValues = endData.keySet();
    Set<TObject> xor = Sets.symmetricDifference(startValues, endValues);
    Set<TObject> intersection = startValues.size() < endValues.size()
            ? Sets.intersection(startValues, endValues)
            : Sets.intersection(endValues, startValues);
    Map<TObject, Map<Diff, Set<Long>>> result = TMaps
            .newLinkedHashMapWithCapacity(xor.size() + intersection.size());
    for (TObject value : xor) {
        Map<Diff, Set<Long>> entry = Maps.newHashMapWithExpectedSize(1);
        if (!startValues.contains(value)) {
            entry.put(Diff.ADDED, endData.get(value));
        } else {
            entry.put(Diff.REMOVED, endData.get(value));
        }
        result.put(value, entry);
    }
    for (TObject value : intersection) {
        Set<Long> startRecords = startData.get(value);
        Set<Long> endRecords = endData.get(value);
        Set<Long> xorRecords = Sets.symmetricDifference(startRecords, endRecords);
        if (!xorRecords.isEmpty()) {
            Set<Long> added = Sets.newHashSetWithExpectedSize(xorRecords.size());
            Set<Long> removed = Sets.newHashSetWithExpectedSize(xorRecords.size());
            for (Long record : xorRecords) {
                if (!startRecords.contains(record)) {
                    added.add(record);
                } else {
                    removed.add(record);
                }
            }
            Map<Diff, Set<Long>> entry = Maps.newHashMapWithExpectedSize(2);
            if (!added.isEmpty()) {
                entry.put(Diff.ADDED, added);
            }
            if (!removed.isEmpty()) {
                entry.put(Diff.REMOVED, removed);
            }
            result.put(value, entry);
        }
    }
    return result;
}

From source file:com.opengamma.engine.view.worker.SingleThreadViewProcessWorker.java

/**
 * Returns the set of unique identifiers that were previously used as targets in the dependency graph for object identifiers (or external identifiers) that now resolve differently.
 * //from  ww  w.  ja  v a 2  s. co m
 * @param previousResolutions the previous cycle's resolution of identifiers, not null
 * @param versionCorrection the resolver version correction for this cycle, not null
 * @return the invalid identifier set, or null if none are invalid, this is a map from the old unique identifier to the new resolution
 */
private Map<UniqueId, ComputationTargetSpecification> getInvalidIdentifiers(
        final Map<ComputationTargetReference, UniqueId> previousResolutions,
        final VersionCorrection versionCorrection) {
    long t = -System.nanoTime();
    final Set<ComputationTargetReference> toCheck;
    if (_targetResolverChanges == null) {
        // Change notifications aren't relevant for historical iteration; must recheck all of the resolutions
        toCheck = previousResolutions.keySet();
    } else {
        // Subscribed to LATEST/LATEST so change manager notifications can filter the set to be checked
        toCheck = Sets.newHashSetWithExpectedSize(previousResolutions.size());
        final Set<ObjectId> allObjectIds = Sets.newHashSetWithExpectedSize(previousResolutions.size());
        for (final Map.Entry<ComputationTargetReference, UniqueId> previousResolution : previousResolutions
                .entrySet()) {
            final ObjectId oid = previousResolution.getValue().getObjectId();
            if (_targetResolverChanges.isChanged(oid)) {
                // A change was seen on this target
                s_logger.debug("Change observed on {}", oid);
                toCheck.add(previousResolution.getKey());
            }
            allObjectIds.add(oid);
        }
        _targetResolverChanges.watchOnly(allObjectIds);
        if (toCheck.isEmpty()) {
            s_logger.debug("No resolutions (from {}) to check", previousResolutions.size());
            return null;
        } else {
            s_logger.debug("Checking {} of {} resolutions for changed objects", toCheck.size(),
                    previousResolutions.size());
        }
    }
    PoolExecutor previousInstance = PoolExecutor
            .setInstance(getProcessContext().getFunctionCompilationService().getExecutorService());
    final Map<ComputationTargetReference, ComputationTargetSpecification> specifications = getProcessContext()
            .getFunctionCompilationService().getFunctionCompilationContext().getRawComputationTargetResolver()
            .getSpecificationResolver().getTargetSpecifications(toCheck, versionCorrection);
    PoolExecutor.setInstance(previousInstance);
    t += System.nanoTime();
    Map<UniqueId, ComputationTargetSpecification> invalidIdentifiers = null;
    for (final Map.Entry<ComputationTargetReference, UniqueId> target : previousResolutions.entrySet()) {
        final ComputationTargetSpecification resolved = specifications.get(target.getKey());
        if ((resolved != null) && target.getValue().equals(resolved.getUniqueId())) {
            // No change
            s_logger.debug("No change resolving {}", target);
        } else if (toCheck.contains(target.getKey())) {
            // Identifier no longer resolved, or resolved differently
            s_logger.info("New resolution of {} to {}", target, resolved);
            if (invalidIdentifiers == null) {
                invalidIdentifiers = new HashMap<>();
            }
            invalidIdentifiers.put(target.getValue(), resolved);
        }
    }
    s_logger.info("{} resolutions checked in {}ms", toCheck.size(), t / 1e6);
    return invalidIdentifiers;
}

From source file:com.cinchapi.concourse.server.ConcourseServer.java

@Override
@ThrowsThriftExceptions// w w w . ja va2  s.com
public Map<String, Map<Diff, Set<TObject>>> diffRecordStartEnd(long record, long start, long end,
        AccessToken creds, TransactionToken transaction, String environment) throws TException {
    checkAccess(creds, transaction);
    AtomicSupport store = getStore(transaction, environment);
    AtomicOperation atomic = null;
    Map<String, Set<TObject>> startData = null;
    Map<String, Set<TObject>> endData = null;
    while (atomic == null || !atomic.commit()) {
        atomic = store.startAtomicOperation();
        try {
            startData = store.select(record, start);
            endData = store.select(record, end);
        } catch (AtomicStateException e) {
            atomic = null;
        }
    }
    Set<String> startKeys = startData.keySet();
    Set<String> endKeys = endData.keySet();
    Set<String> xor = Sets.symmetricDifference(startKeys, endKeys);
    Set<String> intersection = Sets.intersection(startKeys, endKeys);
    Map<String, Map<Diff, Set<TObject>>> result = TMaps
            .newLinkedHashMapWithCapacity(xor.size() + intersection.size());
    for (String key : xor) {
        Map<Diff, Set<TObject>> entry = Maps.newHashMapWithExpectedSize(1);
        if (!startKeys.contains(key)) {
            entry.put(Diff.ADDED, endData.get(key));
        } else {
            entry.put(Diff.REMOVED, endData.get(key));
        }
        result.put(key, entry);
    }
    for (String key : intersection) {
        Set<TObject> startValues = startData.get(key);
        Set<TObject> endValues = endData.get(key);
        Set<TObject> xorValues = Sets.symmetricDifference(startValues, endValues);
        if (!xorValues.isEmpty()) {
            Set<TObject> added = Sets.newHashSetWithExpectedSize(xorValues.size());
            Set<TObject> removed = Sets.newHashSetWithExpectedSize(xorValues.size());
            for (TObject value : xorValues) {
                if (!startValues.contains(value)) {
                    added.add(value);
                } else {
                    removed.add(value);
                }
            }
            Map<Diff, Set<TObject>> entry = Maps.newHashMapWithExpectedSize(2);
            if (!added.isEmpty()) {
                entry.put(Diff.ADDED, added);
            }
            if (!removed.isEmpty()) {
                entry.put(Diff.REMOVED, removed);
            }
            result.put(key, entry);
        }
    }
    return result;
}

From source file:de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelImportServiceImpl.java

void createBuildingBlockRelations(BusinessObject bo, Map<String, CellValueHolder> relations, Locale locale) {
    // Parent//from  w  ww  .  j a v a2s .com
    CellValueHolder hierarchyNameCellValueHolder = relations
            .get(getHierarchyHeaderFor(Constants.BB_BUSINESSOBJECT_PLURAL, locale));
    setParentRelation(bo, hierarchyNameCellValueHolder);

    CellValueHolder specializationsCellValueHolder = relations
            .get(MessageAccess.getStringOrNull(Constants.ASSOC_SPECIALISATION, locale));
    if (specializationsCellValueHolder != null) {
        Set<BusinessObject> specials = loadBuildingBlocksAsSet(TypeOfBuildingBlock.BUSINESSOBJECT,
                specializationsCellValueHolder);
        bo.removeSpecialisationRelations();
        bo.addSpecialisations(specials);
    }

    CellValueHolder bdCellValueHolder = relations
            .get(MessageAccess.getStringOrNull(Constants.BB_BUSINESSDOMAIN_PLURAL, locale));
    if (bdCellValueHolder != null) {
        Set<BusinessDomain> bds = loadBuildingBlocksAsSet(TypeOfBuildingBlock.BUSINESSDOMAIN,
                bdCellValueHolder);
        bo.removeBusinessDomainRelations();
        bo.addBusinessDomains(bds);
    }

    CellValueHolder bfCellValueHolder = relations
            .get(MessageAccess.getStringOrNull(Constants.BB_BUSINESSFUNCTION_PLURAL, locale));
    if (bfCellValueHolder != null) {
        Set<BusinessFunction> bfs = loadBuildingBlocksAsSet(TypeOfBuildingBlock.BUSINESSFUNCTION,
                bfCellValueHolder);
        bo.removeBusinessFunctionRelations();
        bo.addBusinessFunctions(bfs);
    }

    CellValueHolder isrCellValueHolder = relations
            .get(MessageAccess.getStringOrNull(Constants.BB_INFORMATIONSYSTEMRELEASE_PLURAL, locale));
    if (isrCellValueHolder != null) {
        Set<InformationSystemRelease> isrs = loadBuildingBlocksAsSet(
                TypeOfBuildingBlock.INFORMATIONSYSTEMRELEASE, isrCellValueHolder);
        Set<Isr2BoAssociation> associations = Sets.newHashSetWithExpectedSize(isrs.size());
        Set<Isr2BoAssociation> existentAssociations = bo.getInformationSystemReleaseAssociations();
        for (InformationSystemRelease isr : isrs) {
            Isr2BoAssociation assoc = BuildingBlockFactory.createIsr2BoAssociation(isr, bo);
            // try to find out whether that association exists already, and if yes, use the existing object
            for (Isr2BoAssociation connectedAssoc : existentAssociations) {
                if (assoc.equals(connectedAssoc)) {
                    assoc = connectedAssoc;
                    break;
                }
            }
            buildingBlockServiceLocator.getIsr2BoAssociationService().saveAssociations(associations);
            associations.add(assoc);
        }
        bo.connectIsr2BoAssociations(associations);
    }

    getServiceFor(TypeOfBuildingBlock.BUSINESSOBJECT).saveOrUpdate(bo);

}

From source file:org.apache.phoenix.schema.MetaDataClient.java

/**
 * Create an index table by morphing the CreateIndexStatement into a CreateTableStatement and calling
 * MetaDataClient.createTable. In doing so, we perform the following translations:
 * 1) Change the type of any columns being indexed to types that support null if the column is nullable.
 *    For example, a BIGINT type would be coerced to a DECIMAL type, since a DECIMAL type supports null
 *    when it's in the row key while a BIGINT does not.
 * 2) Append any row key column from the data table that is not in the indexed column list. Our indexes
 *    rely on having a 1:1 correspondence between the index and data rows.
 * 3) Change the name of the columns to include the column family. For example, if you have a column
 *    named "B" in a column family named "A", the indexed column name will be "A:B". This makes it easy
 *    to translate the column references in a query to the correct column references in an index table
 *    regardless of whether the column reference is prefixed with the column family name or not. It also
 *    has the side benefit of allowing the same named column in different column families to both be
 *    listed as an index column.//from  w  w  w  .j a  va  2 s .  c  o  m
 * @param statement
 * @param splits
 * @return MutationState from population of index table from data table
 * @throws SQLException
 */
public MutationState createIndex(CreateIndexStatement statement, byte[][] splits) throws SQLException {
    IndexKeyConstraint ik = statement.getIndexConstraint();
    TableName indexTableName = statement.getIndexTableName();

    List<Pair<ParseNode, SortOrder>> indexParseNodeAndSortOrderList = ik.getParseNodeAndSortOrderList();
    List<ColumnName> includedColumns = statement.getIncludeColumns();
    TableRef tableRef = null;
    PTable table = null;
    boolean retry = true;
    Short indexId = null;
    boolean allocateIndexId = false;
    boolean isLocalIndex = statement.getIndexType() == IndexType.LOCAL;
    int hbaseVersion = connection.getQueryServices().getLowestClusterHBaseVersion();
    if (isLocalIndex) {
        if (!connection.getQueryServices().getProps().getBoolean(QueryServices.ALLOW_LOCAL_INDEX_ATTRIB,
                QueryServicesOptions.DEFAULT_ALLOW_LOCAL_INDEX)) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.UNALLOWED_LOCAL_INDEXES)
                    .setTableName(indexTableName.getTableName()).build().buildException();
        }
        if (!connection.getQueryServices().supportsFeature(Feature.LOCAL_INDEX)) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.NO_LOCAL_INDEXES)
                    .setTableName(indexTableName.getTableName()).build().buildException();
        }
    }
    while (true) {
        try {
            ColumnResolver resolver = FromCompiler.getResolver(statement, connection,
                    statement.getUdfParseNodes());
            tableRef = resolver.getTables().get(0);
            PTable dataTable = tableRef.getTable();
            boolean isTenantConnection = connection.getTenantId() != null;
            if (isTenantConnection) {
                if (dataTable.getType() != PTableType.VIEW) {
                    throw new SQLFeatureNotSupportedException(
                            "An index may only be created for a VIEW through a tenant-specific connection");
                }
            }
            if (!dataTable.isImmutableRows()) {
                if (hbaseVersion < PhoenixDatabaseMetaData.MUTABLE_SI_VERSION_THRESHOLD) {
                    throw new SQLExceptionInfo.Builder(SQLExceptionCode.NO_MUTABLE_INDEXES)
                            .setTableName(indexTableName.getTableName()).build().buildException();
                }
                if (connection.getQueryServices().hasInvalidIndexConfiguration()) {
                    throw new SQLExceptionInfo.Builder(SQLExceptionCode.INVALID_MUTABLE_INDEX_CONFIG)
                            .setTableName(indexTableName.getTableName()).build().buildException();
                }
            }
            int posOffset = 0;
            List<PColumn> pkColumns = dataTable.getPKColumns();
            Set<RowKeyColumnExpression> unusedPkColumns;
            if (dataTable.getBucketNum() != null) { // Ignore SALT column
                unusedPkColumns = Sets.newLinkedHashSetWithExpectedSize(pkColumns.size() - 1);
                posOffset++;
            } else {
                unusedPkColumns = Sets.newLinkedHashSetWithExpectedSize(pkColumns.size());
            }
            for (int i = posOffset; i < pkColumns.size(); i++) {
                PColumn column = pkColumns.get(i);
                unusedPkColumns.add(new RowKeyColumnExpression(column, new RowKeyValueAccessor(pkColumns, i),
                        "\"" + column.getName().getString() + "\""));
            }
            List<ColumnDefInPkConstraint> allPkColumns = Lists
                    .newArrayListWithExpectedSize(unusedPkColumns.size());
            List<ColumnDef> columnDefs = Lists.newArrayListWithExpectedSize(
                    includedColumns.size() + indexParseNodeAndSortOrderList.size());

            if (dataTable.isMultiTenant()) {
                // Add tenant ID column as first column in index
                PColumn col = dataTable.getPKColumns().get(posOffset);
                RowKeyColumnExpression columnExpression = new RowKeyColumnExpression(col,
                        new RowKeyValueAccessor(pkColumns, posOffset), col.getName().getString());
                unusedPkColumns.remove(columnExpression);
                PDataType dataType = IndexUtil.getIndexColumnDataType(col);
                ColumnName colName = ColumnName.caseSensitiveColumnName(IndexUtil.getIndexColumnName(col));
                allPkColumns.add(new ColumnDefInPkConstraint(colName, col.getSortOrder(), false));
                columnDefs.add(FACTORY.columnDef(colName, dataType.getSqlTypeName(), col.isNullable(),
                        col.getMaxLength(), col.getScale(), false, SortOrder.getDefault(),
                        col.getName().getString(), col.isRowTimestamp()));
            }
            /*
             * Allocate an index ID in two circumstances:
             * 1) for a local index, as all local indexes will reside in the same HBase table
             * 2) for a view on an index.
             */
            if (isLocalIndex
                    || (dataTable.getType() == PTableType.VIEW && dataTable.getViewType() != ViewType.MAPPED)) {
                allocateIndexId = true;
                // Next add index ID column
                PDataType dataType = MetaDataUtil.getViewIndexIdDataType();
                ColumnName colName = ColumnName
                        .caseSensitiveColumnName(MetaDataUtil.getViewIndexIdColumnName());
                allPkColumns.add(new ColumnDefInPkConstraint(colName, SortOrder.getDefault(), false));
                columnDefs.add(FACTORY.columnDef(colName, dataType.getSqlTypeName(), false, null, null, false,
                        SortOrder.getDefault(), null, false));
            }

            PhoenixStatement phoenixStatment = new PhoenixStatement(connection);
            StatementContext context = new StatementContext(phoenixStatment, resolver);
            IndexExpressionCompiler expressionIndexCompiler = new IndexExpressionCompiler(context);
            Set<ColumnName> indexedColumnNames = Sets
                    .newHashSetWithExpectedSize(indexParseNodeAndSortOrderList.size());
            for (Pair<ParseNode, SortOrder> pair : indexParseNodeAndSortOrderList) {
                ParseNode parseNode = pair.getFirst();
                // normalize the parse node
                parseNode = StatementNormalizer.normalize(parseNode, resolver);
                // compile the parseNode to get an expression
                expressionIndexCompiler.reset();
                Expression expression = parseNode.accept(expressionIndexCompiler);
                if (expressionIndexCompiler.isAggregate()) {
                    throw new SQLExceptionInfo.Builder(
                            SQLExceptionCode.AGGREGATE_EXPRESSION_NOT_ALLOWED_IN_INDEX).build()
                                    .buildException();
                }
                if (expression.getDeterminism() != Determinism.ALWAYS) {
                    throw new SQLExceptionInfo.Builder(
                            SQLExceptionCode.NON_DETERMINISTIC_EXPRESSION_NOT_ALLOWED_IN_INDEX).build()
                                    .buildException();
                }
                if (expression.isStateless()) {
                    throw new SQLExceptionInfo.Builder(
                            SQLExceptionCode.STATELESS_EXPRESSION_NOT_ALLOWED_IN_INDEX).build()
                                    .buildException();
                }
                unusedPkColumns.remove(expression);

                // Go through parse node to get string as otherwise we
                // can lose information during compilation
                StringBuilder buf = new StringBuilder();
                parseNode.toSQL(resolver, buf);
                // need to escape backslash as this expression will be re-parsed later
                String expressionStr = StringUtil.escapeBackslash(buf.toString());

                ColumnName colName = null;
                ColumnRef colRef = expressionIndexCompiler.getColumnRef();
                boolean isRowTimestamp = false;
                if (colRef != null) {
                    // if this is a regular column
                    PColumn column = colRef.getColumn();
                    String columnFamilyName = column.getFamilyName() != null
                            ? column.getFamilyName().getString()
                            : null;
                    colName = ColumnName.caseSensitiveColumnName(
                            IndexUtil.getIndexColumnName(columnFamilyName, column.getName().getString()));
                    isRowTimestamp = column.isRowTimestamp();
                } else {
                    // if this is an expression
                    // TODO column names cannot have double quotes, remove this once this PHOENIX-1621 is fixed
                    String name = expressionStr.replaceAll("\"", "'");
                    colName = ColumnName.caseSensitiveColumnName(IndexUtil.getIndexColumnName(null, name));
                }
                indexedColumnNames.add(colName);
                PDataType dataType = IndexUtil.getIndexColumnDataType(expression.isNullable(),
                        expression.getDataType());
                allPkColumns.add(new ColumnDefInPkConstraint(colName, pair.getSecond(), isRowTimestamp));
                columnDefs.add(FACTORY.columnDef(colName, dataType.getSqlTypeName(), expression.isNullable(),
                        expression.getMaxLength(), expression.getScale(), false, pair.getSecond(),
                        expressionStr, isRowTimestamp));
            }

            // Next all the PK columns from the data table that aren't indexed
            if (!unusedPkColumns.isEmpty()) {
                for (RowKeyColumnExpression colExpression : unusedPkColumns) {
                    PColumn col = dataTable.getPKColumns().get(colExpression.getPosition());
                    // Don't add columns with constant values from updatable views, as
                    // we don't need these in the index
                    if (col.getViewConstant() == null) {
                        ColumnName colName = ColumnName
                                .caseSensitiveColumnName(IndexUtil.getIndexColumnName(col));
                        allPkColumns.add(new ColumnDefInPkConstraint(colName, colExpression.getSortOrder(),
                                col.isRowTimestamp()));
                        PDataType dataType = IndexUtil.getIndexColumnDataType(colExpression.isNullable(),
                                colExpression.getDataType());
                        columnDefs.add(FACTORY.columnDef(colName, dataType.getSqlTypeName(),
                                colExpression.isNullable(), colExpression.getMaxLength(),
                                colExpression.getScale(), false, colExpression.getSortOrder(),
                                colExpression.toString(), col.isRowTimestamp()));
                    }
                }
            }

            // Last all the included columns (minus any PK columns)
            for (ColumnName colName : includedColumns) {
                PColumn col = resolver.resolveColumn(null, colName.getFamilyName(), colName.getColumnName())
                        .getColumn();
                colName = ColumnName.caseSensitiveColumnName(IndexUtil.getIndexColumnName(col));
                // Check for duplicates between indexed and included columns
                if (indexedColumnNames.contains(colName)) {
                    throw new SQLExceptionInfo.Builder(SQLExceptionCode.COLUMN_EXIST_IN_DEF).build()
                            .buildException();
                }
                if (!SchemaUtil.isPKColumn(col) && col.getViewConstant() == null) {
                    // Need to re-create ColumnName, since the above one won't have the column family name
                    colName = ColumnName.caseSensitiveColumnName(col.getFamilyName().getString(),
                            IndexUtil.getIndexColumnName(col));
                    columnDefs.add(FACTORY.columnDef(colName, col.getDataType().getSqlTypeName(),
                            col.isNullable(), col.getMaxLength(), col.getScale(), false, col.getSortOrder(),
                            null, col.isRowTimestamp()));
                }
            }

            // Don't re-allocate indexId on ConcurrentTableMutationException,
            // as there's no need to burn another sequence value.
            if (allocateIndexId && indexId == null) {
                Long scn = connection.getSCN();
                long timestamp = scn == null ? HConstants.LATEST_TIMESTAMP : scn;
                PName tenantId = connection.getTenantId();
                String tenantIdStr = tenantId == null ? null : connection.getTenantId().getString();
                PName physicalName = dataTable.getPhysicalName();
                int nSequenceSaltBuckets = connection.getQueryServices().getSequenceSaltBuckets();
                SequenceKey key = MetaDataUtil.getViewIndexSequenceKey(tenantIdStr, physicalName,
                        nSequenceSaltBuckets);
                // Create at parent timestamp as we know that will be earlier than now
                // and earlier than any SCN if one is set.
                createSequence(key.getTenantId(), key.getSchemaName(), key.getSequenceName(), true,
                        Short.MIN_VALUE, 1, 1, false, Long.MIN_VALUE, Long.MAX_VALUE, dataTable.getTimeStamp());
                long[] seqValues = new long[1];
                SQLException[] sqlExceptions = new SQLException[1];
                connection.getQueryServices().incrementSequences(
                        Collections.singletonList(new SequenceAllocation(key, 1)),
                        Math.max(timestamp, dataTable.getTimeStamp()), seqValues, sqlExceptions);
                if (sqlExceptions[0] != null) {
                    throw sqlExceptions[0];
                }
                long seqValue = seqValues[0];
                if (seqValue > Short.MAX_VALUE) {
                    throw new SQLExceptionInfo.Builder(SQLExceptionCode.TOO_MANY_INDEXES)
                            .setSchemaName(SchemaUtil.getSchemaNameFromFullName(physicalName.getString()))
                            .setTableName(SchemaUtil.getTableNameFromFullName(physicalName.getString())).build()
                            .buildException();
                }
                indexId = (short) seqValue;
            }
            // Set DEFAULT_COLUMN_FAMILY_NAME of index to match data table
            // We need this in the props so that the correct column family is created
            if (dataTable.getDefaultFamilyName() != null && dataTable.getType() != PTableType.VIEW
                    && indexId == null) {
                statement.getProps().put("", new Pair<String, Object>(DEFAULT_COLUMN_FAMILY_NAME,
                        dataTable.getDefaultFamilyName().getString()));
            }
            PrimaryKeyConstraint pk = FACTORY.primaryKey(null, allPkColumns);
            CreateTableStatement tableStatement = FACTORY.createTable(indexTableName, statement.getProps(),
                    columnDefs, pk, statement.getSplitNodes(), PTableType.INDEX, statement.ifNotExists(), null,
                    null, statement.getBindCount());
            table = createTableInternal(tableStatement, splits, dataTable, null, null, null, null, indexId,
                    statement.getIndexType());
            break;
        } catch (ConcurrentTableMutationException e) { // Can happen if parent data table changes while above is in progress
            if (retry) {
                retry = false;
                continue;
            }
            throw e;
        }
    }
    if (table == null) {
        return new MutationState(0, connection);
    }

    // In async process, we return immediately as the MR job needs to be triggered .
    if (statement.isAsync()) {
        return new MutationState(0, connection);
    }

    // If our connection is at a fixed point-in-time, we need to open a new
    // connection so that our new index table is visible.
    if (connection.getSCN() != null) {
        return buildIndexAtTimeStamp(table, statement.getTable());
    }
    return buildIndex(table, tableRef);
}

From source file:com.datatorrent.stram.StreamingContainerManager.java

/**
 * process the heartbeat from each container.
 * called by the RPC thread for each container. (i.e. called by multiple threads)
 *
 * @param heartbeat/* w ww  . j  ava  2 s  . c o  m*/
 * @return heartbeat response
 */
@SuppressWarnings("StatementWithEmptyBody")
public ContainerHeartbeatResponse processHeartbeat(ContainerHeartbeat heartbeat) {
    long currentTimeMillis = clock.getTime();

    final StreamingContainerAgent sca = this.containers.get(heartbeat.getContainerId());
    if (sca == null || sca.container.getState() == PTContainer.State.KILLED) {
        // could be orphaned container that was replaced and needs to terminate
        LOG.error("Unknown container {}", heartbeat.getContainerId());
        ContainerHeartbeatResponse response = new ContainerHeartbeatResponse();
        response.shutdown = true;
        return response;
    }

    //LOG.debug("{} {} {}", new Object[]{sca.container.containerId, sca.container.bufferServerAddress, sca.container.getState()});
    if (sca.container.getState() == PTContainer.State.ALLOCATED) {
        // capture dynamically assigned address from container
        if (sca.container.bufferServerAddress == null && heartbeat.bufferServerHost != null) {
            sca.container.bufferServerAddress = InetSocketAddress.createUnresolved(heartbeat.bufferServerHost,
                    heartbeat.bufferServerPort);
            LOG.info("Container {} buffer server: {}", sca.container.getExternalId(),
                    sca.container.bufferServerAddress);
        }
        final long containerStartTime = System.currentTimeMillis();
        sca.container.setState(PTContainer.State.ACTIVE);
        sca.container.setStartedTime(containerStartTime);
        sca.container.setFinishedTime(-1);
        sca.jvmName = heartbeat.jvmName;
        poolExecutor.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    containerFile.append(sca.getContainerInfo());
                } catch (IOException ex) {
                    LOG.warn("Cannot write to container file");
                }
                for (PTOperator ptOp : sca.container.getOperators()) {
                    try {
                        JSONObject operatorInfo = new JSONObject();
                        operatorInfo.put("name", ptOp.getName());
                        operatorInfo.put("id", ptOp.getId());
                        operatorInfo.put("container", sca.container.getExternalId());
                        operatorInfo.put("startTime", containerStartTime);
                        operatorFile.append(operatorInfo);
                    } catch (IOException | JSONException ex) {
                        LOG.warn("Cannot write to operator file: ", ex);
                    }
                }
            }
        });
    }

    if (heartbeat.restartRequested) {
        LOG.error("Container {} restart request", sca.container.getExternalId());
        containerStopRequests.put(sca.container.getExternalId(), sca.container.getExternalId());
    }

    sca.memoryMBFree = heartbeat.memoryMBFree;
    sca.gcCollectionCount = heartbeat.gcCollectionCount;
    sca.gcCollectionTime = heartbeat.gcCollectionTime;

    sca.undeployOpers.clear();
    sca.deployOpers.clear();
    if (!this.deployChangeInProgress.get()) {
        sca.deployCnt = this.deployChangeCnt;
    }
    Set<Integer> reportedOperators = Sets.newHashSetWithExpectedSize(sca.container.getOperators().size());

    for (OperatorHeartbeat shb : heartbeat.getContainerStats().operators) {

        long maxEndWindowTimestamp = 0;

        reportedOperators.add(shb.nodeId);
        PTOperator oper = this.plan.getAllOperators().get(shb.getNodeId());

        if (oper == null) {
            LOG.info("Heartbeat for unknown operator {} (container {})", shb.getNodeId(),
                    heartbeat.getContainerId());
            sca.undeployOpers.add(shb.nodeId);
            continue;
        }

        if (shb.requestResponse != null) {
            for (StatsListener.OperatorResponse obj : shb.requestResponse) {
                if (obj instanceof OperatorResponse) { // This is to identify platform requests
                    commandResponse.put((Long) obj.getResponseId(), obj.getResponse());
                    LOG.debug(" Got back the response {} for the request {}", obj, obj.getResponseId());
                } else { // This is to identify user requests
                    oper.stats.responses.add(obj);
                }
            }
        }

        //LOG.debug("heartbeat {} {}/{} {}", oper, oper.getState(), shb.getState(), oper.getContainer().getExternalId());
        if (!(oper.getState() == PTOperator.State.ACTIVE
                && shb.getState() == OperatorHeartbeat.DeployState.ACTIVE)) {
            // deploy state may require synchronization
            processOperatorDeployStatus(oper, shb, sca);
        }

        oper.stats.lastHeartbeat = shb;
        List<ContainerStats.OperatorStats> statsList = shb.getOperatorStatsContainer();

        if (!statsList.isEmpty()) {
            long tuplesProcessed = 0;
            long tuplesEmitted = 0;
            long totalCpuTimeUsed = 0;
            int statCount = 0;
            long maxDequeueTimestamp = -1;
            oper.stats.recordingId = null;

            final OperatorStatus status = oper.stats;
            status.statsRevs.checkout();

            for (Map.Entry<String, PortStatus> entry : status.inputPortStatusList.entrySet()) {
                entry.getValue().recordingId = null;
            }
            for (Map.Entry<String, PortStatus> entry : status.outputPortStatusList.entrySet()) {
                entry.getValue().recordingId = null;
            }
            for (ContainerStats.OperatorStats stats : statsList) {
                if (stats == null) {
                    LOG.warn("Operator {} statistics list contains null element", shb.getNodeId());
                    continue;
                }

                /* report checkpoint-ed WindowId status of the operator */
                if (stats.checkpoint instanceof Checkpoint) {
                    if (oper.getRecentCheckpoint() == null
                            || oper.getRecentCheckpoint().windowId < stats.checkpoint.getWindowId()) {
                        addCheckpoint(oper, (Checkpoint) stats.checkpoint);
                        if (stats.checkpointStats != null) {
                            status.checkpointStats = stats.checkpointStats;
                            status.checkpointTimeMA.add(stats.checkpointStats.checkpointTime);
                        }
                        oper.failureCount = 0;
                    }
                }

                oper.stats.recordingId = stats.recordingId;

                /* report all the other stuff */

                // calculate the stats related to end window
                EndWindowStats endWindowStats = new EndWindowStats(); // end window stats for a particular window id for a particular node
                Collection<ContainerStats.OperatorStats.PortStats> ports = stats.inputPorts;
                if (ports != null) {
                    Set<String> currentInputPortSet = Sets.newHashSetWithExpectedSize(ports.size());
                    for (ContainerStats.OperatorStats.PortStats s : ports) {
                        currentInputPortSet.add(s.id);
                        PortStatus ps = status.inputPortStatusList.get(s.id);
                        if (ps == null) {
                            ps = status.new PortStatus();
                            ps.portName = s.id;
                            status.inputPortStatusList.put(s.id, ps);
                        }
                        ps.totalTuples += s.tupleCount;
                        ps.recordingId = s.recordingId;

                        tuplesProcessed += s.tupleCount;
                        endWindowStats.dequeueTimestamps.put(s.id, s.endWindowTimestamp);

                        Pair<Integer, String> operatorPortName = new Pair<>(oper.getId(), s.id);
                        Long lastEndWindowTimestamp = operatorPortLastEndWindowTimestamps.get(operatorPortName);
                        if (lastEndWindowTimestamp == null) {
                            lastEndWindowTimestamp = lastStatsTimestamp;
                        }
                        long portElapsedMillis = Math.max(s.endWindowTimestamp - lastEndWindowTimestamp, 0);
                        //LOG.debug("=== PROCESSED TUPLE COUNT for {}: {}, {}, {}, {}", operatorPortName, s.tupleCount, portElapsedMillis, operatorPortLastEndWindowTimestamps.get(operatorPortName), lastStatsTimestamp);
                        ps.tuplesPMSMA.add(s.tupleCount, portElapsedMillis);
                        ps.bufferServerBytesPMSMA.add(s.bufferServerBytes, portElapsedMillis);
                        ps.queueSizeMA.add(s.queueSize);

                        operatorPortLastEndWindowTimestamps.put(operatorPortName, s.endWindowTimestamp);
                        if (maxEndWindowTimestamp < s.endWindowTimestamp) {
                            maxEndWindowTimestamp = s.endWindowTimestamp;
                        }
                        if (s.endWindowTimestamp > maxDequeueTimestamp) {
                            maxDequeueTimestamp = s.endWindowTimestamp;
                        }
                    }
                    // need to remove dead ports, for unifiers
                    Iterator<Map.Entry<String, PortStatus>> it = status.inputPortStatusList.entrySet()
                            .iterator();
                    while (it.hasNext()) {
                        Map.Entry<String, PortStatus> entry = it.next();
                        if (!currentInputPortSet.contains(entry.getKey())) {
                            it.remove();
                        }
                    }
                }

                ports = stats.outputPorts;
                if (ports != null) {
                    Set<String> currentOutputPortSet = Sets.newHashSetWithExpectedSize(ports.size());
                    for (ContainerStats.OperatorStats.PortStats s : ports) {
                        currentOutputPortSet.add(s.id);
                        PortStatus ps = status.outputPortStatusList.get(s.id);
                        if (ps == null) {
                            ps = status.new PortStatus();
                            ps.portName = s.id;
                            status.outputPortStatusList.put(s.id, ps);
                        }
                        ps.totalTuples += s.tupleCount;
                        ps.recordingId = s.recordingId;

                        tuplesEmitted += s.tupleCount;
                        Pair<Integer, String> operatorPortName = new Pair<>(oper.getId(), s.id);
                        Long lastEndWindowTimestamp = operatorPortLastEndWindowTimestamps.get(operatorPortName);
                        if (lastEndWindowTimestamp == null) {
                            lastEndWindowTimestamp = lastStatsTimestamp;
                        }
                        long portElapsedMillis = Math.max(s.endWindowTimestamp - lastEndWindowTimestamp, 0);
                        //LOG.debug("=== EMITTED TUPLE COUNT for {}: {}, {}, {}, {}", operatorPortName, s.tupleCount, portElapsedMillis, operatorPortLastEndWindowTimestamps.get(operatorPortName), lastStatsTimestamp);
                        ps.tuplesPMSMA.add(s.tupleCount, portElapsedMillis);
                        ps.bufferServerBytesPMSMA.add(s.bufferServerBytes, portElapsedMillis);

                        operatorPortLastEndWindowTimestamps.put(operatorPortName, s.endWindowTimestamp);
                        if (maxEndWindowTimestamp < s.endWindowTimestamp) {
                            maxEndWindowTimestamp = s.endWindowTimestamp;
                        }
                    }
                    if (ports.size() > 0) {
                        endWindowStats.emitTimestamp = ports.iterator().next().endWindowTimestamp;
                    }
                    // need to remove dead ports, for unifiers
                    Iterator<Map.Entry<String, PortStatus>> it = status.outputPortStatusList.entrySet()
                            .iterator();
                    while (it.hasNext()) {
                        Map.Entry<String, PortStatus> entry = it.next();
                        if (!currentOutputPortSet.contains(entry.getKey())) {
                            it.remove();
                        }
                    }
                }

                // for output operator, just take the maximum dequeue time for emit timestamp.
                // (we don't know the latency for output operators because they don't emit tuples)
                if (endWindowStats.emitTimestamp < 0) {
                    endWindowStats.emitTimestamp = maxDequeueTimestamp;
                }

                if (status.currentWindowId.get() != stats.windowId) {
                    status.lastWindowIdChangeTms = currentTimeMillis;
                    status.currentWindowId.set(stats.windowId);
                }
                totalCpuTimeUsed += stats.cpuTimeUsed;
                statCount++;

                if (oper.getOperatorMeta().getValue(OperatorContext.COUNTERS_AGGREGATOR) != null) {
                    endWindowStats.counters = stats.counters;
                }
                if (oper.getOperatorMeta().getMetricAggregatorMeta() != null
                        && oper.getOperatorMeta().getMetricAggregatorMeta().getAggregator() != null) {
                    endWindowStats.metrics = stats.metrics;
                }

                if (stats.windowId > currentEndWindowStatsWindowId) {
                    Map<Integer, EndWindowStats> endWindowStatsMap = endWindowStatsOperatorMap
                            .get(stats.windowId);
                    if (endWindowStatsMap == null) {
                        endWindowStatsMap = new ConcurrentSkipListMap<Integer, EndWindowStats>();
                        Map<Integer, EndWindowStats> endWindowStatsMapPrevious = endWindowStatsOperatorMap
                                .putIfAbsent(stats.windowId, endWindowStatsMap);
                        if (endWindowStatsMapPrevious != null) {
                            endWindowStatsMap = endWindowStatsMapPrevious;
                        }
                    }
                    endWindowStatsMap.put(shb.getNodeId(), endWindowStats);

                    if (!oper.getInputs().isEmpty()) {
                        long latency = Long.MAX_VALUE;
                        long adjustedEndWindowEmitTimestamp = endWindowStats.emitTimestamp;
                        MovingAverageLong rpcLatency = rpcLatencies.get(oper.getContainer().getExternalId());
                        if (rpcLatency != null) {
                            adjustedEndWindowEmitTimestamp += rpcLatency.getAvg();
                        }
                        PTOperator slowestUpstream = null;
                        for (PTInput input : oper.getInputs()) {
                            PTOperator upstreamOp = input.source.source;
                            if (upstreamOp.getOperatorMeta().getOperator() instanceof Operator.DelayOperator) {
                                continue;
                            }
                            EndWindowStats ews = endWindowStatsMap.get(upstreamOp.getId());
                            long portLatency;
                            if (ews == null) {
                                // This is when the operator is likely to be behind too many windows. We need to give an estimate for
                                // latency at this point, by looking at the number of windows behind
                                int widthMillis = plan.getLogicalPlan()
                                        .getValue(LogicalPlan.STREAMING_WINDOW_SIZE_MILLIS);
                                portLatency = (upstreamOp.stats.currentWindowId.get()
                                        - oper.stats.currentWindowId.get()) * widthMillis;
                            } else {
                                MovingAverageLong upstreamRPCLatency = rpcLatencies
                                        .get(upstreamOp.getContainer().getExternalId());
                                portLatency = adjustedEndWindowEmitTimestamp - ews.emitTimestamp;
                                if (upstreamRPCLatency != null) {
                                    portLatency -= upstreamRPCLatency.getAvg();
                                }
                            }
                            if (portLatency < 0) {
                                portLatency = 0;
                            }
                            if (latency > portLatency) {
                                latency = portLatency;
                                slowestUpstream = upstreamOp;
                            }
                        }
                        status.latencyMA.add(latency);
                        slowestUpstreamOp.put(oper, slowestUpstream);
                    }

                    Set<Integer> allCurrentOperators = plan.getAllOperators().keySet();
                    int numOperators = plan.getAllOperators().size();
                    if (allCurrentOperators.containsAll(endWindowStatsMap.keySet())
                            && endWindowStatsMap.size() == numOperators) {
                        completeEndWindowStatsWindowId = stats.windowId;
                    }
                }
            }

            status.totalTuplesProcessed.add(tuplesProcessed);
            status.totalTuplesEmitted.add(tuplesEmitted);
            OperatorMeta logicalOperator = oper.getOperatorMeta();
            LogicalOperatorStatus logicalStatus = logicalOperator.getStatus();
            if (!oper.isUnifier()) {
                logicalStatus.totalTuplesProcessed += tuplesProcessed;
                logicalStatus.totalTuplesEmitted += tuplesEmitted;
            }
            long lastMaxEndWindowTimestamp = operatorLastEndWindowTimestamps.containsKey(oper.getId())
                    ? operatorLastEndWindowTimestamps.get(oper.getId())
                    : lastStatsTimestamp;
            if (maxEndWindowTimestamp >= lastMaxEndWindowTimestamp) {
                double tuplesProcessedPMSMA = 0.0;
                double tuplesEmittedPMSMA = 0.0;
                if (statCount != 0) {
                    //LOG.debug("CPU for {}: {} / {} - {}", oper.getId(), totalCpuTimeUsed, maxEndWindowTimestamp, lastMaxEndWindowTimestamp);
                    status.cpuNanosPMSMA.add(totalCpuTimeUsed,
                            maxEndWindowTimestamp - lastMaxEndWindowTimestamp);
                }

                for (PortStatus ps : status.inputPortStatusList.values()) {
                    tuplesProcessedPMSMA += ps.tuplesPMSMA.getAvg();
                }
                for (PortStatus ps : status.outputPortStatusList.values()) {
                    tuplesEmittedPMSMA += ps.tuplesPMSMA.getAvg();
                }
                status.tuplesProcessedPSMA.set(Math.round(tuplesProcessedPMSMA * 1000));
                status.tuplesEmittedPSMA.set(Math.round(tuplesEmittedPMSMA * 1000));
            } else {
                //LOG.warn("This timestamp for {} is lower than the previous!! {} < {}", oper.getId(), maxEndWindowTimestamp, lastMaxEndWindowTimestamp);
            }
            operatorLastEndWindowTimestamps.put(oper.getId(), maxEndWindowTimestamp);
            status.listenerStats.add(statsList);
            this.reportStats.put(oper, oper);

            status.statsRevs.commit();
        }
        if (lastStatsTimestamp < maxEndWindowTimestamp) {
            lastStatsTimestamp = maxEndWindowTimestamp;
        }
    }

    sca.lastHeartbeatMillis = currentTimeMillis;

    for (PTOperator oper : sca.container.getOperators()) {
        if (!reportedOperators.contains(oper.getId())) {
            processOperatorDeployStatus(oper, null, sca);
        }
    }

    ContainerHeartbeatResponse rsp = getHeartbeatResponse(sca);

    if (heartbeat.getContainerStats().operators.isEmpty() && isApplicationIdle()) {
        LOG.info("requesting idle shutdown for container {}", heartbeat.getContainerId());
        rsp.shutdown = true;
    } else {
        if (sca.shutdownRequested) {
            LOG.info("requesting shutdown for container {}", heartbeat.getContainerId());
            rsp.shutdown = true;
        }
    }

    List<StramToNodeRequest> requests = rsp.nodeRequests != null ? rsp.nodeRequests
            : new ArrayList<StramToNodeRequest>();
    ConcurrentLinkedQueue<StramToNodeRequest> operatorRequests = sca.getOperatorRequests();
    while (true) {
        StramToNodeRequest r = operatorRequests.poll();
        if (r == null) {
            break;
        }
        requests.add(r);
    }
    rsp.nodeRequests = requests;
    rsp.committedWindowId = committedWindowId;
    return rsp;
}

From source file:de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelImportServiceImpl.java

void createBuildingBlockRelations(InfrastructureElement ie, Map<String, CellValueHolder> relations,
        Locale locale) {//from   ww w  . j a v a 2s  . c om
    // Parent
    CellValueHolder hierarchyNameCellValueHolder = relations
            .get(getHierarchyHeaderFor(Constants.BB_INFRASTRUCTUREELEMENT_PLURAL, locale));
    setParentRelation(ie, hierarchyNameCellValueHolder);

    CellValueHolder tcrNamesCellValueHolder = relations
            .get(MessageAccess.getStringOrNull(Constants.BB_TECHNICALCOMPONENTRELEASE_PLURAL, locale));
    if (tcrNamesCellValueHolder != null) {
        Set<TechnicalComponentRelease> tcrSet = loadBuildingBlocksAsSet(
                TypeOfBuildingBlock.TECHNICALCOMPONENTRELEASE, tcrNamesCellValueHolder);
        Set<Tcr2IeAssociation> associations = Sets.newHashSetWithExpectedSize(tcrSet.size());
        Set<Tcr2IeAssociation> existentAssociations = ie.getTechnicalComponentReleaseAssociations();
        for (TechnicalComponentRelease tcr : tcrSet) {
            Tcr2IeAssociation assoc = BuildingBlockFactory.createTcr2IeAssociation(tcr, ie);
            // try to find out whether that association exists already, and if yes, use the existing object
            for (Tcr2IeAssociation connectedAssoc : existentAssociations) {
                assoc = connectedAssoc;
                break;
            }
            buildingBlockServiceLocator.getTcr2IeAssociationService().saveAssociations(associations);
            associations.add(assoc);
        }
        ie.connectTcr2IeAssociations(associations);
    }

    CellValueHolder isrCellValueHolder = relations
            .get(MessageAccess.getStringOrNull(Constants.BB_INFORMATIONSYSTEMRELEASE_PLURAL, locale));
    if (isrCellValueHolder != null) {
        Set<InformationSystemRelease> isrs = loadBuildingBlocksAsSet(
                TypeOfBuildingBlock.INFORMATIONSYSTEMRELEASE, isrCellValueHolder);
        ie.removeInformationSystemReleases();
        ie.addInformationSystemReleases(isrs);
    }

    getServiceFor(TypeOfBuildingBlock.INFRASTRUCTUREELEMENT).saveOrUpdate(ie);
}

From source file:de.iteratec.iteraplan.businesslogic.service.legacyExcel.ExcelImportServiceImpl.java

void createBuildingBlockRelations(TechnicalComponentRelease tcr, Map<String, CellValueHolder> relations,
        Locale locale) {/*from ww w  .  j av a2 s  .c  o  m*/
    CellValueHolder ieNamesCellValueHolder = relations
            .get(MessageAccess.getStringOrNull(Constants.BB_INFRASTRUCTUREELEMENT_PLURAL, locale));
    if (ieNamesCellValueHolder != null) {
        Set<InfrastructureElement> ieSet = loadBuildingBlocksAsSet(TypeOfBuildingBlock.INFRASTRUCTUREELEMENT,
                ieNamesCellValueHolder);
        Set<Tcr2IeAssociation> associations = Sets.newHashSetWithExpectedSize(ieSet.size());
        Set<Tcr2IeAssociation> existentAssociations = tcr.getInfrastructureElementAssociations();
        for (InfrastructureElement ie : ieSet) {
            Tcr2IeAssociation assoc = BuildingBlockFactory.createTcr2IeAssociation(tcr, ie);
            // try to find out whether that association exists already, and if yes, use the existing object
            for (Tcr2IeAssociation connectedAssoc : existentAssociations) {
                assoc = connectedAssoc;
                break;
            }
            buildingBlockServiceLocator.getTcr2IeAssociationService().saveAssociations(associations);
            associations.add(assoc);
        }
        tcr.connectTcr2IeAssociations(associations);
    }

    // Interfaces (not imported, just warn user)
    // Reason: We can't uniquely identify an Interface by its "name", since multiple ISIs with the same 2 ISRs can exist
    CellValueHolder interfacesCellValueHolder = relations.get(MessageAccess
            .getStringOrNull(ExcelConstants.HEADER_TECHNICALCOMPONENTRELEASE_SHEET_INTERFACES_COLUMN, locale));
    if (interfacesCellValueHolder != null) {
        String interfaces = interfacesCellValueHolder.getAttributeValue();
        String cellRef = ExcelImportUtilities.getCellRef(interfacesCellValueHolder.getOriginCell());
        if (!StringUtils.isEmpty(interfaces)) {
            getProcessingLog().warn(
                    "Technical Component {0} has interfaces specified in cell [{1}]. These were not imported. Only interfaces listed in the Interface sheet are imported.",
                    tcr.getNonHierarchicalName(), cellRef);
        }
    }

    CellValueHolder isrCellValueHolder = relations
            .get(MessageAccess.getStringOrNull(Constants.BB_INFORMATIONSYSTEMRELEASE_PLURAL, locale));
    if (isrCellValueHolder != null) {
        Set<InformationSystemRelease> isrs = loadBuildingBlocksAsSet(
                TypeOfBuildingBlock.INFORMATIONSYSTEMRELEASE, isrCellValueHolder);
        tcr.removeInformationSystemReleases();
        tcr.addInformationSystemReleases(isrs);
    }

    // import ArchitecturalDomains
    CellValueHolder adCellValueHolder = relations
            .get(MessageAccess.getStringOrNull(Constants.BB_ARCHITECTURALDOMAIN_PLURAL, locale));
    if (adCellValueHolder != null) {
        Set<ArchitecturalDomain> ads = loadBuildingBlocksAsSet(TypeOfBuildingBlock.ARCHITECTURALDOMAIN,
                adCellValueHolder);
        tcr.removeArchitecturalDomains();
        tcr.addArchitecturalDomains(ads);
    }

    CellValueHolder tcrNamesCellValueHolder = relations.get(MessageAccess.getStringOrNull(
            ExcelConstants.HEADER_TECHNICALCOMPONENTRELEASE_SHEET_BASECOMPONENTS_COLUMN, locale));
    if (tcrNamesCellValueHolder != null) {
        Set<TechnicalComponentRelease> usedTcrs = loadBuildingBlocksAsSet(
                TypeOfBuildingBlock.TECHNICALCOMPONENTRELEASE, tcrNamesCellValueHolder);
        tcr.removeBaseComponents();
        tcr.addBaseComponents(usedTcrs);
    }

    CellValueHolder predecessorsCellValueHolder = relations
            .get(MessageAccess.getStringOrNull(Constants.ASSOC_PREDECESSORS, locale));
    if (predecessorsCellValueHolder != null) {
        Set<TechnicalComponentRelease> preds = loadBuildingBlocksAsSet(
                TypeOfBuildingBlock.TECHNICALCOMPONENTRELEASE, predecessorsCellValueHolder);
        tcr.removePredecessors();
        tcr.addPredecessors(preds);
    }

    CellValueHolder successorsCellValueHolder = relations
            .get(MessageAccess.getStringOrNull(Constants.ASSOC_SUCCESSORS, locale));
    if (successorsCellValueHolder != null) {
        Set<TechnicalComponentRelease> succs = loadBuildingBlocksAsSet(
                TypeOfBuildingBlock.TECHNICALCOMPONENTRELEASE, successorsCellValueHolder);
        tcr.removeSuccessors();
        tcr.addSuccessors(succs);
    }

    getTechnicalComponentReleaseService().saveOrUpdate(tcr);
}

From source file:com.chen.mail.browse.ConversationCursor.java

/**
 * If a destructive notification action was triggered, but has not yet been processed because an
 * "Undo" action is available, we do not want to show the conversation in the list.
 *///from  w  w  w.  j  a  va2 s.  c  om
public void handleNotificationActions() {
    // Needs to be on the UI thread because it updates the ConversationCursor's internal
    // state which violates assumptions about how the ListView works and how
    // the ConversationViewPager works if performed off of the UI thread.
    // Also, prevents ConcurrentModificationExceptions on mNotificationTempDeleted.
    mMainThreadHandler.post(new Runnable() {
        @Override
        public void run() {
            final SparseArrayCompat<NotificationActionUtils.NotificationAction> undoNotifications = NotificationActionUtils.sUndoNotifications;
            final Set<Conversation> undoneConversations = NotificationActionUtils.sUndoneConversations;

            final Set<Conversation> undoConversations = Sets
                    .newHashSetWithExpectedSize(undoNotifications.size());

            boolean changed = false;

            for (int i = 0; i < undoNotifications.size(); i++) {
                final NotificationActionUtils.NotificationAction notificationAction = undoNotifications
                        .get(undoNotifications.keyAt(i));

                // We only care about notifications that were for this folder
                // or if the action was delete
                final Folder folder = notificationAction.getFolder();
                final boolean deleteAction = notificationAction
                        .getNotificationActionType() == NotificationActionUtils.NotificationActionType.DELETE;

                if (folder.conversationListUri.equals(qUri) || deleteAction) {
                    // We only care about destructive actions
                    if (notificationAction.getNotificationActionType().getIsDestructive()) {
                        final Conversation conversation = notificationAction.getConversation();

                        undoConversations.add(conversation);

                        if (!mNotificationTempDeleted.contains(conversation)) {
                            sProvider.deleteLocal(conversation.uri, ConversationCursor.this);
                            mNotificationTempDeleted.add(conversation);

                            changed = true;
                        }
                    }
                }
            }

            // Remove any conversations from the temporary deleted state
            // if they no longer have an undo notification
            final Iterator<Conversation> iterator = mNotificationTempDeleted.iterator();
            while (iterator.hasNext()) {
                final Conversation conversation = iterator.next();

                if (!undoConversations.contains(conversation)) {
                    // We should only be un-deleting local cursor edits
                    // if the notification was undone rather than just
                    // disappearing because the internal cursor
                    // gets updated when the undo goes away via timeout which
                    // will update everything properly.
                    if (undoneConversations.contains(conversation)) {
                        sProvider.undeleteLocal(conversation.uri, ConversationCursor.this);
                        undoneConversations.remove(conversation);
                    }
                    iterator.remove();

                    changed = true;
                }
            }

            if (changed) {
                notifyDataChanged();
            }
        }
    });
}

From source file:com.indeema.mail.browse.ConversationCursor.java

/**
 * If a destructive notification action was triggered, but has not yet been processed because an
 * "Undo" action is available, we do not want to show the conversation in the list.
 *//* w  w  w .jav a 2s  . co m*/
public void handleNotificationActions() {
    // Needs to be on the UI thread because it updates the ConversationCursor's internal
    // state which violates assumptions about how the ListView works and how
    // the ConversationViewPager works if performed off of the UI thread.
    // Also, prevents ConcurrentModificationExceptions on mNotificationTempDeleted.
    mMainThreadHandler.post(new Runnable() {
        @Override
        public void run() {
            final SparseArrayCompat<NotificationAction> undoNotifications = NotificationActionUtils.sUndoNotifications;
            final Set<Conversation> undoneConversations = NotificationActionUtils.sUndoneConversations;

            final Set<Conversation> undoConversations = Sets
                    .newHashSetWithExpectedSize(undoNotifications.size());

            boolean changed = false;

            for (int i = 0; i < undoNotifications.size(); i++) {
                final NotificationAction notificationAction = undoNotifications.get(undoNotifications.keyAt(i));

                // We only care about notifications that were for this folder
                // or if the action was delete
                final Folder folder = notificationAction.getFolder();
                final boolean deleteAction = notificationAction
                        .getNotificationActionType() == NotificationActionType.DELETE;

                if (folder.conversationListUri.equals(qUri) || deleteAction) {
                    // We only care about destructive actions
                    if (notificationAction.getNotificationActionType().getIsDestructive()) {
                        final Conversation conversation = notificationAction.getConversation();

                        undoConversations.add(conversation);

                        if (!mNotificationTempDeleted.contains(conversation)) {
                            sProvider.deleteLocal(conversation.uri, ConversationCursor.this);
                            mNotificationTempDeleted.add(conversation);

                            changed = true;
                        }
                    }
                }
            }

            // Remove any conversations from the temporary deleted state
            // if they no longer have an undo notification
            final Iterator<Conversation> iterator = mNotificationTempDeleted.iterator();
            while (iterator.hasNext()) {
                final Conversation conversation = iterator.next();

                if (!undoConversations.contains(conversation)) {
                    // We should only be un-deleting local cursor edits
                    // if the notification was undone rather than just
                    // disappearing because the internal cursor
                    // gets updated when the undo goes away via timeout which
                    // will update everything properly.
                    if (undoneConversations.contains(conversation)) {
                        sProvider.undeleteLocal(conversation.uri, ConversationCursor.this);
                        undoneConversations.remove(conversation);
                    }
                    iterator.remove();

                    changed = true;
                }
            }

            if (changed) {
                notifyDataChanged();
            }
        }
    });
}