Skip to content

Commit

Permalink
Improve background indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed Apr 17, 2019
1 parent 9f7aeb7 commit db81a9f
Show file tree
Hide file tree
Showing 30 changed files with 452 additions and 427 deletions.
3 changes: 2 additions & 1 deletion api/src/main/java/org/jd/gui/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.nio.file.Path;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.Future;

public interface API {
boolean openURI(URI uri);
Expand Down Expand Up @@ -48,5 +49,5 @@ public interface API {

Map<String, String> getPreferences();

Collection<Indexes> getCollectionOfIndexes();
Collection<Future<Indexes>> getCollectionOfFutureIndexes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import org.jd.gui.api.model.Indexes;

import java.util.Collection;
import java.util.concurrent.Future;

public interface IndexesChangeListener {
void indexesChanged(Collection<Indexes> collectionOfIndexes);
void indexesChanged(Collection<Future<Indexes>> collectionOfFutureIndexes);
}
2 changes: 0 additions & 2 deletions api/src/main/java/org/jd/gui/api/model/Indexes.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,5 @@
* </ul>
*/
public interface Indexes {
void waitIndexers();

Map<String, Collection> getIndex(String name);
}
136 changes: 71 additions & 65 deletions app/src/main/java/org/jd/gui/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class MainController implements API {

protected History history = new History();
protected JComponent currentPage = null;
protected ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
protected ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
protected ArrayList<IndexesChangeListener> containerChangeListeners = new ArrayList<>();

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -111,7 +111,7 @@ public MainController(Configuration configuration) {
file -> openFile((File)file));
});
}

// --- Show GUI --- //
@SuppressWarnings("unchecked")
public void show(List<File> files) {
Expand All @@ -132,9 +132,6 @@ public void show(List<File> files) {
IndexerService.getInstance();
TreeNodeFactoryService.getInstance();
TypeFactoryService.getInstance();
PasteHandlerService.getInstance();
PreferencesPanelService.getInstance();
ContextualActionsFactoryService.getInstance();

SwingUtil.invokeLater(() -> {
// Populate recent files menu
Expand All @@ -159,6 +156,11 @@ public void show(List<File> files) {
new JLayer();
});
}, 400, TimeUnit.MILLISECONDS);

PasteHandlerService.getInstance();
PreferencesPanelService.getInstance();
ContextualActionsFactoryService.getInstance();
SourceSaverService.getInstance();
}

// --- Actions --- //
Expand Down Expand Up @@ -312,13 +314,13 @@ protected void onFindNext() {
}

protected void onOpenType() {
openTypeController.show(getCollectionOfIndexes(), uri -> openURI(uri));
openTypeController.show(getCollectionOfFutureIndexes(), uri -> openURI(uri));
}

protected void onOpenTypeHierarchy() {
if (currentPage instanceof FocusedTypeGettable) {
FocusedTypeGettable ftg = (FocusedTypeGettable)currentPage;
openTypeHierarchyController.show(getCollectionOfIndexes(), ftg.getEntry(), ftg.getFocusedTypeName(), uri -> openURI(uri));
openTypeHierarchyController.show(getCollectionOfFutureIndexes(), ftg.getEntry(), ftg.getFocusedTypeName(), uri -> openURI(uri));
}
}

Expand All @@ -330,7 +332,7 @@ protected void onGoTo() {
}

protected void onSearch() {
searchInConstantPoolsController.show(getCollectionOfIndexes(), uri -> openURI(uri));
searchInConstantPoolsController.show(getCollectionOfFutureIndexes(), uri -> openURI(uri));
}

protected void onFindPrevious() {
Expand Down Expand Up @@ -380,32 +382,32 @@ protected void onAbout() {

protected void onCurrentPageChanged(JComponent page) {
currentPage = page;
checkIndexesChange(page);
checkPreferencesChange(page);
checkIndexesChange(page);
}

protected void checkIndexesChange(JComponent page) {
if (page instanceof IndexesChangeListener) {
Collection<Indexes> collectionOfIndexes = getCollectionOfIndexes();
Integer currentHashcode = Integer.valueOf(collectionOfIndexes.hashCode());
Integer lastHashcode = (Integer)page.getClientProperty("collectionOfIndexes-stamp");
protected void checkPreferencesChange(JComponent page) {
if (page instanceof PreferencesChangeListener) {
Map<String, String> preferences = configuration.getPreferences();
Integer currentHashcode = Integer.valueOf(preferences.hashCode());
Integer lastHashcode = (Integer)page.getClientProperty("preferences-hashCode");

if (!currentHashcode.equals(lastHashcode)) {
((IndexesChangeListener)page).indexesChanged(collectionOfIndexes);
page.putClientProperty("collectionOfIndexes-stamp", currentHashcode);
((PreferencesChangeListener)page).preferencesChanged(preferences);
page.putClientProperty("preferences-hashCode", currentHashcode);
}
}
}

protected void checkPreferencesChange(JComponent page) {
if (page instanceof PreferencesChangeListener) {
Map<String, String> preferences = configuration.getPreferences();
Integer currentHashcode = Integer.valueOf(preferences.hashCode());
Integer lastHashcode = (Integer)page.getClientProperty("preferences-stamp");
protected void checkIndexesChange(JComponent page) {
if (page instanceof IndexesChangeListener) {
Collection<Future<Indexes>> collectionOfFutureIndexes = getCollectionOfFutureIndexes();
Integer currentHashcode = Integer.valueOf(collectionOfFutureIndexes.hashCode());
Integer lastHashcode = (Integer)page.getClientProperty("collectionOfFutureIndexes-hashCode");

if (!currentHashcode.equals(lastHashcode)) {
((PreferencesChangeListener)page).preferencesChanged(preferences);
page.putClientProperty("preferences-stamp", currentHashcode);
((IndexesChangeListener)page).indexesChanged(collectionOfFutureIndexes);
page.putClientProperty("collectionOfFutureIndexes-hashCode", currentHashcode);
}
}
}
Expand Down Expand Up @@ -516,12 +518,12 @@ public void componentResized(ComponentEvent e) {
protected void panelClosed() {
SwingUtil.invokeLater(() -> {
// Fire 'indexesChanged' event
Collection<Indexes> collectionOfIndexes = getCollectionOfIndexes();
Collection<Future<Indexes>> collectionOfFutureIndexes = getCollectionOfFutureIndexes();
for (IndexesChangeListener listener : containerChangeListeners) {
listener.indexesChanged(collectionOfIndexes);
listener.indexesChanged(collectionOfFutureIndexes);
}
if (currentPage instanceof IndexesChangeListener) {
((IndexesChangeListener)currentPage).indexesChanged(collectionOfIndexes);
((IndexesChangeListener)currentPage).indexesChanged(collectionOfFutureIndexes);
}
});
}
Expand Down Expand Up @@ -556,15 +558,15 @@ public boolean openURI(int x, int y, Collection<Container.Entry> entries, String
if (entries.size() == 1) {
// Open the single entry uri
Container.Entry entry = entries.iterator().next();
return openURI(UriUtil.createURI(this, getCollectionOfIndexes(), entry, query, fragment));
return openURI(UriUtil.createURI(this, getCollectionOfFutureIndexes(), entry, query, fragment));
} else {
// Multiple entries -> Open a "Select location" popup
Collection<Indexes> collectionOfIndexes = getCollectionOfIndexes();
Collection<Future<Indexes>> collectionOfFutureIndexes = getCollectionOfFutureIndexes();
selectLocationController.show(
new Point(x+(16+2), y+2),
entries,
entry -> openURI(UriUtil.createURI(this, collectionOfIndexes, entry, query, fragment)), // entry selected closure
() -> {}); // popup close closure
entry -> openURI(UriUtil.createURI(this, collectionOfFutureIndexes, entry, query, fragment)), // entry selected closure
() -> {}); // popup close closure
return true;
}
}
Expand All @@ -586,40 +588,25 @@ public <T extends JComponent & UriGettable> void addPanel(String title, Icon ico
mainView.addMainPanel(title, icon, tip, component);

if (component instanceof ContentIndexable) {
Future<Indexes> futureIndexes = executor.submit(() -> ((ContentIndexable)component).index(this));
Indexes indexes = new Indexes() {
@Override public void waitIndexers() {
try {
futureIndexes.get();
} catch (Exception e) {
assert ExceptionUtil.printStackTrace(e);
Future<Indexes> futureIndexes = executor.submit(() -> {
Indexes indexes = ((ContentIndexable)component).index(this);

SwingUtil.invokeLater(() -> {
// Fire 'indexesChanged' event
Collection<Future<Indexes>> collectionOfFutureIndexes = getCollectionOfFutureIndexes();
for (IndexesChangeListener listener : containerChangeListeners) {
listener.indexesChanged(collectionOfFutureIndexes);
}
}
@Override public Map<String, Collection> getIndex(String name) {
try {
return futureIndexes.get().getIndex(name);
} catch (Exception e) {
assert ExceptionUtil.printStackTrace(e);
return null;
if (currentPage instanceof IndexesChangeListener) {
((IndexesChangeListener) currentPage).indexesChanged(collectionOfFutureIndexes);
}
}
};

component.putClientProperty("indexes", indexes);
});

SwingUtil.invokeLater(() -> {
// Fire 'indexesChanged' event
Collection<Indexes> collectionOfIndexes = getCollectionOfIndexes();
for (IndexesChangeListener listener : containerChangeListeners) {
listener.indexesChanged(collectionOfIndexes);
}
if (currentPage instanceof IndexesChangeListener) {
((IndexesChangeListener)currentPage).indexesChanged(collectionOfIndexes);
}
return indexes;
});
}

checkIndexesChange(currentPage);
component.putClientProperty("indexes", futureIndexes);
}
}

@Override public Collection<Action> getContextualActions(Container.Entry entry, String fragment) { return ContextualActionsFactoryService.getInstance().get(this, entry, fragment); }
Expand All @@ -642,16 +629,35 @@ public <T extends JComponent & UriGettable> void addPanel(String title, Icon ico

@Override public Map<String, String> getPreferences() { return configuration.getPreferences(); }

@Override
@SuppressWarnings("unchecked")
public Collection<Indexes> getCollectionOfIndexes() {
public Collection<Future<Indexes>> getCollectionOfFutureIndexes() {
List<JComponent> mainPanels = mainView.getMainPanels();
ArrayList<Indexes> list = new ArrayList<>(mainPanels.size());
ArrayList<Future<Indexes>> list = new ArrayList<Future<Indexes>>(mainPanels.size()) {
@Override
public int hashCode() {
int hashCode = 1;

for (JComponent panel : mainPanels) {
Indexes indexes = (Indexes)panel.getClientProperty("indexes");
try {
for (Future<Indexes> futureIndexes : this) {
hashCode *= 31;

if (indexes != null) {
list.add(indexes);
if (futureIndexes.isDone()) {
hashCode += futureIndexes.get().hashCode();
}
}
} catch (Exception e) {
assert ExceptionUtil.printStackTrace(e);
}

return hashCode;
}
};

for (JComponent panel : mainPanels) {
Future<Indexes> futureIndexes = (Future<Indexes>)panel.getClientProperty("indexes");
if (futureIndexes != null) {
list.add(futureIndexes);
}
}

Expand Down
Loading

0 comments on commit db81a9f

Please sign in to comment.