Commit b28eaa1a authored by Skylot's avatar Skylot

fix(gui): add synchronization to SimpleIndex class (#435)

parent be509c71
......@@ -176,6 +176,7 @@ public class SearchDialog extends CommonSearchDialog {
.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)
......
......@@ -12,11 +12,15 @@ 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) {
synchronized (syncData) {
keys.add(str);
values.add(value);
}
}
@Override
public void put(StringRef str, T value) {
......@@ -39,7 +43,8 @@ public class SimpleIndex<T> implements SearchIndex<T> {
@Override
public Flowable<T> search(final String searchStr, final boolean caseInsensitive) {
return Flowable.create(emitter -> {
int size = size();
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));
......@@ -48,12 +53,15 @@ public class SimpleIndex<T> implements SearchIndex<T> {
return;
}
}
}
emitter.onComplete();
}, BackpressureStrategy.LATEST);
}
@Override
public int size() {
synchronized (syncData) {
return keys.size();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment