Use more method references

This commit is contained in:
Andrea Cavalli 2022-06-20 12:30:33 +02:00
parent a3d1207d76
commit d2e7c56f06
11 changed files with 72 additions and 60 deletions

View File

@ -643,11 +643,21 @@ public class LLUtils {
}
public static Mono<Void> finalizeResource(Resource<?> resource) {
return Mono.fromRunnable(() -> LLUtils.closeResource(resource));
return Mono.fromRunnable(() -> LLUtils.finalizeResourceNow(resource));
}
public static Mono<Void> finalizeResource(SimpleResource resource) {
return Mono.fromRunnable(() -> LLUtils.closeResource(resource));
return Mono.fromRunnable(resource::close);
}
public static void finalizeResourceNow(Resource<?> resource) {
if (resource.isAccessible()) {
resource.close();
}
}
public static void finalizeResourceNow(SimpleResource resource) {
resource.close();
}
public static <V> Flux<V> handleDiscard(Flux<V> flux) {
@ -869,7 +879,7 @@ public class LLUtils {
newCurr = null;
}
return new Delta<>(newPrev, newCurr);
}), delta -> Mono.fromRunnable(delta::close));
}), LLUtils::finalizeResource);
}
public static <R, V> boolean isDeltaChanged(Delta<V> delta) {

View File

@ -4,6 +4,7 @@ import com.google.common.primitives.Ints;
import it.cavallium.dbengine.database.LLKeyValueDatabaseStructure;
import it.cavallium.dbengine.database.LLSingleton;
import it.cavallium.dbengine.database.LLSnapshot;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.database.serialization.SerializationException;
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
import org.jetbrains.annotations.Nullable;
@ -23,7 +24,7 @@ public class DatabaseInt implements LLKeyValueDatabaseStructure {
var resultMono = singleton.get(snapshot);
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(() -> serializer.deserialize(result)),
result -> Mono.fromRunnable(result::close)
LLUtils::finalizeResource
);
}

View File

@ -5,6 +5,7 @@ import com.google.common.primitives.Longs;
import it.cavallium.dbengine.database.LLKeyValueDatabaseStructure;
import it.cavallium.dbengine.database.LLSingleton;
import it.cavallium.dbengine.database.LLSnapshot;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.database.UpdateReturnMode;
import it.cavallium.dbengine.database.serialization.SerializationException;
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
@ -33,7 +34,7 @@ public class DatabaseLong implements LLKeyValueDatabaseStructure {
return serializer.deserialize(result);
}
}),
result -> Mono.fromRunnable(result::close)
LLUtils::finalizeResource
);
}
@ -80,7 +81,7 @@ public class DatabaseLong implements LLKeyValueDatabaseStructure {
}, updateReturnMode);
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(result::readLong),
result -> Mono.fromRunnable(result::close)
LLUtils::finalizeResource
).single();
}

View File

