From 5adc0672b731970f75603b6bc386b731dcabe921 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 26 Dec 2020 10:07:19 +0800 Subject: 1.2.1-alpha (not finished): add range lookup and `radius` index (in log entry version 2) --- src/main/java/com/keuin/blame/Blame.java | 20 ++++++++++++- .../com/keuin/blame/command/BlameBlockCommand.java | 8 +++-- .../java/com/keuin/blame/data/entry/LogEntry.java | 5 +++- .../com/keuin/blame/data/entry/LogEntryNames.java | 15 ++++++++++ .../keuin/blame/data/entry/LogEntryNamesV1.java | 14 --------- .../transformer/AbstractLogEntryTransformer.java | 7 +++++ .../transformer/LogEntryV1ToV2Transformer.java | 34 ++++++++++++++++++++++ .../blame/data/transformer/TransformerManager.java | 28 ++++++++++++++++++ .../keuin/blame/lookup/BlockPosLookupFilter.java | 6 ++-- 9 files changed, 116 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/keuin/blame/data/entry/LogEntryNames.java delete mode 100644 src/main/java/com/keuin/blame/data/entry/LogEntryNamesV1.java create mode 100644 src/main/java/com/keuin/blame/data/transformer/AbstractLogEntryTransformer.java create mode 100644 src/main/java/com/keuin/blame/data/transformer/LogEntryV1ToV2Transformer.java create mode 100644 src/main/java/com/keuin/blame/data/transformer/TransformerManager.java (limited to 'src') 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 collection = db.getCollection( +// DatabaseUtil.MONGO_CONFIG.getLogCollectionName(), LogEntry.class +// ); +// FindIterable iterable = +// collection.find(Filters.ne(LogEntryNames.VERSION, TransformerManager.LATEST_VERSION)); +// List +// 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 context) { + public static int blameGivenBlockPos(CommandContext 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 context) { + return SUCCESS; + } + private static class Callback implements LookupCallback { private final CommandContext 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/LogEntryNames.java b/src/main/java/com/keuin/blame/data/entry/LogEntryNames.java new file mode 100644 index 0000000..e375d8e --- /dev/null +++ b/src/main/java/com/keuin/blame/data/entry/LogEntryNames.java @@ -0,0 +1,15 @@ +package com.keuin.blame.data.entry; + +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"; + public static final String SUBJECT_ID = "subject_id"; + public static final String SUBJECT_UUID = "subject_uuid"; + public static final String SUBJECT_POS = "subject_pos"; + public static final String ACTION_TYPE = "action_type"; + 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/entry/LogEntryNamesV1.java b/src/main/java/com/keuin/blame/data/entry/LogEntryNamesV1.java deleted file mode 100644 index d21be97..0000000 --- a/src/main/java/com/keuin/blame/data/entry/LogEntryNamesV1.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.keuin.blame.data.entry; - -public class LogEntryNamesV1 { - public static final String VERSION = "version"; - public static final String GAME_VERSION = "game_version"; - public static final String TIMESTAMP_MILLIS = "timestamp_millis"; - public static final String SUBJECT_ID = "subject_id"; - public static final String SUBJECT_UUID = "subject_uuid"; - public static final String SUBJECT_POS = "subject_pos"; - public static final String ACTION_TYPE = "action_type"; - public static final String OBJECT_TYPE = "object_type"; - public static final String OBJECT_ID = "object_id"; - public static final String OBJECT_POS = "object_pos"; -} 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 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 find(FindIterable 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) )); } } -- cgit v1.2.3