-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Refactor : Improve Code Maintainability, Readability, and Encapsulation #5364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
6357860
81dcfc6
4e199f5
adc4273
569faaa
9369464
c45db51
0f7ffd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -65,6 +65,8 @@ public class ReleaseHistoryService { | |||||||||
ApolloThreadFactory.create("ReleaseHistoryService", true)); | ||||||||||
private final AtomicBoolean cleanStopped = new AtomicBoolean(false); | ||||||||||
|
||||||||||
private static final int BATCH_SIZE = 100; // Number of records processed per batch | ||||||||||
|
||||||||||
private final ReleaseHistoryRepository releaseHistoryRepository; | ||||||||||
private final ReleaseRepository releaseRepository; | ||||||||||
private final AuditService auditService; | ||||||||||
|
@@ -189,7 +191,7 @@ private void cleanReleaseHistory(ReleaseHistory cleanRelease) { | |||||||||
String branchName = cleanRelease.getBranchName(); | ||||||||||
|
||||||||||
int retentionLimit = this.getReleaseHistoryRetentionLimit(cleanRelease); | ||||||||||
//Second check, if retentionLimit is default value, do not clean | ||||||||||
// Second check, if retentionLimit is default value, do not clean | ||||||||||
if (retentionLimit == DEFAULT_RELEASE_HISTORY_RETENTION_SIZE) { | ||||||||||
return; | ||||||||||
} | ||||||||||
|
@@ -202,10 +204,11 @@ private void cleanReleaseHistory(ReleaseHistory cleanRelease) { | |||||||||
boolean hasMore = true; | ||||||||||
while (hasMore && !Thread.currentThread().isInterrupted()) { | ||||||||||
List<ReleaseHistory> cleanReleaseHistoryList = releaseHistoryRepository.findFirst100ByAppIdAndClusterNameAndNamespaceNameAndBranchNameAndIdLessThanEqualOrderByIdAsc( | ||||||||||
appId, clusterName, namespaceName, branchName, maxId.get()); | ||||||||||
appId, clusterName, namespaceName, branchName, maxId.get()); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change appears unnecessary.
Suggested change
|
||||||||||
|
||||||||||
Set<Long> releaseIds = cleanReleaseHistoryList.stream() | ||||||||||
.map(ReleaseHistory::getReleaseId) | ||||||||||
.collect(Collectors.toSet()); | ||||||||||
.map(ReleaseHistory::getReleaseId) | ||||||||||
.collect(Collectors.toSet()); | ||||||||||
Comment on lines
+210
to
+211
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change appears unnecessary.
Suggested change
|
||||||||||
|
||||||||||
transactionManager.execute(new TransactionCallbackWithoutResult() { | ||||||||||
@Override | ||||||||||
|
@@ -214,7 +217,8 @@ protected void doInTransactionWithoutResult(TransactionStatus status) { | |||||||||
releaseRepository.deleteAllById(releaseIds); | ||||||||||
} | ||||||||||
}); | ||||||||||
hasMore = cleanReleaseHistoryList.size() == 100; | ||||||||||
|
||||||||||
hasMore = cleanReleaseHistoryList.size() == BATCH_SIZE; | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,11 @@ | |
*/ | ||
package com.ctrip.framework.apollo.common.constants; | ||
|
||
import com.ctrip.framework.apollo.common.constants.VersionUtil; | ||
|
||
/** | ||
* @author Jason Song([email protected]) | ||
*/ | ||
public class ApolloServer { | ||
public final static String VERSION = | ||
"java-" + ApolloServer.class.getPackage().getImplementationVersion(); | ||
public static final String VERSION = VersionUtil.getVersion(ApolloServer.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it's only used here, extracting it as a separate utility isn't necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so you want me to revert it back ? or it will work There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d prefer to revert it. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2024 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.ctrip.framework.apollo.common.constants; | ||
|
||
/** | ||
* Utility class for retrieving version information. | ||
*/ | ||
public class VersionUtil { | ||
private VersionUtil() { | ||
// Prevent instantiation | ||
} | ||
|
||
public static String getVersion(Class<?> clazz) { | ||
String implementationVersion = clazz.getPackage().getImplementationVersion(); | ||
return "java-" + (implementationVersion != null ? implementationVersion : "unknown"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -27,6 +27,8 @@ | |||||||||||||||||||||||||||||||||
import java.util.List; | ||||||||||||||||||||||||||||||||||
import java.util.Map; | ||||||||||||||||||||||||||||||||||
import java.util.stream.Collectors; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
import com.ctrip.framework.apollo.portal.util.UserSearchService; | ||||||||||||||||||||||||||||||||||
import org.springframework.security.core.GrantedAuthority; | ||||||||||||||||||||||||||||||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||||||||||||||||||||||||||||||||||
import org.springframework.security.core.userdetails.User; | ||||||||||||||||||||||||||||||||||
|
@@ -53,11 +55,14 @@ public class OidcLocalUserServiceImpl implements OidcLocalUserService { | |||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
private final UserRepository userRepository; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
private final UserSearchService userSearchService; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
public OidcLocalUserServiceImpl( | ||||||||||||||||||||||||||||||||||
JdbcUserDetailsManager userDetailsManager, | ||||||||||||||||||||||||||||||||||
UserRepository userRepository) { | ||||||||||||||||||||||||||||||||||
this.userDetailsManager = userDetailsManager; | ||||||||||||||||||||||||||||||||||
this.userRepository = userRepository; | ||||||||||||||||||||||||||||||||||
this.userSearchService = new UserSearchService(userRepository); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
@Transactional(rollbackFor = Exception.class) | ||||||||||||||||||||||||||||||||||
|
@@ -89,46 +94,14 @@ public void updateUserInfo(UserInfo newUserInfo) { | |||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||
public List<UserInfo> searchUsers(String keyword, int offset, int limit, | ||||||||||||||||||||||||||||||||||
boolean includeInactiveUsers) { | ||||||||||||||||||||||||||||||||||
List<UserPO> users = this.findUsers(keyword, includeInactiveUsers); | ||||||||||||||||||||||||||||||||||
List<UserPO> users = this.userSearchService.findUsers(keyword, includeInactiveUsers); | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused pagination parameters in the delegated method call The @Override
public List<UserInfo> searchUsers(String keyword, int offset, int limit,
boolean includeInactiveUsers) {
- List<UserPO> users = this.userSearchService.findUsers(keyword, includeInactiveUsers);
+ List<UserPO> users = this.userSearchService.findUsers(keyword, includeInactiveUsers);
+ // Apply pagination if needed
+ if (offset >= 0 && limit > 0 && users.size() > offset) {
+ int toIndex = Math.min(offset + limit, users.size());
+ users = users.subList(offset, toIndex);
+ }
if (CollectionUtils.isEmpty(users)) {
return Collections.emptyList();
}
return users.stream().map(UserPO::toUserInfo)
.collect(Collectors.toList());
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
if (CollectionUtils.isEmpty(users)) { | ||||||||||||||||||||||||||||||||||
return Collections.emptyList(); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
return users.stream().map(UserPO::toUserInfo) | ||||||||||||||||||||||||||||||||||
.collect(Collectors.toList()); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
private List<UserPO> findUsers(String keyword, boolean includeInactiveUsers) { | ||||||||||||||||||||||||||||||||||
Map<Long, UserPO> users = new HashMap<>(); | ||||||||||||||||||||||||||||||||||
List<UserPO> byUsername; | ||||||||||||||||||||||||||||||||||
List<UserPO> byUserDisplayName; | ||||||||||||||||||||||||||||||||||
if (includeInactiveUsers) { | ||||||||||||||||||||||||||||||||||
if (StringUtils.isEmpty(keyword)) { | ||||||||||||||||||||||||||||||||||
return (List<UserPO>) userRepository.findAll(); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
byUsername = userRepository.findByUsernameLike("%" + keyword + "%"); | ||||||||||||||||||||||||||||||||||
byUserDisplayName = userRepository.findByUserDisplayNameLike("%" + keyword + "%"); | ||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||
if (StringUtils.isEmpty(keyword)) { | ||||||||||||||||||||||||||||||||||
return userRepository.findFirst20ByEnabled(1); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
byUsername = userRepository | ||||||||||||||||||||||||||||||||||
.findByUsernameLikeAndEnabled("%" + keyword + "%", 1); | ||||||||||||||||||||||||||||||||||
byUserDisplayName = userRepository | ||||||||||||||||||||||||||||||||||
.findByUserDisplayNameLikeAndEnabled("%" + keyword + "%", 1); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
if (!CollectionUtils.isEmpty(byUsername)) { | ||||||||||||||||||||||||||||||||||
for (UserPO user : byUsername) { | ||||||||||||||||||||||||||||||||||
users.put(user.getId(), user); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
if (!CollectionUtils.isEmpty(byUserDisplayName)) { | ||||||||||||||||||||||||||||||||||
for (UserPO user : byUserDisplayName) { | ||||||||||||||||||||||||||||||||||
users.put(user.getId(), user); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
return new ArrayList<>(users.values()); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||
public UserInfo findByUserId(String userId) { | ||||||||||||||||||||||||||||||||||
UserPO userPO = userRepository.findByUsername(userId); | ||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -27,6 +27,8 @@ | |||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
import java.util.HashMap; | ||||||||||||||||||||||||||||||||||
import java.util.Map; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
import com.ctrip.framework.apollo.portal.util.UserSearchService; | ||||||||||||||||||||||||||||||||||
import org.springframework.security.crypto.password.PasswordEncoder; | ||||||||||||||||||||||||||||||||||
import org.springframework.transaction.annotation.Transactional; | ||||||||||||||||||||||||||||||||||
import org.springframework.util.CollectionUtils; | ||||||||||||||||||||||||||||||||||
|
@@ -47,13 +49,16 @@ public class SpringSecurityUserService implements UserService { | |||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
private final AuthorityRepository authorityRepository; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
private final UserSearchService userSearchService; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
public SpringSecurityUserService( | ||||||||||||||||||||||||||||||||||
PasswordEncoder passwordEncoder, | ||||||||||||||||||||||||||||||||||
UserRepository userRepository, | ||||||||||||||||||||||||||||||||||
AuthorityRepository authorityRepository) { | ||||||||||||||||||||||||||||||||||
this.passwordEncoder = passwordEncoder; | ||||||||||||||||||||||||||||||||||
this.userRepository = userRepository; | ||||||||||||||||||||||||||||||||||
this.authorityRepository = authorityRepository; | ||||||||||||||||||||||||||||||||||
this.userSearchService = new UserSearchService(userRepository); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
@Transactional | ||||||||||||||||||||||||||||||||||
|
@@ -102,45 +107,14 @@ public void changeEnabled(UserPO user) { | |||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||
public List<UserInfo> searchUsers(String keyword, int offset, int limit, | ||||||||||||||||||||||||||||||||||
boolean includeInactiveUsers) { | ||||||||||||||||||||||||||||||||||
List<UserPO> users = this.findUsers(keyword, includeInactiveUsers); | ||||||||||||||||||||||||||||||||||
List<UserPO> users = this.userSearchService.findUsers(keyword, includeInactiveUsers); | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused pagination parameters in the delegated method call The @Override
public List<UserInfo> searchUsers(String keyword, int offset, int limit,
boolean includeInactiveUsers) {
- List<UserPO> users = this.userSearchService.findUsers(keyword, includeInactiveUsers);
+ List<UserPO> users = this.userSearchService.findUsers(keyword, includeInactiveUsers);
+ // Apply pagination if needed
+ if (offset >= 0 && limit > 0 && users.size() > offset) {
+ int toIndex = Math.min(offset + limit, users.size());
+ users = users.subList(offset, toIndex);
+ }
if (CollectionUtils.isEmpty(users)) {
return Collections.emptyList();
}
return users.stream().map(UserPO::toUserInfo)
.collect(Collectors.toList());
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
if (CollectionUtils.isEmpty(users)) { | ||||||||||||||||||||||||||||||||||
return Collections.emptyList(); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
return users.stream().map(UserPO::toUserInfo) | ||||||||||||||||||||||||||||||||||
.collect(Collectors.toList()); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
private List<UserPO> findUsers(String keyword, boolean includeInactiveUsers) { | ||||||||||||||||||||||||||||||||||
Map<Long, UserPO> users = new HashMap<>(); | ||||||||||||||||||||||||||||||||||
List<UserPO> byUsername; | ||||||||||||||||||||||||||||||||||
List<UserPO> byUserDisplayName; | ||||||||||||||||||||||||||||||||||
if (includeInactiveUsers) { | ||||||||||||||||||||||||||||||||||
if (StringUtils.isEmpty(keyword)) { | ||||||||||||||||||||||||||||||||||
return (List<UserPO>) userRepository.findAll(); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
byUsername = userRepository.findByUsernameLike("%" + keyword + "%"); | ||||||||||||||||||||||||||||||||||
byUserDisplayName = userRepository.findByUserDisplayNameLike("%" + keyword + "%"); | ||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||
if (StringUtils.isEmpty(keyword)) { | ||||||||||||||||||||||||||||||||||
return userRepository.findFirst20ByEnabled(1); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
byUsername = userRepository.findByUsernameLikeAndEnabled("%" + keyword + "%", 1); | ||||||||||||||||||||||||||||||||||
byUserDisplayName = userRepository | ||||||||||||||||||||||||||||||||||
.findByUserDisplayNameLikeAndEnabled("%" + keyword + "%", 1); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
if (!CollectionUtils.isEmpty(byUsername)) { | ||||||||||||||||||||||||||||||||||
for (UserPO user : byUsername) { | ||||||||||||||||||||||||||||||||||
users.put(user.getId(), user); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
if (!CollectionUtils.isEmpty(byUserDisplayName)) { | ||||||||||||||||||||||||||||||||||
for (UserPO user : byUserDisplayName) { | ||||||||||||||||||||||||||||||||||
users.put(user.getId(), user); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
return new ArrayList<>(users.values()); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||
public UserInfo findByUserId(String userId) { | ||||||||||||||||||||||||||||||||||
UserPO userPO = userRepository.findByUsername(userId); | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.