Search in sources :

Example 1 with MapDifference

use of com.google.common.collect.MapDifference in project buck by facebook.

the class DaemonicCellState method invalidateIfEnvHasChanged.

Optional<MapDifference<String, String>> invalidateIfEnvHasChanged(Cell cell, Path buildFile) {
    // Invalidate if env vars have changed.
    ImmutableMap<String, Optional<String>> usedEnv;
    try (AutoCloseableLock readLock = rawAndComputedNodesLock.readLock()) {
        usedEnv = buildFileEnv.get(buildFile);
    }
    if (usedEnv == null) {
        this.cell.set(cell);
        return Optional.empty();
    }
    for (Map.Entry<String, Optional<String>> ent : usedEnv.entrySet()) {
        Optional<String> value = Optional.ofNullable(cell.getBuckConfig().getEnvironment().get(ent.getKey()));
        if (!value.equals(ent.getValue())) {
            invalidatePath(buildFile);
            this.cell.set(cell);
            return Optional.of(Maps.difference(value.map(v -> ImmutableMap.of(ent.getKey(), v)).orElse(ImmutableMap.of()), ent.getValue().map(v -> ImmutableMap.of(ent.getKey(), v)).orElse(ImmutableMap.of())));
        }
    }
    return Optional.empty();
}
Also used : Logger(com.facebook.buck.log.Logger) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) BuildTargetException(com.facebook.buck.model.BuildTargetException) AutoCloseableReadWriteUpdateLock(com.facebook.buck.util.concurrent.AutoCloseableReadWriteUpdateLock) HashMap(java.util.HashMap) GuardedBy(javax.annotation.concurrent.GuardedBy) BuildTarget(com.facebook.buck.model.BuildTarget) Maps(com.google.common.collect.Maps) AtomicReference(java.util.concurrent.atomic.AtomicReference) SetMultimap(com.google.common.collect.SetMultimap) ConcurrentMap(java.util.concurrent.ConcurrentMap) MapDifference(com.google.common.collect.MapDifference) AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock) HashMultimap(com.google.common.collect.HashMultimap) Map(java.util.Map) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Cell(com.facebook.buck.rules.Cell) Path(java.nio.file.Path) Optional(java.util.Optional) AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 2 with MapDifference

use of com.google.common.collect.MapDifference in project cassandra by apache.

the class SchemaKeyspace method makeUpdateTableMutation.

static Mutation.SimpleBuilder makeUpdateTableMutation(KeyspaceMetadata keyspace, TableMetadata oldTable, TableMetadata newTable, long timestamp) {
    Mutation.SimpleBuilder builder = makeCreateKeyspaceMutation(keyspace.name, keyspace.params, timestamp);
    addTableToSchemaMutation(newTable, false, builder);
    MapDifference<ByteBuffer, ColumnMetadata> columnDiff = Maps.difference(oldTable.columns, newTable.columns);
    // columns that are no longer needed
    for (ColumnMetadata column : columnDiff.entriesOnlyOnLeft().values()) dropColumnFromSchemaMutation(oldTable, column, builder);
    // newly added columns
    for (ColumnMetadata column : columnDiff.entriesOnlyOnRight().values()) addColumnToSchemaMutation(newTable, column, builder);
    // old columns with updated attributes
    for (ByteBuffer name : columnDiff.entriesDiffering().keySet()) addColumnToSchemaMutation(newTable, newTable.getColumn(name), builder);
    // dropped columns
    MapDifference<ByteBuffer, DroppedColumn> droppedColumnDiff = Maps.difference(oldTable.droppedColumns, newTable.droppedColumns);
    // newly dropped columns
    for (DroppedColumn column : droppedColumnDiff.entriesOnlyOnRight().values()) addDroppedColumnToSchemaMutation(newTable, column, builder);
    // columns added then dropped again
    for (ByteBuffer name : droppedColumnDiff.entriesDiffering().keySet()) addDroppedColumnToSchemaMutation(newTable, newTable.droppedColumns.get(name), builder);
    MapDifference<String, TriggerMetadata> triggerDiff = triggersDiff(oldTable.triggers, newTable.triggers);
    // dropped triggers
    for (TriggerMetadata trigger : triggerDiff.entriesOnlyOnLeft().values()) dropTriggerFromSchemaMutation(oldTable, trigger, builder);
    // newly created triggers
    for (TriggerMetadata trigger : triggerDiff.entriesOnlyOnRight().values()) addTriggerToSchemaMutation(newTable, trigger, builder);
    MapDifference<String, IndexMetadata> indexesDiff = indexesDiff(oldTable.indexes, newTable.indexes);
    // dropped indexes
    for (IndexMetadata index : indexesDiff.entriesOnlyOnLeft().values()) dropIndexFromSchemaMutation(oldTable, index, builder);
    // newly created indexes
    for (IndexMetadata index : indexesDiff.entriesOnlyOnRight().values()) addIndexToSchemaMutation(newTable, index, builder);
    // updated indexes need to be updated
    for (MapDifference.ValueDifference<IndexMetadata> diff : indexesDiff.entriesDiffering().values()) addUpdatedIndexToSchemaMutation(newTable, diff.rightValue(), builder);
    return builder;
}
Also used : MapDifference(com.google.common.collect.MapDifference) ByteBuffer(java.nio.ByteBuffer)

Example 3 with MapDifference

use of com.google.common.collect.MapDifference in project cassandra by apache.

the class Schema method reload.

private void reload(KeyspaceMetadata previous, KeyspaceMetadata updated) {
    Keyspace keyspace = getKeyspaceInstance(updated.name);
    if (keyspace != null)
        keyspace.setMetadata(updated);
    MapDifference<TableId, TableMetadata> tablesDiff = previous.tables.diff(updated.tables);
    MapDifference<TableId, ViewMetadata> viewsDiff = previous.views.diff(updated.views);
    MapDifference<String, TableMetadata> indexesDiff = previous.tables.indexesDiff(updated.tables);
    // clean up after removed entries
    tablesDiff.entriesOnlyOnLeft().values().forEach(table -> metadataRefs.remove(table.id));
    viewsDiff.entriesOnlyOnLeft().values().forEach(view -> metadataRefs.remove(view.metadata.id));
    indexesDiff.entriesOnlyOnLeft().values().forEach(indexTable -> indexMetadataRefs.remove(Pair.create(indexTable.keyspace, indexTable.indexName().get())));
    // load up new entries
    tablesDiff.entriesOnlyOnRight().values().forEach(table -> metadataRefs.put(table.id, new TableMetadataRef(table)));
    viewsDiff.entriesOnlyOnRight().values().forEach(view -> metadataRefs.put(view.metadata.id, new TableMetadataRef(view.metadata)));
    indexesDiff.entriesOnlyOnRight().values().forEach(indexTable -> indexMetadataRefs.put(Pair.create(indexTable.keyspace, indexTable.indexName().get()), new TableMetadataRef(indexTable)));
    // refresh refs to updated ones
    tablesDiff.entriesDiffering().values().forEach(diff -> metadataRefs.get(diff.rightValue().id).set(diff.rightValue()));
    viewsDiff.entriesDiffering().values().forEach(diff -> metadataRefs.get(diff.rightValue().metadata.id).set(diff.rightValue().metadata));
    indexesDiff.entriesDiffering().values().stream().map(MapDifference.ValueDifference::rightValue).forEach(indexTable -> indexMetadataRefs.get(Pair.create(indexTable.keyspace, indexTable.indexName().get())).set(indexTable));
}
Also used : MapDifference(com.google.common.collect.MapDifference) SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) Keyspace(org.apache.cassandra.db.Keyspace)

Example 4 with MapDifference

use of com.google.common.collect.MapDifference in project buck by facebook.

the class DaemonicParserState method invalidateIfBuckConfigOrEnvHasChanged.

private synchronized void invalidateIfBuckConfigOrEnvHasChanged(Cell cell, Path buildFile) {
    try (AutoCloseableLock readLock = cellStateLock.readLock()) {
        DaemonicCellState state = cellPathToDaemonicState.get(cell.getRoot());
        if (state == null) {
            return;
        }
        // Invalidates and also keeps the state cell up-to-date
        state.invalidateIfBuckConfigHasChanged(cell, buildFile);
        Optional<MapDifference<String, String>> envDiff = state.invalidateIfEnvHasChanged(cell, buildFile);
        if (envDiff.isPresent()) {
            MapDifference<String, String> diff = envDiff.get();
            LOG.warn("Invalidating cache on environment change (%s)", diff);
            Set<String> environmentChanges = new HashSet<>();
            environmentChanges.addAll(diff.entriesOnlyOnLeft().keySet());
            environmentChanges.addAll(diff.entriesOnlyOnRight().keySet());
            environmentChanges.addAll(diff.entriesDiffering().keySet());
            cacheInvalidatedByEnvironmentVariableChangeCounter.addAll(environmentChanges);
            broadcastEventListener.broadcast(ParsingEvent.environmentalChange(environmentChanges.toString()));
        }
    }
}
Also used : MapDifference(com.google.common.collect.MapDifference) AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock) HashSet(java.util.HashSet)

Aggregations

MapDifference (com.google.common.collect.MapDifference)4 AutoCloseableLock (com.facebook.buck.util.concurrent.AutoCloseableLock)2 Logger (com.facebook.buck.log.Logger)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 BuildTargetException (com.facebook.buck.model.BuildTargetException)1 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)1 Cell (com.facebook.buck.rules.Cell)1 AutoCloseableReadWriteUpdateLock (com.facebook.buck.util.concurrent.AutoCloseableReadWriteUpdateLock)1 Preconditions (com.google.common.base.Preconditions)1 HashMultimap (com.google.common.collect.HashMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Maps (com.google.common.collect.Maps)1 SetMultimap (com.google.common.collect.SetMultimap)1 ByteBuffer (java.nio.ByteBuffer)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1