All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.nosqlbench.nb.api.config.standard.ConfigSuggestions Maven / Gradle / Ivy

Go to download

The top level API module for NoSQLBench. This module should have no internal module dependencies other than the mvn-default module. All modules within NoSQLBench can safely depend on this module with circular dependencies. This module provides cross-cutting code infrastracture, such as path utilities and ways of describing services used between modules. It is also the transitive aggregation point for system-wide library dependencies for logging and testing or similar needs.

There is a newer version: 5.17.0
Show newest version
package io.nosqlbench.nb.api.config.standard;

import org.apache.commons.text.similarity.LevenshteinDistance;

import java.util.*;
import java.util.stream.Collectors;

public class ConfigSuggestions {

    public static Optional getForParam(ConfigModel model, String param) {
        return suggestAlternateCase(model,param)
            .or(() -> suggestAlternates(model,param));
    }

    private static Optional suggestAlternateCase(ConfigModel model, String param) {
        for (String cname : model.getNamedParams().keySet()) {
            if (cname.equalsIgnoreCase(param)) {
                return Optional.of("Did you mean '" + cname + "'?");
            }
        }
        return Optional.empty();
    }

    private static Optional suggestAlternates(ConfigModel model, String param) {
        Map> suggestions = new HashMap<>();
        for (String candidate : model.getNamedParams().keySet()) {
            try {
                Integer distance = LevenshteinDistance.getDefaultInstance().apply(param, candidate);
                Set strings = suggestions.computeIfAbsent(distance, d -> new HashSet<>());
                strings.add(candidate);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        ArrayList params = new ArrayList<>(suggestions.keySet());
        Collections.sort(params);
        List> orderedSets = params.stream().map(suggestions::get).collect(Collectors.toList());
        if (orderedSets.size()==0) {
            return Optional.empty();
        } else if (orderedSets.get(0).size()==1) {
            return Optional.of("Did you mean '" + orderedSets.get(0).stream().findFirst().get() +"'?");
        } else {
            return Optional.of("Did you mean one of " + orderedSets.get(0).toString() + "?\n");
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy