diff options
author | Keuin <[email protected]> | 2020-12-26 10:07:19 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2020-12-26 10:07:19 +0800 |
commit | 5adc0672b731970f75603b6bc386b731dcabe921 (patch) | |
tree | b3e9e956952916f0d02a51d5d22f8e05893bff2a /src/main/java/com | |
parent | f8b416ec75504101e8aaa86ba6f82e156a378dca (diff) |
1.2.1-alpha (not finished): add range lookup and `radius` index (in log entry version 2)
Diffstat (limited to 'src/main/java/com')
-rw-r--r-- | src/main/java/com/keuin/blame/Blame.java | 20 | ||||
-rw-r--r-- | src/main/java/com/keuin/blame/command/BlameBlockCommand.java | 8 | ||||
-rw-r--r-- | src/main/java/com/keuin/blame/data/entry/LogEntry.java | 5 | ||||
-rw-r--r-- | src/main/java/com/keuin/blame/data/entry/LogEntryNames.java (renamed from src/main/java/com/keuin/blame/data/entry/LogEntryNamesV1.java) | 3 | ||||
-rw-r--r-- | src/main/java/com/keuin/blame/data/transformer/AbstractLogEntryTransformer.java | 7 | ||||
-rw-r--r-- | src/main/java/com/keuin/blame/data/transformer/LogEntryV1ToV2Transformer.java | 34 | ||||
-rw-r--r-- | src/main/java/com/keuin/blame/data/transformer/TransformerManager.java | 28 | ||||
-rw-r--r-- | src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java | 6 |
8 files changed, 103 insertions, 8 deletions
diff --git a/src/main/java/com/keuin/blame/Blame.java b/src/main/java/com/keuin/blame/Blame.java index ab22590..f620f73 100644 --- a/src/main/java/com/keuin/blame/Blame.java +++ b/src/main/java/com/keuin/blame/Blame.java @@ -58,6 +58,24 @@ public class Blame implements ModInitializer { mongoLogger.setLevel(Level.WARNING); } + private static void upgradeOldLogEntries() { +// try (final MongoClient mongoClient = MongoClients.create(DatabaseUtil.CLIENT_SETTINGS)) { +// final MongoDatabase db = mongoClient.getDatabase( +// DatabaseUtil.MONGO_CONFIG.getDatabaseName() +// ); +// final MongoCollection<LogEntry> collection = db.getCollection( +// DatabaseUtil.MONGO_CONFIG.getLogCollectionName(), LogEntry.class +// ); +// FindIterable<LogEntry> iterable = +// collection.find(Filters.ne(LogEntryNames.VERSION, TransformerManager.LATEST_VERSION)); +// List<LogEntry> +// for (LogEntry logEntry : iterable) { +// iterable.batchSize(100). +// } +// +// } + } + @Override public void onInitialize() { // This code runs as soon as Minecraft is in a mod-load-ready state. @@ -102,7 +120,7 @@ public class Blame implements ModInitializer { .then(CommandManager.argument("y", IntegerArgumentType.integer()) .then(CommandManager.argument("z", IntegerArgumentType.integer()) .then(CommandManager.argument("world", StringArgumentType.greedyString()) - .executes(BlameBlockCommand::blameBlock))))))); + .executes(BlameBlockCommand::blameGivenBlockPos))))))); commandDispatcher.register( CommandManager.literal("blame").then(CommandManager.literal("limit") .then(CommandManager.argument("limit", IntegerArgumentType.integer(1, 255)) diff --git a/src/main/java/com/keuin/blame/command/BlameBlockCommand.java b/src/main/java/com/keuin/blame/command/BlameBlockCommand.java index 578870d..4fdd350 100644 --- a/src/main/java/com/keuin/blame/command/BlameBlockCommand.java +++ b/src/main/java/com/keuin/blame/command/BlameBlockCommand.java @@ -16,8 +16,7 @@ import static com.keuin.blame.command.Commands.SUCCESS; public class BlameBlockCommand { - - public static int blameBlock(CommandContext<ServerCommandSource> context) { + public static int blameGivenBlockPos(CommandContext<ServerCommandSource> context) { Entity entity = context.getSource().getEntity(); if (!(entity instanceof ServerPlayerEntity)) { // can only be executed by player @@ -39,6 +38,10 @@ public class BlameBlockCommand { return SUCCESS; } + public static int blameGivenBlockRange(CommandContext<ServerCommandSource> context) { + return SUCCESS; + } + private static class Callback implements LookupCallback { private final CommandContext<ServerCommandSource> context; @@ -63,4 +66,5 @@ public class BlameBlockCommand { PrintUtil.msgInfo(context, printBuilder.toString()); } } + } diff --git a/src/main/java/com/keuin/blame/data/entry/LogEntry.java b/src/main/java/com/keuin/blame/data/entry/LogEntry.java index 090d77c..0ffaf94 100644 --- a/src/main/java/com/keuin/blame/data/entry/LogEntry.java +++ b/src/main/java/com/keuin/blame/data/entry/LogEntry.java @@ -11,7 +11,7 @@ import org.bson.codecs.pojo.annotations.BsonProperty; import java.util.Objects; import java.util.UUID; -import static com.keuin.blame.data.entry.LogEntryNamesV1.*; +import static com.keuin.blame.data.entry.LogEntryNames.*; public class LogEntry { @@ -70,6 +70,9 @@ public class LogEntry { @BsonProperty(OBJECT_POS) public WorldPos objectPos = WorldPos.NULL_POS; + @BsonProperty(RADIUS) + public double radius = 0; + public LogEntry() { } diff --git a/src/main/java/com/keuin/blame/data/entry/LogEntryNamesV1.java b/src/main/java/com/keuin/blame/data/entry/LogEntryNames.java index d21be97..e375d8e 100644 --- a/src/main/java/com/keuin/blame/data/entry/LogEntryNamesV1.java +++ b/src/main/java/com/keuin/blame/data/entry/LogEntryNames.java @@ -1,6 +1,6 @@ package com.keuin.blame.data.entry; -public class LogEntryNamesV1 { +public class LogEntryNames { public static final String VERSION = "version"; public static final String GAME_VERSION = "game_version"; public static final String TIMESTAMP_MILLIS = "timestamp_millis"; @@ -11,4 +11,5 @@ public class LogEntryNamesV1 { public static final String OBJECT_TYPE = "object_type"; public static final String OBJECT_ID = "object_id"; public static final String OBJECT_POS = "object_pos"; + public static final String RADIUS = "radius"; } diff --git a/src/main/java/com/keuin/blame/data/transformer/AbstractLogEntryTransformer.java b/src/main/java/com/keuin/blame/data/transformer/AbstractLogEntryTransformer.java new file mode 100644 index 0000000..78872c5 --- /dev/null +++ b/src/main/java/com/keuin/blame/data/transformer/AbstractLogEntryTransformer.java @@ -0,0 +1,7 @@ +package com.keuin.blame.data.transformer; + +import com.keuin.blame.data.entry.LogEntry; + +public abstract class AbstractLogEntryTransformer { + public abstract LogEntry transform(LogEntry entry); +} diff --git a/src/main/java/com/keuin/blame/data/transformer/LogEntryV1ToV2Transformer.java b/src/main/java/com/keuin/blame/data/transformer/LogEntryV1ToV2Transformer.java new file mode 100644 index 0000000..4f5c660 --- /dev/null +++ b/src/main/java/com/keuin/blame/data/transformer/LogEntryV1ToV2Transformer.java @@ -0,0 +1,34 @@ +package com.keuin.blame.data.transformer; + +import com.keuin.blame.data.WorldPos; +import com.keuin.blame.data.entry.LogEntry; + +public class LogEntryV1ToV2Transformer extends AbstractLogEntryTransformer { + + private static final LogEntryV1ToV2Transformer INSTANCE = new LogEntryV1ToV2Transformer(); + + static { + TransformerManager.setTransformer(1, INSTANCE); + } + + @Override + public LogEntry transform(LogEntry entry) { + LogEntry entryV2 = new LogEntry(); + entryV2.version = 2; + entryV2.gameVersion = entry.gameVersion; + entryV2.timeMillis = entry.timeMillis; + entryV2.subjectId = entry.subjectId; + entryV2.subjectUUID = entry.subjectUUID; + entryV2.subjectPos = entry.subjectPos; + entryV2.actionType = entry.actionType; + entryV2.objectType = entry.objectType; + entryV2.objectId = entry.objectId; + entryV2.objectPos = entry.objectPos; + entryV2.radius = getRadius(entry.objectPos); + return entryV2; + } + + private static double getRadius(WorldPos objectPos) { + return Math.sqrt(Math.pow(objectPos.getX(), 2) + Math.pow(objectPos.getY(), 2) + Math.pow(objectPos.getZ(), 2)); + } +} diff --git a/src/main/java/com/keuin/blame/data/transformer/TransformerManager.java b/src/main/java/com/keuin/blame/data/transformer/TransformerManager.java new file mode 100644 index 0000000..e4db239 --- /dev/null +++ b/src/main/java/com/keuin/blame/data/transformer/TransformerManager.java @@ -0,0 +1,28 @@ +package com.keuin.blame.data.transformer; + +import com.keuin.blame.data.entry.LogEntry; + +import java.util.HashMap; +import java.util.Map; + +public class TransformerManager { + + public static int LATEST_VERSION = 2; + + private static final Map<Integer, AbstractLogEntryTransformer> transformerMap = new HashMap<>(); + + static void setTransformer(int baseVersion, AbstractLogEntryTransformer transformer) { + transformerMap.put(baseVersion, transformer); + } + + public static Object toLatestVersion(LogEntry baseEntry) { + return transformTo(LATEST_VERSION, baseEntry); + } + + public static Object transformTo(int targetVersion, LogEntry baseEntry) { + LogEntry entry = baseEntry; + for (int currentVersion = baseEntry.version; currentVersion != targetVersion; ++currentVersion) + entry = transformerMap.get(currentVersion + 1).transform(entry); + return entry; + } +} diff --git a/src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java b/src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java index a4b2475..d7cec43 100644 --- a/src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java +++ b/src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java @@ -2,7 +2,7 @@ package com.keuin.blame.lookup; import com.keuin.blame.data.WorldPos; import com.keuin.blame.data.entry.LogEntry; -import com.keuin.blame.data.entry.LogEntryNamesV1; +import com.keuin.blame.data.entry.LogEntryNames; import com.mongodb.client.FindIterable; import com.mongodb.client.model.Filters; @@ -16,8 +16,8 @@ public class BlockPosLookupFilter extends AbstractLookupFilter { @Override FindIterable<LogEntry> find(FindIterable<LogEntry> iterable) { return iterable.filter(Filters.and( - Filters.eq(LogEntryNamesV1.VERSION, 1), - Filters.eq(LogEntryNamesV1.OBJECT_POS, blockPos) + Filters.eq(LogEntryNames.VERSION, 1), + Filters.eq(LogEntryNames.OBJECT_POS, blockPos) )); } } |