Skip to content

Commit

Permalink
Further improvements for SQL performance
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Jan 9, 2025
1 parent 7efa895 commit 93beacb
Show file tree
Hide file tree
Showing 35 changed files with 294 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public ISMPServiceGroupManager createServiceGroupMgr ()
public ISMPRedirectManager createRedirectMgr (@Nonnull final IIdentifierFactory aIdentifierFactory,
@Nonnull final ISMPServiceGroupManager aServiceGroupMgr)
{
return new SMPRedirectManagerMongoDB (aIdentifierFactory, aServiceGroupMgr);
return new SMPRedirectManagerMongoDB (aIdentifierFactory);
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@
import com.helger.commons.state.EChange;
import com.helger.commons.string.StringHelper;
import com.helger.peppolid.IDocumentTypeIdentifier;
import com.helger.peppolid.IParticipantIdentifier;
import com.helger.peppolid.factory.IIdentifierFactory;
import com.helger.phoss.smp.domain.redirect.ISMPRedirect;
import com.helger.phoss.smp.domain.redirect.ISMPRedirectCallback;
import com.helger.phoss.smp.domain.redirect.ISMPRedirectManager;
import com.helger.phoss.smp.domain.redirect.SMPRedirect;
import com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup;
import com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager;
import com.helger.photon.audit.AuditHelper;
import com.helger.security.certificate.CertificateHelper;
import com.mongodb.client.model.Filters;
Expand All @@ -69,15 +68,12 @@ public final class SMPRedirectManagerMongoDB extends AbstractManagerMongoDB impl
private static final String BSON_EXTENSIONS = "extensions";

private final IIdentifierFactory m_aIdentifierFactory;
private final ISMPServiceGroupManager m_aServiceGroupMgr;
private final CallbackList <ISMPRedirectCallback> m_aCallbacks = new CallbackList <> ();

public SMPRedirectManagerMongoDB (@Nonnull final IIdentifierFactory aIdentifierFactory,
@Nonnull final ISMPServiceGroupManager aServiceGroupMgr)
public SMPRedirectManagerMongoDB (@Nonnull final IIdentifierFactory aIdentifierFactory)
{
super ("smp-redirect");
m_aIdentifierFactory = aIdentifierFactory;
m_aServiceGroupMgr = aServiceGroupMgr;
getCollection ().createIndex (Indexes.ascending (BSON_ID));
}

Expand Down Expand Up @@ -107,14 +103,13 @@ public static Document toBson (@Nonnull final ISMPRedirect aValue)
@Nonnull
@ReturnsMutableCopy
public static SMPRedirect toDomain (@Nonnull final IIdentifierFactory aIdentifierFactory,
@Nonnull final ISMPServiceGroupManager aServiceGroupMgr,
@Nonnull final Document aDoc)
{
// The ID itself is derived from ServiceGroupID and DocTypeID
final ISMPServiceGroup aServiceGroup = aServiceGroupMgr.getSMPServiceGroupOfID (aIdentifierFactory.parseParticipantIdentifier (aDoc.getString (BSON_SERVICE_GROUP_ID)));
final IParticipantIdentifier aParticipantIdentifier = aIdentifierFactory.parseParticipantIdentifier (aDoc.getString (BSON_SERVICE_GROUP_ID));
final IDocumentTypeIdentifier aDocTypeID = toDocumentTypeID (aDoc.get (BSON_DOCTYPE_ID, Document.class));
final X509Certificate aCert = CertificateHelper.convertStringToCertficateOrNull (aDoc.getString (BSON_TARGET_CERTIFICATE));
return new SMPRedirect (aServiceGroup,
return new SMPRedirect (aParticipantIdentifier,
aDocTypeID,
aDoc.getString (BSON_TARGET_HREF),
aDoc.getString (BSON_TARGET_SUBJECT_CN),
Expand All @@ -126,7 +121,7 @@ public static SMPRedirect toDomain (@Nonnull final IIdentifierFactory aIdentifie
@ReturnsMutableCopy
public SMPRedirect toDomain (@Nonnull final Document aDoc)
{
return toDomain (m_aIdentifierFactory, m_aServiceGroupMgr, aDoc);
return toDomain (m_aIdentifierFactory, aDoc);
}

@Nonnull
Expand Down Expand Up @@ -167,19 +162,19 @@ private void _updateSMPRedirect (@Nonnull final SMPRedirect aSMPRedirect)
}

@Nonnull
public ISMPRedirect createOrUpdateSMPRedirect (@Nonnull final ISMPServiceGroup aServiceGroup,
public ISMPRedirect createOrUpdateSMPRedirect (@Nonnull final IParticipantIdentifier aParticipantID,
@Nonnull final IDocumentTypeIdentifier aDocumentTypeIdentifier,
@Nonnull @Nonempty final String sTargetHref,
@Nonnull @Nonempty final String sSubjectUniqueIdentifier,
@Nullable final X509Certificate aCertificate,
@Nullable final String sExtension)
{
ValueEnforcer.notNull (aServiceGroup, "ServiceGroup");
ValueEnforcer.notNull (aParticipantID, "ParticipantID");
ValueEnforcer.notNull (aDocumentTypeIdentifier, "DocumentTypeIdentifier");

if (LOGGER.isDebugEnabled ())
LOGGER.debug ("createOrUpdateSMPRedirect (" +
aServiceGroup +
aParticipantID +
", " +
aDocumentTypeIdentifier +
", " +
Expand All @@ -192,9 +187,9 @@ public ISMPRedirect createOrUpdateSMPRedirect (@Nonnull final ISMPServiceGroup a
(StringHelper.hasText (sExtension) ? "with extension" : "without extension") +
")");

final ISMPRedirect aOldRedirect = getSMPRedirectOfServiceGroupAndDocumentType (aServiceGroup,
final ISMPRedirect aOldRedirect = getSMPRedirectOfServiceGroupAndDocumentType (aParticipantID,
aDocumentTypeIdentifier);
final SMPRedirect aNewRedirect = new SMPRedirect (aServiceGroup,
final SMPRedirect aNewRedirect = new SMPRedirect (aParticipantID,
aDocumentTypeIdentifier,
sTargetHref,
sSubjectUniqueIdentifier,
Expand Down Expand Up @@ -255,13 +250,13 @@ public EChange deleteSMPRedirect (@Nullable final ISMPRedirect aSMPRedirect)
}

@Nonnull
public EChange deleteAllSMPRedirectsOfServiceGroup (@Nullable final ISMPServiceGroup aServiceGroup)
public EChange deleteAllSMPRedirectsOfServiceGroup (@Nullable final IParticipantIdentifier aParticipantID)
{
if (aServiceGroup == null)
if (aParticipantID == null)
return EChange.UNCHANGED;

EChange eChange = EChange.UNCHANGED;
for (final ISMPRedirect aRedirect : getAllSMPRedirectsOfServiceGroup (aServiceGroup.getID ()))
for (final ISMPRedirect aRedirect : getAllSMPRedirectsOfServiceGroup (aParticipantID))
eChange = eChange.or (deleteSMPRedirect (aRedirect));
return eChange;
}
Expand All @@ -277,9 +272,9 @@ public ICommonsList <ISMPRedirect> getAllSMPRedirects ()

@Nonnull
@ReturnsMutableCopy
public ICommonsList <ISMPRedirect> getAllSMPRedirectsOfServiceGroup (@Nullable final ISMPServiceGroup aServiceGroup)
public ICommonsList <ISMPRedirect> getAllSMPRedirectsOfServiceGroup (@Nullable final IParticipantIdentifier aParticipantID)
{
return getAllSMPRedirectsOfServiceGroup (aServiceGroup == null ? null : aServiceGroup.getID ());
return getAllSMPRedirectsOfServiceGroup (aParticipantID == null ? null : aParticipantID.getURIEncoded ());
}

@Nonnull
Expand All @@ -300,16 +295,16 @@ public long getSMPRedirectCount ()
}

@Nullable
public ISMPRedirect getSMPRedirectOfServiceGroupAndDocumentType (@Nullable final ISMPServiceGroup aServiceGroup,
public ISMPRedirect getSMPRedirectOfServiceGroupAndDocumentType (@Nullable final IParticipantIdentifier aParticipantID,
@Nullable final IDocumentTypeIdentifier aDocTypeID)
{
if (aServiceGroup == null)
if (aParticipantID == null)
return null;
if (aDocTypeID == null)
return null;

final Document aMatch = getCollection ().find (Filters.and (new Document (BSON_SERVICE_GROUP_ID,
aServiceGroup.getID ()),
aParticipantID.getURIEncoded ()),
new Document (BSON_DOCTYPE_ID, toBson (aDocTypeID))))
.first ();
if (aMatch == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ public EChange deleteSMPServiceGroup (@Nonnull final IParticipantIdentifier aPar

// Delete all redirects (must be done before the SG is deleted)
final ISMPRedirectManager aRedirectMgr = SMPMetaManager.getRedirectMgr ();
aRedirectMgr.deleteAllSMPRedirectsOfServiceGroup (aServiceGroup);
aRedirectMgr.deleteAllSMPRedirectsOfServiceGroup (aParticipantID);

// Delete all service information (must be done before the SG is deleted)
final ISMPServiceInformationManager aServiceInfoMgr = SMPMetaManager.getServiceInformationMgr ();
aServiceInfoMgr.deleteAllSMPServiceInformationOfServiceGroup (aServiceGroup);
aServiceInfoMgr.deleteAllSMPServiceInformationOfServiceGroup (aParticipantID);

final String sServiceGroupID = SMPServiceGroup.createSMPServiceGroupID (aParticipantID);
final DeleteResult aDR = getCollection ().deleteOne (new Document (BSON_ID, sServiceGroupID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,10 @@ public EChange deleteSMPServiceInformation (@Nullable final ISMPServiceInformati
}

@Nonnull
public EChange deleteAllSMPServiceInformationOfServiceGroup (@Nullable final ISMPServiceGroup aServiceGroup)
public EChange deleteAllSMPServiceInformationOfServiceGroup (@Nullable final IParticipantIdentifier aParticipantIdentifier)
{
EChange eChange = EChange.UNCHANGED;
for (final ISMPServiceInformation aSMPServiceInformation : getAllSMPServiceInformationOfServiceGroup (aServiceGroup))
for (final ISMPServiceInformation aSMPServiceInformation : getAllSMPServiceInformationOfServiceGroup (aParticipantIdentifier))
eChange = eChange.or (deleteSMPServiceInformation (aSMPServiceInformation));
return eChange;
}
Expand Down Expand Up @@ -446,23 +446,25 @@ public long getSMPServiceInformationCount ()

@Nonnull
@ReturnsMutableCopy
public ICommonsList <ISMPServiceInformation> getAllSMPServiceInformationOfServiceGroup (@Nullable final ISMPServiceGroup aServiceGroup)
public ICommonsList <ISMPServiceInformation> getAllSMPServiceInformationOfServiceGroup (@Nullable final IParticipantIdentifier aParticipantIdentifier)
{
final ICommonsList <ISMPServiceInformation> ret = new CommonsArrayList <> ();
if (aServiceGroup != null)
getCollection ().find (new Document (BSON_SERVICE_GROUP_ID, aServiceGroup.getID ()))
if (aParticipantIdentifier != null)
{
getCollection ().find (new Document (BSON_SERVICE_GROUP_ID, aParticipantIdentifier.getURIEncoded ()))
.forEach ((Consumer <Document>) x -> ret.add (toServiceInformation (x, true)));
}
return ret;
}

@Nonnull
@ReturnsMutableCopy
public ICommonsList <IDocumentTypeIdentifier> getAllSMPDocumentTypesOfServiceGroup (@Nullable final ISMPServiceGroup aServiceGroup)
public ICommonsList <IDocumentTypeIdentifier> getAllSMPDocumentTypesOfServiceGroup (@Nullable final IParticipantIdentifier aParticipantIdentifier)
{
final ICommonsList <IDocumentTypeIdentifier> ret = new CommonsArrayList <> ();
if (aServiceGroup != null)
if (aParticipantIdentifier != null)
{
getCollection ().find (new Document (BSON_SERVICE_GROUP_ID, aServiceGroup.getID ()))
getCollection ().find (new Document (BSON_SERVICE_GROUP_ID, aParticipantIdentifier.getURIEncoded ()))
.forEach ((Consumer <Document>) x -> ret.add (toServiceInformation (x, false)
.getDocumentTypeIdentifier ()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ public void testRedirectBasic () throws SMPServerException
"xml::xml##doctype4711::1");
assertNotNull (aDocTypeID);

final ISMPRedirect aRedirect = aRedirectMgr.createOrUpdateSMPRedirect (aSG,
final ISMPRedirect aRedirect = aRedirectMgr.createOrUpdateSMPRedirect (aPI,
aDocTypeID,
"bla",
"foo",
null,
"<ext/>");
assertNotNull (aRedirect);
assertSame (aSG, aRedirect.getServiceGroup ());
assertSame (aPI, aRedirect.getServiceGroupParticipantIdentifier ());
assertEquals (aDocTypeID, aRedirect.getDocumentTypeIdentifier ());
assertEquals ("bla", aRedirect.getTargetHref ());
assertEquals ("foo", aRedirect.getSubjectUniqueIdentifier ());
Expand Down Expand Up @@ -126,14 +126,14 @@ public void testRedirectUpperCaseSG () throws SMPServerException
"xml::xml##DocType4711::1");
assertNotNull (aDocTypeID);

final ISMPRedirect aRedirect = aRedirectMgr.createOrUpdateSMPRedirect (aSG,
final ISMPRedirect aRedirect = aRedirectMgr.createOrUpdateSMPRedirect (aPI,
aDocTypeID,
"bla",
"foo",
null,
"<ext/>");
assertNotNull (aRedirect);
assertSame (aSG, aRedirect.getServiceGroup ());
assertSame (aPI, aRedirect.getServiceGroupParticipantIdentifier ());
assertEquals (aDocTypeID, aRedirect.getDocumentTypeIdentifier ());
assertEquals ("bla", aRedirect.getTargetHref ());
assertEquals ("foo", aRedirect.getSubjectUniqueIdentifier ());
Expand All @@ -151,9 +151,7 @@ private static void _testConversion (@Nonnull final ISMPRedirect aSrc)
final Document aDoc = SMPRedirectManagerMongoDB.toBson (aSrc);
assertNotNull (aDoc);

final ISMPRedirect aSrc2 = SMPRedirectManagerMongoDB.toDomain (SMPMetaManager.getIdentifierFactory (),
SMPMetaManager.getServiceGroupMgr (),
aDoc);
final ISMPRedirect aSrc2 = SMPRedirectManagerMongoDB.toDomain (SMPMetaManager.getIdentifierFactory (), aDoc);
assertNotNull (aSrc2);
assertEquals (aSrc, aSrc2);
}
Expand All @@ -177,8 +175,8 @@ public void testConversion () throws SMPServerException
assertNotNull (aSG);
try
{
_testConversion (new SMPRedirect (aSG, aDocTypeID, "target href", "what ever", null, null));
_testConversion (new SMPRedirect (aSG, aDocTypeID, "target href", "what ever", null, "<ext/>"));
_testConversion (new SMPRedirect (aPI, aDocTypeID, "target href", "what ever", null, null));
_testConversion (new SMPRedirect (aPI, aDocTypeID, "target href", "what ever", null, "<ext/>"));
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public ISMPServiceGroupManager createServiceGroupMgr ()
public ISMPRedirectManager createRedirectMgr (@Nonnull final IIdentifierFactory aIdentifierFactory,
@Nonnull final ISMPServiceGroupManager aServiceGroupMgr)
{
return new SMPRedirectManagerJDBC (SMPDBExecutor::new, aServiceGroupMgr);
return new SMPRedirectManagerJDBC (SMPDBExecutor::new);
}

@Nonnull
Expand Down
Loading

0 comments on commit 93beacb

Please sign in to comment.