
com.landawn.abacus.core.EntityManagerLV Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2015, Haiyang Li.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.landawn.abacus.core;
import static com.landawn.abacus.core.EntityManagerUtil.getEntityIdByEntity;
import static com.landawn.abacus.core.EntityManagerUtil.getLockCode;
import static com.landawn.abacus.core.EntityManagerUtil.getRecordLockTimeout;
import static com.landawn.abacus.core.EntityManagerUtil.getTransactionId;
import static com.landawn.abacus.core.EntityManagerUtil.removeOffsetCount;
import static com.landawn.abacus.core.EntityManagerUtil.resultSet2EntityId;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.landawn.abacus.DataSet;
import com.landawn.abacus.DirtyMarker;
import com.landawn.abacus.EntityId;
import com.landawn.abacus.LockMode;
import com.landawn.abacus.Transaction;
import com.landawn.abacus.Transaction.Action;
import com.landawn.abacus.condition.Condition;
import com.landawn.abacus.core.AbacusConfiguration.EntityManagerConfiguration;
import com.landawn.abacus.core.AbacusConfiguration.EntityManagerConfiguration.LockConfiguration;
import com.landawn.abacus.core.AbacusConfiguration.EntityManagerConfiguration.VersionConfiguration;
import com.landawn.abacus.exception.RecordLockedException;
import com.landawn.abacus.lock.XLock;
import com.landawn.abacus.lock.XLockFactory;
import com.landawn.abacus.metadata.EntityDefinition;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.OperationType;
import com.landawn.abacus.version.Version;
import com.landawn.abacus.version.VersionFactory;
// TODO: Auto-generated Javadoc
/**
*
* @author Haiyang Li
* @param
* @since 0.8
*/
class EntityManagerLV extends EntityManagerImpl {
static final int VERSION_DELTA_FOR_DELETE = -1;
static final int VERSION_DELTA_FOR_UPDATE = 1;
private final Map> tranIdEntityIdsMap = new ConcurrentHashMap<>();
private final XLock xRecordLock;
private final Version recordVersion;
protected EntityManagerLV(EntityManagerConfiguration entityManagerConfig, DBAccessImpl dbAccess) {
super(entityManagerConfig, dbAccess);
VersionConfiguration versionConfig = entityManagerConfig.getVersionConfiguration();
if ((versionConfig == null) || (versionConfig.getProvider() == null)) {
recordVersion = VersionFactory.createLocalVersion();
} else {
recordVersion = VersionFactory.createVersion(versionConfig.getProvider());
}
LockConfiguration lockConfiguration = entityManagerConfig.getLockConfiguration();
if ((lockConfiguration == null) || (lockConfiguration.getRecordLockProvider() == null)) {
xRecordLock = XLockFactory
.createLocalXLock(lockConfiguration == null ? LockConfiguration.DEFAULT_RECORD_LOCK_TIMEOUT : lockConfiguration.getRecordLockTimeout());
} else {
xRecordLock = XLockFactory.createLock(lockConfiguration.getRecordLockProvider());
}
}
/**
*
* @param
* @param targetClass
* @param entityName
* @param selectPropNames
* @param condition
* @param options
* @return
*/
@Override
protected List internalList(Class targetClass, String entityName, Collection selectPropNames, Condition condition,
Map options) {
final EntityDefinition entityDef = checkEntityName(entityName);
if (N.isNullOrEmpty(entityDef.getIdPropertyList())) {
return super.internalList(targetClass, entityName, selectPropNames, condition, options);
} else {
List entityIds = getEntityIdByCondition(entityName, condition, options);
if ((targetClass == null) && N.isNullOrEmpty(entityIds)) {
targetClass = entityDef.getTypeClass();
}
options = removeOffsetCount(options);
List result = getEntities(targetClass, entityDef, entityIds, selectPropNames, options);
// can't reback for cache and version.
// RecycableEntityId.reback(entityIds);
return result;
}
}
/**
* Gets the entities.
*
* @param
* @param targetClass
* @param entityDef
* @param entityIds
* @param selectPropNames
* @param options
* @return
*/
@Override
protected List getEntities(Class targetClass, EntityDefinition entityDef, List extends EntityId> entityIds, Collection selectPropNames,
Map options) {
if (targetClass == null) {
targetClass = entityDef.getTypeClass();
}
if (entityIds.size() == 0) {
return new ArrayList<>();
}
checkLock(entityIds, LockMode.R, options);
final boolean isDirtyMarker = DirtyMarkerUtil.isDirtyMarker(targetClass);
Map entitiesVersion = null;
if (isDirtyMarker) {
entitiesVersion = new HashMap<>();
for (EntityId entityId : entityIds) {
entitiesVersion.put(entityId, recordVersion.get(entityId));
}
}
List entities = super.getEntities(targetClass, entityDef, selectPropNames, EntityManagerUtil.entityId2Condition(entityIds), options);
if ((entities.size() > 0) && isDirtyMarker) {
EntityId entityId = null;
for (T entity : entities) {
entityId = getEntityIdByEntity(entityDef, entity);
DirtyMarkerUtil.setVersion((DirtyMarker) entity, entitiesVersion.get(entityId));
}
}
return entities;
}
/**
* Adds the entities.
*
* @param entityDef
* @param propsList
* @param options
* @return
*/
@Override
protected List addEntities(EntityDefinition entityDef, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy