Skip to content

Commit

Permalink
GH-1425: refactored indexer mechanics to mostly work on indexer model…
Browse files Browse the repository at this point in the history
… elements instead of concrete bean objects
  • Loading branch information
martinlippert committed Feb 5, 2025
1 parent ae6806b commit b9e90da
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
public interface SpringIndexElement {

List<SpringIndexElement> getChildren();

void addChild(SpringIndexElement child);
void removeChild(SpringIndexElement doc);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -83,6 +84,7 @@
import org.springframework.ide.vscode.commons.protocol.spring.BeansParams;
import org.springframework.ide.vscode.commons.protocol.spring.MatchingBeansParams;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndex;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;
import org.springframework.ide.vscode.commons.util.Futures;
import org.springframework.ide.vscode.commons.util.StringUtil;
import org.springframework.ide.vscode.commons.util.UriUtil;
Expand Down Expand Up @@ -166,15 +168,15 @@ public void afterPropertiesSet() throws Exception {

SymbolHandler handler = new SymbolHandler() {
@Override
public void addSymbols(IJavaProject project, String docURI, EnhancedSymbolInformation[] enhancedSymbols, Bean[] beanDefinitions,
public void addSymbols(IJavaProject project, String docURI, EnhancedSymbolInformation[] enhancedSymbols, List<SpringIndexElement> beanDefinitions,
List<Diagnostic> diagnostics) {

if (enhancedSymbols != null) {
SpringSymbolIndex.this.addSymbolsByDoc(project, docURI, enhancedSymbols);
}

if (beanDefinitions != null) {
springIndex.updateBeans(project.getElementName(), docURI, beanDefinitions);
springIndex.updateElements(project.getElementName(), docURI, beanDefinitions.toArray(SpringIndexElement[]::new));
}

if (diagnostics != null) {
Expand All @@ -185,7 +187,7 @@ public void addSymbols(IJavaProject project, String docURI, EnhancedSymbolInform

@Override
public void addSymbols(IJavaProject project, EnhancedSymbolInformation[] enhancedSymbols,
Bean[] beanDefinitions, Map<String, List<Diagnostic>> diagnosticsPerDoc) {
Map<String, List<SpringIndexElement>> beanDefinitionsByDoc, Map<String, List<Diagnostic>> diagnosticsPerDoc) {

if (enhancedSymbols != null) {

Expand All @@ -207,21 +209,14 @@ public void addSymbols(IJavaProject project, EnhancedSymbolInformation[] enhance
}
}

if (beanDefinitions != null) {
if (beanDefinitionsByDoc != null) {

// organize beans per doc URI
Map<String, List<Bean>> beansPerDoc = new HashMap<>();
for (Bean bean : beanDefinitions) {
String docURI = bean.getLocation().getUri();
beansPerDoc.computeIfAbsent(docURI, k -> new ArrayList<>()).add(bean);
}

// add beans per doc URI
for (Map.Entry<String, List<Bean>> entry : beansPerDoc.entrySet()) {
for (Entry<String, List<SpringIndexElement>> entry : beanDefinitionsByDoc.entrySet()) {
String docURI = entry.getKey();
List<Bean> beans = entry.getValue();
List<SpringIndexElement> elements = entry.getValue();

springIndex.updateBeans(project.getElementName(), docURI, (Bean[]) beans.toArray(new Bean[beans.size()]));
springIndex.updateElements(project.getElementName(), docURI, elements.toArray(SpringIndexElement[]::new));
}
}

Expand All @@ -236,7 +231,7 @@ public void addSymbols(IJavaProject project, EnhancedSymbolInformation[] enhance
@Override
public void removeSymbols(IJavaProject project, String docURI) {
SpringSymbolIndex.this.removeSymbolsByDoc(project, docURI);
springIndex.removeBeans(project.getElementName(), docURI);
springIndex.removeElements(project.getElementName(), docURI);

// TODO remove diagnostics ?!? maybe, maybe not

Expand Down Expand Up @@ -382,19 +377,22 @@ public CompletableFuture<Void> initializeProject(IJavaProject project, boolean c
private CompletableFuture<Void> _initializeProject(IJavaProject project, boolean clean) {
try {
if (SpringProjectUtil.isBootProject(project) || SpringProjectUtil.isSpringProject(project)) {

if (project.getElementName() == null) {
// Projects indexed by name. No name - no index for it
log.debug("Project with NULL name is being initialized");
return CompletableFuture.completedFuture(null);

} else {

synchronized(this) { // synchronized since the `indexers` array can change via a settings change
@SuppressWarnings("unchecked")
CompletableFuture<Void>[] futures = new CompletableFuture[this.indexers.length + 1];

// clean future
futures[0] = CompletableFuture.runAsync(() -> {
removeSymbolsByProject(project);
springIndex.removeBeans(project.getElementName());
springIndex.removeProject(project.getElementName());
}, this.updateQueue);

// index futures
Expand Down Expand Up @@ -923,7 +921,7 @@ public void run() {
try {
for (String doc : this.docURIs) {
removeSymbolsByDoc(project, doc);
springIndex.removeBeans(project.getElementName(), doc);
springIndex.removeElements(project.getElementName(), doc);
}

for (SpringIndexer index : this.indexer) {
Expand Down Expand Up @@ -955,7 +953,7 @@ public void run() {
for (SpringIndexer index : this.indexer) {
index.removeProject(project);
}
springIndex.removeBeans(project.getElementName());
springIndex.removeProject(project.getElementName());
server.getClient().indexUpdated();

log.debug("{} completed", this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,23 @@ public void updateBeans(String projectName, Bean[] beanDefinitions) {
projectRootElements.put(projectName, projectRoot);
}

public void updateBeans(String projectName, String docURI, Bean[] beanDefinitions) {
public void updateElements(String projectName, String docURI, SpringIndexElement[] beanDefinitions) {
ProjectElement project = this.projectRootElements.computeIfAbsent(projectName, name -> new ProjectElement(name));
project.removeDocument(docURI);

DocumentElement document = new DocumentElement(docURI);
for (Bean bean : beanDefinitions) {
for (SpringIndexElement bean : beanDefinitions) {
document.addChild(bean);
}

project.addChild(document);
}

public void removeBeans(String projectName) {
public void removeProject(String projectName) {
projectRootElements.remove(projectName);
}

public void removeBeans(String projectName, String docURI) {
public void removeElements(String projectName, String docURI) {
ProjectElement project = projectRootElements.get(projectName);
if (project != null) {
project.removeDocument(docURI);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 VMware, Inc.
* Copyright (c) 2023, 2025 VMware, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,19 +11,19 @@
package org.springframework.ide.vscode.boot.java.beans;

import org.springframework.ide.vscode.boot.index.cache.AbstractIndexCacheable;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;

public class CachedBean extends AbstractIndexCacheable {

private final Bean bean;
private final SpringIndexElement element;

public CachedBean(String docURI, Bean bean) {
public CachedBean(String docURI, SpringIndexElement bean) {
super(docURI);
this.bean = bean;
this.element = bean;
}

public Bean getBean() {
return this.bean;
public SpringIndexElement getBean() {
return this.element;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
import org.springframework.ide.vscode.commons.languageserver.reconcile.IProblemCollector;
import org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileProblem;
import org.springframework.ide.vscode.commons.protocol.java.Classpath;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;
import org.springframework.ide.vscode.commons.util.UriUtil;
import org.springframework.ide.vscode.commons.util.text.TextDocument;

Expand Down Expand Up @@ -320,7 +320,7 @@ public void accept(String docURI, Diagnostic diagnostic) {
// dependencyTracker.dump();

EnhancedSymbolInformation[] symbols = generatedSymbols.stream().map(cachedSymbol -> cachedSymbol.getEnhancedSymbol()).toArray(EnhancedSymbolInformation[]::new);
Bean[] beans = generatedBeans.stream().filter(cachedBean -> cachedBean.getBean() != null).map(cachedBean -> cachedBean.getBean()).toArray(Bean[]::new);
List<SpringIndexElement> beans = generatedBeans.stream().filter(cachedBean -> cachedBean.getBean() != null).map(cachedBean -> cachedBean.getBean()).toList();
List<Diagnostic> diagnostics = generatedDiagnostics.stream().filter(cachedDiagnostics -> cachedDiagnostics.getDiagnostic() != null).map(cachedDiagnostic -> cachedDiagnostic.getDiagnostic()).collect(Collectors.toList());

symbolHandler.addSymbols(project, docURI, symbols, beans, diagnostics);
Expand Down Expand Up @@ -436,7 +436,7 @@ public void acceptAST(String sourceFilePath, CompilationUnit cu) {
}

EnhancedSymbolInformation[] symbols = generatedSymbols.stream().map(cachedSymbol -> cachedSymbol.getEnhancedSymbol()).toArray(EnhancedSymbolInformation[]::new);
Bean[] beans = generatedBeans.stream().filter(cachedBean -> cachedBean.getBean() != null).map(cachedBean -> cachedBean.getBean()).toArray(Bean[]::new);
Map<String, List<SpringIndexElement>> beans = generatedBeans.stream().filter(cachedBean -> cachedBean.getBean() != null).collect(Collectors.groupingBy(CachedBean::getDocURI, Collectors.mapping(CachedBean::getBean, Collectors.toList())));
Map<String, List<Diagnostic>> diagnosticsByDoc = generatedDiagnostics.stream().filter(cachedDiagnostic -> cachedDiagnostic.getDiagnostic() != null).collect(Collectors.groupingBy(CachedDiagnostics::getDocURI, Collectors.mapping(CachedDiagnostics::getDiagnostic, Collectors.toList())));
addEmptyDiagnostics(diagnosticsByDoc, javaFiles);
symbolHandler.addSymbols(project, symbols, beans, diagnosticsByDoc);
Expand Down Expand Up @@ -548,7 +548,7 @@ public void accept(String docURI, Diagnostic diagnostic) {

if (symbols != null && beans != null) {
EnhancedSymbolInformation[] enhancedSymbols = Arrays.stream(symbols).map(cachedSymbol -> cachedSymbol.getEnhancedSymbol()).toArray(EnhancedSymbolInformation[]::new);
Bean[] allBeans = Arrays.stream(beans).filter(cachedBean -> cachedBean.getBean() != null).map(cachedBean -> cachedBean.getBean()).toArray(Bean[]::new);
Map<String, List<SpringIndexElement>> allBeans = Arrays.stream(beans).filter(cachedBean -> cachedBean.getBean() != null).collect(Collectors.groupingBy(CachedBean::getDocURI, Collectors.mapping(CachedBean::getBean, Collectors.toList())));
Map<String, List<Diagnostic>> diagnosticsByDoc = Arrays.stream(diagnostics).filter(cachedDiagnostic -> cachedDiagnostic.getDiagnostic() != null).collect(Collectors.groupingBy(CachedDiagnostics::getDocURI, Collectors.mapping(CachedDiagnostics::getDiagnostic, Collectors.toList())));
addEmptyDiagnostics(diagnosticsByDoc, javaFiles);
symbolHandler.addSymbols(project, enhancedSymbols, allBeans, diagnosticsByDoc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.springframework.ide.vscode.commons.java.IClasspathUtil;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;
import org.springframework.ide.vscode.commons.util.UriUtil;
import org.springframework.ide.vscode.commons.util.text.TextDocument;

Expand Down Expand Up @@ -142,7 +142,7 @@ public void initializeProject(IJavaProject project, boolean clean) throws Except

if (symbols != null && beans != null) {
EnhancedSymbolInformation[] enhancedSymbols = Arrays.stream(symbols).map(cachedSymbol -> cachedSymbol.getEnhancedSymbol()).toArray(EnhancedSymbolInformation[]::new);
Bean[] allBeans = Arrays.stream(beans).filter(cachedBean -> cachedBean.getBean() != null).map(cachedBean -> cachedBean.getBean()).toArray(Bean[]::new);
Map<String, List<SpringIndexElement>> allBeans = Arrays.stream(beans).filter(cachedBean -> cachedBean.getBean() != null).collect(Collectors.groupingBy(CachedBean::getDocURI, Collectors.mapping(CachedBean::getBean, Collectors.toList())));
symbolHandler.addSymbols(project, enhancedSymbols, allBeans, null);
}

Expand Down Expand Up @@ -180,7 +180,7 @@ public void updateFile(IJavaProject project, DocumentDescriptor updatedDoc, Stri
this.cache.update(beansCacheKey, file, updatedDoc.getLastModified(), generatedBeans, null, CachedBean.class);

EnhancedSymbolInformation[] symbols = generatedSymbols.stream().map(cachedSymbol -> cachedSymbol.getEnhancedSymbol()).toArray(EnhancedSymbolInformation[]::new);
Bean[] beans = generatedBeans.stream().filter(cachedBean -> cachedBean.getBean() != null).map(cachedBean -> cachedBean.getBean()).toArray(Bean[]::new);
List<SpringIndexElement> beans = generatedBeans.stream().filter(cachedBean -> cachedBean.getBean() != null).map(cachedBean -> cachedBean.getBean()).toList();
symbolHandler.addSymbols(project, docURI, symbols, beans, null);
}

Expand All @@ -207,7 +207,7 @@ public void updateFiles(IJavaProject project, DocumentDescriptor[] updatedDocs)
this.cache.update(beansCacheKey, file, updatedDoc.getLastModified(), generatedBeans, null, CachedBean.class);

EnhancedSymbolInformation[] symbols = generatedSymbols.stream().map(cachedSymbol -> cachedSymbol.getEnhancedSymbol()).toArray(EnhancedSymbolInformation[]::new);
Bean[] beans = generatedBeans.stream().filter(cachedBean -> cachedBean.getBean() != null).map(cachedBean -> cachedBean.getBean()).toArray(Bean[]::new);
List<SpringIndexElement> beans = generatedBeans.stream().filter(cachedBean -> cachedBean.getBean() != null).map(cachedBean -> cachedBean.getBean()).toList();
symbolHandler.addSymbols(project, docURI, symbols, beans, null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
import org.eclipse.lsp4j.Diagnostic;
import org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement;

/**
* @author Martin Lippert
*/
public interface SymbolHandler {

void addSymbols(IJavaProject project, String docURI, EnhancedSymbolInformation[] enhancedSymbols, Bean[] beanDefinitions, List<Diagnostic> diagnostics);
void addSymbols(IJavaProject project, EnhancedSymbolInformation[] enhancedSymbols, Bean[] beanDefinitions, Map<String, List<Diagnostic>> diagnosticsByDoc);
void addSymbols(IJavaProject project, String docURI, EnhancedSymbolInformation[] enhancedSymbols, List<SpringIndexElement> beanDefinitions, List<Diagnostic> diagnostics);
void addSymbols(IJavaProject project, EnhancedSymbolInformation[] enhancedSymbols, Map<String, List<SpringIndexElement>> beanDefinitionsByDoc, Map<String, List<Diagnostic>> diagnosticsByDoc);

void removeSymbols(IJavaProject project, String docURI);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ void testUpdateBeansForSpecificDoc() {
Bean bean2 = new Bean("beanName2", "beanType", locationForDoc1, emptyInjectionPoints, emptySupertypes, emptyAnnotations, false);
Bean bean3 = new Bean("beanName3", "beanType", locationForDoc2, emptyInjectionPoints, emptySupertypes, emptyAnnotations, false);

index.updateBeans("someProject", locationForDoc1.getUri(), new Bean[] {bean1, bean2});
index.updateBeans("someProject", locationForDoc2.getUri(), new Bean[] {bean3});
index.updateElements("someProject", locationForDoc1.getUri(), new Bean[] {bean1, bean2});
index.updateElements("someProject", locationForDoc2.getUri(), new Bean[] {bean3});

Bean updatedBean1 = new Bean("updated1", "beanType", locationForDoc1, emptyInjectionPoints, emptySupertypes, emptyAnnotations, false);
Bean updatedBean2 = new Bean("updated2", "beanType", locationForDoc1, emptyInjectionPoints, emptySupertypes, emptyAnnotations, false);

index.updateBeans("someProject", locationForDoc1.getUri(), new Bean[] {updatedBean1, updatedBean2});
index.updateElements("someProject", locationForDoc1.getUri(), new Bean[] {updatedBean1, updatedBean2});

Bean[] beans = index.getBeansOfProject("someProject");
assertNotNull(beans);
Expand Down Expand Up @@ -192,7 +192,7 @@ void testRemoveAllBeansForSpecificProject() {
index.updateBeans("someProject1", new Bean[] {bean1, bean2});
index.updateBeans("someProject2", new Bean[] {bean3});

index.removeBeans("someProject1");
index.removeProject("someProject1");

Bean[] beans = index.getBeansOfProject("someProject2");
assertNotNull(beans);
Expand All @@ -214,7 +214,7 @@ void testRemoveAllBeansForSpecificDocument() {
Bean bean3 = new Bean("beanName3", "beanType", locationForDoc2, emptyInjectionPoints, emptySupertypes, emptyAnnotations, false);

index.updateBeans("someProject", new Bean[] {bean1, bean2, bean3});
index.removeBeans("someProject", locationForDoc1.getUri());
index.removeElements("someProject", locationForDoc1.getUri());

Bean[] beans = index.getBeansOfProject("someProject");
assertNotNull(beans);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public class TestDependsOnClass {

List<Bean> beansOfDoc = new ArrayList<>(List.of(springIndex.getBeansOfDocument(expectedDefinitionUri)));
beansOfDoc.add(new Bean("bean1", "type", new Location(expectedDefinitionUri, new Range(new Position(20, 1), new Position(20, 10))), null, null, null, false));
springIndex.updateBeans(project.getElementName(), expectedDefinitionUri, beansOfDoc.toArray(new Bean[0]));
springIndex.updateElements(project.getElementName(), expectedDefinitionUri, beansOfDoc.toArray(new Bean[0]));

Bean[] beans = springIndex.getBeansWithName(project.getElementName(), "bean1");
assertEquals(2, beans.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public class TestDependsOnClass {

List<Bean> beansOfDoc = new ArrayList<>(List.of(springIndex.getBeansOfDocument(expectedDefinitionUri)));
beansOfDoc.add(new Bean("bean1", "type", new Location(expectedDefinitionUri, new Range(new Position(20, 1), new Position(20, 10))), null, null, null, false));
springIndex.updateBeans(project.getElementName(), expectedDefinitionUri, beansOfDoc.toArray(new Bean[0]));
springIndex.updateElements(project.getElementName(), expectedDefinitionUri, beansOfDoc.toArray(new Bean[0]));

Bean[] beans = springIndex.getBeansWithName(project.getElementName(), "bean1");
assertEquals(2, beans.length);
Expand Down

0 comments on commit b9e90da

Please sign in to comment.