Skip to content

Commit

Permalink
fix(gui): add synchronization to SimpleIndex class (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Jan 23, 2019
1 parent be509c7 commit b28eaa1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions jadx-gui/src/main/java/jadx/gui/ui/SearchDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ private void searchFieldSubscribe() {
.subscribeOn(Schedulers.single())
.doOnNext(r -> LOG.debug("search event: {}", r))
.switchMap(text -> prepareSearch(text)
.doOnError(e -> LOG.error("Error prepare search: {}", e.getMessage(), e))
.subscribeOn(Schedulers.single())
.toList()
.toFlowable(), 1)
Expand Down
28 changes: 18 additions & 10 deletions jadx-gui/src/main/java/jadx/gui/utils/search/SimpleIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ public class SimpleIndex<T> implements SearchIndex<T> {
private final List<String> keys = new ArrayList<>();
private final List<T> values = new ArrayList<>();

private final Object syncData = new Object();

@Override
public void put(String str, T value) {
keys.add(str);
values.add(value);
synchronized (syncData) {
keys.add(str);
values.add(value);
}
}

@Override
Expand All @@ -39,13 +43,15 @@ private boolean isMatched(String str, String searchStr, boolean caseInsensitive)
@Override
public Flowable<T> search(final String searchStr, final boolean caseInsensitive) {
return Flowable.create(emitter -> {
int size = size();
for (int i = 0; i < size; i++) {
if (isMatched(keys.get(i), searchStr, caseInsensitive)) {
emitter.onNext(values.get(i));
}
if (emitter.isCancelled()) {
return;
synchronized (syncData) {
int size = keys.size();
for (int i = 0; i < size; i++) {
if (isMatched(keys.get(i), searchStr, caseInsensitive)) {
emitter.onNext(values.get(i));
}
if (emitter.isCancelled()) {
return;
}
}
}
emitter.onComplete();
Expand All @@ -54,6 +60,8 @@ public Flowable<T> search(final String searchStr, final boolean caseInsensitive)

@Override
public int size() {
return keys.size();
synchronized (syncData) {
return keys.size();
}
}
}

0 comments on commit b28eaa1

Please sign in to comment.