Sort order and diff fixes

This commit is contained in:
Andrii Shvaika 2022-05-31 16:38:10 +03:00
parent 09402f40ac
commit 59325226ac
4 changed files with 40 additions and 14 deletions

View File

@ -328,16 +328,17 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont
EntityId externalId = ((ExportableEntity<EntityId>) entity).getExternalId();
if (externalId == null) externalId = entityId;
EntityExportData<?> currentVersion = exportImportService.exportEntity(user, entityId, EntityExportSettings.builder()
.exportRelations(true)
.exportAttributes(true)
.build());
return transformAsync(gitServiceQueue.getEntity(user.getTenantId(), versionId, externalId),
otherVersion -> transform(gitServiceQueue.getContentsDiff(user.getTenantId(),
JacksonUtil.toPrettyString(currentVersion),
JacksonUtil.toPrettyString(otherVersion)), rawDiff -> {
return new EntityDataDiff(currentVersion, otherVersion, rawDiff);
}, MoreExecutors.directExecutor()), MoreExecutors.directExecutor());
otherVersion -> {
EntityExportData<?> currentVersion = exportImportService.exportEntity(user, entityId, EntityExportSettings.builder()
.exportRelations(otherVersion.getRelations() != null)
.exportAttributes(otherVersion.getAttributes() != null)
.build());
return transform(gitServiceQueue.getContentsDiff(user.getTenantId(),
JacksonUtil.toPrettyString(currentVersion.sort()),
JacksonUtil.toPrettyString(otherVersion.sort())),
rawDiff -> new EntityDataDiff(currentVersion, otherVersion, rawDiff), MoreExecutors.directExecutor());
}, MoreExecutors.directExecutor());
}
@Override

View File

@ -109,7 +109,7 @@ public class DefaultGitVersionControlQueueService implements GitVersionControlQu
SettableFuture<Void> future = SettableFuture.create();
String path = getRelativePath(entityData.getEntityType(), entityData.getEntity().getId());
String entityDataJson = JacksonUtil.toPrettyString(entityData);
String entityDataJson = JacksonUtil.toPrettyString(entityData.sort());
registerAndSend(commit, builder -> builder.setCommitRequest(
buildCommitRequest(commit).setAddMsg(
@ -130,7 +130,7 @@ public class DefaultGitVersionControlQueueService implements GitVersionControlQu
buildCommitRequest(commit).setDeleteMsg(
TransportProtos.DeleteMsg.newBuilder().setRelativePath(path).build()
).build()
).build(), wrap(commit.getFuture(), null));
).build(), wrap(future, null));
return future;
}

View File

@ -28,6 +28,8 @@ import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.sync.JsonTbEntity;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@ -41,6 +43,15 @@ import java.util.Map;
@Data
public class EntityExportData<E extends ExportableEntity<? extends EntityId>> {
public static final Comparator<EntityRelation> relationsComparator = Comparator
.comparing(EntityRelation::getFrom, Comparator.comparing(EntityId::getId))
.thenComparing(EntityRelation::getTo, Comparator.comparing(EntityId::getId))
.thenComparing(EntityRelation::getTypeGroup)
.thenComparing(EntityRelation::getType);
public static final Comparator<AttributeExportData> attrComparator = Comparator
.comparing(AttributeExportData::getKey).thenComparing(AttributeExportData::getLastUpdateTs);
@JsonTbEntity
private E entity;
private EntityType entityType;
@ -48,4 +59,14 @@ public class EntityExportData<E extends ExportableEntity<? extends EntityId>> {
private List<EntityRelation> relations;
private Map<String, List<AttributeExportData>> attributes;
public EntityExportData<E> sort() {
if (relations != null && !relations.isEmpty()) {
relations.sort(relationsComparator);
}
if (attributes != null && !attributes.isEmpty()) {
attributes.values().forEach(list -> list.sort(attrComparator));
}
return this;
}
}

View File

@ -18,8 +18,10 @@ package org.thingsboard.common.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
@ -31,8 +33,10 @@ import java.util.Arrays;
public class JacksonUtil {
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static final ObjectMapper PRETTY_JSON_MAPPER = new ObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT);
public static final ObjectMapper PRETTY_SORTED_JSON_MAPPER = JsonMapper.builder().enable(SerializationFeature.INDENT_OUTPUT)
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.build();
public static <T> T convertValue(Object fromValue, Class<T> toValueType) {
try {
@ -99,7 +103,7 @@ public class JacksonUtil {
public static String toPrettyString(Object o) {
try {
return PRETTY_JSON_MAPPER.writeValueAsString(o);
return PRETTY_SORTED_JSON_MAPPER.writeValueAsString(o);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}