Skip to content

Commit

Permalink
storage method name fix and indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
bale1017 committed Oct 31, 2023
1 parent a974b0d commit 5b0e9f2
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ public interface StorageImplementation {
Collection<NodeGroup> loadGroupsByMod(Collection<NamespacedKey> key);

default Optional<NodeGroup> loadGroup(NamespacedKey key) {
return loadGroupsByMod(Set.of(key)).stream().findAny();
return loadGroups(Set.of(key)).stream().findAny();
}

Map<UUID, Collection<NodeGroup>> loadGroups(Collection<UUID> ids);
Collection<NodeGroup> loadGroups(Collection<NamespacedKey> keys);

List<NodeGroup> loadGroups(Range range);
Map<UUID, Collection<NodeGroup>> loadGroupsByNodes(Collection<UUID> ids);

Collection<NodeGroup> loadGroupsByNode(UUID node);

Collection<NodeGroup> loadGroups(UUID node);
List<NodeGroup> loadGroups(Range range);

<M extends Modifier> Collection<NodeGroup> loadGroups(NamespacedKey modifier);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public CompletableFuture<Map<UUID, Collection<NodeGroup>>> loadGroups(Collection
}, () -> toLoad.add(uuid));
}
if (toLoad.size() > 0) {
result.putAll(implementation.loadGroups(toLoad));
result.putAll(implementation.loadGroupsByNodes(toLoad));
toLoad.forEach(uuid -> result.computeIfAbsent(uuid, u -> new HashSet<>()));
}
result.forEach((uuid, groups) -> {
Expand Down Expand Up @@ -412,7 +412,7 @@ public CompletableFuture<Collection<NodeGroup>> loadGroups(UUID node) {
return cached
.map(CompletableFuture::completedFuture)
.orElseGet(() -> asyncFuture(() -> {
Collection<NodeGroup> loaded = implementation.loadGroups(node);
Collection<NodeGroup> loaded = implementation.loadGroupsByNode(node);
cache.getGroupCache().write(node, loaded);
return loaded;
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ public Logger getLogger() {
return logger;
}

private Map<Long, Long> threadStartMap = new HashMap<>();

private void debug(String msg) {
logger.log(Level.INFO, msg + "\u001B[90m" + "(" + Thread.currentThread().getName() + ")" + "\u001B[0m");
long id = Thread.currentThread().getId();
Long dur = null;
if (!threadStartMap.containsKey(id)) {
threadStartMap.put(id, System.nanoTime());
} else {
dur = System.nanoTime() - threadStartMap.remove(id);
}
logger.log(Level.INFO, msg + "\u001B[90m" + "(" + Thread.currentThread().getName() + (dur == null ? "" : ", " + (dur / 1_000_000.) + "ms") + ")" + "\u001B[0m");
}

@Override
Expand Down Expand Up @@ -134,9 +143,17 @@ public Collection<NodeGroup> loadGroupsByMod(Collection<NamespacedKey> key) {
}

@Override
public Map<UUID, Collection<NodeGroup>> loadGroups(Collection<UUID> ids) {
public Collection<NodeGroup> loadGroups(Collection<NamespacedKey> keys) {
debug("> loadGroups(Collection<NamespacedKey> keys)");
var x = implementation.loadGroups(keys);
debug("< loadGroups(Collection<NamespacedKey> keys)");
return x;
}

@Override
public Map<UUID, Collection<NodeGroup>> loadGroupsByNodes(Collection<UUID> ids) {
debug("> loadGroups(Collection<UUID> ids " + ids.size() + ")");
var x = implementation.loadGroups(ids);
var x = implementation.loadGroupsByNodes(ids);
debug("< loadGroups(Collection<UUID> ids)");
return x;
}
Expand All @@ -150,9 +167,9 @@ public List<NodeGroup> loadGroups(Range range) {
}

@Override
public Collection<NodeGroup> loadGroups(UUID node) {
public Collection<NodeGroup> loadGroupsByNode(UUID node) {
debug("> loadGroups(UUID nodes)");
var x = implementation.loadGroups(node);
var x = implementation.loadGroupsByNode(node);
debug("< loadGroups(UUID nodes)");
return x;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package de.cubbossa.pathfinder.storage.implementation;

import static de.cubbossa.pathfinder.jooq.Tables.PATHFINDER_VISUALIZER;
import static de.cubbossa.pathfinder.jooq.Tables.PATHFINDER_VISUALIZER_TYPE_RELATION;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderDiscoverings.PATHFINDER_DISCOVERINGS;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderEdges.PATHFINDER_EDGES;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderGroupModifierRelation.PATHFINDER_GROUP_MODIFIER_RELATION;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderNodeTypeRelation.PATHFINDER_NODE_TYPE_RELATION;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderNodegroupNodes.PATHFINDER_NODEGROUP_NODES;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderNodegroups.PATHFINDER_NODEGROUPS;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderWaypoints.PATHFINDER_WAYPOINTS;

import de.cubbossa.pathapi.group.Modifier;
import de.cubbossa.pathapi.group.ModifierRegistry;
import de.cubbossa.pathapi.group.ModifierType;
Expand All @@ -15,13 +25,27 @@
import de.cubbossa.pathapi.visualizer.VisualizerType;
import de.cubbossa.pathapi.visualizer.VisualizerTypeRegistry;
import de.cubbossa.pathfinder.jooq.tables.records.PathfinderEdgesRecord;
import de.cubbossa.pathfinder.jooq.tables.records.PathfinderGroupModifierRelationRecord;
import de.cubbossa.pathfinder.jooq.tables.records.PathfinderNodegroupsRecord;
import de.cubbossa.pathfinder.jooq.tables.records.PathfinderVisualizerRecord;
import de.cubbossa.pathfinder.jooq.tables.records.PathfinderWaypointsRecord;
import de.cubbossa.pathfinder.node.SimpleEdge;
import de.cubbossa.pathfinder.node.implementation.Waypoint;
import de.cubbossa.pathfinder.nodegroup.SimpleNodeGroup;
import de.cubbossa.pathfinder.util.HashedRegistry;
import java.io.StringReader;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;
import java.util.stream.Collectors;
import javax.sql.DataSource;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jooq.DSLContext;
Expand All @@ -32,23 +56,6 @@
import org.jooq.conf.Settings;
import org.jooq.impl.DSL;

import javax.sql.DataSource;
import java.io.StringReader;
import java.time.LocalDateTime;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;

import static de.cubbossa.pathfinder.jooq.Tables.PATHFINDER_VISUALIZER;
import static de.cubbossa.pathfinder.jooq.Tables.PATHFINDER_VISUALIZER_TYPE_RELATION;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderDiscoverings.PATHFINDER_DISCOVERINGS;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderEdges.PATHFINDER_EDGES;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderGroupModifierRelation.PATHFINDER_GROUP_MODIFIER_RELATION;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderNodeTypeRelation.PATHFINDER_NODE_TYPE_RELATION;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderNodegroupNodes.PATHFINDER_NODEGROUP_NODES;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderNodegroups.PATHFINDER_NODEGROUPS;
import static de.cubbossa.pathfinder.jooq.tables.PathfinderWaypoints.PATHFINDER_WAYPOINTS;

public abstract class SqlStorage extends CommonStorage {

private final RecordMapper<? super PathfinderWaypointsRecord, Waypoint> nodeMapper;
Expand Down Expand Up @@ -172,27 +179,46 @@ private void createNodeTable() {
}

private void createEdgeTable() {
create
.createTableIfNotExists(PATHFINDER_EDGES)
.columns(PATHFINDER_EDGES.fields())
.primaryKey(PATHFINDER_EDGES.START_ID, PATHFINDER_EDGES.END_ID)
.execute();
create.transaction(c -> {
var ctx = DSL.using(c);
ctx.createTableIfNotExists(PATHFINDER_EDGES)
.columns(PATHFINDER_EDGES.fields())
.primaryKey(PATHFINDER_EDGES.START_ID, PATHFINDER_EDGES.END_ID)
.execute();
ctx.createIndexIfNotExists()
.on(PATHFINDER_EDGES)
.include(PATHFINDER_EDGES.START_ID)
.execute();
ctx.createIndexIfNotExists()
.on(PATHFINDER_EDGES)
.include(PATHFINDER_EDGES.END_ID)
.execute();
});
}

private void createNodeGroupTable() {
create
.createTableIfNotExists(PATHFINDER_NODEGROUPS)
create.createTableIfNotExists(PATHFINDER_NODEGROUPS)
.columns(PATHFINDER_NODEGROUPS.fields())
.primaryKey(PATHFINDER_NODEGROUPS.KEY)
.execute();
}

private void createNodeGroupNodesTable() {
create
.createTableIfNotExists(PATHFINDER_NODEGROUP_NODES)
.columns(PATHFINDER_NODEGROUP_NODES.fields())
.primaryKey(PATHFINDER_NODEGROUP_NODES.fields())
.execute();
create.transaction(cfg -> {
var ctx = DSL.using(cfg);
ctx.createTableIfNotExists(PATHFINDER_NODEGROUP_NODES)
.columns(PATHFINDER_NODEGROUP_NODES.fields())
.primaryKey(PATHFINDER_NODEGROUP_NODES.fields())
.execute();
ctx.createIndexIfNotExists()
.on(PATHFINDER_NODEGROUP_NODES)
.include(PATHFINDER_NODEGROUP_NODES.NODE_ID)
.execute();
ctx.createIndexIfNotExists()
.on(PATHFINDER_NODEGROUP_NODES)
.include(PATHFINDER_NODEGROUP_NODES.GROUP_KEY)
.execute();
});
}

private void createPathVisualizerTable() {
Expand All @@ -204,36 +230,74 @@ private void createPathVisualizerTable() {
}

private void createPathVisualizerTypeTable() {
create
.createTableIfNotExists(PATHFINDER_VISUALIZER_TYPE_RELATION)
.columns(PATHFINDER_VISUALIZER_TYPE_RELATION.fields())
.primaryKey(PATHFINDER_VISUALIZER_TYPE_RELATION.VISUALIZER_KEY, PATHFINDER_VISUALIZER_TYPE_RELATION.TYPE_KEY)
.execute();
create.transaction(cfg -> {
var ctx = DSL.using(cfg);
ctx.createTableIfNotExists(PATHFINDER_VISUALIZER_TYPE_RELATION)
.columns(PATHFINDER_VISUALIZER_TYPE_RELATION.fields())
.primaryKey(PATHFINDER_VISUALIZER_TYPE_RELATION.VISUALIZER_KEY, PATHFINDER_VISUALIZER_TYPE_RELATION.TYPE_KEY)
.execute();
ctx.createIndexIfNotExists()
.on(PATHFINDER_VISUALIZER_TYPE_RELATION)
.include(PATHFINDER_VISUALIZER_TYPE_RELATION.TYPE_KEY)
.execute();
ctx.createIndexIfNotExists()
.on(PATHFINDER_VISUALIZER_TYPE_RELATION)
.include(PATHFINDER_VISUALIZER_TYPE_RELATION.VISUALIZER_KEY)
.execute();
});
}

private void createDiscoverInfoTable() {
create
.createTableIfNotExists(PATHFINDER_DISCOVERINGS)
.columns(PATHFINDER_DISCOVERINGS.fields())
.primaryKey(PATHFINDER_DISCOVERINGS.PLAYER_ID, PATHFINDER_DISCOVERINGS.DISCOVER_KEY)
.execute();
create.transaction(cfg -> {
var ctx = DSL.using(cfg);
ctx.createTableIfNotExists(PATHFINDER_DISCOVERINGS)
.columns(PATHFINDER_DISCOVERINGS.fields())
.primaryKey(PATHFINDER_DISCOVERINGS.PLAYER_ID, PATHFINDER_DISCOVERINGS.DISCOVER_KEY)
.execute();
ctx.createIndexIfNotExists()
.on(PATHFINDER_DISCOVERINGS)
.include(PATHFINDER_DISCOVERINGS.PLAYER_ID)
.execute();
});
}


private void createNodeTypeRelation() {
create
.createTableIfNotExists(PATHFINDER_NODE_TYPE_RELATION)
.columns(PATHFINDER_NODE_TYPE_RELATION.fields())
.primaryKey(PATHFINDER_NODE_TYPE_RELATION.NODE_ID, PATHFINDER_NODE_TYPE_RELATION.NODE_TYPE)
.execute();

create.transaction(cfg -> {
var ctx = DSL.using(cfg);
ctx.createTableIfNotExists(PATHFINDER_NODE_TYPE_RELATION)
.columns(PATHFINDER_NODE_TYPE_RELATION.fields())
.primaryKey(PATHFINDER_NODE_TYPE_RELATION.NODE_ID, PATHFINDER_NODE_TYPE_RELATION.NODE_TYPE)
.execute();
ctx.createIndexIfNotExists()
.on(PATHFINDER_NODE_TYPE_RELATION)
.include(PATHFINDER_NODE_TYPE_RELATION.NODE_ID)
.execute();
ctx.createIndexIfNotExists()
.on(PATHFINDER_NODE_TYPE_RELATION)
.include(PATHFINDER_NODE_TYPE_RELATION.NODE_TYPE)
.execute();
});

}

private void createModifierGroupRelation() {
create
.createTableIfNotExists(PATHFINDER_GROUP_MODIFIER_RELATION)
.columns(PATHFINDER_GROUP_MODIFIER_RELATION.fields())
.primaryKey(PATHFINDER_GROUP_MODIFIER_RELATION.GROUP_KEY, PATHFINDER_GROUP_MODIFIER_RELATION.MODIFIER_KEY)
.execute();
create.transaction(cfg -> {
var ctx = DSL.using(cfg);
ctx.createTableIfNotExists(PATHFINDER_GROUP_MODIFIER_RELATION)
.columns(PATHFINDER_GROUP_MODIFIER_RELATION.fields())
.primaryKey(PATHFINDER_GROUP_MODIFIER_RELATION.GROUP_KEY, PATHFINDER_GROUP_MODIFIER_RELATION.MODIFIER_KEY)
.execute();
ctx.createIndexIfNotExists()
.on(PATHFINDER_GROUP_MODIFIER_RELATION)
.include(PATHFINDER_GROUP_MODIFIER_RELATION.GROUP_KEY)
.execute();
ctx.createIndexIfNotExists()
.on(PATHFINDER_GROUP_MODIFIER_RELATION)
.include(PATHFINDER_GROUP_MODIFIER_RELATION.MODIFIER_KEY)
.execute();
});
}

@Override
Expand Down Expand Up @@ -413,8 +477,7 @@ public Collection<UUID> loadGroupNodes(NodeGroup group) {
.fetch(Record1::value1);
}

@Override
public Map<UUID, Collection<NodeGroup>> loadGroups(Collection<UUID> ids) {
public Map<UUID, Collection<NodeGroup>> loadGroupsByNodes(Collection<UUID> ids) {
Map<UUID, Collection<NodeGroup>> result = new HashMap<>();
create.transaction(cfg -> {
Map<UUID, Collection<NamespacedKey>> mapping = new HashMap<>();
Expand All @@ -437,8 +500,21 @@ public Map<UUID, Collection<NodeGroup>> loadGroups(Collection<UUID> ids) {

@Override
public Collection<NodeGroup> loadGroupsByMod(Collection<NamespacedKey> key) {
return create.transactionResult(cfg -> {
var ctx = DSL.using(cfg);
Collection<NamespacedKey> groups = ctx.selectFrom(PATHFINDER_GROUP_MODIFIER_RELATION)
.where(PATHFINDER_GROUP_MODIFIER_RELATION.MODIFIER_KEY.in(key))
.fetch(PathfinderGroupModifierRelationRecord::getGroupKey);
return ctx.selectFrom(PATHFINDER_NODEGROUPS)
.where(PATHFINDER_NODEGROUPS.KEY.in(groups))
.fetch(groupMapper);
});
}

@Override
public Collection<NodeGroup> loadGroups(Collection<NamespacedKey> keys) {
return create.selectFrom(PATHFINDER_NODEGROUPS)
.where(PATHFINDER_NODEGROUPS.KEY.in(key))
.where(PATHFINDER_NODEGROUPS.KEY.in(keys))
.fetch(groupMapper);
}

Expand All @@ -451,7 +527,7 @@ public List<NodeGroup> loadGroups(Range range) {
}

@Override
public Collection<NodeGroup> loadGroups(UUID node) {
public Collection<NodeGroup> loadGroupsByNode(UUID node) {
return create.select().from(PATHFINDER_NODEGROUPS)
.join(PATHFINDER_NODEGROUP_NODES)
.on(PATHFINDER_NODEGROUPS.KEY.eq(PATHFINDER_NODEGROUP_NODES.GROUP_KEY))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public List<NodeGroup> loadGroups(Range range) {
}

@Override
public Collection<NodeGroup> loadGroups(UUID node) {
public Collection<NodeGroup> loadGroupsByNode(UUID node) {
return loadAllGroups().stream()
.filter(g -> g.contains(node))
.collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.cubbossa.pathfinder.storage;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import de.cubbossa.pathapi.group.Modifier;
import de.cubbossa.pathapi.group.ModifierRegistry;
import de.cubbossa.pathapi.group.NodeGroup;
Expand Down Expand Up @@ -222,6 +223,26 @@ void loadGroupsByMod() {
Assertions.assertTrue(Maps.difference(expected, result).areEqual());
}

@Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS, threadMode = Timeout.ThreadMode.SEPARATE_THREAD)
void loadGroupsByMod2() {
NamespacedKey xKey = NamespacedKey.fromString("pathfinder:x");
NamespacedKey yKey = NamespacedKey.fromString("pathfinder:y");

NodeGroup x = makeGroup(xKey);
NodeGroup y = makeGroup(yKey);
Set<NodeGroup> groups = Set.of(x, y);

int i = 0;
for (NodeGroup g : groups) {
g.addModifier(new TestModifier(i++ + ""));
storage.saveGroup(g).join();
}

Set<NodeGroup> result = new HashSet<>(storage.loadGroupsByMod(Collections.singletonList(TestModifierType.KEY)).join());
assertTrue(Sets.difference(groups, result).isEmpty());
}

@Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS, threadMode = Timeout.ThreadMode.SEPARATE_THREAD)
void getNodeGroupKeySet() {
Expand Down

0 comments on commit 5b0e9f2

Please sign in to comment.