summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/cluster/SchemaResolver.java
blob: 3a2125d1d38218c8681646802f8dd372d8f84187 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.prelude.cluster;

import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.fastsearch.DocumentdbInfoConfig;
import com.yahoo.search.Query;
import com.yahoo.search.searchchain.Execution;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * Resolves schemas from query and execution context
 *
 * @author bjorncs
 */
class SchemaResolver {

    private final Set<String> schemas;

    SchemaResolver(DocumentdbInfoConfig cfg) {
        this(cfg.documentdb().stream().map(DocumentdbInfoConfig.Documentdb::name).toList());
    }

    SchemaResolver(Collection<String> schemas) {
        this.schemas = new LinkedHashSet<>(schemas);
    }

    Set<String> resolve(Query query, Execution execution) {
        return resolve(query, execution.context().getIndexFacts());
    }

    Set<String> resolve(Query query, IndexFacts indexFacts) {
        if (schemas.size() == 1) return Set.of(schemas.iterator().next());
        var restrict = query.getModel().getRestrict();
        if (restrict == null || restrict.isEmpty()) {
            Set<String> sources = query.getModel().getSources();
            return (sources == null || sources.isEmpty())
                    ? schemas
                    : new LinkedHashSet<>(indexFacts.newSession(sources, Set.of(), schemas).documentTypes());
        } else {
            return filterValidDocumentTypes(restrict);
        }
    }

    private Set<String> filterValidDocumentTypes(Collection<String> restrict) {
        Set<String> retval = new LinkedHashSet<>();
        for (String docType : restrict) {
            if (docType != null && schemas.contains(docType)) {
                retval.add(docType);
            }
        }
        return retval;
    }

}