|
@@ -11,14 +11,12 @@ import java.util.concurrent.ExecutionException;
|
|
|
import java.util.concurrent.Future;
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
-import javax.ws.rs.DefaultValue;
|
|
|
import javax.ws.rs.GET;
|
|
|
import javax.ws.rs.Path;
|
|
|
import javax.ws.rs.Produces;
|
|
|
import javax.ws.rs.QueryParam;
|
|
|
import javax.ws.rs.core.Context;
|
|
|
|
|
|
-import org.hibernate.IdentifierLoadAccess;
|
|
|
import org.hibernate.Session;
|
|
|
import org.hibernate.SessionFactory;
|
|
|
|
|
@@ -35,15 +33,12 @@ public class DbResource {
|
|
|
|
|
|
@GET
|
|
|
@Produces(APPLICATION_JSON + "; charset=utf-8")
|
|
|
- public Object db(@QueryParam("queries") String queriesParam)
|
|
|
+ public Object db(@QueryParam("queries") String queryParam, @QueryParam("single") boolean isSingle)
|
|
|
throws ExecutionException, InterruptedException {
|
|
|
|
|
|
- final int queries = getQueries(queriesParam);
|
|
|
+ final int queries = getQueries(queryParam);
|
|
|
final World[] worlds = new World[queries];
|
|
|
final Random random = ThreadLocalRandom.current();
|
|
|
- final Session session = sessionFactory.openSession();
|
|
|
- session.setDefaultReadOnly(true);
|
|
|
- final IdentifierLoadAccess accessor = session.byId(World.class);
|
|
|
|
|
|
Map<Integer, Future<World>> futureWorlds = new ConcurrentHashMap<>();
|
|
|
for (int i = 0; i < queries; i++) {
|
|
@@ -51,7 +46,14 @@ public class DbResource {
|
|
|
new Callable<World>() {
|
|
|
@Override
|
|
|
public World call() throws Exception {
|
|
|
- return (World) accessor.load(random.nextInt(DB_ROWS) + 1);
|
|
|
+ Session session = sessionFactory.openSession();
|
|
|
+ session.setDefaultReadOnly(true);
|
|
|
+
|
|
|
+ try {
|
|
|
+ return (World) session.byId(World.class).load(random.nextInt(DB_ROWS) + 1);
|
|
|
+ } finally {
|
|
|
+ session.close();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
));
|
|
@@ -61,16 +63,16 @@ public class DbResource {
|
|
|
worlds[i] = futureWorlds.get(i).get();
|
|
|
}
|
|
|
|
|
|
- return queries == 1 ? worlds[0] : worlds;
|
|
|
+ return isSingle ? worlds[0] : worlds;
|
|
|
}
|
|
|
|
|
|
private int getQueries(String proto) {
|
|
|
int result = 1;
|
|
|
try {
|
|
|
- result = Integer.parseInt(proto);
|
|
|
- } catch (NumberFormatException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ if (proto != null && !proto.trim().isEmpty()) {
|
|
|
+ result = Integer.parseInt(proto);
|
|
|
+ }
|
|
|
+ } catch (NumberFormatException e) {/* by test contract */}
|
|
|
|
|
|
return Math.min(500, Math.max(1, result));
|
|
|
}
|