Skip to content

Commit

Permalink
LPD-27982 When db partitioning is enabled, split the batch into each …
Browse files Browse the repository at this point in the history
…company.
  • Loading branch information
shuyangzhou authored and brianchandotcom committed Jun 8, 2024
1 parent a414aeb commit e90e7f3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
compileOnly project(":apps:portal-security-audit:portal-security-audit-storage-api")
compileOnly project(":apps:portal:portal-aop-api")
compileOnly project(":apps:static:portal:portal-upgrade-api")
compileOnly project(":core:petra:petra-function")
compileOnly project(":core:petra:petra-lang")
compileOnly project(":core:petra:petra-sql-dsl-api")
compileOnly project(":core:petra:petra-string")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package com.liferay.portal.security.audit.storage.internal;

import com.liferay.portal.kernel.audit.AuditMessage;
import com.liferay.portal.kernel.db.partition.DBPartition;
import com.liferay.portal.kernel.service.CompanyLocalService;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.security.audit.AuditEvent;
import com.liferay.portal.security.audit.AuditEventManager;
Expand All @@ -14,7 +16,9 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand All @@ -33,7 +37,29 @@ public AuditEvent addAuditEvent(AuditMessage auditMessage) {

@Override
public void addAuditEvents(List<AuditMessage> auditMessages) {
_auditEventLocalService.addAuditEvents(auditMessages);
if (DBPartition.isPartitionEnabled()) {
Map<Long, List<AuditMessage>> auditMessagesMap = new HashMap<>();

for (AuditMessage auditMessage : auditMessages) {
List<AuditMessage> companyAuditMessages =
auditMessagesMap.computeIfAbsent(
auditMessage.getCompanyId(), key -> new ArrayList<>());

companyAuditMessages.add(auditMessage);
}

for (Map.Entry<Long, List<AuditMessage>> entry :
auditMessagesMap.entrySet()) {

_companyLocalService.forEachCompanyId(
companyId -> _auditEventLocalService.addAuditEvents(
entry.getValue()),
new long[] {entry.getKey()});
}
}
else {
_auditEventLocalService.addAuditEvents(auditMessages);
}
}

@Override
Expand Down Expand Up @@ -121,4 +147,7 @@ private List<AuditEvent> _translate(
@Reference
private AuditEventLocalService _auditEventLocalService;

@Reference
private CompanyLocalService _companyLocalService;

}

0 comments on commit e90e7f3

Please sign in to comment.