Skip to content

Commit

Permalink
include all target platforms when searching for extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
amvanbaren committed Feb 10, 2022
1 parent 46c45f8 commit b690d12
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@
import org.eclipse.openvsx.search.ISearchService;
import org.eclipse.openvsx.search.SearchUtilService;
import org.eclipse.openvsx.storage.StorageUtilService;
import org.eclipse.openvsx.util.CollectionUtil;
import org.eclipse.openvsx.util.ErrorResultException;
import org.eclipse.openvsx.util.NotFoundException;
import org.eclipse.openvsx.util.SemanticVersion;
import org.eclipse.openvsx.util.TimeUtil;
import org.eclipse.openvsx.util.UrlUtil;
import org.eclipse.openvsx.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.SearchHit;
Expand Down Expand Up @@ -245,8 +240,8 @@ public QueryResultJson query(QueryParamJson param) {
param.extensionName = split[1];
}

var targetPlatform = param.targetPlatform == null ? ExtensionTargetPlatform.NAME_ANY : param.targetPlatform;
List<ExtensionVersionDTO> extensionVersions = new ArrayList<>();
var targetPlatform = TargetPlatformValidator.isValid(param.targetPlatform) ? param.targetPlatform : null;

// Add extension by UUID (public_id)
if (!Strings.isNullOrEmpty(param.extensionUuid)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public ExtensionQueryResult extensionQuery(@RequestBody ExtensionQueryParam para
pageRequest = PageRequest.of(0, DEFAULT_PAGE_SIZE);
sortBy = "relevance";
sortOrder = "desc";
targetPlatform = ExtensionTargetPlatform.NAME_ANY;
targetPlatform = null;
extensionIds = Collections.emptySet();
extensionNames = Collections.emptySet();
} else {
Expand All @@ -106,7 +106,7 @@ public ExtensionQueryResult extensionQuery(@RequestBody ExtensionQueryParam para

category = filter.findCriterion(FILTER_CATEGORY);
var targetCriterion = filter.findCriterion(FILTER_TARGET);
targetPlatform = TargetPlatformValidator.isValid(targetCriterion) ? targetCriterion : ExtensionTargetPlatform.NAME_ANY;
targetPlatform = TargetPlatformValidator.isValid(targetCriterion) ? targetCriterion : null;

var pageSize = filter.pageSize > 0 ? filter.pageSize : DEFAULT_PAGE_SIZE;
pageRequest = PageRequest.of(filter.pageNumber - 1, pageSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,33 @@ public class ExtensionDTORepository {
DSLContext dsl;

public List<ExtensionDTO> findAllActiveById(Collection<Long> ids, String targetPlatform) {
return fetch(findAllActive().and(EXTENSION.ID.in(ids)).and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform)));
var query = findAllActive().and(EXTENSION.ID.in(ids));
if(targetPlatform != null) {
query = query.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform));
}

return fetch(query);
}

public List<ExtensionDTO> findAllActiveByPublicIdAndTargetPlatform(Collection<String> publicIds, String targetPlatform) {
var query = findAllActive()
.and(EXTENSION.PUBLIC_ID.in(publicIds))
.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform));
var query = findAllActive().and(EXTENSION.PUBLIC_ID.in(publicIds));
if(targetPlatform != null) {
query = query.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform));
}

return fetch(query);
}

public ExtensionDTO findActiveByNameIgnoreCaseAndNamespaceNameIgnoreCaseAndTargetPlatform(String name, String namespaceName, String targetPlatform) {
return findAllActive()
var query = findAllActive()
.and(DSL.upper(EXTENSION.NAME).eq(DSL.upper(name)))
.and(DSL.upper(NAMESPACE.NAME).eq(DSL.upper(namespaceName)))
.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform))
.fetchOneInto(ExtensionDTO.class);
.and(DSL.upper(NAMESPACE.NAME).eq(DSL.upper(namespaceName)));

if(targetPlatform != null) {
query = query.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform));
}

return query.fetchOneInto(ExtensionDTO.class);
}

public List<ExtensionReviewCountDTO> findAllActiveReviewCountsById(Collection<Long> ids) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ExtensionVersionDTORepository {
DSLContext dsl;

public List<ExtensionVersionDTO> findAllActiveByExtensionIdAndTargetPlatform(Collection<Long> extensionIds, String targetPlatform) {
return dsl.select(
var query = dsl.select(
EXTENSION_TARGET_PLATFORM.EXTENSION_ID,
EXTENSION_VERSION.ID,
EXTENSION_VERSION.VERSION,
Expand All @@ -51,49 +51,74 @@ public List<ExtensionVersionDTO> findAllActiveByExtensionIdAndTargetPlatform(Col
.from(EXTENSION_VERSION)
.join(EXTENSION_TARGET_PLATFORM).on(EXTENSION_TARGET_PLATFORM.ID.eq(EXTENSION_VERSION.TARGET_PLATFORM_ID))
.where(EXTENSION_VERSION.ACTIVE.eq(true))
.and(EXTENSION_TARGET_PLATFORM.EXTENSION_ID.in(extensionIds))
.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform))
.fetchInto(ExtensionVersionDTO.class);
.and(EXTENSION_TARGET_PLATFORM.EXTENSION_ID.in(extensionIds));

if(targetPlatform != null) {
query = query.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform));
}

return query.fetchInto(ExtensionVersionDTO.class);
}

List<ExtensionVersionDTO> findAllActiveByExtensionPublicId(String targetPlatform, String extensionPublicId) {
return fetch(findAllActive()
.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform))
.and(EXTENSION.PUBLIC_ID.eq(extensionPublicId)));
var query = findAllActive().and(EXTENSION.PUBLIC_ID.eq(extensionPublicId));
if(targetPlatform != null) {
query = query.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform));
}

return fetch(query);
}

List<ExtensionVersionDTO> findAllActiveByNamespacePublicId(String targetPlatform, String namespacePublicId) {
return fetch(findAllActive()
.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform))
.and(NAMESPACE.PUBLIC_ID.eq(namespacePublicId)));
var query = findAllActive().and(NAMESPACE.PUBLIC_ID.eq(namespacePublicId));
if(query != null) {
query = query.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform));
}

return fetch(query);
}

ExtensionVersionDTO findActiveByVersionAndExtensionNameAndNamespaceName(String extensionVersion, String targetPlatform, String extensionName, String namespaceName) {
return findAllActive()
var query = findAllActive()
.and(EXTENSION_VERSION.VERSION.eq(extensionVersion))
.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform))
.and(DSL.upper(EXTENSION.NAME).eq(DSL.upper(extensionName)))
.and(DSL.upper(NAMESPACE.NAME).eq(DSL.upper(namespaceName)))
.fetchOneInto(ExtensionVersionDTO.class);
.and(DSL.upper(NAMESPACE.NAME).eq(DSL.upper(namespaceName)));

if(targetPlatform != null) {
query = query.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform));
}

return query.fetchOneInto(ExtensionVersionDTO.class);
}

List<ExtensionVersionDTO> findAllActiveByExtensionNameAndNamespaceName(String targetPlatform, String extensionName, String namespaceName) {
return fetch(findAllActive()
.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform))
var query = findAllActive()
.and(DSL.upper(EXTENSION.NAME).eq(DSL.upper(extensionName)))
.and(DSL.upper(NAMESPACE.NAME).eq(DSL.upper(namespaceName))));
.and(DSL.upper(NAMESPACE.NAME).eq(DSL.upper(namespaceName)));

if(targetPlatform != null) {
query = query.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform));
}

return fetch(query);
}

List<ExtensionVersionDTO> findAllActiveByNamespaceName(String targetPlatform, String namespaceName) {
return fetch(findAllActive()
.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform))
.and(DSL.upper(NAMESPACE.NAME).eq(DSL.upper(namespaceName))));
var query = findAllActive().and(DSL.upper(NAMESPACE.NAME).eq(DSL.upper(namespaceName)));
if(targetPlatform != null) {
query = query.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform));
}

return fetch(query);
}

List<ExtensionVersionDTO> findAllActiveByExtensionName(String targetPlatform, String extensionName) {
return fetch(findAllActive()
.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform))
.and(DSL.upper(EXTENSION.NAME).eq(DSL.upper(extensionName))));
var query = findAllActive().and(DSL.upper(EXTENSION.NAME).eq(DSL.upper(extensionName)));
if(targetPlatform != null) {
query = query.and(EXTENSION_TARGET_PLATFORM.NAME.eq(targetPlatform));
}

return fetch(query);
}

private SelectConditionStep<Record> findAllActive() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class RepositoryService {
@Autowired NamespaceMembershipDTORepository namespaceMembershipDTORepo;
@Autowired AdminStatisticsRepository adminStatisticsRepo;
@Autowired AdminStatisticCalculationsRepository adminStatisticCalculationsRepo;
@Autowired ExtensionTargetPlatformRepository extensionTargetPlatformRepo;

public Namespace findNamespace(String name) {
return namespaceRepo.findByNameIgnoreCase(name);
Expand Down Expand Up @@ -87,10 +86,6 @@ public Streamable<Extension> findAllActiveExtensions() {
return extensionRepo.findByActiveTrue();
}

public Streamable<ExtensionTargetPlatform> findAllActiveExtensionTargetPlatforms(String targetPlatform) {
return extensionTargetPlatformRepo.findByNameAndExtensionActiveTrue(targetPlatform);
}

public long countExtensions() {
return extensionRepo.count();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.openvsx.util.TargetPlatformValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.util.Streamable;
import org.springframework.stereotype.Component;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.SearchHit;
Expand Down Expand Up @@ -52,19 +53,23 @@ public boolean isEnabled() {

@Cacheable("database.search")
public SearchHits<ExtensionSearch> search(ISearchService.Options options, Pageable pageRequest) {
var targetPlatformName = Optional.ofNullable(options.targetPlatform)
.filter(TargetPlatformValidator::isValid)
.orElse(ExtensionTargetPlatform.NAME_ANY);

// grab all extension target platforms
var targetPlatforms = repositories.findAllActiveExtensionTargetPlatforms(targetPlatformName);
var targetPlatforms = repositories.findAllActiveExtensions().stream()
.map(Extension::getTargetPlatforms)
.flatMap(List::stream)
.collect(Streamable.toStreamable());

// no extensions in the database
if (targetPlatforms.isEmpty()) {
Aggregations aggregations = new Aggregations(Collections.emptyList());
return new SearchHitsImpl<ExtensionSearch>(0,TotalHitsRelation.OFF, 0f, "", Collections.emptyList(), aggregations);
}

// filter target platform
if(TargetPlatformValidator.isValid(options.targetPlatform)) {
targetPlatforms = targetPlatforms.filter(tp -> tp.getName().equals(options.targetPlatform));
}

// filter category
if (options.category != null) {
targetPlatforms = targetPlatforms.filter(target -> target.getLatest().getCategories().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public class TargetPlatformValidator {
private TargetPlatformValidator(){}

public static boolean isValid(String targetPlatform) {
return TARGET_PLATFORM_NAMES.contains(targetPlatform);
return targetPlatform != null && TARGET_PLATFORM_NAMES.contains(targetPlatform);
}
}
22 changes: 10 additions & 12 deletions server/src/test/java/org/eclipse/openvsx/RegistryAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ public void testPostQueryNamespace() throws Exception {
@Test
public void testPostQueryUnknownExtension() throws Exception {
mockExtensionVersionDTO();
Mockito.when(repositories.findActiveExtensionVersionDTOsByExtensionName(ExtensionTargetPlatform.NAME_ANY, "baz"))
Mockito.when(repositories.findActiveExtensionVersionDTOsByExtensionName(null, "baz"))
.thenReturn(Collections.emptyList());

mockMvc.perform(post("/api/-/query")
Expand Down Expand Up @@ -1062,12 +1062,11 @@ private String namespaceJson(Consumer<NamespaceJson> content) throws JsonProcess
}

private void mockInactiveExtensionVersionDTO(String namespaceName, String extensionName) {
var targetPlatform = ExtensionTargetPlatform.NAME_ANY;
Mockito.when(repositories.findActiveExtensionVersionDTOsByExtensionName(targetPlatform, extensionName, namespaceName))
Mockito.when(repositories.findActiveExtensionVersionDTOsByExtensionName(null, extensionName, namespaceName))
.thenReturn(Collections.emptyList());
Mockito.when(repositories.findActiveExtensionVersionDTOsByNamespaceName(targetPlatform, namespaceName))
Mockito.when(repositories.findActiveExtensionVersionDTOsByNamespaceName(null, namespaceName))
.thenReturn(Collections.emptyList());
Mockito.when(repositories.findActiveExtensionVersionDTOsByExtensionName(targetPlatform, extensionName))
Mockito.when(repositories.findActiveExtensionVersionDTOsByExtensionName(null, extensionName))
.thenReturn(Collections.emptyList());
}

Expand All @@ -1092,18 +1091,17 @@ private ExtensionVersionDTO mockExtensionVersionDTO() {
);

var extensionPublicId = "5678";
var targetPlatform = ExtensionTargetPlatform.NAME_ANY;
Mockito.when(repositories.findActiveExtensionVersionDTOsByExtensionPublicId(targetPlatform, extensionPublicId))
Mockito.when(repositories.findActiveExtensionVersionDTOsByExtensionPublicId(null, extensionPublicId))
.thenReturn(List.of(extVersion));
Mockito.when(repositories.findActiveExtensionVersionDTOsByNamespacePublicId(targetPlatform, namespacePublicId))
Mockito.when(repositories.findActiveExtensionVersionDTOsByNamespacePublicId(null, namespacePublicId))
.thenReturn(List.of(extVersion));
Mockito.when(repositories.findActiveExtensionVersionDTOByVersion(version, targetPlatform, extensionName, namespaceName))
Mockito.when(repositories.findActiveExtensionVersionDTOByVersion(version, null, extensionName, namespaceName))
.thenReturn(extVersion);
Mockito.when(repositories.findActiveExtensionVersionDTOsByExtensionName(targetPlatform, extensionName, namespaceName))
Mockito.when(repositories.findActiveExtensionVersionDTOsByExtensionName(null, extensionName, namespaceName))
.thenReturn(List.of(extVersion));
Mockito.when(repositories.findActiveExtensionVersionDTOsByNamespaceName(targetPlatform, namespaceName))
Mockito.when(repositories.findActiveExtensionVersionDTOsByNamespaceName(null, namespaceName))
.thenReturn(List.of(extVersion));
Mockito.when(repositories.findActiveExtensionVersionDTOsByExtensionName(targetPlatform, extensionName))
Mockito.when(repositories.findActiveExtensionVersionDTOsByExtensionName(null, extensionName))
.thenReturn(List.of(extVersion));

Mockito.when(repositories.findAllActiveReviewCountsByExtensionId(Set.of(extensionId)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public void testAssetNotFound() throws Exception {

// ---------- UTILITY ----------//
private void mockSearch(boolean active) {
mockSearch(ExtensionTargetPlatform.NAME_ANY, active);
mockSearch(null, active);
}

private void mockSearch(String targetPlatform, boolean active) {
Expand Down
Loading

0 comments on commit b690d12

Please sign in to comment.