Fixes #804 - Refactor code for listAfter and listBefore methods in UserRepository (#855)

Co-authored-by: Allan Krueger <allankrg@amazon.com>
This commit is contained in:
Allan Krueger 2021-10-19 16:03:40 -03:00 committed by GitHub
parent 805839f9f4
commit 67a257a384
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 56 deletions

View File

@ -17,9 +17,10 @@
package org.openmetadata.catalog;
import com.codahale.metrics.health.HealthCheck;
import org.openmetadata.catalog.entity.teams.User;
import org.openmetadata.catalog.jdbi3.UserRepository;
import org.openmetadata.catalog.resources.teams.UserResource.UserList;
import org.openmetadata.catalog.util.EntityUtil;
import org.openmetadata.catalog.util.ResultList;
import org.skife.jdbi.v2.DBI;
import java.io.IOException;
@ -38,7 +39,7 @@ public class CatalogHealthCheck extends HealthCheck {
@Override
protected Result check() throws Exception {
try {
UserList users = userRepository.listAfter(fields, 1, "");
ResultList<User> users = userRepository.listAfter(fields, 1, "");
return Result.healthy();
} catch (IOException e) {
return Result.unhealthy(e.getMessage());

View File

@ -42,7 +42,7 @@ import org.openmetadata.catalog.util.EntityUtil.Fields;
import org.openmetadata.catalog.util.JsonUtils;
import org.openmetadata.catalog.util.RestUtil;
import org.openmetadata.catalog.util.RestUtil.PutResponse;
import org.openmetadata.common.utils.CipherText;
import org.openmetadata.catalog.util.ResultList;
import org.skife.jdbi.v2.sqlobject.Bind;
import org.skife.jdbi.v2.sqlobject.CreateSqlObject;
import org.skife.jdbi.v2.sqlobject.SqlQuery;
@ -55,7 +55,9 @@ import javax.json.JsonPatch;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
@ -122,45 +124,48 @@ public abstract class UserRepository {
@CreateSqlObject
abstract ModelDAO modelDAO();
EntityRepository<User> entityRepository = new EntityRepository<User>() {
@Override
public List<String> listAfter(String fqnPrefix, int limitParam, String after) {
return UserRepository.this.userDAO().listAfter(limitParam, after);
}
@Override
public List<String> listBefore(String fqnPrefix, int limitParam, String before) {
return UserRepository.this.userDAO().listBefore(limitParam, before);
}
@Override
public int listCount(String fqnPrefix) {
return UserRepository.this.userDAO().listCount();
}
@Override
public String getFullyQualifiedName(User entity) {
// User does not have a FullyQualifiedName but needs a valid field to paginate
return entity.getName();
}
@Override
public User setFields(User entity, Fields fields) throws IOException, ParseException {
return UserRepository.this.setFields(entity, fields);
}
@Override
public ResultList<User> getResultList(List<User> entities, String beforeCursor, String afterCursor,
int total) throws GeneralSecurityException, UnsupportedEncodingException {
return new UserList(entities, beforeCursor, afterCursor, total);
}
};
@Transaction
public UserList listAfter(Fields fields, int limitParam, String after) throws IOException, GeneralSecurityException {
// forward scrolling, if after == null then first page is being asked being asked
List<String> jsons = userDAO().listAfter(limitParam + 1, after == null ? "" :
CipherText.instance().decrypt(after));
List<User> users = new ArrayList<>();
for (String json : jsons) {
users.add(setFields(JsonUtils.readValue(json, User.class), fields));
}
int total = userDAO().listCount();
String beforeCursor, afterCursor = null;
beforeCursor = after == null ? null : users.get(0).getName();
if (users.size() > limitParam) { // If extra result exists, then next page exists - return after cursor
users.remove(limitParam);
afterCursor = users.get(limitParam - 1).getName();
}
return new UserList(users, beforeCursor, afterCursor, total);
public ResultList<User> listAfter(Fields fields, int limitParam, String after) throws IOException, GeneralSecurityException, ParseException {
return EntityUtil.listAfter(entityRepository, User.class, fields, null, limitParam, after);
}
@Transaction
public UserList listBefore(Fields fields, int limitParam, String before) throws IOException, GeneralSecurityException {
// Reverse scrolling - Get one extra result used for computing before cursor
List<String> jsons = userDAO().listBefore(limitParam + 1, CipherText.instance().decrypt(before));
List<User> users = new ArrayList<>();
for (String json : jsons) {
users.add(setFields(JsonUtils.readValue(json, User.class), fields));
}
int total = userDAO().listCount();
String beforeCursor = null, afterCursor;
if (users.size() > limitParam) { // If extra result exists, then previous page exists - return before cursor
users.remove(0);
beforeCursor = users.get(0).getName();
}
afterCursor = users.get(users.size() - 1).getName();
return new UserList(users, beforeCursor, afterCursor, total);
public ResultList<User> listBefore(Fields fields, int limitParam, String before) throws IOException, GeneralSecurityException, ParseException {
return EntityUtil.listBefore(entityRepository, User.class, fields, null, limitParam, before);
}
@Transaction

View File

@ -63,6 +63,7 @@ import javax.ws.rs.core.UriInfo;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
@ -125,27 +126,28 @@ public class UserResource {
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = UserList.class)))
})
public UserList list(@Context UriInfo uriInfo,
@Context SecurityContext securityContext,
@Parameter(description = "Fields requested in the returned resource",
schema = @Schema(type = "string", example = FIELDS))
@QueryParam("fields") String fieldsParam,
@Parameter(description = "Limit the number users returned. (1 to 1000000, default = 10) ",
schema = @Schema(type = "string", example = "snowflakeWestCoast.financeDB"))
@DefaultValue("10")
@Min(1)
@Max(1000000)
@QueryParam("limit") int limitParam,
@Parameter(description = "Returns list of users before this cursor",
schema = @Schema(type = "string"))
@QueryParam("before") String before,
@Parameter(description = "Returns list of users after this cursor",
schema = @Schema(type = "string"))
@QueryParam("after") String after) throws IOException, GeneralSecurityException {
public ResultList<User> list(@Context UriInfo uriInfo,
@Context SecurityContext securityContext,
@Parameter(description = "Fields requested in the returned resource",
schema = @Schema(type = "string", example = FIELDS))
@QueryParam("fields") String fieldsParam,
@Parameter(description = "Limit the number users returned. (1 to 1000000, default = 10) ",
schema = @Schema(type = "string", example = "snowflakeWestCoast.financeDB"))
@DefaultValue("10")
@Min(1)
@Max(1000000)
@QueryParam("limit") int limitParam,
@Parameter(description = "Returns list of users before this cursor",
schema = @Schema(type = "string"))
@QueryParam("before") String before,
@Parameter(description = "Returns list of users after this cursor",
schema = @Schema(type = "string"))
@QueryParam("after") String after)
throws IOException, GeneralSecurityException, ParseException {
RestUtil.validateCursors(before, after);
Fields fields = new Fields(FIELD_LIST, fieldsParam);
UserList users;
ResultList<User> users;
if (before != null) { // Reverse paging
users = dao.listBefore(fields, limitParam, before);
} else { // Forward paging or first page