@ -274,7 +274,7 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
return Mono.usingWhen(dictionary
.get(resolveSnapshot(snapshot), Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix))),
value -> Mono.fromCallable(() -> deserializeValue(keySuffix, value)),
value -> Mono.fromRunnable(value::close));
LLUtils::finalizeResource);
}
@Override
@ -283,7 +283,7 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
var valueMono = Mono.fromCallable(() -> serializeValue(value)).single();
return Mono.usingWhen(dictionary.put(keyMono, valueMono, LLDictionaryResultType.VOID),
v -> Mono.empty(),
v -> Mono.fromRunnable(v::close)
LLUtils::finalizeResource
);
}
@ -298,7 +298,7 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
var keyMono = Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix));
return Mono.usingWhen(dictionary.update(keyMono, getSerializedUpdater(updater), updateReturnMode),
result -> Mono.fromCallable(() -> deserializeValue(keySuffix, result)),
result -> Mono.fromRunnable(result::close)
LLUtils::finalizeResource
);
}
@ -355,7 +355,7 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
var valueMono = Mono.fromCallable(() -> serializeValue(value));
return Mono.usingWhen(dictionary.put(keyMono, valueMono, LLDictionaryResultType.PREVIOUS_VALUE),
valueBuf -> Mono.fromCallable(() -> deserializeValue(keySuffix, valueBuf)),
valueBuf -> Mono.fromRunnable(valueBuf::close)
LLUtils::finalizeResource
);
}
@ -366,7 +366,7 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
return Mono
.usingWhen(dictionary.put(keyMono, valueMono, LLDictionaryResultType.PREVIOUS_VALUE),
valueBuf -> Mono.fromCallable(() -> deserializeValue(keySuffix, valueBuf)),
valueBuf -> Mono.fromRunnable(valueBuf::close)
LLUtils::finalizeResource
)
.map(oldValue -> !Objects.equals(oldValue, value))
.defaultIfEmpty(value != null);
@ -377,7 +377,7 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
var keyMono = Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix));
return dictionary
.remove(keyMono, LLDictionaryResultType.VOID)
.doOnNext(Resource::close)
.doOnNext(LLUtils::finalizeResourceNow)
.then();
}
@ -386,7 +386,7 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
var keyMono = Mono.fromCallable(() -> serializeKeySuffixToKey(keySuffix));
return Mono.usingWhen(dictionary.remove(keyMono, LLDictionaryResultType.PREVIOUS_VALUE),
valueBuf -> Mono.fromCallable(() -> deserializeValue(keySuffix, valueBuf)),
valueBuf -> Mono.fromRunnable(valueBuf::close)
LLUtils::finalizeResource
);
}
@ -535,7 +535,7 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
} else {
Mono<LLRange> boundedRangeMono = Mono.usingWhen(rangeMono,
range -> Mono.fromCallable(() -> getPatchedRange(range, keyMin, keyMax)),
range -> Mono.fromRunnable(range::close));
LLUtils::finalizeResource);
return getAllValues(snapshot, boundedRangeMono, reverse, smallRange);
}
}
@ -580,12 +580,12 @@ public class DatabaseMapDictionary<T, U> extends DatabaseMapDictionaryDeep<T, U,
} else if (range.isSingle()) {
return dictionary
.remove(Mono.fromCallable(range::getSingleUnsafe), LLDictionaryResultType.VOID)
.doOnNext(Resource::close)
.doOnNext(LLUtils::finalizeResourceNow)
.then();
} else {
return dictionary.setRange(rangeMono, Flux.empty(), false);
}
}, ResourceSupport::close);
}, LLUtils::finalizeResourceNow);
}
}

View File

@ -421,12 +421,12 @@ public class DatabaseMapDictionaryDeep<T, U, US extends DatabaseStage<U>> extend
} else if (range.isSingle()) {
return dictionary
.remove(Mono.fromCallable(range::getSingleUnsafe), LLDictionaryResultType.VOID)
.doOnNext(Resource::close)
.doOnNext(LLUtils::finalizeResourceNow)
.then();
} else {
return dictionary.setRange(rangeMono, Flux.empty(), false);
}
}, ResourceSupport::close);
}, LLUtils::finalizeResourceNow);
}
protected T deserializeSuffix(@NotNull Buffer keySuffix) throws SerializationException {

View File

@ -113,7 +113,7 @@ public class DatabaseMapSingle<U> extends ResourceSupport<DatabaseStage<U>, Data
public Mono<U> get(@Nullable CompositeSnapshot snapshot) {
return Mono.usingWhen(dictionary.get(resolveSnapshot(snapshot), keyMono),
buf -> Mono.fromSupplier(() -> deserializeValue(buf)),
buf -> Mono.fromRunnable(buf::close)
LLUtils::finalizeResource
);
}
@ -122,7 +122,7 @@ public class DatabaseMapSingle<U> extends ResourceSupport<DatabaseStage<U>, Data
return Mono.usingWhen(dictionary
.put(keyMono, Mono.fromCallable(() -> serializeValue(value)), LLDictionaryResultType.PREVIOUS_VALUE),
buf -> Mono.fromSupplier(() -> deserializeValue(buf)),
buf -> Mono.fromRunnable(buf::close));
LLUtils::finalizeResource);
}
@Override
@ -147,7 +147,7 @@ public class DatabaseMapSingle<U> extends ResourceSupport<DatabaseStage<U>, Data
}, updateReturnMode);
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(() -> deserializeValue(result)),
result -> Mono.fromRunnable(result::close)
LLUtils::finalizeResource
);
}
@ -174,7 +174,7 @@ public class DatabaseMapSingle<U> extends ResourceSupport<DatabaseStage<U>, Data
public Mono<U> clearAndGetPrevious() {
return Mono.usingWhen(dictionary.remove(keyMono, LLDictionaryResultType.PREVIOUS_VALUE),
result -> Mono.fromSupplier(() -> deserializeValue(result)),
result -> Mono.fromRunnable(result::close)
LLUtils::finalizeResource
);
}

