Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/main/java/part2/cache/CachingDataStorageImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package part2.cache;

import com.sun.javafx.geom.AreaOp;
import db.DataStorage;
import db.SlowCompletableFutureDb;

Expand Down Expand Up @@ -38,6 +39,30 @@ public OutdatableResult<T> getOutdatable(String key) {
// TODO don't use obtrudeException on result - just don't
// TODO use remove(Object key, Object value) to remove target value
// TODO Start timeout after receiving result in CompletableFuture, not after receiving CompletableFuture itself
throw new UnsupportedOperationException();

OutdatableResult<T> res = new OutdatableResult<>(new CompletableFuture<>(), new CompletableFuture<>());
OutdatableResult<T> tOutdatableResult = cache.putIfAbsent(key, res);

if (tOutdatableResult == null) {
db.get(key).whenComplete(
(t, e) -> {
scheduledExecutorService.schedule(() -> {
cache.remove(key, cache.get(key));
res.getOutdated().complete(null);
},
timeout,
timeoutUnits
);
if (e != null) {
res.getResult().completeExceptionally(e);
} else {
res.getResult().complete(t);
}
}
);
return res;
} else {
return tOutdatableResult;
}
}
}
49 changes: 42 additions & 7 deletions src/main/java/part3/exercise/ComposeCachingDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,55 @@

import part2.cache.CachingDataStorage;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

