Skip to content

Commit

Permalink
Fix delete statements doing a full table scan
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed Apr 18, 2024
1 parent 37dd181 commit a640285
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ public int writeMapTile(

@Override
public int deleteMapTile(String mapId, int lod, int x, int z, Compression compression) throws IOException {
int mapKey = mapKey(mapId);
int compressionKey = compressionKey(compression);
return db.run(connection -> {
return executeUpdate(connection,
deleteMapTileStatement(),
mapId, lod, x, z, compression.getKey().getFormatted()
mapKey, lod, x, z, compressionKey
);
});
}
Expand Down Expand Up @@ -156,10 +158,11 @@ public int countAllMapTiles(String mapId) throws IOException {

@Override
public int purgeMapTiles(String mapId, int limit) throws IOException {
int mapKey = mapKey(mapId);
return db.run(connection -> {
return executeUpdate(connection,
purgeMapTilesStatement(),
mapId, limit
mapKey, limit
);
});
}
Expand Down Expand Up @@ -230,10 +233,11 @@ public int writeMapMeta(String mapId, String itemName, byte[] bytes) throws IOEx

@Override
public int deleteMapMeta(String mapId, String itemName) throws IOException {
int mapKey = mapKey(mapId);
return db.run(connection -> {
return executeUpdate(connection,
deleteMapMetaStatement(),
mapId, itemName
mapKey, itemName
);
});
}
Expand Down Expand Up @@ -265,21 +269,22 @@ public boolean hasMapMeta(String mapId, String itemName) throws IOException {
@Override
public void purgeMap(String mapId) throws IOException {
synchronized (mapKeys) {
int mapKey = mapKey(mapId);
db.run(connection -> {

executeUpdate(connection,
purgeMapTileTableStatement(),
mapId
mapKey
);

executeUpdate(connection,
purgeMapMetaTableStatement(),
mapId
mapKey
);

executeUpdate(connection,
deleteMapStatement(),
mapId
mapKey
);

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,11 @@ public String deleteMapTileStatement() {
return """
DELETE
FROM `bluemap_map_tile`
WHERE `map` IN (
SELECT m.`id`
FROM `bluemap_map` m
WHERE m.`map_id` = ?
)
WHERE `map` = ?
AND `lod` = ?
AND `x` = ?
AND `z` = ?
AND `compression` IN (
SELECT c.`id`
FROM `bluemap_map_tile_compression` c
WHERE c.`compression` = ?
)
AND `compression` = ?
""";
}

Expand Down Expand Up @@ -199,11 +191,7 @@ public String purgeMapTilesStatement() {
return """
DELETE
FROM `bluemap_map_tile`
WHERE `map` IN (
SELECT `id`
FROM `bluemap_map`
WHERE `map_id` = ?
)
WHERE `map` = ?
LIMIT ?
""";
}
Expand Down Expand Up @@ -254,11 +242,7 @@ public String deleteMapMetaStatement() {
return """
DELETE
FROM `bluemap_map_meta`
WHERE `map` IN (
SELECT `id`
FROM `bluemap_map`
WHERE `map_id` = ?
)
WHERE `map` = ?
AND `key` = ?
""";
}
Expand All @@ -282,11 +266,7 @@ public String purgeMapTileTableStatement() {
return """
DELETE
FROM `bluemap_map_tile`
WHERE `map` IN (
SELECT m.`id`
FROM `bluemap_map` m
WHERE m.`map_id` = ?
)
WHERE `map` = ?
""";
}

Expand All @@ -296,11 +276,7 @@ public String purgeMapMetaTableStatement() {
return """
DELETE
FROM `bluemap_map_meta`
WHERE `map` IN (
SELECT m.`id`
FROM `bluemap_map` m
WHERE m.`map_id` = ?
)
WHERE `map` = ?
""";
}

Expand All @@ -310,7 +286,7 @@ public String deleteMapStatement() {
return """
DELETE
FROM `bluemap_map`
WHERE `map_id` = ?
WHERE `map` = ?
""";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,11 @@ public String deleteMapTileStatement() {
return """
DELETE
FROM bluemap_map_tile
WHERE map IN (
SELECT m.id
FROM bluemap_map m
WHERE m.map_id = ?
)
WHERE map = ?
AND lod = ?
AND x = ?
AND z = ?
AND compression IN (
SELECT c.id
FROM bluemap_map_tile_compression c
WHERE c.compression = ?
)
AND compression = ?
""";
}

Expand Down Expand Up @@ -196,9 +188,7 @@ public String purgeMapTilesStatement() {
WHERE CTID IN (
SELECT CTID
FROM bluemap_map_tile t
INNER JOIN bluemap_map m
ON t.map = m.id
WHERE m.map_id = ?
WHERE t.map = ?
LIMIT ?
)
""";
Expand Down Expand Up @@ -253,11 +243,7 @@ public String deleteMapMetaStatement() {
return """
DELETE
FROM bluemap_map_meta
WHERE map IN (
SELECT id
FROM bluemap_map
WHERE map_id = ?
)
WHERE map = ?
AND key = ?
""";
}
Expand All @@ -281,11 +267,7 @@ public String purgeMapTileTableStatement() {
return """
DELETE
FROM bluemap_map_tile
WHERE map IN (
SELECT m.id
FROM bluemap_map m
WHERE m.map_id = ?
)
WHERE map = ?
""";
}

Expand All @@ -295,11 +277,7 @@ public String purgeMapMetaTableStatement() {
return """
DELETE
FROM bluemap_map_meta
WHERE map IN (
SELECT m.id
FROM bluemap_map m
WHERE m.map_id = ?
)
WHERE map = ?
""";
}

Expand All @@ -309,7 +287,7 @@ public String deleteMapStatement() {
return """
DELETE
FROM bluemap_map
WHERE map_id = ?
WHERE map = ?
""";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,11 @@ public String deleteMapTileStatement() {
return """
DELETE
FROM `bluemap_map_tile`
WHERE `map` IN (
SELECT m.`id`
FROM `bluemap_map` m
WHERE m.`map_id` = ?
)
WHERE `map` = ?
AND `lod` = ?
AND `x` = ?
AND `z` = ?
AND `compression` IN (
SELECT c.`id`
FROM `bluemap_map_tile_compression` c
WHERE c.`compression` = ?
)
AND `compression` = ?
""";
}

Expand Down Expand Up @@ -198,9 +190,7 @@ public String purgeMapTilesStatement() {
WHERE ROWID IN (
SELECT t.ROWID
FROM bluemap_map_tile t
INNER JOIN bluemap_map m
ON t.map = m.id
WHERE m.map_id = ?
WHERE t.map = ?
LIMIT ?
)
""";
Expand Down Expand Up @@ -252,11 +242,7 @@ public String deleteMapMetaStatement() {
return """
DELETE
FROM `bluemap_map_meta`
WHERE `map` IN (
SELECT `id`
FROM `bluemap_map`
WHERE `map_id` = ?
)
WHERE `map` = ?
AND `key` = ?
""";
}
Expand All @@ -280,11 +266,7 @@ public String purgeMapTileTableStatement() {
return """
DELETE
FROM `bluemap_map_tile`
WHERE `map` IN (
SELECT m.`id`
FROM `bluemap_map` m
WHERE m.`map_id` = ?
)
WHERE `map` = ?
""";
}

Expand All @@ -294,11 +276,7 @@ public String purgeMapMetaTableStatement() {
return """
DELETE
FROM `bluemap_map_meta`
WHERE `map` IN (
SELECT m.`id`
FROM `bluemap_map` m
WHERE m.`map_id` = ?
)
WHERE `map` = ?
""";
}

Expand All @@ -308,7 +286,7 @@ public String deleteMapStatement() {
return """
DELETE
FROM `bluemap_map`
WHERE `map_id` = ?
WHERE `map` = ?
""";
}

Expand Down

0 comments on commit a640285

Please sign in to comment.