2121import com .ctrip .framework .apollo .common .exception .BadRequestException ;
2222import com .ctrip .framework .apollo .common .utils .BeanUtils ;
2323
24+ import com .ctrip .framework .apollo .core .utils .StringUtils ;
2425import com .google .common .base .Strings ;
2526import org .springframework .stereotype .Component ;
27+ import org .springframework .util .CollectionUtils ;
2628
2729import javax .validation .constraints .NotNull ;
30+ import java .util .ArrayList ;
31+ import java .util .Comparator ;
2832import java .util .HashMap ;
2933import java .util .HashSet ;
3034import java .util .List ;
3135import java .util .Map ;
3236import java .util .Set ;
37+ import java .util .stream .Collectors ;
3338
3439/**
3540 * normal property file resolver.
@@ -45,12 +50,21 @@ public class PropertyResolver implements ConfigTextResolver {
4550 @ Override
4651 public ItemChangeSets resolve (long namespaceId , String configText , List <ItemDTO > baseItems ) {
4752
48- Map <Integer , ItemDTO > oldLineNumMapItem = BeanUtils .mapByKey ("lineNum" , baseItems );
4953 Map <String , ItemDTO > oldKeyMapItem = BeanUtils .mapByKey ("key" , baseItems );
50-
5154 //remove comment and blank item map.
5255 oldKeyMapItem .remove ("" );
5356
57+ // comment items
58+ List <ItemDTO > baseCommentItems = new ArrayList <>();
59+ // blank items
60+ List <ItemDTO > baseBlankItems = new ArrayList <>();
61+ if (!CollectionUtils .isEmpty (baseItems )) {
62+
63+ baseCommentItems = baseItems .stream ().filter (itemDTO -> isCommentItem (itemDTO )).sorted (Comparator .comparing (ItemDTO ::getLineNum )).collect (Collectors .toList ());
64+
65+ baseBlankItems = baseItems .stream ().filter (itemDTO -> isBlankItem (itemDTO )).sorted (Comparator .comparing (ItemDTO ::getLineNum )).collect (Collectors .toList ());
66+ }
67+
5468 String [] newItems = configText .split (ITEM_SEPARATOR );
5569 Set <String > repeatKeys = new HashSet <>();
5670 if (isHasRepeatKey (newItems , repeatKeys )) {
@@ -63,17 +77,25 @@ public ItemChangeSets resolve(long namespaceId, String configText, List<ItemDTO>
6377 for (String newItem : newItems ) {
6478 newItem = newItem .trim ();
6579 newLineNumMapItem .put (lineCounter , newItem );
66- ItemDTO oldItemByLine = oldLineNumMapItem .get (lineCounter );
6780
6881 //comment item
6982 if (isCommentItem (newItem )) {
83+ ItemDTO oldItemDTO = null ;
84+ if (!CollectionUtils .isEmpty (baseCommentItems )) {
85+ oldItemDTO = baseCommentItems .remove (0 );
86+ }
7087
71- handleCommentLine (namespaceId , oldItemByLine , newItem , lineCounter , changeSets );
88+ handleCommentLine (namespaceId , oldItemDTO , newItem , lineCounter , changeSets );
7289
7390 //blank item
7491 } else if (isBlankItem (newItem )) {
7592
76- handleBlankLine (namespaceId , oldItemByLine , lineCounter , changeSets );
93+ ItemDTO oldItemDTO = null ;
94+ if (!CollectionUtils .isEmpty (baseBlankItems )) {
95+ oldItemDTO = baseBlankItems .remove (0 );
96+ }
97+
98+ handleBlankLine (namespaceId , oldItemDTO , lineCounter , changeSets );
7799
78100 //normal item
79101 } else {
@@ -83,7 +105,7 @@ public ItemChangeSets resolve(long namespaceId, String configText, List<ItemDTO>
83105 lineCounter ++;
84106 }
85107
86- deleteCommentAndBlankItem (oldLineNumMapItem , newLineNumMapItem , changeSets );
108+ deleteCommentAndBlankItem (baseCommentItems , baseBlankItems , changeSets );
87109 deleteNormalKVItem (oldKeyMapItem , changeSets );
88110
89111 return changeSets ;
@@ -122,16 +144,18 @@ private String[] parseKeyValueFromItem(String item) {
122144 }
123145
124146 private void handleCommentLine (Long namespaceId , ItemDTO oldItemByLine , String newItem , int lineCounter , ItemChangeSets changeSets ) {
125- String oldComment = oldItemByLine == null ? "" : oldItemByLine .getComment ();
126- //create comment. implement update comment by delete old comment and create new comment
127- if (!(isCommentItem (oldItemByLine ) && newItem .equals (oldComment ))) {
147+ if (null == oldItemByLine ){
128148 changeSets .addCreateItem (buildCommentItem (0L , namespaceId , newItem , lineCounter ));
149+ }else if (!StringUtils .equals (oldItemByLine .getComment (), newItem ) || lineCounter != oldItemByLine .getLineNum ()) {
150+ changeSets .addUpdateItem (buildCommentItem (oldItemByLine .getId (), namespaceId , newItem , lineCounter ));
129151 }
130152 }
131153
132154 private void handleBlankLine (Long namespaceId , ItemDTO oldItem , int lineCounter , ItemChangeSets changeSets ) {
133- if (! isBlankItem ( oldItem )) {
155+ if ( null == oldItem ) {
134156 changeSets .addCreateItem (buildBlankItem (0L , namespaceId , lineCounter ));
157+ }else if (lineCounter != oldItem .getLineNum ()) {
158+ changeSets .addUpdateItem (buildBlankItem (oldItem .getId (), namespaceId , lineCounter ));
135159 }
136160 }
137161
@@ -183,23 +207,11 @@ private void deleteNormalKVItem(Map<String, ItemDTO> baseKeyMapItem, ItemChangeS
183207 }
184208 }
185209
186- private void deleteCommentAndBlankItem (Map < Integer , ItemDTO > oldLineNumMapItem ,
187- Map < Integer , String > newLineNumMapItem ,
210+ private void deleteCommentAndBlankItem (List < ItemDTO > baseCommentItems ,
211+ List < ItemDTO > baseBlankItems ,
188212 ItemChangeSets changeSets ) {
189-
190- for (Map .Entry <Integer , ItemDTO > entry : oldLineNumMapItem .entrySet ()) {
191- int lineNum = entry .getKey ();
192- ItemDTO oldItem = entry .getValue ();
193- String newItem = newLineNumMapItem .get (lineNum );
194-
195- //1. old is blank by now is not
196- //2.old is comment by now is not exist or modified
197- //3.old is blank by now is not exist or modified
198- if ((isBlankItem (oldItem ) && !isBlankItem (newItem ))
199- || (isCommentItem (oldItem ) || isBlankItem (oldItem )) && (newItem == null || !newItem .equals (oldItem .getComment ()))) {
200- changeSets .addDeleteItem (oldItem );
201- }
202- }
213+ baseCommentItems .forEach (oldItemDTO -> changeSets .addDeleteItem (oldItemDTO ));
214+ baseBlankItems .forEach (oldItemDTO -> changeSets .addDeleteItem (oldItemDTO ));
203215 }
204216
205217 private ItemDTO buildCommentItem (Long id , Long namespaceId , String comment , int lineNum ) {
0 commit comments