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

io.deephaven.lang.completion.CompletionLookups Maven / Gradle / Ivy

There is a newer version: 0.37.0
Show newest version
//
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending
//
package io.deephaven.lang.completion;

import io.deephaven.engine.context.ExecutionContext;
import io.deephaven.engine.table.TableDefinition;
import io.deephaven.engine.context.QueryLibrary;
import io.deephaven.engine.util.ScriptSession;
import io.deephaven.util.datastructures.CachingSupplier;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ForkJoinPool;

/**
 * A lookup object for various values that the {@link ChunkerCompleter} might be interested in.
 *
 * This is extracted into its own class, so preloading can start as soon as the console session starts.
 *
 */
public class CompletionLookups {

    private static final WeakHashMap lookups = new WeakHashMap<>();

    private final CachingSupplier>> statics;
    private final Map referencedTables;
    private final CachingSupplier customCompletions;

    public CompletionLookups(Set customCompletionFactory) {
        final QueryLibrary ql = ExecutionContext.getContext().getQueryLibrary();
        statics = new CachingSupplier<>(ql::getStaticImports);
        referencedTables = new ConcurrentHashMap<>();
        customCompletions = new CachingSupplier<>(() -> new DelegatingCustomCompletion(customCompletionFactory));

        // This can be slow, so lets start it on a background thread right away.
        final ForkJoinPool pool = ForkJoinPool.commonPool();
        pool.execute(statics::get);
    }

    public static CompletionLookups preload(ScriptSession session,
            Set customCompletionFactory) {
        return lookups.computeIfAbsent(session, s -> new CompletionLookups(customCompletionFactory));
    }

    public Collection> getStatics() {
        return statics.get();
    }

    public Map getReferencedTables() {
        return referencedTables;
    }

    public CustomCompletion getCustomCompletions() {
        return customCompletions.get();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy