Fix some possible leaks

This commit is contained in:
Andrea Cavalli 2022-05-26 13:13:14 +02:00
parent 96de3023a0
commit 6056eedd75
4 changed files with 90 additions and 80 deletions

View File

@ -20,13 +20,11 @@ public class DatabaseInt implements LLKeyValueDatabaseStructure {
}
public Mono<Integer> get(@Nullable LLSnapshot snapshot) {
return singleton.get(snapshot).handle((data, sink) -> {
try (data) {
sink.next(serializer.deserialize(data));
} catch (SerializationException e) {
sink.error(e);
}
});
var resultMono = singleton.get(snapshot);
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(() -> serializer.deserialize(result)),
result -> Mono.fromRunnable(result::close)
);
}
public Mono<Void> set(int value) {

View File

@ -24,17 +24,17 @@ public class DatabaseLong implements LLKeyValueDatabaseStructure {
}
public Mono<Long> get(@Nullable LLSnapshot snapshot) {
return singleton.get(snapshot).handle((data, sink) -> {
try (data) {
if (data.readableBytes() == 4) {
sink.next((long) (int) bugSerializer.deserialize(data));
var resultMono = singleton.get(snapshot);
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(() -> {
if (result.readableBytes() == 4) {
return (long) (int) bugSerializer.deserialize(result);
} else {
sink.next(serializer.deserialize(data));
return serializer.deserialize(result);
}
} catch (SerializationException e) {
sink.error(e);
}
});
}),
result -> Mono.fromRunnable(result::close)
);
}
public Mono<Long> incrementAndGet() {
@ -62,7 +62,8 @@ public class DatabaseLong implements LLKeyValueDatabaseStructure {
}
private Mono<Long> addAnd(long count, UpdateReturnMode updateReturnMode) {
return singleton.update(prev -> {
var resultMono = singleton.update(prev -> {
try (prev) {
if (prev != null) {
var prevLong = prev.readLong();
var alloc = singleton.getAllocator();
@ -75,11 +76,12 @@ public class DatabaseLong implements LLKeyValueDatabaseStructure {
buf.writeLong(count);
return buf;
}
}, updateReturnMode).map(buf -> {
try (buf) {
return buf.readLong();
}
}).single();
}, updateReturnMode);
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(result::readLong),
result -> Mono.fromRunnable(result::close)
).single();
}
public Mono<Void> set(long value) {

View File

@ -130,6 +130,7 @@ public class DatabaseMapSingle<U> extends ResourceSupport<DatabaseStage<U>, Data
UpdateReturnMode updateReturnMode) {
var resultMono = dictionary
.update(keyMono, (oldValueSer) -> {
try (oldValueSer) {
U result;
if (oldValueSer == null) {
result = updater.apply(null);
@ -142,6 +143,7 @@ public class DatabaseMapSingle<U> extends ResourceSupport<DatabaseStage<U>, Data
} else {
return serializeValue(result);
}
}
}, updateReturnMode);
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(() -> deserializeValue(result)),

View File

@ -67,24 +67,22 @@ public class DatabaseSingleton<U> extends ResourceSupport<DatabaseStage<U>, Data
}
}
private void deserializeValue(Buffer value, SynchronousSink<U> sink) {
private U deserializeValue(Buffer value) {
try {
U deserializedValue;
try (value) {
deserializedValue = serializer.deserialize(value);
}
sink.next(deserializedValue);
return deserializedValue;
} catch (IndexOutOfBoundsException ex) {
var exMessage = ex.getMessage();
if (exMessage != null && exMessage.contains("read 0 to 0, write 0 to ")) {
LOG.error("Unexpected zero-bytes value at "
+ singleton.getDatabaseName() + ":" + singleton.getColumnName() + ":" + singleton.getName());
sink.complete();
LOG.error("Unexpected zero-bytes value at " + singleton.getDatabaseName()
+ ":" + singleton.getColumnName() + ":" + singleton.getName());
return null;
} else {
sink.error(ex);
throw ex;
}
} catch (SerializationException ex) {
sink.error(ex);
}
}
@ -103,8 +101,11 @@ public class DatabaseSingleton<U> extends ResourceSupport<DatabaseStage<U>, Data
@Override
public Mono<U> get(@Nullable CompositeSnapshot snapshot) {
return singleton.get(resolveSnapshot(snapshot))
.handle(this::deserializeValue);
var resultMono = singleton.get(resolveSnapshot(snapshot));
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(() -> this.deserializeValue(result)),
result -> Mono.fromRunnable(result::close)
);
}
@Override
@ -114,16 +115,19 @@ public class DatabaseSingleton<U> extends ResourceSupport<DatabaseStage<U>, Data
@Override
public Mono<U> setAndGetPrevious(U value) {
return Flux
var resultMono = Flux
.concat(singleton.get(null), singleton.set(Mono.fromCallable(() -> serializeValue(value))).then(Mono.empty()))
.last()
.handle(this::deserializeValue);
.last();
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(() -> this.deserializeValue(result)),
result -> Mono.fromRunnable(result::close)
);
}
@Override
public Mono<U> update(SerializationFunction<@Nullable U, @Nullable U> updater,
UpdateReturnMode updateReturnMode) {
return singleton
var resultMono = singleton
.update((oldValueSer) -> {
try (oldValueSer) {
U result;
@ -139,8 +143,11 @@ public class DatabaseSingleton<U> extends ResourceSupport<DatabaseStage<U>, Data
return serializeValue(result);
}
}
}, updateReturnMode)
.handle(this::deserializeValue);
}, updateReturnMode);
return Mono.usingWhen(resultMono,
result -> Mono.fromSupplier(() -> this.deserializeValue(result)),
result -> Mono.fromRunnable(result::close)
);
}
@Override
@ -171,10 +178,11 @@ public class DatabaseSingleton<U> extends ResourceSupport<DatabaseStage<U>, Data
@Override
public Mono<U> clearAndGetPrevious() {
return Flux
.concat(singleton.get(null), singleton.set(Mono.empty()).then(Mono.empty()))
.last()
.handle(this::deserializeValue);
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)
);
}
@Override