View File

@ -104,7 +104,7 @@ public class DatabaseSingleton<U> extends ResourceSupport<DatabaseStage<U>, Data
var resultMono = singleton.get(resolveSnapshot(snapshot));
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(() -> this.deserializeValue(result)),
result -> Mono.fromRunnable(result::close)
LLUtils::finalizeResource
);
}
@ -120,7 +120,7 @@ public class DatabaseSingleton<U> extends ResourceSupport<DatabaseStage<U>, Data
.last();
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(() -> this.deserializeValue(result)),
result -> Mono.fromRunnable(result::close)
LLUtils::finalizeResource
);
}
@ -146,7 +146,7 @@ public class DatabaseSingleton<U> extends ResourceSupport<DatabaseStage<U>, Data
}, updateReturnMode);
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(() -> this.deserializeValue(result)),
result -> Mono.fromRunnable(result::close)
LLUtils::finalizeResource
);
}
@ -181,7 +181,7 @@ public class DatabaseSingleton<U> extends ResourceSupport<DatabaseStage<U>, Data
var resultMono = Flux.concat(singleton.get(null), singleton.set(Mono.empty()).then(Mono.empty())).last();
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(() -> this.deserializeValue(result)),
result -> Mono.fromRunnable(result::close)
LLUtils::finalizeResource
);
}

View File

@ -258,7 +258,7 @@ public class LLLocalDictionary implements LLDictionary {
public Mono<Buffer> get(@Nullable LLSnapshot snapshot, Mono<Buffer> keyMono) {
return Mono.usingWhen(keyMono,
key -> runOnDb(false, () -> this.getSync(snapshot, key)),
key -> Mono.fromRunnable(key::close)
LLUtils::finalizeResource
);
}
@ -329,7 +329,7 @@ public class LLLocalDictionary implements LLDictionary {
} finally {
endedContains.increment();
}
}), range -> Mono.fromRunnable(range::close));
}), LLUtils::finalizeResource);
}
private boolean containsKey(@Nullable LLSnapshot snapshot, Buffer key) throws RocksDBException {
@ -454,7 +454,7 @@ public class LLLocalDictionary implements LLDictionary {
}
throw ex;
}
}), key -> Mono.fromRunnable(key::close));
}), LLUtils::finalizeResource);
}
@SuppressWarnings("DuplicatedCode")
@ -492,7 +492,7 @@ public class LLLocalDictionary implements LLDictionary {
}
throw ex;
}
}), key -> Mono.fromRunnable(key::close));
}), LLUtils::finalizeResource);
}
@Override
@ -516,7 +516,7 @@ public class LLLocalDictionary implements LLDictionary {
} catch (RocksDBException ex) {
throw new RocksDBException("Failed to delete: " + ex.getMessage());
}
}), key -> Mono.fromRunnable(key::close));
}), LLUtils::finalizeResource);
// Read the previous data, then delete the data, then return the previous data
return Flux.concat(previousDataMono, removeMono).singleOrEmpty();
}
@ -526,7 +526,7 @@ public class LLLocalDictionary implements LLDictionary {
case PREVIOUS_VALUE_EXISTENCE -> Mono.usingWhen(keyMono, key -> runOnDb(false, () -> {
var contained = containsKey(null, key);
return LLUtils.booleanToResponseByteBuffer(alloc, contained);
}), key -> Mono.fromRunnable(key::close));
}), LLUtils::finalizeResource);
case PREVIOUS_VALUE -> Mono.usingWhen(keyMono, key -> runOnDb(false, () -> {
assert !Schedulers.isInNonBlockingThread() : "Called getPreviousData in a nonblocking thread";
Buffer result;
@ -540,7 +540,7 @@ public class LLLocalDictionary implements LLDictionary {
}
logger.trace(MARKER_ROCKSDB, "Read {}: {}", () -> toStringSafe(key), () -> toStringSafe(result));
return result;
}), key -> Mono.fromRunnable(key::close));
}), LLUtils::finalizeResource);
case VOID -> Mono.empty();
};
}
@ -671,7 +671,7 @@ public class LLLocalDictionary implements LLDictionary {
}
} finally {
for (var mappedInput : mappedInputs) {
mappedInput.getT3().ifPresent(Resource::close);
mappedInput.getT3().ifPresent(LLUtils::finalizeResourceNow);
}
}
@ -761,7 +761,7 @@ public class LLLocalDictionary implements LLDictionary {
});
return Flux.usingWhen(iteratorMono,
iterator -> iterator.flux().subscribeOn(dbRScheduler, false),
iterator -> Mono.fromRunnable(iterator::close)
LLUtils::finalizeResource
);
}
@ -780,7 +780,7 @@ public class LLLocalDictionary implements LLDictionary {
return Flux.usingWhen(
iteratorMono,
iterator -> iterator.flux().subscribeOn(dbRScheduler, false),
iterator -> Mono.fromRunnable(iterator::close)
LLUtils::finalizeResource
);
}
@ -821,7 +821,7 @@ public class LLLocalDictionary implements LLDictionary {
});
return Flux.usingWhen(iteratorMono,
iterator -> iterator.flux().subscribeOn(dbRScheduler, false),
iterator -> Mono.fromRunnable(iterator::close)
LLUtils::finalizeResource
);
}
@ -860,7 +860,7 @@ public class LLLocalDictionary implements LLDictionary {
}
})
.subscribeOn(dbRScheduler),
range -> Mono.fromRunnable(range::close)
LLUtils::finalizeResource
);
}
@ -880,7 +880,7 @@ public class LLLocalDictionary implements LLDictionary {
});
return Flux.usingWhen(iteratorMono,
iterator -> iterator.flux().subscribeOn(dbRScheduler),
iterator -> Mono.fromRunnable(iterator::close)
LLUtils::finalizeResource
);
}
@ -891,7 +891,7 @@ public class LLLocalDictionary implements LLDictionary {
} else {
return null;
}
}), key -> Mono.fromRunnable(key::close)).flux();
}), LLUtils::finalizeResource).flux();
}
private record RocksObjTuple<T extends AbstractNativeReference, U extends Resource<?>>(T t1, U t2) implements SafeCloseable {
@ -969,7 +969,7 @@ public class LLLocalDictionary implements LLDictionary {
} catch (RocksDBException ex) {
throw new RocksDBException("Failed to set a range: " + ex.getMessage());
}
}), range -> Mono.fromRunnable(range::close))
}), LLUtils::finalizeResource)
.thenMany(entries.window(MULTI_GET_WINDOW))
.flatMap(keysWindowFlux -> keysWindowFlux
.collectList()
@ -1199,7 +1199,7 @@ public class LLLocalDictionary implements LLDictionary {
} catch (RocksDBException ex) {
throw new RocksDBException("Failed to get size of range: " + ex.getMessage());
}
}), range -> Mono.fromRunnable(range::close));
}), LLUtils::finalizeResource);
}
@Override
@ -1228,7 +1228,7 @@ public class LLLocalDictionary implements LLDictionary {
} catch (RocksDBException ex) {
throw new RocksDBException("Failed to get one entry: " + ex.getMessage());
}
}), range -> Mono.fromRunnable(range::close));
}), LLUtils::finalizeResource);
}
@Override
@ -1253,7 +1253,7 @@ public class LLLocalDictionary implements LLDictionary {
} catch (RocksDBException ex) {
throw new RocksDBException("Failed to get one key: " + ex.getMessage());
}
}), range -> Mono.fromRunnable(range::close));
}), LLUtils::finalizeResource);
}
private long fastSizeAll(@Nullable LLSnapshot snapshot) throws RocksDBException {
@ -1387,7 +1387,7 @@ public class LLLocalDictionary implements LLDictionary {
return LLEntry.of(key, value);
}
}
}), range -> Mono.fromRunnable(range::close));
}), LLUtils::finalizeResource);
}
}

View File

@ -151,7 +151,7 @@ public class LLLocalSingleton implements LLSingleton {
case GET_OLD_VALUE -> ((UpdateAtomicResultPrevious) result).previous();
};
}).onErrorMap(cause -> new IOException("Failed to read or write", cause)),
keySend -> Mono.fromRunnable(keySend::close));
LLUtils::finalizeResource);
}
@Override
@ -166,7 +166,7 @@ public class LLLocalSingleton implements LLSingleton {
}
return ((UpdateAtomicResultDelta) result).delta();
}).onErrorMap(cause -> new IOException("Failed to read or write", cause)),
keySend -> Mono.fromRunnable(keySend::close));
LLUtils::finalizeResource);
}
@Override

