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

tech.ydb.yoj.repository.db.cache.TransactionLocal Maven / Gradle / Ivy

Go to download

Core YOJ (YDB ORM for Java) abstractions and APIs for domain entities, repositories, transactions etc.

There is a newer version: 2.6.1
Show newest version
package tech.ydb.yoj.repository.db.cache;

import lombok.NonNull;
import tech.ydb.yoj.repository.BaseDb;
import tech.ydb.yoj.repository.db.TxOptions;
import tech.ydb.yoj.repository.db.projection.ProjectionCache;
import tech.ydb.yoj.repository.db.projection.RoProjectionCache;
import tech.ydb.yoj.repository.db.projection.RwProjectionCache;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.function.Supplier;

public class TransactionLocal {
    private final Map, Object> singletons = new IdentityHashMap<>();

    private final Supplier firstLevelCacheSupplier;
    private final Supplier projectionCacheSupplier;
    private final Supplier logSupplier;

    public TransactionLocal(@NonNull TxOptions options) {
        this.firstLevelCacheSupplier = options.isFirstLevelCache() ? FirstLevelCache::create : FirstLevelCache::empty;
        this.projectionCacheSupplier = options.isMutable() ? RwProjectionCache::new : RoProjectionCache::new;
        this.logSupplier = () -> new TransactionLog(options.getLogLevel());
    }

    public static TransactionLocal get() {
        return BaseDb.current(Holder.class).getTransactionLocal();
    }

    @SuppressWarnings("unchecked")
    public  X instance(@NonNull Supplier supplier) {
        return (X) singletons.computeIfAbsent(supplier, Supplier::get);
    }

    public ProjectionCache projectionCache() {
        return instance(projectionCacheSupplier);
    }

    public FirstLevelCache firstLevelCache() {
        return instance(firstLevelCacheSupplier);
    }

    public TransactionLog log() {
        return instance(logSupplier);
    }

    public interface Holder {
        TransactionLocal getTransactionLocal();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy