下面列出了com.google.common.collect.MapDifference#entriesOnlyOnLeft ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private CubeAssignment reassignCubeImpl(String cubeName, CubeAssignment preAssignments,
CubeAssignment newAssignments) {
logger.info("start cube reBalance, cube:{}, previous assignments:{}, new assignments:{}", cubeName,
preAssignments, newAssignments);
if (newAssignments.equals(preAssignments)) {
logger.info("the new assignment is the same as the previous assignment, do nothing for this reassignment");
return newAssignments;
}
CubeInstance cubeInstance = CubeManager.getInstance(KylinConfig.getInstanceFromEnv()).getCube(cubeName);
doReassign(cubeInstance, preAssignments, newAssignments);
MapDifference<Integer, List<Partition>> assignDiff = Maps.difference(preAssignments.getAssignments(),
newAssignments.getAssignments());
// add empty partitions to the removed replica sets, means that there's still data in the replica set, but no new data will be consumed.
Map<Integer, List<Partition>> removedAssign = assignDiff.entriesOnlyOnLeft();
for (Integer removedReplicaSet : removedAssign.keySet()) {
newAssignments.addAssignment(removedReplicaSet, Lists.<Partition> newArrayList());
}
streamMetadataStore.saveNewCubeAssignment(newAssignments);
AssignmentsCache.getInstance().clearCubeCache(cubeName);
return newAssignments;
}
@NotAtomicAndNotIdempotent
void reassignCubeImpl(String cubeName, CubeAssignment preAssignments, CubeAssignment newAssignments) {
logger.info("start cube reBalance, cube:{}, previous assignments:{}, new assignments:{}", cubeName,
preAssignments, newAssignments);
if (newAssignments.equals(preAssignments)) {
logger.info("the new assignment is the same as the previous assignment, do nothing for this reassignment");
return;
}
CubeInstance cubeInstance = getCoordinator().getCubeManager().getCube(cubeName);
doReassignWithoutCommit(cubeInstance, preAssignments, newAssignments);
// add empty partitions to the removed replica sets, means that there's still data in the replica set, but no new data will be consumed.
MapDifference<Integer, List<Partition>> assignDiff = Maps.difference(preAssignments.getAssignments(),
newAssignments.getAssignments());
Map<Integer, List<Partition>> removedAssign = assignDiff.entriesOnlyOnLeft();
for (Integer removedReplicaSet : removedAssign.keySet()) {
newAssignments.addAssignment(removedReplicaSet, Lists.<Partition> newArrayList());
}
logger.info("Commit reassign {} transaction.", cubeName);
getCoordinator().getStreamMetadataStore().saveNewCubeAssignment(newAssignments);
AssignmentsCache.getInstance().clearCubeCache(cubeName);
}
@Test
public void givenDifferentMaps_whenGetEntriesOnOneSideUsingGuava_thenSuccess() {
Map<String, String> asia1 = new HashMap<String, String>();
asia1.put("Japan", "Tokyo");
asia1.put("South Korea", "Seoul");
asia1.put("India", "New Delhi");
Map<String, String> asia2 = new HashMap<String, String>();
asia2.put("Japan", "Tokyo");
asia2.put("China", "Beijing");
asia2.put("India", "Delhi");
MapDifference<String, String> diff = Maps.difference(asia1, asia2);
Map<String, String> entriesOnlyOnRight = diff.entriesOnlyOnRight();
Map<String, String> entriesOnlyOnLeft = diff.entriesOnlyOnLeft();
assertEquals(1, entriesOnlyOnRight.size());
assertThat(entriesOnlyOnRight, hasEntry("China", "Beijing"));
assertEquals(1, entriesOnlyOnLeft.size());
assertThat(entriesOnlyOnLeft, hasEntry("South Korea", "Seoul"));
}
public static void safeUpdateByUpdateOrder(Map t, Map updateInfo) {
MapDifference difference = Maps.difference(updateInfo, t);
Map commonMap = difference.entriesInCommon();//求交集,交集为可以持续提供服务的数据源
t.putAll(commonMap);
Map entriesOnlyOnLeft = difference.entriesOnlyOnLeft();//两个map,左边有,右边没有的entry,为需要移除的数据源
for (Object s : entriesOnlyOnLeft.keySet()) {
t.remove(s);
}
t.putAll(updateInfo);
}
public Map<String, String> getAvailableNamedIds(NameIdConfig config) {
MapDifference<String, String> diff = Maps.difference(availableNamedIds, usedNamedIds);
Map<String, String> value = diff.entriesOnlyOnLeft();
Map<String, String> result = Maps.newHashMap(value);
if (config.getNameIdType() != null) {
result.put(config.getNameIdType(), config.getNameIdType());
}
return result;
}
@Test
public void entries_only_on_left() {
MapDifference<Integer, Student> mapDifference = Maps.difference(
geometryClass, gymClass);
Map<Integer, Student> studentsOnLeft = mapDifference
.entriesOnlyOnLeft();
logger.info(studentsOnLeft);
assertThat(studentsOnLeft, hasKey(new Integer(456)));
assertThat(studentsOnLeft, hasKey(new Integer(912)));
}
/**
* @return the difference between oldProps and newProps.
*/
public static String getPropertyDiff(Props oldProps, Props newProps) {
final StringBuilder builder = new StringBuilder("");
// oldProps can not be null during the below comparison process.
if (oldProps == null) {
oldProps = new Props();
}
if (newProps == null) {
newProps = new Props();
}
final MapDifference<String, String> md =
Maps.difference(toStringMap(oldProps, false), toStringMap(newProps, false));
final Map<String, String> newlyCreatedProperty = md.entriesOnlyOnRight();
if (newlyCreatedProperty != null && newlyCreatedProperty.size() > 0) {
builder.append("Newly created Properties: ");
newlyCreatedProperty.forEach((k, v) -> {
builder.append("[ " + k + ", " + v + "], ");
});
builder.append("\n");
}
final Map<String, String> deletedProperty = md.entriesOnlyOnLeft();
if (deletedProperty != null && deletedProperty.size() > 0) {
builder.append("Deleted Properties: ");
deletedProperty.forEach((k, v) -> {
builder.append("[ " + k + ", " + v + "], ");
});
builder.append("\n");
}
final Map<String, MapDifference.ValueDifference<String>> diffProperties = md.entriesDiffering();
if (diffProperties != null && diffProperties.size() > 0) {
builder.append("Modified Properties: ");
diffProperties.forEach((k, v) -> {
builder.append("[ " + k + ", " + v.leftValue() + "-->" + v.rightValue() + "], ");
});
}
return builder.toString();
}