Browse Source

Parallelize write queries; use tabs for formatting (doh!)

denkab 10 years ago
parent
commit
b4bc5ca37f

+ 34 - 23
frameworks/Java/spring/src/main/java/com/techempower/spring/web/WorldDatabaseController.java

@@ -10,6 +10,7 @@ import java.util.concurrent.ThreadLocalRandom;
 
 
 import com.techempower.spring.Common;
 import com.techempower.spring.Common;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -37,9 +38,7 @@ final class WorldDatabaseController {
 	List<World> multipleQueries(@RequestParam(value="queries", required=false, defaultValue="1") String rawQueryCount) {
 	List<World> multipleQueries(@RequestParam(value="queries", required=false, defaultValue="1") String rawQueryCount) {
 		Integer queryCount = boundQueryCount(rawQueryCount);
 		Integer queryCount = boundQueryCount(rawQueryCount);
 
 
-		List<World> worlds = new ArrayList<>(queryCount);
 		List<Future<World>> wfs = new ArrayList<>(queryCount);
 		List<Future<World>> wfs = new ArrayList<>(queryCount);
-
 		// it gets better with Java 8, promise!
 		// it gets better with Java 8, promise!
 		for (int i = 0; i < queryCount; i++) {
 		for (int i = 0; i < queryCount; i++) {
 			wfs.add(
 			wfs.add(
@@ -53,40 +52,52 @@ final class WorldDatabaseController {
 					}));
 					}));
 		}
 		}
 
 
-		for (Future<World> wf: wfs) {
-			try {
-				worlds.add(wf.get());
-			} catch (InterruptedException | ExecutionException e) {
-				throw new RuntimeException(e);
-			}
-		}
-		return worlds;
+		return waitFor(wfs);
 	}
 	}
 
 
 	@RequestMapping(value = "/updates", produces = "application/json")
 	@RequestMapping(value = "/updates", produces = "application/json")
 	List<World> updateQueries(@RequestParam(value="queries", required=false, defaultValue="1") String rawQueryCount) {
 	List<World> updateQueries(@RequestParam(value="queries", required=false, defaultValue="1") String rawQueryCount) {
 		Integer queryCount = boundQueryCount(rawQueryCount);
 		Integer queryCount = boundQueryCount(rawQueryCount);
 
 
-		List<World> worlds = new ArrayList<World>(queryCount);
-		Random random = ThreadLocalRandom.current();
+		List<Future<World>> wfs = new ArrayList<>(queryCount);
 
 
 		for (int i = 0; i < queryCount; i++) {
 		for (int i = 0; i < queryCount; i++) {
-			World world = this.worldRepository.findOne(random.nextInt(DB_ROWS) + 1);
-			world.setRandomNumber(random.nextInt(DB_ROWS) + 1);
-			this.worldRepository.save(world);
-			worlds.add(world);
+			wfs.add(Common.EXECUTOR.submit(
+				new Callable<World>() {
+					@Override
+					@Transactional(propagation = Propagation.REQUIRES_NEW)
+					public World call() throws Exception {
+						Random random = ThreadLocalRandom.current();
+						World world = worldRepository.findOne(random.nextInt(DB_ROWS) + 1);
+						world.setRandomNumber(random.nextInt(DB_ROWS) + 1);
+						worldRepository.save(world);
+						return world;
+					}
+				}));
 		}
 		}
 
 
-		return worlds;
+		return waitFor(wfs);
 	}
 	}
 
 
+    private List<World> waitFor(List<Future<World>> wfs) {
+		List<World> worlds = new ArrayList<>(wfs.size());
+		for (Future<World> wf: wfs) {
+			try {
+				worlds.add(wf.get());
+			} catch (InterruptedException | ExecutionException e) {
+				throw new RuntimeException(e);
+			}
+		}
+		return worlds;
+    }
+
 	private Integer boundQueryCount(final String rawString) {
 	private Integer boundQueryCount(final String rawString) {
-        Integer raw;
-        try {
-           raw = Integer.parseInt(rawString);
-        } catch (NumberFormatException e) {
-           raw = null;
-        }
+		Integer raw;
+		try {
+			raw = Integer.parseInt(rawString);
+		} catch (NumberFormatException e) {
+			raw = null;
+		}
 		if (raw == null || raw < 1) {
 		if (raw == null || raw < 1) {
 			return 1;
 			return 1;
 		} else if (raw > 500) {
 		} else if (raw > 500) {