|
@@ -13,7 +13,7 @@ import javax.annotation.Resource;
|
|
import org.redkale.net.http.*;
|
|
import org.redkale.net.http.*;
|
|
import org.redkale.service.AbstractService;
|
|
import org.redkale.service.AbstractService;
|
|
import org.redkale.source.*;
|
|
import org.redkale.source.*;
|
|
-import org.redkale.util.StringWrapper;
|
|
|
|
|
|
+import org.redkale.util.AnyValue;
|
|
|
|
|
|
/**
|
|
/**
|
|
*
|
|
*
|
|
@@ -22,13 +22,22 @@ import org.redkale.util.StringWrapper;
|
|
@RestService(name = " ", repair = false)
|
|
@RestService(name = " ", repair = false)
|
|
public class Service extends AbstractService {
|
|
public class Service extends AbstractService {
|
|
|
|
|
|
- private static final byte[] helloBytes = "Hello, world!".getBytes();
|
|
|
|
|
|
+ private static final byte[] helloBytes = "Hello, world!".intern().getBytes();
|
|
|
|
|
|
private final ThreadLocal<Random> localRandom = ThreadLocal.withInitial(Random::new);
|
|
private final ThreadLocal<Random> localRandom = ThreadLocal.withInitial(Random::new);
|
|
|
|
|
|
@Resource
|
|
@Resource
|
|
private DataSource source;
|
|
private DataSource source;
|
|
|
|
|
|
|
|
+ private EntityCache<CachedWorld> entityCache;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void init(AnyValue conf) {
|
|
|
|
+ if (source instanceof DataSqlSource) {
|
|
|
|
+ this.entityCache = ((DataSqlSource) source).loadCache(CachedWorld.class);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
@RestMapping(name = "plaintext")
|
|
@RestMapping(name = "plaintext")
|
|
public byte[] getHelloBytes() {
|
|
public byte[] getHelloBytes() {
|
|
return helloBytes;
|
|
return helloBytes;
|
|
@@ -41,7 +50,7 @@ public class Service extends AbstractService {
|
|
|
|
|
|
@RestMapping(name = "db")
|
|
@RestMapping(name = "db")
|
|
public CompletableFuture<World> findWorldAsync() {
|
|
public CompletableFuture<World> findWorldAsync() {
|
|
- return source.findAsync(World.class, randomId());
|
|
|
|
|
|
+ return source.findAsync(World.class, 1 + localRandom.get().nextInt(10000));
|
|
}
|
|
}
|
|
|
|
|
|
@RestMapping(name = "queries")
|
|
@RestMapping(name = "queries")
|
|
@@ -51,7 +60,7 @@ public class Service extends AbstractService {
|
|
final World[] worlds = new World[size];
|
|
final World[] worlds = new World[size];
|
|
|
|
|
|
final AtomicInteger index = new AtomicInteger();
|
|
final AtomicInteger index = new AtomicInteger();
|
|
- final Function<?, CompletableFuture> func = f -> source.findAsync(World.class, randomId(random))
|
|
|
|
|
|
+ final Function<?, CompletableFuture> func = f -> source.findAsync(World.class, 1 + random.nextInt(10000))
|
|
.thenAccept(v -> worlds[index.getAndIncrement()] = v);
|
|
.thenAccept(v -> worlds[index.getAndIncrement()] = v);
|
|
CompletableFuture future = func.apply(null);
|
|
CompletableFuture future = func.apply(null);
|
|
for (int i = 1; i < size; i++) {
|
|
for (int i = 1; i < size; i++) {
|
|
@@ -67,20 +76,24 @@ public class Service extends AbstractService {
|
|
final World[] worlds = new World[size];
|
|
final World[] worlds = new World[size];
|
|
|
|
|
|
final AtomicInteger index = new AtomicInteger();
|
|
final AtomicInteger index = new AtomicInteger();
|
|
- final Function<?, CompletableFuture> func = f -> source.findAsync(World.class, randomId(random))
|
|
|
|
- .thenAccept(v -> worlds[index.getAndIncrement()] = v.randomNumber(randomId(random)));
|
|
|
|
|
|
+ final Function<?, CompletableFuture> func = f -> source.findAsync(World.class, 1 + random.nextInt(10000))
|
|
|
|
+ .thenAccept(v -> worlds[index.getAndIncrement()] = v.randomNumber(1 + random.nextInt(10000)));
|
|
CompletableFuture future = func.apply(null);
|
|
CompletableFuture future = func.apply(null);
|
|
for (int i = 1; i < size; i++) {
|
|
for (int i = 1; i < size; i++) {
|
|
future = future.thenCompose(func);
|
|
future = future.thenCompose(func);
|
|
}
|
|
}
|
|
- return future.thenCompose(v -> source.updateAsync(sort(worlds))).thenApply(v -> worlds);
|
|
|
|
|
|
+ return future.thenCompose(v -> {
|
|
|
|
+ Arrays.sort(worlds);
|
|
|
|
+ return source.updateAsync(worlds);
|
|
|
|
+ }).thenApply(v -> worlds);
|
|
}
|
|
}
|
|
|
|
|
|
@RestMapping(name = "fortunes")
|
|
@RestMapping(name = "fortunes")
|
|
public CompletableFuture<HttpResult<String>> queryFortunes() {
|
|
public CompletableFuture<HttpResult<String>> queryFortunes() {
|
|
return source.queryListAsync(Fortune.class).thenApply((fortunes) -> {
|
|
return source.queryListAsync(Fortune.class).thenApply((fortunes) -> {
|
|
fortunes.add(new Fortune(0, "Additional fortune added at request time."));
|
|
fortunes.add(new Fortune(0, "Additional fortune added at request time."));
|
|
- String html = FortunesTemplate.template(sort(fortunes)).render().toString();
|
|
|
|
|
|
+ Collections.sort(fortunes);
|
|
|
|
+ String html = FortunesTemplate.template(fortunes).render().toString();
|
|
return new HttpResult("text/html; charset=utf-8", html);
|
|
return new HttpResult("text/html; charset=utf-8", html);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -91,26 +104,9 @@ public class Service extends AbstractService {
|
|
final Random random = localRandom.get();
|
|
final Random random = localRandom.get();
|
|
final CachedWorld[] worlds = new CachedWorld[size];
|
|
final CachedWorld[] worlds = new CachedWorld[size];
|
|
for (int i = 0; i < size; i++) {
|
|
for (int i = 0; i < size; i++) {
|
|
- worlds[i] = source.find(CachedWorld.class, randomId(random));
|
|
|
|
|
|
+ worlds[i] = entityCache.find(1 + random.nextInt(10000));
|
|
}
|
|
}
|
|
return worlds;
|
|
return worlds;
|
|
}
|
|
}
|
|
|
|
|
|
- private World[] sort(World[] words) {
|
|
|
|
- Arrays.sort(words);
|
|
|
|
- return words;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private List<Fortune> sort(List<Fortune> fortunes) {
|
|
|
|
- Collections.sort(fortunes);
|
|
|
|
- return fortunes;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private int randomId() {
|
|
|
|
- return 1 + localRandom.get().nextInt(10000);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private int randomId(Random random) {
|
|
|
|
- return 1 + random.nextInt(10000);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|