org.voltdb.importer.ChannelAssignment Maven / Gradle / Ivy
/* This file is part of VoltDB.
* Copyright (C) 2008-2018 VoltDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with VoltDB. If not, see .
*/
package org.voltdb.importer;
import java.net.URI;
import java.util.List;
import java.util.NavigableSet;
import java.util.Set;
import com.google_voltpatches.common.collect.ImmutableList;
import com.google_voltpatches.common.collect.ImmutableSet;
import com.google_voltpatches.common.collect.ImmutableSetMultimap;
import com.google_voltpatches.common.collect.SetMultimap;
import com.google_voltpatches.common.collect.Sets;
public class ChannelAssignment {
final Set added;
final Set removed;
final NavigableSet channels;
final int version;
final List assignments;
ChannelAssignment(NavigableSet prev, NavigableSet next, int version) {
this.version = version;
this.added = Sets.difference(next, prev);
this.removed = Sets.difference(prev, next);
this.channels = next;
this.assignments = perImporterAssignments();
}
public Set getAdded() {
return added;
}
public Set getRemoved() {
return removed;
}
public NavigableSet getChannels() {
return channels;
}
public int getVersion() {
return version;
}
public boolean hasChanges() {
return !removed.isEmpty() || !added.isEmpty();
}
@Override
public String toString() {
return "ChannelAssignment [added=" + added + ", removed=" + removed
+ ", channels=" + channels + ", version=" + version + "]";
}
public List getImporterChannelAssignments() {
return assignments;
}
private SetMultimap mapByImporter(Set specs) {
ImmutableSetMultimap.Builder mmbldr = ImmutableSetMultimap.builder();
for (ChannelSpec spec: specs) {
mmbldr.put(spec.getImporter(),spec.getUri());
}
return mmbldr.build();
}
private List perImporterAssignments() {
ImmutableSet.Builder sbldr = ImmutableSet.builder();
for (ChannelSpec spec: Sets.union(added, removed)) {
sbldr.add(spec.getImporter());
}
ImmutableList.Builder lbldr = ImmutableList.builder();
final SetMultimap added = mapByImporter(getAdded());
final SetMultimap removed = mapByImporter(getRemoved());
final SetMultimap assigned = mapByImporter(getChannels());
for (String importer: sbldr.build()) {
lbldr.add(new ImporterChannelAssignment(
importer,
added.get(importer),
removed.get(importer),
assigned.get(importer),
version
));
}
return lbldr.build();
}
}