Skip to content

Commit

Permalink
Merge pull request #3 from ywy2090/fix
Browse files Browse the repository at this point in the history
use PageHelper for listDataset paging query
  • Loading branch information
ywy2090 authored Aug 21, 2024
2 parents 911b733 + a9af6c1 commit 2337cef
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,6 @@ Dataset getDatasetByDatasetId(
* @return
*/
List<Dataset> queryVisibleDatasetsForUser(
@Param("loginAgency") String loginAgency,
@Param("loginUserSubject") String loginUserSubject,
@Param("loginUserGroupSubjectList") List<String> loginUserGroupSubjectList,
@Param("ownerUser") String ownerUser,
@Param("ownerAgency") String ownerAgency,
@Param("datasetTitle") String datasetTitle,
@Param("permissionType") Integer permissionType,
@Param("startTime") String startTime,
@Param("endTime") String endTime,
@Param("pageOffset") Integer pageOffset,
@Param("pageSize") Integer pageSize);

/**
* count all visible dataset for the user
*
* @return
*/
int countVisibleDatasetsForUser(
@Param("loginAgency") String loginAgency,
@Param("loginUserSubject") String loginUserSubject,
@Param("loginUserGroupSubjectList") List<String> loginUserGroupSubjectList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@Data
@Builder
public class ListDatasetResponse {
int totalCount;
long totalCount;
boolean isLast;
List<Dataset> content;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.webank.wedpr.components.dataset.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.webank.wedpr.components.dataset.common.DatasetConstant;
import com.webank.wedpr.components.dataset.common.DatasetStatus;
import com.webank.wedpr.components.dataset.config.DatasetConfig;
Expand Down Expand Up @@ -463,8 +466,6 @@ public ListDatasetResponse listDataset(
pageSize = datasetConfig.getMaxBatchSize();
}

int pageOffset = (pageNum - 1) * pageSize;

String user = userInfo.getUser();
String agency = userInfo.getAgency();
List<String> userGroupList = userInfo.getUserGroupList();
Expand All @@ -474,22 +475,9 @@ public ListDatasetResponse listDataset(
DatasetPermissionUtils.toSubjectStrList(userGroupList, agency);

try {
int totalCount =
datasetMapper.countVisibleDatasetsForUser(
agency,
userSubject,
userGroupSubjectList,
ownerUser,
ownerAgency,
datasetTitle,
permissionType,
startTime,
endTime);
boolean isLast = true;

List<Dataset> datasetList = new ArrayList<>();
if (totalCount > 0) {
datasetList =
try (Page<Object> objectPage = PageMethod.startPage(pageNum, pageSize)) {

List<Dataset> datasetList =
datasetMapper.queryVisibleDatasetsForUser(
agency,
userSubject,
Expand All @@ -499,35 +487,26 @@ public ListDatasetResponse listDataset(
datasetTitle,
permissionType,
startTime,
endTime,
pageOffset,
pageSize);

for (Dataset dataset : datasetList) {
String datasetId = dataset.getDatasetId();
DatasetUserPermissions datasetUserPermissions =
DatasetUserPermissionValidator.confirmUserDatasetPermissions(
datasetId, userInfo, datasetPermissionMapper, false);
dataset.setPermissions(datasetUserPermissions);
}

isLast = (datasetList.size() < pageSize);
}
endTime);

long endTimeMillis = System.currentTimeMillis();
long totalCount = new PageInfo<>(datasetList).getTotal();
long pageEndOffset = (long) pageNum * pageSize;
boolean isLast = (pageEndOffset >= totalCount);

logger.info(
"list dataset end, totalCount: {}, isLast: {}, cost(ms): {}",
totalCount,
isLast,
(endTimeMillis - startTimeMillis));
long endTimeMillis = System.currentTimeMillis();

return ListDatasetResponse.builder()
.totalCount(totalCount)
.isLast(isLast)
.content(datasetList)
.build();
logger.info(
"list dataset end, totalCount: {}, isLast: {}, cost(ms): {}",
totalCount,
isLast,
(endTimeMillis - startTimeMillis));

return ListDatasetResponse.builder()
.totalCount(totalCount)
.isLast(isLast)
.content(datasetList)
.build();
}
} catch (Exception e) {
logger.error("query visible datasets for user db operation exception ,e: ", e);
throw new DatasetException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,76 +131,6 @@
</if>
ORDER BY
t1.create_at DESC
<if test="pageOffset >= 0">
limit #{pageOffset}, #{pageSize}
</if>
</select>

<select id="countVisibleDatasetsForUser" resultType="java.lang.Integer">
SELECT
COUNT(*)
FROM
(
SELECT
t1.dataset_id
FROM
wedpr_dataset t1
WHERE
1 = 1
<if test="datasetTitle != null and datasetTitle.trim() != ''">
AND t1.dataset_title LIKE CONCAT(#{datasetTitle}, '%')
</if>

<if test="startTime != null and startTime.trim() != ''">
<![CDATA[ and DATEDIFF(t1.create_at, #{startTime}) >= 0]]>
</if>

<if test="endTime != null and endTime.trim() != ''">
<![CDATA[ and DATEDIFF(t1.create_at, #{endTime}) <= 0]]>
</if>

<if test="ownerUser != null and ownerUser.trim() != ''">
AND t1.owner_user_name = #{ownerUser}
</if>
<if test="ownerAgency != null and ownerAgency.trim() != ''">
AND t1.owner_agency_name = #{ownerAgency}
</if>
) AS t1
INNER JOIN (
SELECT
distinct dataset_id
FROM
wedpr_dataset_permission
WHERE
(1 = 1)
<if test="permissionType != null">
AND (permission_type = #{permissionType})
</if>
AND
CURDATE() &lt;= expired_at
AND (
permission_scope = 'global'
OR (
permission_scope = 'agency'
AND permission_subject_id = #{loginAgency}
)

<if test="loginUserGroupSubjectList != null and !loginUserGroupSubjectList.isEmpty()">
OR (
permission_scope = 'user_group'
AND permission_subject_id IN
<foreach item="loginUserGroupSubject" index="index" collection="loginUserGroupSubjectList" open="(" separator="," close=")">
#{loginUserGroupSubject}
</foreach>
)
</if>
OR (
permission_scope = 'user'
AND permission_subject_id = #{loginUserSubject}
)
)
) AS t2
ON t1.dataset_id = t2.dataset_id
</select>

<insert id="insertDataset" parameterType="com.webank.wedpr.components.dataset.dao.Dataset">
Expand Down

0 comments on commit 2337cef

Please sign in to comment.