mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-23 21:15:52 +00:00
#1432 - Refactor internals for RemoteTransactionEvent / BeanPersistIds ... to not send inserted ids
This commit is contained in:
@@ -1476,6 +1476,13 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
|
||||
cacheHelp.beanCacheRemove(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a collection of beans from the cache given the ids.
|
||||
*/
|
||||
public void cacheHandleInvalidate(Collection<Object> ids) {
|
||||
cacheHelp.beanCacheInvalidate(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if it managed to populate/load the bean from the cache.
|
||||
*/
|
||||
|
||||
@@ -680,6 +680,21 @@ final class BeanDescriptorCacheHelp<T> {
|
||||
return bean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a bean from the cache given its Id.
|
||||
*/
|
||||
void beanCacheInvalidate(Collection<Object> ids) {
|
||||
if (beanCache != null) {
|
||||
if (beanLog.isDebugEnabled()) {
|
||||
beanLog.debug(" REMOVE {}({})", cacheName, ids);
|
||||
}
|
||||
beanCache.removeAll(new HashSet<>(ids));
|
||||
}
|
||||
for (BeanPropertyAssocOne<?> imported : propertiesOneImported) {
|
||||
imported.cacheClear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a bean from the cache given its Id.
|
||||
*/
|
||||
|
||||
@@ -34,14 +34,12 @@ public final class BeanPersistIdMap {
|
||||
public void add(BeanDescriptor<?> desc, PersistRequest.Type type, Object id) {
|
||||
|
||||
BeanPersistIds r = getPersistIds(desc);
|
||||
r.addId(type, (Serializable) id);
|
||||
r.addId(type, id);
|
||||
}
|
||||
|
||||
private BeanPersistIds getPersistIds(BeanDescriptor<?> desc) {
|
||||
String beanType = desc.getFullName();
|
||||
BeanPersistIds r = beanMap.computeIfAbsent(beanType, k -> new BeanPersistIds(desc));
|
||||
return r;
|
||||
return beanMap.computeIfAbsent(beanType, k -> new BeanPersistIds(desc));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -34,9 +34,10 @@ public class BeanPersistIds implements BinaryWritable {
|
||||
|
||||
private final String descriptorId;
|
||||
|
||||
private List<Object> insertIds;
|
||||
private List<Object> updateIds;
|
||||
private List<Object> deleteIds;
|
||||
/**
|
||||
* The ids to invalidate from the cache (updates and deletes).
|
||||
*/
|
||||
private List<Object> ids;
|
||||
|
||||
/**
|
||||
* Create the payload.
|
||||
@@ -56,47 +57,24 @@ public class BeanPersistIds implements BinaryWritable {
|
||||
|
||||
private void read(BinaryReadContext dataInput) throws IOException {
|
||||
|
||||
IdBinder idBinder = beanDescriptor.getIdBinder();
|
||||
|
||||
int iudType = dataInput.readInt();
|
||||
List<Object> idList = readIdList(dataInput.in(), idBinder);
|
||||
switch (iudType) {
|
||||
case 0:
|
||||
insertIds = idList;
|
||||
break;
|
||||
case 1:
|
||||
updateIds = idList;
|
||||
break;
|
||||
case 2:
|
||||
deleteIds = idList;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new RuntimeException("Invalid iudType " + iudType);
|
||||
}
|
||||
dataInput.readInt(); // legacy read type
|
||||
ids = readIdList(dataInput.in(), beanDescriptor.getIdBinder());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBinary(BinaryWriteContext out) throws IOException {
|
||||
writeBinaryList(out, 0, insertIds);
|
||||
writeBinaryList(out, 1, updateIds);
|
||||
writeBinaryList(out, 2, deleteIds);
|
||||
}
|
||||
|
||||
private void writeBinaryList(BinaryWriteContext out, int type, List<Object> ids) throws IOException {
|
||||
|
||||
if (ids != null) {
|
||||
DataOutputStream os = out.start(TYPE_BEANIUD);
|
||||
os.writeUTF(descriptorId);
|
||||
os.writeInt(1); // legacy marker for update
|
||||
if (ids == null) {
|
||||
os.writeInt(0);
|
||||
} else {
|
||||
os.writeInt(ids.size());
|
||||
IdBinder idBinder = beanDescriptor.getIdBinder();
|
||||
int count = ids.size();
|
||||
|
||||
DataOutputStream os = out.start(TYPE_BEANIUD);
|
||||
os.writeUTF(descriptorId);
|
||||
os.writeInt(type);
|
||||
os.writeInt(count);
|
||||
for (Object insertId : ids) {
|
||||
idBinder.writeData(os, insertId);
|
||||
for (Object idValue : ids) {
|
||||
idBinder.writeData(os, idValue);
|
||||
}
|
||||
os.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,90 +99,37 @@ public class BeanPersistIds implements BinaryWritable {
|
||||
} else {
|
||||
sb.append("descId:").append(descriptorId);
|
||||
}
|
||||
if (insertIds != null) {
|
||||
sb.append(" insertIds:").append(insertIds);
|
||||
}
|
||||
if (updateIds != null) {
|
||||
sb.append(" updateIds:").append(updateIds);
|
||||
}
|
||||
if (deleteIds != null) {
|
||||
sb.append(" deleteIds:").append(deleteIds);
|
||||
if (ids != null) {
|
||||
sb.append(" ids:").append(ids);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void addId(PersistRequest.Type type, Serializable id) {
|
||||
switch (type) {
|
||||
case INSERT:
|
||||
addInsertId(id);
|
||||
break;
|
||||
case UPDATE:
|
||||
addUpdateId(id);
|
||||
break;
|
||||
case DELETE:
|
||||
case DELETE_SOFT:
|
||||
addDeleteId(id);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
public void addId(PersistRequest.Type type, Object id) {
|
||||
if (type != PersistRequest.Type.INSERT) {
|
||||
if (ids == null) {
|
||||
ids = new ArrayList<>();
|
||||
}
|
||||
ids.add(id);
|
||||
}
|
||||
}
|
||||
|
||||
private void addInsertId(Serializable id) {
|
||||
if (insertIds == null) {
|
||||
insertIds = new ArrayList<>();
|
||||
}
|
||||
insertIds.add(id);
|
||||
}
|
||||
|
||||
private void addUpdateId(Serializable id) {
|
||||
if (updateIds == null) {
|
||||
updateIds = new ArrayList<>();
|
||||
}
|
||||
updateIds.add(id);
|
||||
}
|
||||
|
||||
private void addDeleteId(Serializable id) {
|
||||
if (deleteIds == null) {
|
||||
deleteIds = new ArrayList<>();
|
||||
}
|
||||
deleteIds.add(id);
|
||||
}
|
||||
|
||||
public BeanDescriptor<?> getBeanDescriptor() {
|
||||
return beanDescriptor;
|
||||
}
|
||||
|
||||
public List<Object> getDeleteIds() {
|
||||
return deleteIds;
|
||||
}
|
||||
|
||||
public List<Object> getInsertIds() {
|
||||
return insertIds;
|
||||
}
|
||||
|
||||
public List<Object> getUpdateIds() {
|
||||
return updateIds;
|
||||
public List<Object> getIds() {
|
||||
return ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the cache of this event that came from another server in the cluster.
|
||||
*/
|
||||
void notifyCacheAndListener() {
|
||||
|
||||
// any change invalidates the query cache
|
||||
beanDescriptor.clearQueryCache();
|
||||
|
||||
if (updateIds != null) {
|
||||
for (Object id : updateIds) {
|
||||
beanDescriptor.cacheHandleDeleteById(id);
|
||||
}
|
||||
}
|
||||
if (deleteIds != null) {
|
||||
for (Object id : deleteIds) {
|
||||
beanDescriptor.cacheHandleDeleteById(id);
|
||||
}
|
||||
if (ids != null) {
|
||||
beanDescriptor.cacheHandleInvalidate(ids);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,12 +28,10 @@ public final class DeleteByIdMap {
|
||||
public void notifyCache(CacheChangeSet changeSet) {
|
||||
for (BeanPersistIds deleteIds : beanMap.values()) {
|
||||
BeanDescriptor<?> d = deleteIds.getBeanDescriptor();
|
||||
List<Object> idValues = deleteIds.getDeleteIds();
|
||||
List<Object> idValues = deleteIds.getIds();
|
||||
if (idValues != null) {
|
||||
d.queryCacheClear(changeSet);
|
||||
for (Object idValue : idValues) {
|
||||
d.cacheHandleDeleteById(idValue, changeSet);
|
||||
}
|
||||
d.cacheHandleInvalidate(idValues);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,7 +50,7 @@ public final class DeleteByIdMap {
|
||||
public void add(BeanDescriptor<?> desc, Object id) {
|
||||
|
||||
BeanPersistIds r = getPersistIds(desc);
|
||||
r.addId(PersistRequest.Type.DELETE, (Serializable) id);
|
||||
r.addId(PersistRequest.Type.DELETE, id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,15 +59,14 @@ public final class DeleteByIdMap {
|
||||
public void addList(BeanDescriptor<?> desc, List<Object> idList) {
|
||||
|
||||
BeanPersistIds r = getPersistIds(desc);
|
||||
for (Object anIdList : idList) {
|
||||
r.addId(PersistRequest.Type.DELETE, (Serializable) anIdList);
|
||||
for (Object idValue : idList) {
|
||||
r.addId(PersistRequest.Type.DELETE, idValue);
|
||||
}
|
||||
}
|
||||
|
||||
private BeanPersistIds getPersistIds(BeanDescriptor<?> desc) {
|
||||
String beanType = desc.getFullName();
|
||||
BeanPersistIds r = beanMap.computeIfAbsent(beanType, k -> new BeanPersistIds(desc));
|
||||
return r;
|
||||
return beanMap.computeIfAbsent(beanType, k -> new BeanPersistIds(desc));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +80,7 @@ public final class DeleteByIdMap {
|
||||
// Add to queue or bulk update entries
|
||||
boolean queue = (DocStoreMode.QUEUE == mode);
|
||||
String queueId = desc.getDocStoreQueueId();
|
||||
List<Object> idValues = deleteIds.getDeleteIds();
|
||||
List<Object> idValues = deleteIds.getIds();
|
||||
if (idValues != null) {
|
||||
for (Object idValue : idValues) {
|
||||
if (queue) {
|
||||
|
||||
@@ -120,9 +120,7 @@ public class RemoteTransactionEvent implements Runnable, BinaryWritable {
|
||||
public void writeBinary(BinaryWriteContext out) throws IOException {
|
||||
|
||||
DataOutputStream os = out.os();
|
||||
//os.writeInt(TRANSACTION_EVENT);
|
||||
os.writeUTF(serverName);
|
||||
|
||||
if (remoteTableMod != null) {
|
||||
remoteTableMod.writeBinary(out);
|
||||
}
|
||||
|
||||
+3
-4
@@ -57,6 +57,7 @@ public class BinaryTransactionEventReadWriteTest extends BaseTestCase {
|
||||
beanPersistIds.addId(PersistRequest.Type.INSERT, 43);
|
||||
beanPersistIds.addId(PersistRequest.Type.UPDATE, 55);
|
||||
beanPersistIds.addId(PersistRequest.Type.DELETE, 66);
|
||||
beanPersistIds.addId(PersistRequest.Type.UPDATE, 92);
|
||||
|
||||
event.addBeanPersistIds(beanPersistIds);
|
||||
|
||||
@@ -74,10 +75,8 @@ public class BinaryTransactionEventReadWriteTest extends BaseTestCase {
|
||||
|
||||
// Bean persist ids
|
||||
List<BeanPersistIds> beanPersistList = read.getBeanPersistList();
|
||||
assertThat(beanPersistList).hasSize(3);
|
||||
assertThat(beanPersistList.get(0).getInsertIds()).containsOnly(42, 43);
|
||||
assertThat(beanPersistList.get(1).getUpdateIds()).containsOnly(55);
|
||||
assertThat(beanPersistList.get(2).getDeleteIds()).containsOnly(66);
|
||||
assertThat(beanPersistList).hasSize(1);
|
||||
assertThat(beanPersistList.get(0).getIds()).containsOnly(55, 66, 92);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user