Skip to content
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

optimize find items interface #605

Merged
merged 1 commit into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
Expand Down Expand Up @@ -99,7 +98,7 @@ private void tryUnlock(Namespace namespace) {

boolean isModified(Namespace namespace) {
Release release = releaseService.findLatestActiveRelease(namespace);
List<Item> items = itemService.findItems(namespace.getId());
List<Item> items = itemService.findItemsWithoutOrdered(namespace.getId());

if (release == null) {
return hasNormalItems(items);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void delete(@PathVariable("itemId") long itemId, @RequestParam String ope
public List<ItemDTO> findItems(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
return BeanUtils.batchTransform(ItemDTO.class, itemService.findItems(appId, clusterName, namespaceName));
return BeanUtils.batchTransform(ItemDTO.class, itemService.findItemsWithOrdered(appId, clusterName, namespaceName));
}

@RequestMapping(value = "/items/{itemId}", method = RequestMethod.GET)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void testNamespaceHasNoNormalItemsAndRelease() {
Namespace namespace = createNamespace(namespaceId);

when(releaseService.findLatestActiveRelease(namespace)).thenReturn(null);
when(itemService.findItems(namespaceId)).thenReturn(Collections.singletonList(createItem("", "")));
when(itemService.findItemsWithOrdered(namespaceId)).thenReturn(Collections.singletonList(createItem("", "")));

boolean isModified = namespaceUnlockAspect.isModified(namespace);

Expand All @@ -57,7 +57,7 @@ public void testNamespaceAddItem() {
List<Item> items = Arrays.asList(createItem("k1", "v1"), createItem("k2", "v2"));

when(releaseService.findLatestActiveRelease(namespace)).thenReturn(release);
when(itemService.findItems(namespaceId)).thenReturn(items);
when(itemService.findItemsWithOrdered(namespaceId)).thenReturn(items);
when(namespaceService.findParentNamespace(namespace)).thenReturn(null);

boolean isModified = namespaceUnlockAspect.isModified(namespace);
Expand All @@ -74,7 +74,7 @@ public void testNamespaceModifyItem() {
List<Item> items = Arrays.asList(createItem("k1", "v2"));

when(releaseService.findLatestActiveRelease(namespace)).thenReturn(release);
when(itemService.findItems(namespaceId)).thenReturn(items);
when(itemService.findItemsWithOrdered(namespaceId)).thenReturn(items);
when(namespaceService.findParentNamespace(namespace)).thenReturn(null);

boolean isModified = namespaceUnlockAspect.isModified(namespace);
Expand All @@ -91,7 +91,7 @@ public void testNamespaceDeleteItem() {
List<Item> items = Arrays.asList(createItem("k2", "v2"));

when(releaseService.findLatestActiveRelease(namespace)).thenReturn(release);
when(itemService.findItems(namespaceId)).thenReturn(items);
when(itemService.findItemsWithOrdered(namespaceId)).thenReturn(items);
when(namespaceService.findParentNamespace(namespace)).thenReturn(null);

boolean isModified = namespaceUnlockAspect.isModified(namespace);
Expand All @@ -111,7 +111,7 @@ public void testChildNamespaceModified() {

when(releaseService.findLatestActiveRelease(childNamespace)).thenReturn(childRelease);
when(releaseService.findLatestActiveRelease(parentNamespace)).thenReturn(parentRelease);
when(itemService.findItems(childNamespaceId)).thenReturn(childItems);
when(itemService.findItemsWithoutOrdered(childNamespaceId)).thenReturn(childItems);
when(namespaceService.findParentNamespace(childNamespace)).thenReturn(parentNamespace);

boolean isModified = namespaceUnlockAspect.isModified(childNamespace);
Expand All @@ -131,7 +131,7 @@ public void testChildNamespaceNotModified() {

when(releaseService.findLatestActiveRelease(childNamespace)).thenReturn(childRelease);
when(releaseService.findLatestActiveRelease(parentNamespace)).thenReturn(parentRelease);
when(itemService.findItems(childNamespaceId)).thenReturn(childItems);
when(itemService.findItemsWithoutOrdered(childNamespaceId)).thenReturn(childItems);
when(namespaceService.findParentNamespace(childNamespace)).thenReturn(parentNamespace);

boolean isModified = namespaceUnlockAspect.isModified(childNamespace);
Expand All @@ -150,7 +150,7 @@ public void testParentNamespaceNotReleased() {

when(releaseService.findLatestActiveRelease(childNamespace)).thenReturn(childRelease);
when(releaseService.findLatestActiveRelease(parentNamespace)).thenReturn(null);
when(itemService.findItems(childNamespaceId)).thenReturn(childItems);
when(itemService.findItemsWithOrdered(childNamespaceId)).thenReturn(childItems);
when(namespaceService.findParentNamespace(childNamespace)).thenReturn(parentNamespace);

boolean isModified = namespaceUnlockAspect.isModified(childNamespace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {

List<Item> findByNamespaceIdOrderByLineNumAsc(Long namespaceId);

List<Item> findByNamespaceId(Long namespaceId);

List<Item> findByNamespaceIdAndDataChangeLastModifiedTimeGreaterThan(Long namespaceId, Date date);

Item findFirst1ByNamespaceIdOrderByLineNumDesc(Long namespaceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,35 @@ public Item findOne(long itemId) {
return item;
}

public List<Item> findItems(Long namespaceId) {
public List<Item> findItemsWithoutOrdered(Long namespaceId) {
List<Item> items = itemRepository.findByNamespaceId(namespaceId);
if (items == null) {
return Collections.emptyList();
}
return items;
}

public List<Item> findItemsWithoutOrdered(String appId, String clusterName, String namespaceName) {
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace != null) {
return findItemsWithoutOrdered(namespace.getId());
} else {
return Collections.emptyList();
}
}

public List<Item> findItemsWithOrdered(Long namespaceId) {
List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespaceId);
if (items == null) {
return Collections.emptyList();
}
return items;
}

public List<Item> findItems(String appId, String clusterName, String namespaceName) {
public List<Item> findItemsWithOrdered(String appId, String clusterName, String namespaceName) {
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace != null) {
return findItems(namespace.getId());
return findItemsWithOrdered(namespace.getId());
} else {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.ctrip.framework.apollo.biz.entity.Audit;
import com.ctrip.framework.apollo.biz.entity.GrayReleaseRule;
Expand All @@ -30,7 +29,6 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -308,7 +306,7 @@ private Map<String, String> mergeConfiguration(Map<String, String> baseConfigura


private Map<String, String> getNamespaceItems(Namespace namespace) {
List<Item> items = itemService.findItems(namespace.getId());
List<Item> items = itemService.findItemsWithoutOrdered(namespace.getId());
Map<String, String> configurations = new HashMap<String, String>();
for (Item item : items) {
if (StringUtils.isEmpty(item.getKey())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void testDeleteNamespace() {

namespaceService.deleteNamespace(namespace, testUser);

List<Item> items = itemService.findItems(testApp, testCluster, testPrivateNamespace);
List<Item> items = itemService.findItemsWithoutOrdered(testApp, testCluster, testPrivateNamespace);
List<Commit> commits = commitService.find(testApp, testCluster, testPrivateNamespace, new PageRequest(0, 10));
AppNamespace appNamespace = appNamespaceService.findOne(testApp, testPrivateNamespace);
List<Cluster> childClusters = clusterService.findChildClusters(testApp, testCluster);
Expand Down
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,11 @@
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils-core</artifactId>
</exclusion>
<!-- duplicated with spring-aop -->
<exclusion>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- end of eureka -->
Expand Down Expand Up @@ -767,6 +772,11 @@
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils-core</artifactId>
</exclusion>
<!-- duplicated with spring-aop -->
<exclusion>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- end of eureka -->
Expand Down