Update Example.java

This commit is contained in:
Andrea Cavalli 2021-01-31 15:59:21 +01:00
parent cef2c796c0
commit aabf925f2a

View File

@ -11,6 +11,7 @@ import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks; import reactor.core.publisher.Sinks;
import reactor.core.publisher.Sinks.One; import reactor.core.publisher.Sinks.One;
@ -27,24 +28,34 @@ public class Example {
One<Instant> instantFirst = Sinks.one(); One<Instant> instantFirst = Sinks.one();
One<Instant> instantSecond = Sinks.one(); One<Instant> instantSecond = Sinks.one();
One<Instant> instantThird = Sinks.one(); One<Instant> instantThird = Sinks.one();
AtomicInteger ai = new AtomicInteger(0);
new LLLocalDatabaseConnection(Path.of("/tmp/"), true) new LLLocalDatabaseConnection(Path.of("/tmp/"), true)
.connect() .connect()
.flatMap(conn -> conn.getDatabase("testdb", List.of(Column.dictionary("testmap")), false)) .flatMap(conn -> conn.getDatabase("testdb", List.of(Column.dictionary("testmap")), false))
.flatMap(db -> db.getDictionary("testmap")) .flatMap(db -> db.getDictionary("testmap"))
.map(dictionary -> new DatabaseMapDictionary<>(dictionary, ssg, ser, 10)) .map(dictionary -> new DatabaseMapDictionary<>(dictionary, ssg, ser, 10))
.doOnSuccess(s -> System.out.println("Setting new value at key " + Arrays.toString(itemKey) + ": " + Arrays.toString(newValue))) .flatMapMany(map -> Mono
.defer(() -> Mono
.fromRunnable(() -> System.out.println("Setting new value at key " + Arrays.toString(itemKey) + ": " + Arrays.toString(newValue)))
.doOnSuccess(s -> instantFirst.tryEmitValue(Instant.now())) .doOnSuccess(s -> instantFirst.tryEmitValue(Instant.now()))
.flatMap(map -> map.at(null, itemKeyBuffer)) .flatMap(s -> map.at(null, itemKeyBuffer))
.doOnSuccess(s -> instantSecond.tryEmitValue(Instant.now())) .doOnSuccess(s -> instantSecond.tryEmitValue(Instant.now()))
.flatMap(handle -> handle.setAndGetPrevious(newValue)) .flatMap(handle -> handle.setAndGetPrevious(newValue))
.doOnSuccess(s -> instantThird.tryEmitValue(Instant.now())) .doOnSuccess(s -> instantThird.tryEmitValue(Instant.now()))
.doOnSuccess(oldValue -> System.out.println("Old value: " + (oldValue == null ? "None" : Arrays.toString(oldValue)))) .doOnSuccess(oldValue -> System.out.println("Old value: " + (oldValue == null ? "None" : Arrays.toString(oldValue))))
.then(Mono.zip(instantFirst.asMono(), instantSecond.asMono(), instantThird.asMono())) .then(Mono.zip(instantFirst.asMono(), instantSecond.asMono(), instantThird.asMono()))
.doOnSuccess(s -> { .doOnSuccess(s -> {
System.out.println("Time to get value reference: " + Duration.between(s.getT1(), s.getT2()).toMillis() + "ms"); var dur1 = Duration.between(s.getT1(), s.getT2());
System.out.println("Time to set new value and get previous: " + Duration.between(s.getT2(), s.getT3()).toMillis() + "ms"); var dur2 = Duration.between(s.getT2(), s.getT3());
System.out.println("(Total time) " + Duration.between(s.getT1(), s.getT3()).toMillis() + "ms"); var durtot = Duration.between(s.getT1(), s.getT3());
System.out.println("Iteration " + ai.incrementAndGet());
System.out.println("Time to get value reference: " + dur1.toMillisPart() + "ms " + dur1.toNanosPart() + "ns");
System.out.println("Time to set new value and get previous: " + dur2.toMillisPart() + "ms " + dur2.toNanosPart() + "ns");
System.out.println("(Total time) " + durtot.toMillisPart() + "ms " + durtot.toNanosPart() + "ns");
}) })
.blockOptional(); )
.repeat(100)
)
.blockLast();
} }
} }