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

org.opencds.cqf.cql.engine.execution.Cache Maven / Gradle / Ivy

Go to download

The engine library for the Clinical Quality Language Java reference implementation

There is a newer version: 3.18.0
Show newest version
package org.opencds.cqf.cql.engine.execution;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.hl7.elm.r1.FunctionDef;
import org.hl7.elm.r1.FunctionRef;
import org.hl7.elm.r1.VersionedIdentifier;

/**
 * There are at least two types of data that need to be cached, some that is context dependent, like expression results
 * (and thus can be invalidated during the course of evaluation) and some that is not, like Function resolutions (and thus
 * can be cache for the entire duration of the evaluation).
 */
public class Cache {

    private boolean enableExpressionCache = false;

    private Map functionCache = new HashMap<>();

    @SuppressWarnings("serial")
    private Map> expressions =
            new LinkedHashMap>(10, 0.9f, true) {
                @Override
                protected boolean removeEldestEntry(
                        Map.Entry> eldestEntry) {
                    return size() > 10;
                }
            };

    @SuppressWarnings("serial")
    protected Map constructLibraryExpressionHashMap() {
        return new LinkedHashMap(15, 0.9f, true) {
            @Override
            protected boolean removeEldestEntry(Map.Entry eldestEntry) {
                return size() > 15;
            }
        };
    }

    public Map> getExpressions() {
        return expressions;
    }

    public void setExpressionCaching(boolean yayOrNay) {
        this.enableExpressionCache = yayOrNay;
    }

    protected Map getExpressionCache(VersionedIdentifier libraryId) {
        return getExpressions().computeIfAbsent(libraryId, k -> constructLibraryExpressionHashMap());
    }

    public boolean isExpressionCached(VersionedIdentifier libraryId, String name) {
        return getExpressionCache(libraryId).containsKey(name);
    }

    public boolean isExpressionCachingEnabled() {
        return this.enableExpressionCache;
    }

    public void cacheExpression(VersionedIdentifier libraryId, String name, ExpressionResult er) {
        getExpressionCache(libraryId).put(name, er);
    }

    public ExpressionResult getCachedExpression(VersionedIdentifier libraryId, String name) {
        return getExpressionCache(libraryId).get(name);
    }

    public Map getFunctionCache() {
        return functionCache;
    }

    public void cacheFunctionDef(FunctionRef functionRef, FunctionDef functionDef) {
        this.functionCache.put(functionRef, functionDef);
    }

    public FunctionDef getCachedFunctionDef(FunctionRef functionRef) {
        return this.functionCache.get(functionRef);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy