Fix storage performance

This commit is contained in:
Lukas Rieger (Blue) 2024-02-08 13:18:49 +01:00
parent e02a43a521
commit 81e8da3b70
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2
10 changed files with 137 additions and 23 deletions

View File

@ -172,7 +172,7 @@ public class MapStorageRequestHandler implements HttpRequestHandler {
byte[] compressedData = byteOut.toByteArray();
response.setData(new ByteArrayInputStream(compressedData));
} else {
response.setData(data.decompress());
response.setData(new BufferedInputStream(data.decompress()));
}
}

View File

@ -24,11 +24,12 @@
*/
package de.bluecolored.bluemap.core.storage;
import java.io.FilterInputStream;
import de.bluecolored.bluemap.core.util.DelegateInputStream;
import java.io.IOException;
import java.io.InputStream;
public class CompressedInputStream extends FilterInputStream {
public class CompressedInputStream extends DelegateInputStream {
private final Compression compression;

View File

@ -72,10 +72,8 @@ public class FileStorage extends Storage {
Path file = getFilePath(mapId, lod, tile);
OutputStream os = FileHelper.createFilepartOutputStream(file);
os = new BufferedOutputStream(os);
try {
os = compression.compress(os);
os = new BufferedOutputStream(compression.compress(os));
} catch (IOException ex) {
os.close();
throw ex;
@ -92,8 +90,6 @@ public class FileStorage extends Storage {
if (!Files.exists(file)) return Optional.empty();
InputStream is = Files.newInputStream(file, StandardOpenOption.READ);
is = new BufferedInputStream(is);
return Optional.of(new CompressedInputStream(is, compression));
}
@ -142,9 +138,7 @@ public class FileStorage extends Storage {
Path file = getMetaFilePath(mapId, name);
OutputStream os = FileHelper.createFilepartOutputStream(file);
os = new BufferedOutputStream(os);
return os;
return new BufferedOutputStream(os);
}
@Override
@ -154,9 +148,7 @@ public class FileStorage extends Storage {
if (!Files.exists(file)) return Optional.empty();
InputStream is = Files.newInputStream(file, StandardOpenOption.READ);
is = new BufferedInputStream(is);
return Optional.of(is);
return Optional.of(new BufferedInputStream(is));
}
@Override

View File

@ -28,7 +28,7 @@ import de.bluecolored.bluemap.core.storage.sql.dialect.MySQLDialect;
import java.net.MalformedURLException;
public class MySQLStorage extends SQLStorage{
public class MySQLStorage extends SQLStorage {
public MySQLStorage(SQLStorageSettings config) throws MalformedURLException, SQLDriverException {
super(MySQLDialect.INSTANCE, config);

View File

@ -51,7 +51,7 @@ public class PostgreSQLStorage extends SQLStorage {
public OutputStream writeMapTile(String mapId, int lod, Vector2i tile) throws IOException {
Compression compression = lod == 0 ? this.hiresCompression : Compression.NONE;
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
return new OnCloseOutputStream(compression.compress(byteOut), () -> {
return new OnCloseOutputStream(new BufferedOutputStream(compression.compress(byteOut)), () -> {
int mapFK = getMapFK(mapId);
int tileCompressionFK = getMapTileCompressionFK(compression);

View File

@ -108,7 +108,7 @@ public abstract class SQLStorage extends Storage {
Compression compression = lod == 0 ? this.hiresCompression : Compression.NONE;
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
return new OnCloseOutputStream(compression.compress(byteOut), () -> {
return new OnCloseOutputStream(new BufferedOutputStream(compression.compress(byteOut)), () -> {
int mapFK = getMapFK(mapId);
int tileCompressionFK = getMapTileCompressionFK(compression);

View File

@ -0,0 +1,82 @@
package de.bluecolored.bluemap.core.util;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DelegateInputStream extends InputStream {
protected final InputStream in;
public DelegateInputStream(InputStream in) {
this.in = in;
}
@Override
public int read() throws IOException {
return in.read();
}
@Override
public int read(byte @NotNull [] b) throws IOException {
return in.read(b);
}
@Override
public int read(byte @NotNull [] b, int off, int len) throws IOException {
return in.read(b, off, len);
}
@Override
public byte[] readAllBytes() throws IOException {
return in.readAllBytes();
}
@Override
public byte[] readNBytes(int len) throws IOException {
return in.readNBytes(len);
}
@Override
public int readNBytes(byte[] b, int off, int len) throws IOException {
return in.readNBytes(b, off, len);
}
@Override
public long skip(long n) throws IOException {
return in.skip(n);
}
@Override
public int available() throws IOException {
return in.available();
}
@Override
public void close() throws IOException {
in.close();
}
@Override
public boolean markSupported() {
return in.markSupported();
}
@Override
public synchronized void mark(int readlimit) {
in.mark(readlimit);
}
@Override
public synchronized void reset() throws IOException {
in.reset();
}
@Override
public long transferTo(OutputStream out) throws IOException {
return in.transferTo(out);
}
}

View File

@ -0,0 +1,41 @@
package de.bluecolored.bluemap.core.util;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.OutputStream;
public class DelegateOutputStream extends OutputStream {
protected final OutputStream out;
public DelegateOutputStream(OutputStream out) {
this.out = out;
}
@Override
public void write(int b) throws IOException {
out.write(b);
}
@Override
public void write(byte @NotNull [] b) throws IOException {
out.write(b);
}
@Override
public void write(byte @NotNull [] b, int off, int len) throws IOException {
out.write(b, off, len);
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void close() throws IOException {
out.close();
}
}

View File

@ -24,11 +24,10 @@
*/
package de.bluecolored.bluemap.core.util;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
public class OnCloseInputStream extends FilterInputStream {
public class OnCloseInputStream extends DelegateInputStream {
private final AutoCloseable onClose;
@ -42,7 +41,7 @@ public class OnCloseInputStream extends FilterInputStream {
IOException ioExcetion = null;
try {
in.close();
super.close();
} catch (IOException ex) {
ioExcetion = ex;
}

View File

@ -24,11 +24,10 @@
*/
package de.bluecolored.bluemap.core.util;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class OnCloseOutputStream extends FilterOutputStream {
public class OnCloseOutputStream extends DelegateOutputStream {
private final AutoCloseable onClose;
@ -42,7 +41,7 @@ public class OnCloseOutputStream extends FilterOutputStream {
IOException ioExcetion = null;
try {
out.close();
super.close();
} catch (IOException ex) {
ioExcetion = ex;
}