public class ComposeCachingDataStorage<K1, T1, K2, T2> implements CachingDataStorage<K1, T2> {

public ComposeCachingDataStorage(CachingDataStorage<K1, T1> storage1,
CachingDataStorage<K2, T2> storage2,
Function<T1, K2> mapping) {
// TODO
throw new UnsupportedOperationException();
private CachingDataStorage<K1, T1> storage1;
private CachingDataStorage<K2, T2> storage2;
private Function<T1, K2> mapping;

public ComposeCachingDataStorage(CachingDataStorage<K1, T1> storage1, CachingDataStorage<K2, T2> storage2, Function<T1, K2> mapping) {
this.storage1 = storage1;
this.storage2 = storage2;
this.mapping = mapping;
}

@Override
public OutdatableResult<T2> getOutdatable(K1 key) {
// TODO
throw new UnsupportedOperationException();

OutdatableResult<T1> outdatableFromStorage1 = storage1.getOutdatable(key);

CompletableFuture<OutdatableResult<T2>> outdatableResultCompletableFuture = outdatableFromStorage1.getResult().thenApply(
v -> storage2.getOutdatable(mapping.apply(v))
);

OutdatableResult<T2> result = new OutdatableResult<>(new CompletableFuture<>(), new CompletableFuture<>());

thenComplete(outdatableFromStorage1.getOutdated(), result.getOutdated());
outdatableResultCompletableFuture.thenAccept(
v -> v.getResult().thenAccept(
res -> {
result.getResult().complete(res);
thenComplete(v.getOutdated(),result.getOutdated());
}
)
);

return result;
}

private void thenComplete(CompletableFuture<Void> source, CompletableFuture<Void> target) {
source.whenComplete(
(r,e) -> {
if (e == null) {
target.complete(null);
} else {
target.completeExceptionally(e);
}
}
);
}

}
41 changes: 37 additions & 4 deletions src/main/java/part3/exercise/ListCachingDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,50 @@
import part2.cache.CachingDataStorage;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

public class ListCachingDataStorage<K, T> implements CachingDataStorage<List<K>, List<T>> {

private CachingDataStorage<K,T> storage;

public ListCachingDataStorage(CachingDataStorage<K, T> storage) {
// TODO
throw new UnsupportedOperationException();
this.storage = storage;
}

@Override
public OutdatableResult<List<T>> getOutdatable(List<K> key) {
// TODO
throw new UnsupportedOperationException();
List<OutdatableResult<T>> collect = key.stream()
.map(storage::getOutdatable)
.collect(Collectors.toList());

List<CompletableFuture<Void>> outdatables = collect.stream()
.map(OutdatableResult::getOutdated)
.collect(Collectors.toList());

List<CompletableFuture<T>> results = collect.stream()
.map(OutdatableResult::getResult)
.collect(Collectors.toList());

CompletableFuture<List<T>> listCompletableFuture = CompletableFuture.allOf(results.toArray(new CompletableFuture[0]))
.thenApply(v -> results.stream()
.map(ListCachingDataStorage::getOrNull)
.collect(Collectors.toList()));

return new OutdatableResult<>(
listCompletableFuture,
CompletableFuture.anyOf(outdatables.toArray(new CompletableFuture[0])).thenApply(v -> null)
);
}

private static <T> T getOrNull(Future<T> f) {
try {
return f.get();
} catch (InterruptedException | ExecutionException e1) {
e1.printStackTrace();
return null;
}
}
}
21 changes: 14 additions & 7 deletions src/main/java/part3/exercise/MappingCachingDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@

public class MappingCachingDataStorage<K, K1, T1, T> implements CachingDataStorage<K, T> {

public MappingCachingDataStorage(CachingDataStorage<K1, T1> storage,
Function<K, K1> mapKey,
BiFunction<K, T1, T> mapValue) {
// TODO
throw new UnsupportedOperationException();
private CachingDataStorage<K1, T1> storage;
private Function<K, K1> mapKey;
private BiFunction<K, T1, T> mapValue;

public MappingCachingDataStorage(CachingDataStorage<K1, T1> storage, Function<K, K1> mapKey, BiFunction<K, T1, T> mapValue) {
this.storage = storage;
this.mapKey = mapKey;
this.mapValue = mapValue;
}

@Override
public OutdatableResult<T> getOutdatable(K key) {
// TODO
throw new UnsupportedOperationException();
OutdatableResult<T1> outdatable = storage.getOutdatable(mapKey.apply(key));

return new OutdatableResult<>(
outdatable.getResult().thenApply(t1 -> mapValue.apply(key, t1)),
outdatable.getOutdated()
);
}
}
36 changes: 27 additions & 9 deletions src/main/java/part3/exercise/PairCachingDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,41 @@

import part2.cache.CachingDataStorage;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Function;

public class PairCachingDataStorage<K, T, K1, T1, K2, T2> implements CachingDataStorage<K, T> {

public PairCachingDataStorage(CachingDataStorage<K1, T1> storage1,
CachingDataStorage<K2, T2> storage2,
Function<K, K1> getKey1,
Function<K, K2> getKey2,
Function<K, BiFunction<T1, T2, T>> resultMapper) {
// TODO
throw new UnsupportedOperationException();
private CachingDataStorage<K1, T1> storage1;
private CachingDataStorage<K2, T2> storage2;
private Function<K, K1> getKey1;
private Function<K, K2> getKey2;
private Function<K, BiFunction<T1, T2, T>> resultMapper;


public PairCachingDataStorage(
CachingDataStorage<K1, T1> storage1,
CachingDataStorage<K2, T2> storage2,
Function<K, K1> getKey1,
Function<K, K2> getKey2,
Function<K, BiFunction<T1, T2, T>> resultMapper
) {
this.storage1 = storage1;
this.storage2 = storage2;
this.getKey1 = getKey1;
this.getKey2 = getKey2;
this.resultMapper = resultMapper;
}

@Override
public OutdatableResult<T> getOutdatable(K key) {
// TODO
throw new UnsupportedOperationException();
OutdatableResult<T1> outdatable1 = storage1.getOutdatable(getKey1.apply(key));
OutdatableResult<T2> outdatable2 = storage2.getOutdatable(getKey2.apply(key));

return new OutdatableResult<>(
outdatable1.getResult().thenCombine(outdatable2.getResult(), resultMapper.apply(key)),
CompletableFuture.anyOf(outdatable1.getOutdated(),outdatable2.getOutdated()).thenApply(v -> null)
);
}
}