|
@@ -1,15 +1,15 @@
|
|
|
package com.example.helloworld.db.jdbi;
|
|
|
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Comparator;
|
|
|
+
|
|
|
+import org.jdbi.v3.core.Handle;
|
|
|
import org.jdbi.v3.core.Jdbi;
|
|
|
|
|
|
import com.example.helloworld.db.WorldDAO;
|
|
|
import com.example.helloworld.db.model.World;
|
|
|
import com.example.helloworld.resources.Helper;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Comparator;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
public class WorldRepository implements WorldDAO {
|
|
|
private final Jdbi jdbi;
|
|
|
|
|
@@ -22,6 +22,18 @@ public class WorldRepository implements WorldDAO {
|
|
|
return jdbi.withExtension(WorldJDBIImpl.class, dao -> dao.findById(id));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public World[] findById(int[] ids) {
|
|
|
+ return jdbi.withExtension(WorldJDBIImpl.class, dao -> {
|
|
|
+ World[] worlds = new World[ids.length];
|
|
|
+ for(int i = 0; i < ids.length; i++) {
|
|
|
+ worlds[i] = dao.findById(ids[i]);
|
|
|
+ }
|
|
|
+ return worlds;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public World findAndModify(int id, int newRandomNumber) {
|
|
|
throw new RuntimeException("Don't call this");
|
|
@@ -29,24 +41,23 @@ public class WorldRepository implements WorldDAO {
|
|
|
|
|
|
@Override
|
|
|
public World[] updatesQueries(int totalQueries) {
|
|
|
- return jdbi.withExtension(WorldJDBIImpl.class, dao -> {
|
|
|
- final List<World> updates = new ArrayList<>(totalQueries);
|
|
|
+ try (Handle handle = jdbi.open()) {
|
|
|
+ WorldJDBIImpl dao = handle.attach(WorldJDBIImpl.class);
|
|
|
+
|
|
|
+ final World updates[] = new World[totalQueries];
|
|
|
|
|
|
for (int i = 0; i < totalQueries; i++) {
|
|
|
final World world = dao.findById(Helper.randomWorld());
|
|
|
world.setRandomNumber(Helper.randomWorld());
|
|
|
- updates.add(i, world);
|
|
|
+ updates[i] = world;
|
|
|
}
|
|
|
-
|
|
|
// Reason for sorting : https://github.com/TechEmpower/FrameworkBenchmarks/pull/2684
|
|
|
- updates.sort(Comparator.comparingInt(World::getId));
|
|
|
-
|
|
|
- final World[] updatesArray = updates.toArray(new World[totalQueries]);
|
|
|
-
|
|
|
- dao.update(updatesArray);
|
|
|
-
|
|
|
- return updatesArray;
|
|
|
- });
|
|
|
+ Arrays.sort(updates, Comparator.comparingInt(World::getId));
|
|
|
+ dao.update(updates);
|
|
|
+ handle.commit();
|
|
|
+
|
|
|
+ return updates;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|