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

apoc.cache.Static Maven / Gradle / Ivy

There is a newer version: 4.4.0.34
Show newest version
package apoc.cache;

import apoc.ApocConfiguration;
import org.neo4j.procedure.*;
import apoc.result.KeyValueResult;
import apoc.result.MapResult;
import apoc.result.ObjectResult;
import apoc.util.Util;
import org.neo4j.kernel.internal.GraphDatabaseAPI;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @author mh
 * @since 22.05.16
 */
public class Static {

    @Context
    public GraphDatabaseAPI db;

    private static Map storage = new HashMap<>();

    @Procedure("apoc.static.get")
    @Deprecated
    @Description("apoc.static.get(name) - returns statically stored value from config (apoc.static.) or server lifetime storage")
    public Stream getProcedure(@Name("key") String key) {
        return Stream.of(new ObjectResult(storage.getOrDefault(key, fromConfig(key))));
    }

    @UserFunction("apoc.static.get")
    @Description("apoc.static.get(name) - returns statically stored value from config (apoc.static.) or server lifetime storage")
    public Object get(@Name("key") String key) {
        return storage.getOrDefault(key, fromConfig(key));
    }

    @Deprecated
    @Procedure("apoc.static.getAll")
    @Description("apoc.static.getAll(prefix) - returns statically stored values from config (apoc.static..*) or server lifetime storage")
    public Stream getAllProc(@Name("prefix") String prefix) {
        return Stream.of(new MapResult(getFromConfigAndStorage(prefix)));
    }

    @UserFunction("apoc.static.getAll")
    @Description("apoc.static.getAll(prefix) - returns statically stored values from config (apoc.static..*) or server lifetime storage")
    public Map getAll(@Name("prefix") String prefix) {
        return getFromConfigAndStorage(prefix);
    }

    private HashMap getFromConfigAndStorage(@Name("prefix") String prefix) {
        Map config = ApocConfiguration.get("static." + prefix);
        HashMap result = new HashMap<>(config);
        result.putAll(Util.subMap(storage, prefix));
        return result;
    }

    @Procedure("apoc.static.list")
    @Description("apoc.static.list(prefix) - returns statically stored values from config (apoc.static..*) or server lifetime storage")
    public Stream list(@Name("prefix") String prefix) {
        HashMap result = getFromConfigAndStorage(prefix);
        return result.entrySet().stream().map( e -> new KeyValueResult(e.getKey(),e.getValue()));
    }

    private Object fromConfig(@Name("key") String key) {
        return ApocConfiguration.get("static."+key,null);
    }

    @Procedure("apoc.static.set")
    @Description("apoc.static.set(name, value) - stores value under key for server livetime storage, returns previously stored or configured value")
    public Stream set(@Name("key") String key, @Name("value") Object value) {
        Object previous = value == null ? storage.remove(key) : storage.put(key, value);
        return Stream.of(new ObjectResult(previous==null ? fromConfig(key) : previous));
    }

    public static void clear() {
        storage.clear();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy