-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
amvanbaren
committed
Mar 30, 2023
1 parent
cdf9e01
commit 45f9cca
Showing
11 changed files
with
224 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
server/src/main/java/org/eclipse/openvsx/cache/SearchEntryJsonCacheKeyGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** ****************************************************************************** | ||
* Copyright (c) 2022 Precies. Software Ltd and others | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* ****************************************************************************** */ | ||
package org.eclipse.openvsx.cache; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class SearchEntryJsonCacheKeyGenerator { | ||
|
||
public Object generate(long extensionId, boolean includeAllVersions) { | ||
return "extensionId=" + extensionId + ",includeAllVersions=" + includeAllVersions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
server/src/main/java/org/eclipse/openvsx/json/SearchEntryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** ****************************************************************************** | ||
* Copyright (c) 2022 Precies. Software Ltd and others | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* ****************************************************************************** */ | ||
package org.eclipse.openvsx.json; | ||
|
||
import org.eclipse.openvsx.cache.CacheService; | ||
import org.eclipse.openvsx.entities.Extension; | ||
import org.eclipse.openvsx.entities.ExtensionVersion; | ||
import org.eclipse.openvsx.repositories.RepositoryService; | ||
import org.eclipse.openvsx.search.ExtensionSearch; | ||
import org.eclipse.openvsx.search.SearchUtilService; | ||
import org.eclipse.openvsx.storage.StorageUtilService; | ||
import org.eclipse.openvsx.util.UrlUtil; | ||
import org.eclipse.openvsx.util.VersionService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.elasticsearch.core.SearchHit; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.transaction.Transactional; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.eclipse.openvsx.entities.FileResource.DOWNLOAD; | ||
import static org.eclipse.openvsx.entities.FileResource.ICON; | ||
import static org.eclipse.openvsx.util.UrlUtil.createApiUrl; | ||
|
||
@Component | ||
public class SearchEntryService { | ||
|
||
@Autowired | ||
EntityManager entityManager; | ||
|
||
@Autowired | ||
VersionService versions; | ||
|
||
@Autowired | ||
StorageUtilService storageUtil; | ||
|
||
@Autowired | ||
SearchUtilService search; | ||
|
||
@Autowired | ||
RepositoryService repositories; | ||
|
||
@Autowired | ||
CacheService cache; | ||
|
||
@Transactional | ||
public SearchEntryJson toJson(SearchHit<ExtensionSearch> searchHit, boolean includeAllVersions) { | ||
var searchEntry = cache.getSearchEntryJson(searchHit, includeAllVersions); | ||
if(searchEntry != null) { | ||
return searchEntry; | ||
} | ||
|
||
var extension = getExtension(searchHit); | ||
if(extension == null) { | ||
return null; | ||
} | ||
|
||
var serverUrl = UrlUtil.getBaseUrl(); | ||
if(includeAllVersions && cache != null) { | ||
searchEntry = cache.getSearchEntryJson(searchHit, false); | ||
} | ||
if(searchEntry == null) { | ||
var latest = versions.getLatest(extension, null, false, true); | ||
searchEntry = latest.toSearchEntryJson(); | ||
searchEntry.url = createApiUrl(serverUrl, "api", searchEntry.namespace, searchEntry.name); | ||
searchEntry.files = storageUtil.getFileUrls(latest, serverUrl, DOWNLOAD, ICON); | ||
cache.putSearchEntryJson(searchEntry, searchHit, false); | ||
} | ||
if (includeAllVersions) { | ||
var activeVersions = repositories.findActiveVersions(extension).toList(); | ||
var versionUrls = storageUtil.getFileUrls(activeVersions, serverUrl, DOWNLOAD); | ||
searchEntry.allVersions = getAllVersionReferences(activeVersions, versionUrls, serverUrl); | ||
cache.putSearchEntryJson(searchEntry, searchHit, true); | ||
} | ||
|
||
return searchEntry; | ||
} | ||
|
||
private Extension getExtension(SearchHit<ExtensionSearch> searchHit) { | ||
var searchItem = searchHit.getContent(); | ||
var extension = entityManager.find(Extension.class, searchItem.id); | ||
if (extension == null || !extension.isActive()) { | ||
extension = new Extension(); | ||
extension.setId(searchItem.id); | ||
search.removeSearchEntry(extension); | ||
return null; | ||
} | ||
|
||
return extension; | ||
} | ||
|
||
private List<SearchEntryJson.VersionReference> getAllVersionReferences( | ||
List<ExtensionVersion> extVersions, | ||
Map<Long, Map<String, String>> versionUrls, | ||
String serverUrl | ||
) { | ||
return extVersions.stream() | ||
.sorted(ExtensionVersion.SORT_COMPARATOR) | ||
.map(extVersion -> { | ||
var ref = new SearchEntryJson.VersionReference(); | ||
ref.version = extVersion.getVersion(); | ||
ref.engines = extVersion.getEnginesMap(); | ||
ref.url = UrlUtil.createApiVersionUrl(serverUrl, extVersion); | ||
ref.files = versionUrls.get(extVersion.getId()); | ||
return ref; | ||
}).collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.