From 1c7dcf8e177b6119fe0542e03e33d9aa1463fe35 Mon Sep 17 00:00:00 2001 From: yangzl Date: Thu, 19 Sep 2024 20:55:41 +0800 Subject: [PATCH 1/6] feat: 1. solve the problem of duplicate comments and blank lines 2. reduce the data growth of the item table 3. in the same case, the size of ItemChangeSets can be reduced 4. when revoke configuration, if there is only one comment or blank line, it will not be deleted. --- .../txtresolver/PropertyResolver.java | 64 +++++++++++-------- .../apollo/portal/service/ItemService.java | 12 ++-- .../portal/service/NamespaceService.java | 9 +-- .../txtresolver/PropertyResolverTest.java | 18 +++--- 4 files changed, 59 insertions(+), 44 deletions(-) diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java index 44080d7520d..39772c34763 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java @@ -21,15 +21,20 @@ import com.ctrip.framework.apollo.common.exception.BadRequestException; import com.ctrip.framework.apollo.common.utils.BeanUtils; +import com.ctrip.framework.apollo.core.utils.StringUtils; import com.google.common.base.Strings; import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; import javax.validation.constraints.NotNull; +import java.util.ArrayList; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; /** * normal property file resolver. @@ -45,12 +50,21 @@ public class PropertyResolver implements ConfigTextResolver { @Override public ItemChangeSets resolve(long namespaceId, String configText, List baseItems) { - Map oldLineNumMapItem = BeanUtils.mapByKey("lineNum", baseItems); Map oldKeyMapItem = BeanUtils.mapByKey("key", baseItems); - //remove comment and blank item map. oldKeyMapItem.remove(""); + // comment items + List baseCommentItems = new ArrayList<>(); + // blank items + List baseBlankItems = new ArrayList<>(); + if(!CollectionUtils.isEmpty(baseItems)) { + + baseCommentItems = baseItems.stream().filter(itemDTO -> isCommentItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toList()); + + baseBlankItems = baseItems.stream().filter(itemDTO -> isBlankItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toList()); + } + String[] newItems = configText.split(ITEM_SEPARATOR); Set repeatKeys = new HashSet<>(); if (isHasRepeatKey(newItems, repeatKeys)) { @@ -63,17 +77,25 @@ public ItemChangeSets resolve(long namespaceId, String configText, List for (String newItem : newItems) { newItem = newItem.trim(); newLineNumMapItem.put(lineCounter, newItem); - ItemDTO oldItemByLine = oldLineNumMapItem.get(lineCounter); //comment item if (isCommentItem(newItem)) { + ItemDTO oldItemDTO = null; + if(!CollectionUtils.isEmpty(baseCommentItems)) { + oldItemDTO = baseCommentItems.remove(0); + } - handleCommentLine(namespaceId, oldItemByLine, newItem, lineCounter, changeSets); + handleCommentLine(namespaceId, oldItemDTO, newItem, lineCounter, changeSets); //blank item } else if (isBlankItem(newItem)) { - handleBlankLine(namespaceId, oldItemByLine, lineCounter, changeSets); + ItemDTO oldItemDTO = null; + if(!CollectionUtils.isEmpty(baseBlankItems)) { + oldItemDTO = baseBlankItems.remove(0); + } + + handleBlankLine(namespaceId, oldItemDTO, lineCounter, changeSets); //normal item } else { @@ -83,7 +105,7 @@ public ItemChangeSets resolve(long namespaceId, String configText, List lineCounter++; } - deleteCommentAndBlankItem(oldLineNumMapItem, newLineNumMapItem, changeSets); + deleteCommentAndBlankItem(baseCommentItems, baseBlankItems, changeSets); deleteNormalKVItem(oldKeyMapItem, changeSets); return changeSets; @@ -122,16 +144,18 @@ private String[] parseKeyValueFromItem(String item) { } private void handleCommentLine(Long namespaceId, ItemDTO oldItemByLine, String newItem, int lineCounter, ItemChangeSets changeSets) { - String oldComment = oldItemByLine == null ? "" : oldItemByLine.getComment(); - //create comment. implement update comment by delete old comment and create new comment - if (!(isCommentItem(oldItemByLine) && newItem.equals(oldComment))) { + if(null == oldItemByLine ){ changeSets.addCreateItem(buildCommentItem(0L, namespaceId, newItem, lineCounter)); + }else if(!StringUtils.equals(oldItemByLine.getComment(), newItem) || lineCounter != oldItemByLine.getLineNum()) { + changeSets.addUpdateItem(buildCommentItem(oldItemByLine.getId(), namespaceId, newItem, lineCounter)); } } private void handleBlankLine(Long namespaceId, ItemDTO oldItem, int lineCounter, ItemChangeSets changeSets) { - if (!isBlankItem(oldItem)) { + if(null == oldItem ){ changeSets.addCreateItem(buildBlankItem(0L, namespaceId, lineCounter)); + }else if (lineCounter != oldItem.getLineNum()) { + changeSets.addUpdateItem(buildBlankItem(oldItem.getId(), namespaceId, lineCounter)); } } @@ -183,23 +207,11 @@ private void deleteNormalKVItem(Map baseKeyMapItem, ItemChangeS } } - private void deleteCommentAndBlankItem(Map oldLineNumMapItem, - Map newLineNumMapItem, + private void deleteCommentAndBlankItem(List baseCommentItems, + List baseBlankItems, ItemChangeSets changeSets) { - - for (Map.Entry entry : oldLineNumMapItem.entrySet()) { - int lineNum = entry.getKey(); - ItemDTO oldItem = entry.getValue(); - String newItem = newLineNumMapItem.get(lineNum); - - //1. old is blank by now is not - //2.old is comment by now is not exist or modified - //3.old is blank by now is not exist or modified - if ((isBlankItem(oldItem) && !isBlankItem(newItem)) - || (isCommentItem(oldItem) || isBlankItem(oldItem)) && (newItem == null || !newItem.equals(oldItem.getComment()))) { - changeSets.addDeleteItem(oldItem); - } - } + baseCommentItems.forEach(oldItemDTO-> changeSets.addDeleteItem(oldItemDTO)); + baseBlankItems.forEach(oldItemDTO-> changeSets.addDeleteItem(oldItemDTO)); } private ItemDTO buildCommentItem(Long id, Long namespaceId, String comment, int lineNum) { diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java index b1677e18858..2c7aa57c195 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java @@ -50,6 +50,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; @Service public class ItemService { @@ -216,12 +217,13 @@ public void revokeItem(String appId, Env env, String clusterName, String namespa } List baseItems = itemAPI.findItems(appId, env, clusterName, namespaceName); Map oldKeyMapItem = BeanUtils.mapByKey("key", baseItems); - Map deletedItemDTOs = new HashMap<>(); + //remove comment and blank item map. + oldKeyMapItem.remove(""); //deleted items for comment - findDeletedItems(appId, env, clusterName, namespaceName).forEach(item -> { - deletedItemDTOs.put(item.getKey(),item); - }); + Map deletedItemDTOs = findDeletedItems(appId, env, clusterName, namespaceName).stream() + .filter(itemDTO -> !StringUtils.isEmpty(itemDTO.getKey())) + .collect(Collectors.toMap(itemDTO -> itemDTO.getKey(), v -> v, (v1, v2) -> v2)); ItemChangeSets changeSets = new ItemChangeSets(); AtomicInteger lineNum = new AtomicInteger(1); @@ -238,7 +240,7 @@ public void revokeItem(String appId, Env env, String clusterName, String namespa oldKeyMapItem.remove(key); lineNum.set(lineNum.get() + 1); }); - oldKeyMapItem.forEach((key, value) -> changeSets.addDeleteItem(oldKeyMapItem.get(key))); + oldKeyMapItem.forEach((key, value) -> changeSets.addDeleteItem(value)); changeSets.setDataChangeLastModifiedBy(userInfoHolder.getUser().getUserId()); updateItems(appId, env, clusterName, namespaceName, changeSets); diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java index 43f05e4de94..183a58b891e 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java @@ -309,7 +309,6 @@ private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boole //latest Release ReleaseDTO latestRelease; Map releaseItems = new HashMap<>(); - Map deletedItemDTOs = new HashMap<>(); latestRelease = releaseService.loadLatestRelease(appId, env, clusterName, namespaceName); if (latestRelease != null) { releaseItems = GSON.fromJson(latestRelease.getConfigurations(), GsonType.CONFIG); @@ -333,9 +332,9 @@ private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boole if (includeDeletedItems) { //deleted items - itemService.findDeletedItems(appId, env, clusterName, namespaceName).forEach(item -> { - deletedItemDTOs.put(item.getKey(), item); - }); + Map deletedItemDTOs = itemService.findDeletedItems(appId, env, clusterName, namespaceName).stream() + .filter(itemDTO -> !StringUtils.isEmpty(itemDTO.getKey())) + .collect(Collectors.toMap(itemDTO -> itemDTO.getKey(), v -> v, (v1, v2) -> v2)); List deletedItems = parseDeletedItems(items, releaseItems, deletedItemDTOs); itemBOs.addAll(deletedItems); @@ -385,6 +384,8 @@ private void fillAppNamespaceProperties(NamespaceBO namespace) { private List parseDeletedItems(List newItems, Map releaseItems, Map deletedItemDTOs) { Map newItemMap = BeanUtils.mapByKey("key", newItems); + //remove comment and blank item map. + newItemMap.remove(""); List deletedItems = new LinkedList<>(); for (Map.Entry entry : releaseItems.entrySet()) { diff --git a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolverTest.java b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolverTest.java index 4c8664f7b47..ed18a70c448 100644 --- a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolverTest.java +++ b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolverTest.java @@ -91,9 +91,9 @@ public void testDeleteItem() { @Test public void testDeleteCommentItem() { ItemChangeSets changeSets = resolver.resolve(1, "a=b\n\nb=c", mockBaseItemWith2Key1Comment1Blank()); - Assert.assertEquals(2, changeSets.getDeleteItems().size()); - Assert.assertEquals(2, changeSets.getUpdateItems().size()); - Assert.assertEquals(1, changeSets.getCreateItems().size()); + Assert.assertEquals(1, changeSets.getDeleteItems().size()); + Assert.assertEquals(3, changeSets.getUpdateItems().size()); + Assert.assertEquals(0, changeSets.getCreateItems().size()); } @Test @@ -120,17 +120,17 @@ public void testUpdateCommentItem() { + "a=b\n" +"\n" + "b=c", mockBaseItemWith2Key1Comment1Blank()); - Assert.assertEquals(1, changeSets.getDeleteItems().size()); - Assert.assertEquals(0, changeSets.getUpdateItems().size()); - Assert.assertEquals(1, changeSets.getCreateItems().size()); + Assert.assertEquals(0, changeSets.getDeleteItems().size()); + Assert.assertEquals(1, changeSets.getUpdateItems().size()); + Assert.assertEquals(0, changeSets.getCreateItems().size()); } @Test public void testAllSituation(){ ItemChangeSets changeSets = resolver.resolve(1, "#ww\nd=e\nb=c\na=b\n\nq=w\n#eee", mockBaseItemWith2Key1Comment1Blank()); - Assert.assertEquals(2, changeSets.getDeleteItems().size()); - Assert.assertEquals(2, changeSets.getUpdateItems().size()); - Assert.assertEquals(5, changeSets.getCreateItems().size()); + Assert.assertEquals(0, changeSets.getDeleteItems().size()); + Assert.assertEquals(4, changeSets.getUpdateItems().size()); + Assert.assertEquals(3, changeSets.getCreateItems().size()); } /** From 859bbdbf00934e6f308c8685bee616c106b42ebe Mon Sep 17 00:00:00 2001 From: yangzl Date: Sun, 29 Sep 2024 13:40:02 +0800 Subject: [PATCH 2/6] =?UTF-8?q?fix=EF=BC=9A=201.=20add=20CHANGES.md=202.?= =?UTF-8?q?=20PropertyResolver=20code=20style=20format=203.=20Revoke=20con?= =?UTF-8?q?figurations=20cause=20the=20line=20num=20to=20be=20disordered?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.md | 4 +-- .../txtresolver/PropertyResolver.java | 32 +++++++++---------- .../apollo/portal/service/ItemService.java | 9 +++--- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ed756c3f80e..ce5388027ef 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,6 @@ Apollo 2.4.0 * [Update the server config link in system info page](https://github.com/apolloconfig/apollo/pull/5204) * [Feature support portal restTemplate Client connection pool config](https://github.com/apolloconfig/apollo/pull/5200) * [Feature added the ability for administrators to globally search for Value](https://github.com/apolloconfig/apollo/pull/5182) - +* [Fix: The problem of duplicate comments and blank lines](https://github.com/apolloconfig/apollo/pull/5232) ------------------ -All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/15?closed=1) \ No newline at end of file +All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/15?closed=1) diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java index 39772c34763..f80915661c2 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java @@ -58,7 +58,7 @@ public ItemChangeSets resolve(long namespaceId, String configText, List List baseCommentItems = new ArrayList<>(); // blank items List baseBlankItems = new ArrayList<>(); - if(!CollectionUtils.isEmpty(baseItems)) { + if (!CollectionUtils.isEmpty(baseItems)) { baseCommentItems = baseItems.stream().filter(itemDTO -> isCommentItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toList()); @@ -81,7 +81,7 @@ public ItemChangeSets resolve(long namespaceId, String configText, List //comment item if (isCommentItem(newItem)) { ItemDTO oldItemDTO = null; - if(!CollectionUtils.isEmpty(baseCommentItems)) { + if (!CollectionUtils.isEmpty(baseCommentItems)) { oldItemDTO = baseCommentItems.remove(0); } @@ -91,7 +91,7 @@ public ItemChangeSets resolve(long namespaceId, String configText, List } else if (isBlankItem(newItem)) { ItemDTO oldItemDTO = null; - if(!CollectionUtils.isEmpty(baseBlankItems)) { + if (!CollectionUtils.isEmpty(baseBlankItems)) { oldItemDTO = baseBlankItems.remove(0); } @@ -119,7 +119,7 @@ private boolean isHasRepeatKey(String[] newItems, @NotNull Set repeatKey String[] kv = parseKeyValueFromItem(item); if (kv != null) { String key = kv[0].toLowerCase(); - if(!keys.add(key)){ + if (!keys.add(key)) { repeatKeys.add(key); } } else { @@ -144,17 +144,17 @@ private String[] parseKeyValueFromItem(String item) { } private void handleCommentLine(Long namespaceId, ItemDTO oldItemByLine, String newItem, int lineCounter, ItemChangeSets changeSets) { - if(null == oldItemByLine ){ + if (null == oldItemByLine) { changeSets.addCreateItem(buildCommentItem(0L, namespaceId, newItem, lineCounter)); - }else if(!StringUtils.equals(oldItemByLine.getComment(), newItem) || lineCounter != oldItemByLine.getLineNum()) { + } else if (!StringUtils.equals(oldItemByLine.getComment(), newItem) || lineCounter != oldItemByLine.getLineNum()) { changeSets.addUpdateItem(buildCommentItem(oldItemByLine.getId(), namespaceId, newItem, lineCounter)); } } private void handleBlankLine(Long namespaceId, ItemDTO oldItem, int lineCounter, ItemChangeSets changeSets) { - if(null == oldItem ){ + if (null == oldItem) { changeSets.addCreateItem(buildBlankItem(0L, namespaceId, lineCounter)); - }else if (lineCounter != oldItem.getLineNum()) { + } else if (lineCounter != oldItem.getLineNum()) { changeSets.addUpdateItem(buildBlankItem(oldItem.getId(), namespaceId, lineCounter)); } } @@ -173,12 +173,12 @@ private void handleNormalLine(Long namespaceId, Map keyMapOldIt ItemDTO oldItem = keyMapOldItem.get(newKey); - if (oldItem == null) {//new item + //new item + if (oldItem == null) { changeSets.addCreateItem(buildNormalItem(0L, namespaceId, newKey, newValue, "", lineCounter)); - } else if (!newValue.equals(oldItem.getValue()) || lineCounter != oldItem.getLineNum()) {//update item - changeSets.addUpdateItem( - buildNormalItem(oldItem.getId(), namespaceId, newKey, newValue, oldItem.getComment(), - lineCounter)); + //update item + } else if (!newValue.equals(oldItem.getValue()) || lineCounter != oldItem.getLineNum()) { + changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, newKey, newValue, oldItem.getComment(), lineCounter)); } keyMapOldItem.remove(newKey); } @@ -197,7 +197,7 @@ private boolean isBlankItem(ItemDTO item) { } private boolean isBlankItem(String line) { - return Strings.nullToEmpty(line).trim().isEmpty(); + return Strings.nullToEmpty(line).trim().isEmpty(); } private void deleteNormalKVItem(Map baseKeyMapItem, ItemChangeSets changeSets) { @@ -210,8 +210,8 @@ private void deleteNormalKVItem(Map baseKeyMapItem, ItemChangeS private void deleteCommentAndBlankItem(List baseCommentItems, List baseBlankItems, ItemChangeSets changeSets) { - baseCommentItems.forEach(oldItemDTO-> changeSets.addDeleteItem(oldItemDTO)); - baseBlankItems.forEach(oldItemDTO-> changeSets.addDeleteItem(oldItemDTO)); + baseCommentItems.forEach(oldItemDTO -> changeSets.addDeleteItem(oldItemDTO)); + baseBlankItems.forEach(oldItemDTO -> changeSets.addDeleteItem(oldItemDTO)); } private ItemDTO buildCommentItem(Long id, Long namespaceId, String comment, int lineNum) { diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java index 2c7aa57c195..63db781fc08 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java @@ -231,11 +231,10 @@ public void revokeItem(String appId, Env env, String clusterName, String namespa ItemDTO oldItem = oldKeyMapItem.get(key); if (oldItem == null) { ItemDTO deletedItemDto = deletedItemDTOs.computeIfAbsent(key, k -> new ItemDTO()); - changeSets.addCreateItem(buildNormalItem(0L, namespaceId,key,value,deletedItemDto.getComment(),lineNum.get())); - } else if (!oldItem.getValue().equals(value) || lineNum.get() != oldItem - .getLineNum()) { - changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, key, - value, oldItem.getComment(), lineNum.get())); + int newLineNum = 0 == deletedItemDto.getLineNum() ? lineNum.get() : deletedItemDto.getLineNum(); + changeSets.addCreateItem(buildNormalItem(0L, namespaceId, key, value, deletedItemDto.getComment(), newLineNum)); + } else if (!oldItem.getValue().equals(value) || lineNum.get() != oldItem.getLineNum()) { + changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, key, value, oldItem.getComment(), oldItem.getLineNum())); } oldKeyMapItem.remove(key); lineNum.set(lineNum.get() + 1); From 904ab8793544098a87239b0a363b8f4e4df12a2e Mon Sep 17 00:00:00 2001 From: yangzl Date: Sun, 29 Sep 2024 14:40:33 +0800 Subject: [PATCH 3/6] =?UTF-8?q?fix=EF=BC=9Acode=20optimization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.md | 3 ++- .../component/txtresolver/PropertyResolver.java | 12 ++++++------ .../framework/apollo/portal/service/ItemService.java | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ce5388027ef..62a6ba5ffb0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,7 @@ Apollo 2.4.0 * [Update the server config link in system info page](https://github.com/apolloconfig/apollo/pull/5204) * [Feature support portal restTemplate Client connection pool config](https://github.com/apolloconfig/apollo/pull/5200) * [Feature added the ability for administrators to globally search for Value](https://github.com/apolloconfig/apollo/pull/5182) -* [Fix: The problem of duplicate comments and blank lines](https://github.com/apolloconfig/apollo/pull/5232) +* [Fix: Resolve issues with duplicate comments and blank lines in configuration management](https://github.com/apolloconfig/apollo/pull/5232) + ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/15?closed=1) diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java index f80915661c2..9c3751f7bea 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java @@ -27,10 +27,10 @@ import org.springframework.util.CollectionUtils; import javax.validation.constraints.NotNull; -import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -55,14 +55,14 @@ public ItemChangeSets resolve(long namespaceId, String configText, List oldKeyMapItem.remove(""); // comment items - List baseCommentItems = new ArrayList<>(); + List baseCommentItems = new LinkedList<>(); // blank items - List baseBlankItems = new ArrayList<>(); + List baseBlankItems = new LinkedList<>(); if (!CollectionUtils.isEmpty(baseItems)) { - baseCommentItems = baseItems.stream().filter(itemDTO -> isCommentItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toList()); + baseCommentItems = baseItems.stream().filter(itemDTO -> isCommentItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toCollection(LinkedList::new)); - baseBlankItems = baseItems.stream().filter(itemDTO -> isBlankItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toList()); + baseBlankItems = baseItems.stream().filter(itemDTO -> isBlankItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toCollection(LinkedList::new)); } String[] newItems = configText.split(ITEM_SEPARATOR); @@ -177,7 +177,7 @@ private void handleNormalLine(Long namespaceId, Map keyMapOldIt if (oldItem == null) { changeSets.addCreateItem(buildNormalItem(0L, namespaceId, newKey, newValue, "", lineCounter)); //update item - } else if (!newValue.equals(oldItem.getValue()) || lineCounter != oldItem.getLineNum()) { + } else if (!StringUtils.equals(newValue, oldItem.getValue()) || lineCounter != oldItem.getLineNum()) { changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, newKey, newValue, oldItem.getComment(), lineCounter)); } keyMapOldItem.remove(newKey); diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java index 63db781fc08..589052a6f82 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java @@ -233,7 +233,7 @@ public void revokeItem(String appId, Env env, String clusterName, String namespa ItemDTO deletedItemDto = deletedItemDTOs.computeIfAbsent(key, k -> new ItemDTO()); int newLineNum = 0 == deletedItemDto.getLineNum() ? lineNum.get() : deletedItemDto.getLineNum(); changeSets.addCreateItem(buildNormalItem(0L, namespaceId, key, value, deletedItemDto.getComment(), newLineNum)); - } else if (!oldItem.getValue().equals(value) || lineNum.get() != oldItem.getLineNum()) { + } else if (!StringUtils.equals(oldItem.getValue(), value) || lineNum.get() != oldItem.getLineNum()) { changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, key, value, oldItem.getComment(), oldItem.getLineNum())); } oldKeyMapItem.remove(key); From 09b4601552fb3059f12b4e503a6de5fc6cdc1c37 Mon Sep 17 00:00:00 2001 From: yangzl Date: Thu, 19 Sep 2024 20:55:41 +0800 Subject: [PATCH 4/6] feat: 1. solve the problem of duplicate comments and blank lines 2. reduce the data growth of the item table 3. in the same case, the size of ItemChangeSets can be reduced 4. when revoke configuration, if there is only one comment or blank line, it will not be deleted. --- .../txtresolver/PropertyResolver.java | 64 +++++++++++-------- .../apollo/portal/service/ItemService.java | 12 ++-- .../portal/service/NamespaceService.java | 9 +-- .../txtresolver/PropertyResolverTest.java | 18 +++--- 4 files changed, 59 insertions(+), 44 deletions(-) diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java index 44080d7520d..39772c34763 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java @@ -21,15 +21,20 @@ import com.ctrip.framework.apollo.common.exception.BadRequestException; import com.ctrip.framework.apollo.common.utils.BeanUtils; +import com.ctrip.framework.apollo.core.utils.StringUtils; import com.google.common.base.Strings; import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; import javax.validation.constraints.NotNull; +import java.util.ArrayList; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; /** * normal property file resolver. @@ -45,12 +50,21 @@ public class PropertyResolver implements ConfigTextResolver { @Override public ItemChangeSets resolve(long namespaceId, String configText, List baseItems) { - Map oldLineNumMapItem = BeanUtils.mapByKey("lineNum", baseItems); Map oldKeyMapItem = BeanUtils.mapByKey("key", baseItems); - //remove comment and blank item map. oldKeyMapItem.remove(""); + // comment items + List baseCommentItems = new ArrayList<>(); + // blank items + List baseBlankItems = new ArrayList<>(); + if(!CollectionUtils.isEmpty(baseItems)) { + + baseCommentItems = baseItems.stream().filter(itemDTO -> isCommentItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toList()); + + baseBlankItems = baseItems.stream().filter(itemDTO -> isBlankItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toList()); + } + String[] newItems = configText.split(ITEM_SEPARATOR); Set repeatKeys = new HashSet<>(); if (isHasRepeatKey(newItems, repeatKeys)) { @@ -63,17 +77,25 @@ public ItemChangeSets resolve(long namespaceId, String configText, List for (String newItem : newItems) { newItem = newItem.trim(); newLineNumMapItem.put(lineCounter, newItem); - ItemDTO oldItemByLine = oldLineNumMapItem.get(lineCounter); //comment item if (isCommentItem(newItem)) { + ItemDTO oldItemDTO = null; + if(!CollectionUtils.isEmpty(baseCommentItems)) { + oldItemDTO = baseCommentItems.remove(0); + } - handleCommentLine(namespaceId, oldItemByLine, newItem, lineCounter, changeSets); + handleCommentLine(namespaceId, oldItemDTO, newItem, lineCounter, changeSets); //blank item } else if (isBlankItem(newItem)) { - handleBlankLine(namespaceId, oldItemByLine, lineCounter, changeSets); + ItemDTO oldItemDTO = null; + if(!CollectionUtils.isEmpty(baseBlankItems)) { + oldItemDTO = baseBlankItems.remove(0); + } + + handleBlankLine(namespaceId, oldItemDTO, lineCounter, changeSets); //normal item } else { @@ -83,7 +105,7 @@ public ItemChangeSets resolve(long namespaceId, String configText, List lineCounter++; } - deleteCommentAndBlankItem(oldLineNumMapItem, newLineNumMapItem, changeSets); + deleteCommentAndBlankItem(baseCommentItems, baseBlankItems, changeSets); deleteNormalKVItem(oldKeyMapItem, changeSets); return changeSets; @@ -122,16 +144,18 @@ private String[] parseKeyValueFromItem(String item) { } private void handleCommentLine(Long namespaceId, ItemDTO oldItemByLine, String newItem, int lineCounter, ItemChangeSets changeSets) { - String oldComment = oldItemByLine == null ? "" : oldItemByLine.getComment(); - //create comment. implement update comment by delete old comment and create new comment - if (!(isCommentItem(oldItemByLine) && newItem.equals(oldComment))) { + if(null == oldItemByLine ){ changeSets.addCreateItem(buildCommentItem(0L, namespaceId, newItem, lineCounter)); + }else if(!StringUtils.equals(oldItemByLine.getComment(), newItem) || lineCounter != oldItemByLine.getLineNum()) { + changeSets.addUpdateItem(buildCommentItem(oldItemByLine.getId(), namespaceId, newItem, lineCounter)); } } private void handleBlankLine(Long namespaceId, ItemDTO oldItem, int lineCounter, ItemChangeSets changeSets) { - if (!isBlankItem(oldItem)) { + if(null == oldItem ){ changeSets.addCreateItem(buildBlankItem(0L, namespaceId, lineCounter)); + }else if (lineCounter != oldItem.getLineNum()) { + changeSets.addUpdateItem(buildBlankItem(oldItem.getId(), namespaceId, lineCounter)); } } @@ -183,23 +207,11 @@ private void deleteNormalKVItem(Map baseKeyMapItem, ItemChangeS } } - private void deleteCommentAndBlankItem(Map oldLineNumMapItem, - Map newLineNumMapItem, + private void deleteCommentAndBlankItem(List baseCommentItems, + List baseBlankItems, ItemChangeSets changeSets) { - - for (Map.Entry entry : oldLineNumMapItem.entrySet()) { - int lineNum = entry.getKey(); - ItemDTO oldItem = entry.getValue(); - String newItem = newLineNumMapItem.get(lineNum); - - //1. old is blank by now is not - //2.old is comment by now is not exist or modified - //3.old is blank by now is not exist or modified - if ((isBlankItem(oldItem) && !isBlankItem(newItem)) - || (isCommentItem(oldItem) || isBlankItem(oldItem)) && (newItem == null || !newItem.equals(oldItem.getComment()))) { - changeSets.addDeleteItem(oldItem); - } - } + baseCommentItems.forEach(oldItemDTO-> changeSets.addDeleteItem(oldItemDTO)); + baseBlankItems.forEach(oldItemDTO-> changeSets.addDeleteItem(oldItemDTO)); } private ItemDTO buildCommentItem(Long id, Long namespaceId, String comment, int lineNum) { diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java index b1677e18858..2c7aa57c195 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java @@ -50,6 +50,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; @Service public class ItemService { @@ -216,12 +217,13 @@ public void revokeItem(String appId, Env env, String clusterName, String namespa } List baseItems = itemAPI.findItems(appId, env, clusterName, namespaceName); Map oldKeyMapItem = BeanUtils.mapByKey("key", baseItems); - Map deletedItemDTOs = new HashMap<>(); + //remove comment and blank item map. + oldKeyMapItem.remove(""); //deleted items for comment - findDeletedItems(appId, env, clusterName, namespaceName).forEach(item -> { - deletedItemDTOs.put(item.getKey(),item); - }); + Map deletedItemDTOs = findDeletedItems(appId, env, clusterName, namespaceName).stream() + .filter(itemDTO -> !StringUtils.isEmpty(itemDTO.getKey())) + .collect(Collectors.toMap(itemDTO -> itemDTO.getKey(), v -> v, (v1, v2) -> v2)); ItemChangeSets changeSets = new ItemChangeSets(); AtomicInteger lineNum = new AtomicInteger(1); @@ -238,7 +240,7 @@ public void revokeItem(String appId, Env env, String clusterName, String namespa oldKeyMapItem.remove(key); lineNum.set(lineNum.get() + 1); }); - oldKeyMapItem.forEach((key, value) -> changeSets.addDeleteItem(oldKeyMapItem.get(key))); + oldKeyMapItem.forEach((key, value) -> changeSets.addDeleteItem(value)); changeSets.setDataChangeLastModifiedBy(userInfoHolder.getUser().getUserId()); updateItems(appId, env, clusterName, namespaceName, changeSets); diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java index 43f05e4de94..183a58b891e 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/NamespaceService.java @@ -309,7 +309,6 @@ private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boole //latest Release ReleaseDTO latestRelease; Map releaseItems = new HashMap<>(); - Map deletedItemDTOs = new HashMap<>(); latestRelease = releaseService.loadLatestRelease(appId, env, clusterName, namespaceName); if (latestRelease != null) { releaseItems = GSON.fromJson(latestRelease.getConfigurations(), GsonType.CONFIG); @@ -333,9 +332,9 @@ private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boole if (includeDeletedItems) { //deleted items - itemService.findDeletedItems(appId, env, clusterName, namespaceName).forEach(item -> { - deletedItemDTOs.put(item.getKey(), item); - }); + Map deletedItemDTOs = itemService.findDeletedItems(appId, env, clusterName, namespaceName).stream() + .filter(itemDTO -> !StringUtils.isEmpty(itemDTO.getKey())) + .collect(Collectors.toMap(itemDTO -> itemDTO.getKey(), v -> v, (v1, v2) -> v2)); List deletedItems = parseDeletedItems(items, releaseItems, deletedItemDTOs); itemBOs.addAll(deletedItems); @@ -385,6 +384,8 @@ private void fillAppNamespaceProperties(NamespaceBO namespace) { private List parseDeletedItems(List newItems, Map releaseItems, Map deletedItemDTOs) { Map newItemMap = BeanUtils.mapByKey("key", newItems); + //remove comment and blank item map. + newItemMap.remove(""); List deletedItems = new LinkedList<>(); for (Map.Entry entry : releaseItems.entrySet()) { diff --git a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolverTest.java b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolverTest.java index 4c8664f7b47..ed18a70c448 100644 --- a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolverTest.java +++ b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolverTest.java @@ -91,9 +91,9 @@ public void testDeleteItem() { @Test public void testDeleteCommentItem() { ItemChangeSets changeSets = resolver.resolve(1, "a=b\n\nb=c", mockBaseItemWith2Key1Comment1Blank()); - Assert.assertEquals(2, changeSets.getDeleteItems().size()); - Assert.assertEquals(2, changeSets.getUpdateItems().size()); - Assert.assertEquals(1, changeSets.getCreateItems().size()); + Assert.assertEquals(1, changeSets.getDeleteItems().size()); + Assert.assertEquals(3, changeSets.getUpdateItems().size()); + Assert.assertEquals(0, changeSets.getCreateItems().size()); } @Test @@ -120,17 +120,17 @@ public void testUpdateCommentItem() { + "a=b\n" +"\n" + "b=c", mockBaseItemWith2Key1Comment1Blank()); - Assert.assertEquals(1, changeSets.getDeleteItems().size()); - Assert.assertEquals(0, changeSets.getUpdateItems().size()); - Assert.assertEquals(1, changeSets.getCreateItems().size()); + Assert.assertEquals(0, changeSets.getDeleteItems().size()); + Assert.assertEquals(1, changeSets.getUpdateItems().size()); + Assert.assertEquals(0, changeSets.getCreateItems().size()); } @Test public void testAllSituation(){ ItemChangeSets changeSets = resolver.resolve(1, "#ww\nd=e\nb=c\na=b\n\nq=w\n#eee", mockBaseItemWith2Key1Comment1Blank()); - Assert.assertEquals(2, changeSets.getDeleteItems().size()); - Assert.assertEquals(2, changeSets.getUpdateItems().size()); - Assert.assertEquals(5, changeSets.getCreateItems().size()); + Assert.assertEquals(0, changeSets.getDeleteItems().size()); + Assert.assertEquals(4, changeSets.getUpdateItems().size()); + Assert.assertEquals(3, changeSets.getCreateItems().size()); } /** From fdc5e60fbddef54c6852ee568acfd4f56c701c56 Mon Sep 17 00:00:00 2001 From: yangzl Date: Sun, 29 Sep 2024 13:40:02 +0800 Subject: [PATCH 5/6] =?UTF-8?q?fix=EF=BC=9A=201.=20add=20CHANGES.md=202.?= =?UTF-8?q?=20PropertyResolver=20code=20style=20format=203.=20Revoke=20con?= =?UTF-8?q?figurations=20cause=20the=20line=20num=20to=20be=20disordered?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.md | 4 +-- .../txtresolver/PropertyResolver.java | 32 +++++++++---------- .../apollo/portal/service/ItemService.java | 9 +++--- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ed756c3f80e..ce5388027ef 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,6 @@ Apollo 2.4.0 * [Update the server config link in system info page](https://github.com/apolloconfig/apollo/pull/5204) * [Feature support portal restTemplate Client connection pool config](https://github.com/apolloconfig/apollo/pull/5200) * [Feature added the ability for administrators to globally search for Value](https://github.com/apolloconfig/apollo/pull/5182) - +* [Fix: The problem of duplicate comments and blank lines](https://github.com/apolloconfig/apollo/pull/5232) ------------------ -All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/15?closed=1) \ No newline at end of file +All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/15?closed=1) diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java index 39772c34763..f80915661c2 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java @@ -58,7 +58,7 @@ public ItemChangeSets resolve(long namespaceId, String configText, List List baseCommentItems = new ArrayList<>(); // blank items List baseBlankItems = new ArrayList<>(); - if(!CollectionUtils.isEmpty(baseItems)) { + if (!CollectionUtils.isEmpty(baseItems)) { baseCommentItems = baseItems.stream().filter(itemDTO -> isCommentItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toList()); @@ -81,7 +81,7 @@ public ItemChangeSets resolve(long namespaceId, String configText, List //comment item if (isCommentItem(newItem)) { ItemDTO oldItemDTO = null; - if(!CollectionUtils.isEmpty(baseCommentItems)) { + if (!CollectionUtils.isEmpty(baseCommentItems)) { oldItemDTO = baseCommentItems.remove(0); } @@ -91,7 +91,7 @@ public ItemChangeSets resolve(long namespaceId, String configText, List } else if (isBlankItem(newItem)) { ItemDTO oldItemDTO = null; - if(!CollectionUtils.isEmpty(baseBlankItems)) { + if (!CollectionUtils.isEmpty(baseBlankItems)) { oldItemDTO = baseBlankItems.remove(0); } @@ -119,7 +119,7 @@ private boolean isHasRepeatKey(String[] newItems, @NotNull Set repeatKey String[] kv = parseKeyValueFromItem(item); if (kv != null) { String key = kv[0].toLowerCase(); - if(!keys.add(key)){ + if (!keys.add(key)) { repeatKeys.add(key); } } else { @@ -144,17 +144,17 @@ private String[] parseKeyValueFromItem(String item) { } private void handleCommentLine(Long namespaceId, ItemDTO oldItemByLine, String newItem, int lineCounter, ItemChangeSets changeSets) { - if(null == oldItemByLine ){ + if (null == oldItemByLine) { changeSets.addCreateItem(buildCommentItem(0L, namespaceId, newItem, lineCounter)); - }else if(!StringUtils.equals(oldItemByLine.getComment(), newItem) || lineCounter != oldItemByLine.getLineNum()) { + } else if (!StringUtils.equals(oldItemByLine.getComment(), newItem) || lineCounter != oldItemByLine.getLineNum()) { changeSets.addUpdateItem(buildCommentItem(oldItemByLine.getId(), namespaceId, newItem, lineCounter)); } } private void handleBlankLine(Long namespaceId, ItemDTO oldItem, int lineCounter, ItemChangeSets changeSets) { - if(null == oldItem ){ + if (null == oldItem) { changeSets.addCreateItem(buildBlankItem(0L, namespaceId, lineCounter)); - }else if (lineCounter != oldItem.getLineNum()) { + } else if (lineCounter != oldItem.getLineNum()) { changeSets.addUpdateItem(buildBlankItem(oldItem.getId(), namespaceId, lineCounter)); } } @@ -173,12 +173,12 @@ private void handleNormalLine(Long namespaceId, Map keyMapOldIt ItemDTO oldItem = keyMapOldItem.get(newKey); - if (oldItem == null) {//new item + //new item + if (oldItem == null) { changeSets.addCreateItem(buildNormalItem(0L, namespaceId, newKey, newValue, "", lineCounter)); - } else if (!newValue.equals(oldItem.getValue()) || lineCounter != oldItem.getLineNum()) {//update item - changeSets.addUpdateItem( - buildNormalItem(oldItem.getId(), namespaceId, newKey, newValue, oldItem.getComment(), - lineCounter)); + //update item + } else if (!newValue.equals(oldItem.getValue()) || lineCounter != oldItem.getLineNum()) { + changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, newKey, newValue, oldItem.getComment(), lineCounter)); } keyMapOldItem.remove(newKey); } @@ -197,7 +197,7 @@ private boolean isBlankItem(ItemDTO item) { } private boolean isBlankItem(String line) { - return Strings.nullToEmpty(line).trim().isEmpty(); + return Strings.nullToEmpty(line).trim().isEmpty(); } private void deleteNormalKVItem(Map baseKeyMapItem, ItemChangeSets changeSets) { @@ -210,8 +210,8 @@ private void deleteNormalKVItem(Map baseKeyMapItem, ItemChangeS private void deleteCommentAndBlankItem(List baseCommentItems, List baseBlankItems, ItemChangeSets changeSets) { - baseCommentItems.forEach(oldItemDTO-> changeSets.addDeleteItem(oldItemDTO)); - baseBlankItems.forEach(oldItemDTO-> changeSets.addDeleteItem(oldItemDTO)); + baseCommentItems.forEach(oldItemDTO -> changeSets.addDeleteItem(oldItemDTO)); + baseBlankItems.forEach(oldItemDTO -> changeSets.addDeleteItem(oldItemDTO)); } private ItemDTO buildCommentItem(Long id, Long namespaceId, String comment, int lineNum) { diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java index 2c7aa57c195..63db781fc08 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java @@ -231,11 +231,10 @@ public void revokeItem(String appId, Env env, String clusterName, String namespa ItemDTO oldItem = oldKeyMapItem.get(key); if (oldItem == null) { ItemDTO deletedItemDto = deletedItemDTOs.computeIfAbsent(key, k -> new ItemDTO()); - changeSets.addCreateItem(buildNormalItem(0L, namespaceId,key,value,deletedItemDto.getComment(),lineNum.get())); - } else if (!oldItem.getValue().equals(value) || lineNum.get() != oldItem - .getLineNum()) { - changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, key, - value, oldItem.getComment(), lineNum.get())); + int newLineNum = 0 == deletedItemDto.getLineNum() ? lineNum.get() : deletedItemDto.getLineNum(); + changeSets.addCreateItem(buildNormalItem(0L, namespaceId, key, value, deletedItemDto.getComment(), newLineNum)); + } else if (!oldItem.getValue().equals(value) || lineNum.get() != oldItem.getLineNum()) { + changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, key, value, oldItem.getComment(), oldItem.getLineNum())); } oldKeyMapItem.remove(key); lineNum.set(lineNum.get() + 1); From 48da7db66fa63cd2e916e74e48be6de000b6f338 Mon Sep 17 00:00:00 2001 From: yangzl Date: Sun, 29 Sep 2024 14:40:33 +0800 Subject: [PATCH 6/6] =?UTF-8?q?fix=EF=BC=9Acode=20optimization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.md | 3 ++- .../component/txtresolver/PropertyResolver.java | 12 ++++++------ .../framework/apollo/portal/service/ItemService.java | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ce5388027ef..62a6ba5ffb0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,7 @@ Apollo 2.4.0 * [Update the server config link in system info page](https://github.com/apolloconfig/apollo/pull/5204) * [Feature support portal restTemplate Client connection pool config](https://github.com/apolloconfig/apollo/pull/5200) * [Feature added the ability for administrators to globally search for Value](https://github.com/apolloconfig/apollo/pull/5182) -* [Fix: The problem of duplicate comments and blank lines](https://github.com/apolloconfig/apollo/pull/5232) +* [Fix: Resolve issues with duplicate comments and blank lines in configuration management](https://github.com/apolloconfig/apollo/pull/5232) + ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/15?closed=1) diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java index f80915661c2..9c3751f7bea 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/txtresolver/PropertyResolver.java @@ -27,10 +27,10 @@ import org.springframework.util.CollectionUtils; import javax.validation.constraints.NotNull; -import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -55,14 +55,14 @@ public ItemChangeSets resolve(long namespaceId, String configText, List oldKeyMapItem.remove(""); // comment items - List baseCommentItems = new ArrayList<>(); + List baseCommentItems = new LinkedList<>(); // blank items - List baseBlankItems = new ArrayList<>(); + List baseBlankItems = new LinkedList<>(); if (!CollectionUtils.isEmpty(baseItems)) { - baseCommentItems = baseItems.stream().filter(itemDTO -> isCommentItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toList()); + baseCommentItems = baseItems.stream().filter(itemDTO -> isCommentItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toCollection(LinkedList::new)); - baseBlankItems = baseItems.stream().filter(itemDTO -> isBlankItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toList()); + baseBlankItems = baseItems.stream().filter(itemDTO -> isBlankItem(itemDTO)).sorted(Comparator.comparing(ItemDTO::getLineNum)).collect(Collectors.toCollection(LinkedList::new)); } String[] newItems = configText.split(ITEM_SEPARATOR); @@ -177,7 +177,7 @@ private void handleNormalLine(Long namespaceId, Map keyMapOldIt if (oldItem == null) { changeSets.addCreateItem(buildNormalItem(0L, namespaceId, newKey, newValue, "", lineCounter)); //update item - } else if (!newValue.equals(oldItem.getValue()) || lineCounter != oldItem.getLineNum()) { + } else if (!StringUtils.equals(newValue, oldItem.getValue()) || lineCounter != oldItem.getLineNum()) { changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, newKey, newValue, oldItem.getComment(), lineCounter)); } keyMapOldItem.remove(newKey); diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java index 63db781fc08..589052a6f82 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java @@ -233,7 +233,7 @@ public void revokeItem(String appId, Env env, String clusterName, String namespa ItemDTO deletedItemDto = deletedItemDTOs.computeIfAbsent(key, k -> new ItemDTO()); int newLineNum = 0 == deletedItemDto.getLineNum() ? lineNum.get() : deletedItemDto.getLineNum(); changeSets.addCreateItem(buildNormalItem(0L, namespaceId, key, value, deletedItemDto.getComment(), newLineNum)); - } else if (!oldItem.getValue().equals(value) || lineNum.get() != oldItem.getLineNum()) { + } else if (!StringUtils.equals(oldItem.getValue(), value) || lineNum.get() != oldItem.getLineNum()) { changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, key, value, oldItem.getComment(), oldItem.getLineNum())); } oldKeyMapItem.remove(key);