Unverified Commit 02faec66 authored by Féry Mathieu (Mathius)'s avatar Féry Mathieu (Mathius)
Browse files

feat(db): Reduce call of datastore with getAllWithoutLinks

parent ebfbd6e9
......@@ -53,7 +53,7 @@ public class Posts extends BaseController {
public WithCursor<List<Post>> posts(
@Parameter(description = "Limit for request", example = "2") @RequestParam @Nullable Integer limit,
@Parameter(description = "Cursor for next entity", example = "CgA=") @RequestParam @Nullable String cursor) {
return postDB.getAllCreatedDesc(limit, cursor != null ? Cursor.fromUrlSafe(cursor) : null);
return postDB.getAllCreatedDescWithoutLinks(limit, cursor != null ? Cursor.fromUrlSafe(cursor) : null);
}
@CrossOrigin
......
......@@ -55,7 +55,7 @@ public class Users extends BaseController {
Cursor cursorObject = cursor != null ? Cursor.fromUrlSafe(cursor) : null;
if (userName != null)
return userDB.searchWithUserNameStartsWith(limit, cursorObject, userName);
return userDB.getAll(limit, cursorObject);
return userDB.getAllWithoutLinks(limit, cursorObject);
}
@CrossOrigin
......
......@@ -355,6 +355,17 @@ public class DBService<EntityUsed extends WithIdentifier> extends ServiceUsingDa
return new WithCursor<>(entities, queryResult.getCursorAfter());
}
public WithCursor<List<EntityUsed>> searchWithoutLinksWith(Integer limit, Cursor lastCursor, OrderBy[] orders,
Filter... filters) {
List<EntityUsed> entities = new ArrayList<>();
QueryResults<Entity> queryResult = getDatastore().run(
buildQuery(Query.newEntityQueryBuilder(), limit, lastCursor, orders, filters));
while (queryResult.hasNext()) {
entities.add(utils.createFromEntityWithoutLinks(queryResult.next()));
}
return new WithCursor<>(entities, queryResult.getCursorAfter());
}
public WithCursor<EntityUsed> searchFirstWith(EntityUsed entity, FilterFieldOf<EntityUsed>... filterFieldOfs) {
return searchFirstWith(getFieldOf(entity, filterFieldOfs));
}
......@@ -509,4 +520,36 @@ public class DBService<EntityUsed extends WithIdentifier> extends ServiceUsingDa
public WithCursor<List<EntityUsed>> getAll(Integer limit, Cursor lastCursor, OrderBy... orders) {
return searchWith(limit, lastCursor, orders);
}
public WithCursor<List<EntityUsed>> getAllWithoutLinks() {
return getAllWithoutLinks(null, (Cursor) null);
}
public WithCursor<List<EntityUsed>> getAllWithoutLinks(Integer limit) {
return getAllWithoutLinks(limit, (Cursor) null);
}
public WithCursor<List<EntityUsed>> getAllWithoutLinks(Cursor lastCursor) {
return getAllWithoutLinks(null, lastCursor);
}
public WithCursor<List<EntityUsed>> getAllWithoutLinks(OrderBy... orders) {
return getAllWithoutLinks(null, null, orders);
}
public WithCursor<List<EntityUsed>> getAllWithoutLinks(Integer limit, Cursor lastCursor) {
return getAllWithoutLinks(limit, lastCursor, new OrderBy[0]);
}
public WithCursor<List<EntityUsed>> getAllWithoutLinks(Integer limit, OrderBy... orders) {
return getAllWithoutLinks(limit, null, orders);
}
public WithCursor<List<EntityUsed>> getAllWithoutLinks(Cursor lastCursor, OrderBy... orders) {
return getAllWithoutLinks(null, lastCursor, orders);
}
public WithCursor<List<EntityUsed>> getAllWithoutLinks(Integer limit, Cursor lastCursor, OrderBy... orders) {
return searchWithoutLinksWith(limit, lastCursor, orders);
}
}
......@@ -52,4 +52,10 @@ public class DBPost extends DBAncestorService<Post, User> {
ordersList.add(createdDesc);
return getAll(limit, lastCursor, ordersList.toArray(new OrderBy[ordersList.size()]));
}
public WithCursor<List<Post>> getAllCreatedDescWithoutLinks(Integer limit, Cursor lastCursor, OrderBy... orders) {
List<OrderBy> ordersList = new ArrayList<>(Arrays.asList(orders));
ordersList.add(createdDesc);
return getAllWithoutLinks(limit, lastCursor, ordersList.toArray(new OrderBy[ordersList.size()]));
}
}
......@@ -102,7 +102,7 @@ public class PostUtils extends AncestorUtils<Post, User> {
try {
Date postTime = entity.getTimestamp("postTime").toDate();
return new Post(entity.getKey().getName(), uri, msg, title, postTime,
getAncestorWithoutLinks(entity), new ArrayList<>());
getAncestorWithoutLinks(entity), null);
} catch (DatastoreException ignored) {
throw new RuntimeException(
String.format("Integrity error of post %s (postTime missing)", entity.getKey().getName()));
......
......@@ -93,8 +93,10 @@ public class UserUtils extends BaseUtils<User> {
if (instance.getUri() != null)
builder.set("uri", instance.getUri());
builder.set("email", instance.getEmail());
updateFollows(instance);
updateLikes(instance);
if (instance.getFollows() != null)
updateFollows(instance);
if (instance.getLikedPosts() != null)
updateLikes(instance);
return builder;
}
......@@ -139,7 +141,7 @@ public class UserUtils extends BaseUtils<User> {
String userName = entity.getString("userName");
try {
String email = entity.getString("email");
return new User(entity.getKey().getName(), userName, email, uri);
return new User(entity.getKey().getName(), userName, email, uri, null, null, null);
} catch (DatastoreException ignored) {
throw new RuntimeException(
String.format("Integrity error of user %s (email missing)", entity.getKey().getName()));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment