Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precoumpte totals to give exact response fast in autocomplete #2620

Merged
merged 3 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions backend/src/main/java/com/bakdata/conquery/apiv1/FilterSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@
import com.bakdata.conquery.models.jobs.SimpleJob;
import com.bakdata.conquery.util.search.TrieSearch;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Functions;
import com.google.common.collect.Sets;
import lombok.Value;
import it.unimi.dsi.fastutil.objects.Object2LongAVLTreeMap;
import it.unimi.dsi.fastutil.objects.Object2LongMap;
import it.unimi.dsi.fastutil.objects.Object2LongMaps;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;


@Slf4j
@Value
@Data
public class FilterSearch {

private final NamespaceStorage storage;
Expand All @@ -48,6 +52,7 @@ public class FilterSearch {
*/
@JsonIgnore
private final Map<Searchable, TrieSearch<FEValue>> searchCache = new HashMap<>();
private Object2LongMap<SelectFilter<?>> totals = Object2LongMaps.emptyMap();

/**
* From a given {@link FEValue} extract all relevant keywords.
Expand Down Expand Up @@ -75,6 +80,10 @@ public List<TrieSearch<FEValue>> getSearchesFor(SelectFilter<?> filter) {
.collect(Collectors.toList());
}

public long getTotal(SelectFilter<?> filter) {
return totals.getOrDefault(filter, 0);
}


/**
* Scan all SelectFilters and submit {@link SimpleJob}s to create interactive searches for them.
Expand Down Expand Up @@ -159,6 +168,23 @@ public void updateSearch() {
log.debug("Still waiting for {} to finish.", Sets.difference(collectedSearchables, synchronizedResult.keySet()));
}

log.debug("BEGIN counting Search totals.");


// Precompute totals as that can be extremly slow.
totals = new Object2LongAVLTreeMap<>(
allSelectFilters.parallelStream()
.collect(Collectors.toMap(
Functions.identity(),
filter -> filter.getSearchReferences().stream()
.map(synchronizedResult::get)
.flatMap(TrieSearch::stream)
.mapToInt(FEValue::hashCode)
.distinct()
.count()
)));


Comment on lines +175 to +187
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Du könntest überlegen ob du totals zu einem Future machst, damit die Jobqueue nicht blockiert. Du hast ja beschrieben, dass das sehr langsam sein kann

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ne das sind maximal 5sec/Filter aber eher weniger. Und ein Job der durchläuft aber nicht fertig ist finde ich problematisch.

log.debug("DONE loading SourceSearch");
}
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.validation.Validator;

import javax.inject.Inject;
import javax.validation.Validator;

import com.bakdata.conquery.apiv1.IdLabel;
import com.bakdata.conquery.apiv1.frontend.FEList;
Expand Down Expand Up @@ -54,7 +53,6 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
Expand Down Expand Up @@ -273,10 +271,7 @@ private long countAllValues(SelectFilter<?> filter) {
final Namespace namespace = namespaces.get(filter.getDataset().getId());


return namespace.getFilterSearch()
.getSearchesFor(filter).stream()
.mapToLong(TrieSearch::calculateSize)
.sum();
return namespace.getFilterSearch().getTotal(filter);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions docs/REST API JSONs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ Supported Fields:
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/apiv1/ExecutionStatus.java#L25) | tags | list of `String` | `null` | | |
</p></details>

### Type ResolvedConceptsResult<sup><sub><sup> [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L359)</sup></sub></sup>
### Type ResolvedConceptsResult<sup><sub><sup> [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L354)</sup></sub></sup>


<details><summary>Details</summary><p>
Expand All @@ -1308,12 +1308,12 @@ Supported Fields:

| | Field | Type | Default | Example | Description |
| --- | --- | --- | --- | --- | --- |
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L364) | resolvedConcepts | `Set<ConceptElementId<?>>` | ? | | |
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L365) | resolvedFilter | [ResolvedFilterResult](#Type-ResolvedFilterResult) | ? | | |
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L366) | unknownCodes | `Collection<String>` | ? | | |
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L359) | resolvedConcepts | `Set<ConceptElementId<?>>` | ? | | |
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L360) | resolvedFilter | [ResolvedFilterResult](#Type-ResolvedFilterResult) | ? | | |
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L361) | unknownCodes | `Collection<String>` | ? | | |
</p></details>

### Type ResolvedFilterResult<sup><sub><sup> [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L349)</sup></sub></sup>
### Type ResolvedFilterResult<sup><sub><sup> [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L344)</sup></sub></sup>


<details><summary>Details</summary><p>
Expand All @@ -1324,9 +1324,9 @@ Supported Fields:

| | Field | Type | Default | Example | Description |
| --- | --- | --- | --- | --- | --- |
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L355) | filterId | ID of `Filter` | ? | | |
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L354) | tableId | ID of `Connector` | ? | | |
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L356) | value | `Collection<FEValue>` | ? | | |
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L350) | filterId | ID of `Filter` | ? | | |
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L349) | tableId | ID of `Connector` | ? | | |
| [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/resources/api/ConceptsProcessor.java#L351) | value | `Collection<FEValue>` | ? | | |
</p></details>

### Type ValidityDateContainer<sup><sub><sup> [✎](https://github.com/bakdata/conquery/edit/develop/backend/src/main/java/com/bakdata/conquery/apiv1/query/concept/filter/ValidityDateContainer.java#L9)</sup></sub></sup>
Expand Down