View File

@ -184,7 +184,7 @@ public class LLMemoryDictionary implements LLDictionary {
.fromCallable(() -> snapshots.get(resolveSnapshot(snapshot)).get(kShr(key)))
.map(this::kkB)
.onErrorMap(cause -> new IOException("Failed to read", cause)),
key -> Mono.fromRunnable(key::close)
LLUtils::finalizeResource
);
}
@ -238,7 +238,7 @@ public class LLMemoryDictionary implements LLDictionary {
var oldVal = oldRef.get();
return LLDelta.of(oldVal != null ? kkB(oldRef.get()) : null, newValue != null ? kkB(newValue) : null);
}),
key -> Mono.fromRunnable(key::close)
LLUtils::finalizeResource
);
}
@ -266,7 +266,7 @@ public class LLMemoryDictionary implements LLDictionary {
}
}))
.onErrorMap(cause -> new IOException("Failed to read", cause)),
key -> Mono.fromRunnable(key::close)
LLUtils::finalizeResource
);
}
@ -330,7 +330,7 @@ public class LLMemoryDictionary implements LLDictionary {
})
.map(entry -> LLEntry.of(kkB(entry.getKey()), kkB(entry.getValue())));
}
}, range -> Mono.fromRunnable(range::close));
}, LLUtils::finalizeResource);
}
@Override
@ -362,7 +362,7 @@ public class LLMemoryDictionary implements LLDictionary {
);
}
}
}, range -> Mono.fromRunnable(range::close));
}, LLUtils::finalizeResource);
}
@Override
@ -397,7 +397,7 @@ public class LLMemoryDictionary implements LLDictionary {
.map(this::kkB);
}
},
range -> Mono.fromRunnable(range::close)
LLUtils::finalizeResource
);
}
@ -434,7 +434,7 @@ public class LLMemoryDictionary implements LLDictionary {
);
}
}
}, range -> Mono.fromRunnable(range::close));
}, LLUtils::finalizeResource);
}
@SuppressWarnings("RedundantCast")
@ -466,7 +466,7 @@ public class LLMemoryDictionary implements LLDictionary {
.map(this::kkB);
}
}
}, range -> Mono.fromRunnable(range::close));
}, LLUtils::finalizeResource);
}
@Override
@ -503,7 +503,7 @@ public class LLMemoryDictionary implements LLDictionary {
}
})
.then();
}, range -> Mono.fromRunnable(range::close));
}, LLUtils::finalizeResource);
}
private boolean isInsideRange(BLRange range, ByteList key) {
@ -528,7 +528,7 @@ public class LLMemoryDictionary implements LLDictionary {
@Override
public Mono<Boolean> isRangeEmpty(@Nullable LLSnapshot snapshot, Mono<LLRange> rangeMono, boolean fillCache) {
return getRangeKeys(snapshot, rangeMono, false, false)
.doOnNext(Resource::close)
.doOnNext(LLUtils::finalizeResourceNow)
.count()
.map(count -> count == 0);
}
@ -537,7 +537,7 @@ public class LLMemoryDictionary implements LLDictionary {
public Mono<Long> sizeRange(@Nullable LLSnapshot snapshot, Mono<LLRange> rangeMono, boolean fast) {
return Mono.usingWhen(rangeMono,
range -> Mono.fromCallable(() -> (long) mapSlice(snapshot, range).size()),
range -> Mono.fromRunnable(range::close)
LLUtils::finalizeResource
);
}
@ -587,7 +587,7 @@ public class LLMemoryDictionary implements LLDictionary {
.map(entry -> LLEntry.of(kkB(entry.getKey()), kkB(entry.getValue())));
}
}
}, range -> Mono.fromRunnable(range::close));
}, LLUtils::finalizeResource);
}
@Override

View File

@ -74,7 +74,7 @@ public class CountMultiSearcher implements MultiSearcher {
.take(queryParams2.limitLong(), true);
return new LuceneSearchResult(totalHitsCount, mergedFluxes, () -> {
resultsToDrop.forEach(SimpleResource::close);
resultsToDrop.forEach(LLUtils::finalizeResourceNow);
try {
indexSearchers.close();
} catch (IOException e) {