Skip to content

Commit

Permalink
perf: reduce queries performed by eventRows (dhis2#7710)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnespolino authored Mar 24, 2021
1 parent a033318 commit 27bb970
Show file tree
Hide file tree
Showing 20 changed files with 532 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2004-2021, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.dxf2.events.event;

import java.util.Map;

import lombok.Data;
import lombok.RequiredArgsConstructor;

import org.apache.commons.lang3.tuple.Pair;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.Program;

public class EventContext
{
private final Map<String, TrackedEntityOuInfo> trackedEntityOuInfoByUid;

private final Map<String, Program> programsByUid;

private final Map<Pair<String, String>, String> orgUnitByTeiUidAndProgramUidPairs;

private final Map<String, OrganisationUnit> orgUnitsByUid;

public EventContext( Map<String, TrackedEntityOuInfo> trackedEntityOuInfoByUid,
Map<String, Program> programsByUid,
Map<Pair<String, String>, String> orgUnitByTeiUidAndProgramUidPairs,
Map<String, OrganisationUnit> orgUnitsByUid )
{
this.trackedEntityOuInfoByUid = trackedEntityOuInfoByUid;
this.programsByUid = programsByUid;
this.orgUnitByTeiUidAndProgramUidPairs = orgUnitByTeiUidAndProgramUidPairs;
this.orgUnitsByUid = orgUnitsByUid;
}

public Map<String, TrackedEntityOuInfo> getTrackedEntityOuInfoByUid()
{
return trackedEntityOuInfoByUid;
}

public Map<String, Program> getProgramsByUid()
{
return programsByUid;
}

public Map<Pair<String, String>, String> getOrgUnitByTeiUidAndProgramUidPairs()
{
return orgUnitByTeiUidAndProgramUidPairs;
}

public Map<String, OrganisationUnit> getOrgUnitsByUid()
{
return orgUnitsByUid;
}

@Data
@RequiredArgsConstructor
public static class TrackedEntityOuInfo
{
private final Long trackerEntityId;

private final String trackedEntityUid;

private final Long orgUnitId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package org.hisp.dhis.program;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -100,6 +101,8 @@ public interface ProgramService
*/
List<Program> getAllPrograms();

Collection<Program> getPrograms( Collection<String> uids );

/**
* Get all {@link Program} belong to a orgunit
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.hisp.dhis.common.Grid;
import org.hisp.dhis.common.IllegalQueryException;
import org.hisp.dhis.dxf2.events.event.EventContext;
import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
import org.hisp.dhis.user.User;

Expand Down Expand Up @@ -282,4 +283,7 @@ long createTrackedEntityInstance( TrackedEntityInstance entityInstance,
Set<TrackedEntityAttributeValue> attributeValues );

List<TrackedEntityInstance> getTrackedEntityInstancesByUid( List<String> uids, User user );

List<EventContext.TrackedEntityOuInfo> getTrackedEntityOuInfoByUid( List<String> uids, User user );

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Map;

import org.hisp.dhis.common.IdentifiableObjectStore;
import org.hisp.dhis.dxf2.events.event.EventContext;
import org.hisp.dhis.user.User;

/**
Expand Down Expand Up @@ -98,4 +99,6 @@ public interface TrackedEntityInstanceStore
void updateTrackedEntityInstancesSyncTimestamp( List<String> trackedEntityInstanceUIDs, Date lastSynchronized );

List<TrackedEntityInstance> getTrackedEntityInstancesByUid( List<String> uids, User user );

List<EventContext.TrackedEntityOuInfo> getTrackedEntityOuInfoByUid( List<String> uids, User user );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2004-2004-2020, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.trackedentity;

public class TrackedEntityProgramOwnerIds
{
private final String trackedEntityInstanceId;

private final String programId;

private final String orgUnitUid;

public TrackedEntityProgramOwnerIds( String trackedEntityInstanceId, String programId, String orgUnitUid )
{
this.trackedEntityInstanceId = trackedEntityInstanceId;
this.programId = programId;
this.orgUnitUid = orgUnitUid;
}

public String getTrackedEntityInstanceId()
{
return trackedEntityInstanceId;
}

public String getProgramId()
{
return programId;
}

public String getOrgUnitUid()
{
return orgUnitUid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public interface TrackedEntityProgramOwnerService
*/
List<TrackedEntityProgramOwner> getTrackedEntityProgramOwnersUsingId( List<Long> teiIds, Program program );

List<TrackedEntityProgramOwnerIds> getTrackedEntityProgramOwnersUidsUsingId( List<Long> teiIds, Program program );

/**
* Assign an orgUnit as the owner for a tracked entity instance for the
* given program. If another owner already exist then it would be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@ public interface TrackedEntityProgramOwnerStore extends GenericStore<TrackedEnti
* @return matching tracked entity program owner entities.
*/
List<TrackedEntityProgramOwner> getTrackedEntityProgramOwners( List<Long> teiIds, long programId );

List<TrackedEntityProgramOwnerIds> getTrackedEntityProgramOwnersUids( List<Long> teiIds, long programId );

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package org.hisp.dhis.trackedentity;

import org.hisp.dhis.dxf2.events.event.EventContext;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.user.User;
Expand Down Expand Up @@ -71,6 +72,9 @@ void assignOwnership( TrackedEntityInstance entityInstance, Program program, Org

boolean hasAccess( User user, String entityInstance, OrganisationUnit organisationUnit, Program program );

boolean hasAccessUsingContext( User user, String trackedEntityInstanceUid, String programUid,
EventContext eventContext );

/**
* Grant temporary ownership for a user for a specific tei-program
* combination
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -760,11 +760,22 @@ public List<T> getByUid( Collection<String> uids )

CriteriaBuilder builder = getCriteriaBuilder();

JpaQueryParameters<T> jpaQueryParameters = new JpaQueryParameters<T>()
.addPredicates( getSharingPredicates( builder ) )
.addPredicate( root -> root.get( "uid" ).in( uids ) );
List<List<String>> uidPartitions = Lists.partition( new ArrayList<>( uids ), 20000 );

return getList( builder, jpaQueryParameters );
List<Function<Root<T>, Predicate>> sharingPredicates = getSharingPredicates( builder );

List<T> returnList = new ArrayList<>();

for ( List<String> partition : uidPartitions )
{
JpaQueryParameters<T> jpaQueryParameters = new JpaQueryParameters<T>()
.addPredicates( sharingPredicates )
.addPredicate( root -> root.get( "uid" ).in( partition ) );

returnList.addAll( getList( builder, jpaQueryParameters ) );
}

return returnList;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -205,7 +206,7 @@ public List<OrganisationUnit> getOrganisationUnits( Collection<Long> identifiers
@Transactional( readOnly = true )
public List<OrganisationUnit> getOrganisationUnitsByUid( Collection<String> uids )
{
return organisationUnitStore.getByUid( uids );
return organisationUnitStore.getByUid( new HashSet<>( uids ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package org.hisp.dhis.program;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -108,6 +109,13 @@ public Program getProgram( long id )
return programStore.get( id );
}

@Override
@Transactional( readOnly = true )
public Collection<Program> getPrograms( Collection<String> uids )
{
return programStore.getByUid( uids );
}

@Override
@Transactional( readOnly = true )
public List<Program> getPrograms( OrganisationUnit organisationUnit )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -67,6 +68,7 @@
import org.hisp.dhis.common.Pager;
import org.hisp.dhis.common.QueryItem;
import org.hisp.dhis.common.ValueType;
import org.hisp.dhis.dxf2.events.event.EventContext;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.organisationunit.OrganisationUnitService;
import org.hisp.dhis.security.Authorities;
Expand Down Expand Up @@ -830,9 +832,24 @@ public long createTrackedEntityInstance( TrackedEntityInstance instance,
@Transactional( readOnly = true )
public List<TrackedEntityInstance> getTrackedEntityInstancesByUid( List<String> uids, User user )
{
if ( uids == null || uids.isEmpty() )
{
return Collections.emptyList();
}
return trackedEntityInstanceStore.getTrackedEntityInstancesByUid( uids, user );
}

@Override
@Transactional( readOnly = true )
public List<EventContext.TrackedEntityOuInfo> getTrackedEntityOuInfoByUid( List<String> uids, User user )
{
if ( uids == null || uids.isEmpty() )
{
return Collections.emptyList();
}
return trackedEntityInstanceStore.getTrackedEntityOuInfoByUid( uids, user );
}

@Override
@Transactional
public void updateTrackedEntityInstance( TrackedEntityInstance instance )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Collections;
import java.util.List;

import org.hisp.dhis.organisationunit.OrganisationUnit;
Expand Down Expand Up @@ -340,4 +341,16 @@ public List<TrackedEntityProgramOwner> getTrackedEntityProgramOwnersUsingId( Lis
return trackedEntityProgramOwnerStore.getTrackedEntityProgramOwners( teiIds, program.getId() );
}

@Override
@Transactional( readOnly = true )
public List<TrackedEntityProgramOwnerIds> getTrackedEntityProgramOwnersUidsUsingId( List<Long> teiIds,
Program program )
{
if ( teiIds.isEmpty() )
{
return Collections.emptyList();
}
return trackedEntityProgramOwnerStore.getTrackedEntityProgramOwnersUids( teiIds, program.getId() );
}

}
Loading

0 comments on commit 27bb970

Please sign in to comment.