tech.ydb.yoj.repository.db.cache.TransactionLocal Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yoj-repository Show documentation
Show all versions of yoj-repository Show documentation
Core YOJ (YDB ORM for Java) abstractions and APIs for domain entities, repositories, transactions etc.
The 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();
}
}