Skip to content

Commit

Permalink
issue #16: Make the hyperlinking faster / use less IO - draft (need t…
Browse files Browse the repository at this point in the history
…o refactor the caching aspect into a separate class)
  • Loading branch information
markiewb committed Jan 18, 2015
1 parent d9c2822 commit 66cba81
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>de.markiewb.netbeans.plugins</groupId>
<artifactId>open-file-at-cursor-plugin</artifactId>
<version>1.3.0.4-SNAPSHOT</version>
<version>1.3.0.5-SNAPSHOT</version>
<packaging>nbm</packaging>

<properties>
Expand Down Expand Up @@ -193,6 +193,8 @@ Features:
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-resource-hyperlink-at-cursor/issues/12"&gt;Feature 12&lt;/a&gt;]: Support fully qualified classnames&lt;/li&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-resource-hyperlink-at-cursor/issues/14"&gt;Feature 14&lt;/a&gt;]: Search for classname in dependencies too (only works for dependencies with sources)&lt;/li&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-resource-hyperlink-at-cursor/issues/10"&gt;Feature 10&lt;/a&gt;]: Find files in same package but different source root&lt;/li&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-resource-hyperlink-at-cursor/issues/16"&gt;Issue 16&lt;/a&gt;]: Make the hyperlinking faster / use less IO&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;Updates in 1.2.2:&lt;/h2&gt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -94,6 +96,8 @@ public class ResourceHyperlinkProvider implements HyperlinkProviderExt {
public static final String MAVEN_TYPE_OTHER = "Resources"; //NOI18N
public static final String MAVEN_TYPE_TEST_OTHER = "TestResources"; //NOI18N
public static final String MAVEN_TYPE_GEN_SOURCES = "GeneratedSources"; //NOI18N
public static Cache<ResultTO> cache = new Cache<ResultTO>();
private static final int EXPIRE_CACHE_IN_SECONDS = 2;

public static void openInEditor(FileObject fileToOpen) {
DataObject fileDO;
Expand Down Expand Up @@ -134,8 +138,11 @@ public void preferenceChange(PreferenceChangeEvent evt) {

@Override
public boolean isHyperlinkPoint(Document document, int offset, HyperlinkType type) {
ResultTO matches = findResources(document, offset);
return matches.isValid();
updateCacheIfNecessary(document, offset);
if (null == cache.matches) {
return false;
}
return cache.matches.isValid();
}

private ResultTO findResources(Document document, int offset) {
Expand Down Expand Up @@ -183,7 +190,7 @@ private ResultTO findResources(Document document, int offset) {
// StatusDisplayer.getDefault().setStatusText("Path :" + startOffset + "/" + endOffset + "/" + offset + "//" + (offset - startOffset) + "=" + innerSelectedText);
Set<FileObject> findFiles = findFiles(doc, linkTarget);
if (findFiles.isEmpty()) {
return ResultTO.createEmpty();
return ResultTO.createEmpty(startOffset, endOffset);
}
return ResultTO.create(startOffset, endOffset, linkTarget, findFiles);
}
Expand Down Expand Up @@ -312,14 +319,48 @@ private FileObject getMatchingFileInCurrentDirectory(Document doc, String path)

@Override
public int[] getHyperlinkSpan(Document doc, int offset, HyperlinkType type) {
ResultTO matches = findResources(doc, offset);
updateCacheIfNecessary(doc, offset);
ResultTO matches = cache.matches;
if (matches.isValid()) {
return new int[]{matches.startOffsetInLiteral, matches.endOffsetInLiteral};
} else {
return new int[]{-1, -1};
}
}

private void updateCacheIfNecessary(Document doc, int offset) {
boolean isSameRequest = false;
boolean timeExpired = false;
FileObject fileObject = NbEditorUtilities.getFileObject(doc);
if (null != cache.request_filePath && null != cache.request_lastUpdated) {
final boolean sameFile = fileObject.getPath().equals(cache.request_filePath);
//if there was a previously match
boolean withinOffSetRange = cache.matches.startOffsetInLiteral <= offset && offset <= cache.matches.endOffsetInLiteral;
//if there was not a previously match
final boolean sameOffset = offset == cache.request_offset;

Calendar nowMinus2Seconds = java.util.Calendar.getInstance();
nowMinus2Seconds.roll(Calendar.SECOND, -1 * EXPIRE_CACHE_IN_SECONDS);
timeExpired = cache.request_lastUpdated.before(nowMinus2Seconds.getTime());

isSameRequest = sameFile && (withinOffSetRange || sameOffset);

}
if (isSameRequest && !timeExpired) {

} else {
cache.request_filePath = fileObject.getPath();
cache.request_offset = offset;

cache.request_lastUpdated = new Date();
ResultTO matches = findResources(doc, offset);
cache.matches = matches;
System.out.println(String.format("cacheMiss = %s %s", offset, fileObject));

}

}

@Override
public void performClickAction(Document doc, int position, HyperlinkType type) {
ResultTO matches = findResources(doc, position);
Expand Down Expand Up @@ -378,7 +419,9 @@ public Set<HyperlinkType> getSupportedHyperlinkTypes() {

@Override
public String getTooltipText(Document doc, int offset, HyperlinkType type) {
ResultTO result = findResources(doc, offset);
updateCacheIfNecessary(doc, offset);

ResultTO result = cache.matches;
if (!result.isValid()) {
return null;
}
Expand Down Expand Up @@ -455,7 +498,7 @@ private Collection<? extends FileObject> getMatchingFilesFromOtherSourceRootsBut
String packageName = null;
for (SourceGroup sourceGroup : getAllSourceGroups(p)) {
//SourceGroup: c:/myprojects/project/src/main/java/
//OriginFolder: c:/myprojects/project/src/main/java/com/foo/impl
//OriginFolder: c:/myprojects/project/src/main/java/com/foo/impl
//Result: com/foo/impl (!=null so we found the source root)
final FileObject rootFolder = sourceGroup.getRootFolder();
if (null == rootFolder) {
Expand Down Expand Up @@ -501,6 +544,14 @@ private List<SourceGroup> getAllSourceGroups(Project p) {
return list;
}

static class Cache<T> {

int request_offset;
Date request_lastUpdated;
String request_filePath;
T matches;
}

private static class FileObjectTuple extends Pair<FileObject, String> {

public FileObjectTuple(FileObject first, String second) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,31 @@
* @author markiewb
*/
class ResultTO {

static ResultTO createEmpty(int startOffset, int endOffset) {
return new ResultTO(startOffset, endOffset, null, Collections.<FileObject>emptySet());
}
static ResultTO createEmpty() {
return new ResultTO(-1, -1, null, Collections.<FileObject>emptySet());
}
static ResultTO create(int startOffset, int endOffset, String linkTarget, Collection<FileObject> foundFiles) {
return new ResultTO(startOffset, endOffset, linkTarget, foundFiles);
}
int startOffsetInLiteral;
int endOffsetInLiteral;
String linkTarget;

Collection<FileObject> foundFiles;
ResultTO(int startOffset, int endOffset, String linkTarget, Collection<FileObject> foundFiles) {
this.startOffsetInLiteral = startOffset;
this.endOffsetInLiteral = endOffset;
this.linkTarget = linkTarget;
this.foundFiles = foundFiles;
}
Collection<FileObject> foundFiles;

boolean isValid() {
return !foundFiles.isEmpty();
}

static ResultTO createEmpty() {
return new ResultTO(-1, -1, null, Collections.<FileObject>emptySet());
}

static ResultTO create(int startOffset, int endOffset, String linkTarget, Collection<FileObject> foundFiles) {
return new ResultTO(startOffset, endOffset, linkTarget, foundFiles);
}

}

0 comments on commit 66cba81

Please sign in to comment.