diff options
author | Keuin <[email protected]> | 2020-12-24 21:11:30 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2020-12-24 21:11:30 +0800 |
commit | 5b76ebe34e5237f48a12baa70b33105c02fc9d5f (patch) | |
tree | 7349fbb85ad7bd1476a945181ecb8498aef7828a /src/main/java | |
parent | 907fce5f377de394ed5cab1734569c91a72a3420 (diff) |
clean codes, small refactor.
Diffstat (limited to 'src/main/java')
15 files changed, 95 insertions, 249 deletions
diff --git a/src/main/java/com/keuin/blame/EventHandler.java b/src/main/java/com/keuin/blame/EventHandler.java index df10e5b..f2e4f09 100644 --- a/src/main/java/com/keuin/blame/EventHandler.java +++ b/src/main/java/com/keuin/blame/EventHandler.java @@ -1,8 +1,8 @@ package com.keuin.blame; import com.keuin.blame.adapter.handler.*; -import com.keuin.blame.data.LogEntry; import com.keuin.blame.data.LogEntryFactory; +import com.keuin.blame.data.entry.LogEntry; import com.keuin.blame.data.enums.ActionType; import com.keuin.blame.util.MinecraftUtil; import com.keuin.blame.util.PrintUtil; diff --git a/src/main/java/com/keuin/blame/SubmitWorker.java b/src/main/java/com/keuin/blame/SubmitWorker.java index c7334f8..cfbf4a0 100644 --- a/src/main/java/com/keuin/blame/SubmitWorker.java +++ b/src/main/java/com/keuin/blame/SubmitWorker.java @@ -1,6 +1,6 @@ package com.keuin.blame; -import com.keuin.blame.data.LogEntry; +import com.keuin.blame.data.entry.LogEntry; import com.keuin.blame.util.DatabaseUtil; import com.mongodb.MongoClientException; import com.mongodb.client.MongoClient; diff --git a/src/main/java/com/keuin/blame/command/BlameBlockCommand.java b/src/main/java/com/keuin/blame/command/BlameBlockCommand.java index 63d99ec..7d85e77 100644 --- a/src/main/java/com/keuin/blame/command/BlameBlockCommand.java +++ b/src/main/java/com/keuin/blame/command/BlameBlockCommand.java @@ -1,7 +1,7 @@ package com.keuin.blame.command; -import com.keuin.blame.data.LogEntry; import com.keuin.blame.data.WorldPos; +import com.keuin.blame.data.entry.LogEntry; import com.keuin.blame.lookup.BlockPosLookupFilter; import com.keuin.blame.lookup.LookupCallback; import com.keuin.blame.lookup.LookupManager; diff --git a/src/main/java/com/keuin/blame/data/LogEntryFactory.java b/src/main/java/com/keuin/blame/data/LogEntryFactory.java index bf71b27..c7be5a5 100644 --- a/src/main/java/com/keuin/blame/data/LogEntryFactory.java +++ b/src/main/java/com/keuin/blame/data/LogEntryFactory.java @@ -1,5 +1,6 @@ package com.keuin.blame.data; +import com.keuin.blame.data.entry.LogEntry; import com.keuin.blame.data.enums.ActionType; import com.keuin.blame.data.enums.ObjectType; import net.minecraft.entity.player.PlayerEntity; diff --git a/src/main/java/com/keuin/blame/data/LogEntry.java b/src/main/java/com/keuin/blame/data/entry/LogEntry.java index e178dc2..586ba91 100644 --- a/src/main/java/com/keuin/blame/data/LogEntry.java +++ b/src/main/java/com/keuin/blame/data/entry/LogEntry.java @@ -1,14 +1,18 @@ -package com.keuin.blame.data; +package com.keuin.blame.data.entry; +import com.keuin.blame.data.WorldPos; import com.keuin.blame.data.enums.ActionType; import com.keuin.blame.data.enums.ObjectType; import com.keuin.blame.util.PrettyUtil; import com.keuin.blame.util.UuidUtils; import net.minecraft.MinecraftVersion; +import org.bson.codecs.pojo.annotations.BsonProperty; import java.util.Objects; import java.util.UUID; +import static com.keuin.blame.data.entry.LogEntryNamesV1.*; + public class LogEntry { // { @@ -36,36 +40,42 @@ public class LogEntry { // } //} - // @BsonProperty("version") - private static int version = 1; - // @BsonProperty("game_version") - private static String gameVersion = MinecraftVersion.field_25319.getName(); - // @BsonProperty("timestamp_millis") - private long timeMillis = 0; - // @BsonProperty("subject_id") - private String subjectId = ""; - // @BsonProperty("subject_uuid") - private String subjectUUID = UuidUtils.UUID_NULL.toString(); // TODO: use Binary instead (BasicDBObject("_id", Binary(session.getIp().getAddress()))) (https://stackoverflow.com/questions/30566905/store-byte-in-mongodb-using-java/40843195) - // @BsonProperty("subject_pos") - private WorldPos subjectPos = WorldPos.NULL_POS; // TODO: write codec and transformer for this - // @BsonProperty("action_type") - private ActionType actionType = ActionType.NULL; - // @BsonProperty("object_type") - private ObjectType objectType = ObjectType.NULL; - // @BsonProperty("object_id") - private String objectId = ""; - // @BsonProperty("object_pos") - private WorldPos objectPos = WorldPos.NULL_POS; - - public static final LogEntry EMPTY_ENTRY = new LogEntry(); - - protected LogEntry() { + @BsonProperty(VERSION) + public int version = 1; + + @BsonProperty(GAME_VERSION) + public String gameVersion = MinecraftVersion.field_25319.getName(); + + @BsonProperty(TIMESTAMP_MILLIS) + public long timeMillis = 0; + + @BsonProperty(SUBJECT_ID) + public String subjectId = ""; + + @BsonProperty(SUBJECT_UUID) + public UUID subjectUUID = UuidUtils.UUID_NULL; + + @BsonProperty(SUBJECT_POS) + public WorldPos subjectPos = WorldPos.NULL_POS; + + @BsonProperty(ACTION_TYPE) + public ActionType actionType = ActionType.NULL; + + @BsonProperty(OBJECT_TYPE) + public ObjectType objectType = ObjectType.NULL; + + @BsonProperty(OBJECT_ID) + public String objectId = ""; + + @BsonProperty(OBJECT_POS) + public WorldPos objectPos = WorldPos.NULL_POS; + + public LogEntry() { } public LogEntry(long timeMillis, String subjectId, UUID subjectUUID, WorldPos subjectPos, ActionType actionType, ObjectType objectType, String objectId, WorldPos objectPos) { - this.subjectId = subjectId; -// this.subjectUUID = UuidUtils.asBytes(subjectUUID); -// this.subjectUUID + if (subjectId == null) + throw new IllegalArgumentException("subjectId cannot be null"); if (subjectUUID == null) throw new IllegalArgumentException("subjectUUID cannot be null"); if (subjectPos == null) @@ -79,8 +89,9 @@ public class LogEntry { if (objectPos == null) throw new IllegalArgumentException("objectPos cannot be null"); + this.subjectId = subjectId; this.timeMillis = timeMillis; - this.subjectUUID = subjectUUID.toString(); + this.subjectUUID = subjectUUID; this.subjectPos = subjectPos; this.actionType = actionType; this.objectType = objectType; @@ -88,52 +99,14 @@ public class LogEntry { this.objectPos = objectPos; } - public int getVersion() { - return version; - } - - public String getGameVersion() { - return gameVersion; - } - - public long getTimeMillis() { - return timeMillis; - } - - public String getSubjectId() { - return subjectId; - } - - public UUID getSubjectUUID() { - return UUID.fromString(subjectUUID); - } - - public WorldPos getSubjectPos() { - return subjectPos; - } - - public ActionType getActionType() { - return actionType; - } - - public ObjectType getObjectType() { - return objectType; - } - - public String getObjectId() { - return objectId; - } - - public WorldPos getObjectPos() { - return objectPos; - } - @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; LogEntry entry = (LogEntry) o; - return timeMillis == entry.timeMillis && + return version == entry.version && + timeMillis == entry.timeMillis && + Objects.equals(gameVersion, entry.gameVersion) && Objects.equals(subjectId, entry.subjectId) && Objects.equals(subjectUUID, entry.subjectUUID) && Objects.equals(subjectPos, entry.subjectPos) && @@ -145,7 +118,7 @@ public class LogEntry { @Override public int hashCode() { - return Objects.hash(timeMillis, subjectId, subjectUUID, subjectPos, actionType, objectType, objectId, objectPos); + return Objects.hash(version, gameVersion, timeMillis, subjectId, subjectUUID, subjectPos, actionType, objectType, objectId, objectPos); } @Override diff --git a/src/main/java/com/keuin/blame/data/enums/codec/LogEntryNames.java b/src/main/java/com/keuin/blame/data/entry/LogEntryNamesV1.java index 94edf4a..d21be97 100644 --- a/src/main/java/com/keuin/blame/data/enums/codec/LogEntryNames.java +++ b/src/main/java/com/keuin/blame/data/entry/LogEntryNamesV1.java @@ -1,6 +1,6 @@ -package com.keuin.blame.data.enums.codec; +package com.keuin.blame.data.entry; -public class LogEntryNames { +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"; diff --git a/src/main/java/com/keuin/blame/data/enums/codec/LogEntryCodec.java b/src/main/java/com/keuin/blame/data/enums/codec/LogEntryCodec.java deleted file mode 100644 index 4bb61a3..0000000 --- a/src/main/java/com/keuin/blame/data/enums/codec/LogEntryCodec.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.keuin.blame.data.enums.codec; - -import com.keuin.blame.data.LogEntry; -import com.keuin.blame.data.WorldPos; -import com.keuin.blame.data.enums.ActionType; -import com.keuin.blame.data.enums.ObjectType; -import org.bson.*; -import org.bson.codecs.*; -import org.bson.codecs.configuration.CodecRegistries; -import org.bson.codecs.configuration.CodecRegistry; -import org.bson.codecs.pojo.PojoCodecProvider; - -import java.util.Objects; -import java.util.UUID; - -import static com.keuin.blame.data.enums.codec.LogEntryNames.*; -import static org.bson.codecs.configuration.CodecRegistries.fromProviders; - -public class LogEntryCodec implements CollectibleCodec<LogEntry> { - - private final Codec<Document> documentCodec; - - public LogEntryCodec() { - CodecRegistry CODEC_REGISTRY = CodecRegistries.fromRegistries( - com.mongodb.MongoClient.getDefaultCodecRegistry(), - CodecRegistries.fromCodecs( - new ActionTypeCodec(), - new ObjectTypeCodec(), - new WorldPosCodec() - ), - fromProviders(PojoCodecProvider.builder().automatic(true).build()) - ); - documentCodec = new DocumentCodec( - CODEC_REGISTRY - ); - } - - public LogEntryCodec(Codec<Document> documentCodec) { - this.documentCodec = documentCodec; - } - - - @Override - public LogEntry decode(BsonReader reader, DecoderContext decoderContext) { - Document document = documentCodec.decode(reader, decoderContext); - Integer entryVersion = document.getInteger("version"); - if (entryVersion == null) - return LogEntry.EMPTY_ENTRY; - if (Objects.equals(LogEntry.EMPTY_ENTRY.getVersion(), entryVersion)) { - LogEntry entry = new LogEntry( - document.getLong(TIMESTAMP_MILLIS), - document.getString(SUBJECT_ID), - document.get(SUBJECT_UUID, UUID.class), - WorldPos.NULL_POS, -// document.get(SUBJECT_POS, WorldPos.class), - ActionType.parseInt(document.getInteger(ACTION_TYPE)), - ObjectType.parseInt(document.getInteger(OBJECT_TYPE)), - document.getString(OBJECT_ID), - WorldPos.NULL_POS -// document.get(OBJECT_POS, WorldPos.class) - ); - return entry; - } - throw new RuntimeException(String.format("unsupported LogEntry version: %d. Perhaps your Blame is too old.", entryVersion)); - } - - @Override - public void encode(BsonWriter writer, LogEntry value, EncoderContext encoderContext) { - Document document = new Document(); - - document.put(VERSION, value.getVersion()); - document.put(GAME_VERSION, value.getGameVersion()); - document.put(TIMESTAMP_MILLIS, value.getTimeMillis()); - document.put(SUBJECT_ID, value.getSubjectId()); - document.put(SUBJECT_UUID, value.getSubjectUUID()); -// document.put(SUBJECT_POS, value.getSubjectPos()); - document.put(ACTION_TYPE, value.getActionType().getValue()); - document.put(OBJECT_TYPE, value.getObjectType().getValue()); - document.put(OBJECT_ID, value.getObjectId()); -// document.put(OBJECT_POS, value.getObjectPos()); - - documentCodec.encode(writer, document, encoderContext); - } - - @Override - public Class<LogEntry> getEncoderClass() { - return LogEntry.class; - } - - @Override - public LogEntry generateIdIfAbsentFromDocument(LogEntry document) { - return document; - } - - @Override - public boolean documentHasId(LogEntry document) { - return document.getObjectId() != null; - } - - @Override - public BsonValue getDocumentId(LogEntry document) { - return new BsonString(document.getObjectId()); - } -} diff --git a/src/main/java/com/keuin/blame/lookup/AbstractLookupFilter.java b/src/main/java/com/keuin/blame/lookup/AbstractLookupFilter.java index f8774e0..4c18442 100644 --- a/src/main/java/com/keuin/blame/lookup/AbstractLookupFilter.java +++ b/src/main/java/com/keuin/blame/lookup/AbstractLookupFilter.java @@ -1,6 +1,6 @@ package com.keuin.blame.lookup; -import com.keuin.blame.data.LogEntry; +import com.keuin.blame.data.entry.LogEntry; import com.mongodb.client.FindIterable; public abstract class AbstractLookupFilter { diff --git a/src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java b/src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java index 67a564f..a4b2475 100644 --- a/src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java +++ b/src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java @@ -1,7 +1,8 @@ package com.keuin.blame.lookup; -import com.keuin.blame.data.LogEntry; import com.keuin.blame.data.WorldPos; +import com.keuin.blame.data.entry.LogEntry; +import com.keuin.blame.data.entry.LogEntryNamesV1; import com.mongodb.client.FindIterable; import com.mongodb.client.model.Filters; @@ -15,8 +16,8 @@ public class BlockPosLookupFilter extends AbstractLookupFilter { @Override FindIterable<LogEntry> find(FindIterable<LogEntry> iterable) { return iterable.filter(Filters.and( - Filters.eq("version", 1), - Filters.eq("object_pos", blockPos) + Filters.eq(LogEntryNamesV1.VERSION, 1), + Filters.eq(LogEntryNamesV1.OBJECT_POS, blockPos) )); } } diff --git a/src/main/java/com/keuin/blame/lookup/DummyFilter.java b/src/main/java/com/keuin/blame/lookup/DummyFilter.java new file mode 100644 index 0000000..0932506 --- /dev/null +++ b/src/main/java/com/keuin/blame/lookup/DummyFilter.java @@ -0,0 +1,11 @@ +package com.keuin.blame.lookup; + +import com.keuin.blame.data.entry.LogEntry; +import com.mongodb.client.FindIterable; + +public class DummyFilter extends AbstractLookupFilter { + @Override + protected FindIterable<LogEntry> find(FindIterable<LogEntry> iterable) { + return iterable; + } +} diff --git a/src/main/java/com/keuin/blame/lookup/LookupCallback.java b/src/main/java/com/keuin/blame/lookup/LookupCallback.java index eba954c..3c203b2 100644 --- a/src/main/java/com/keuin/blame/lookup/LookupCallback.java +++ b/src/main/java/com/keuin/blame/lookup/LookupCallback.java @@ -1,6 +1,6 @@ package com.keuin.blame.lookup; -import com.keuin.blame.data.LogEntry; +import com.keuin.blame.data.entry.LogEntry; public interface LookupCallback { void onLookupFinishes(Iterable<LogEntry> logEntries); diff --git a/src/main/java/com/keuin/blame/lookup/LookupWorker.java b/src/main/java/com/keuin/blame/lookup/LookupWorker.java index a8e5b7d..1442615 100644 --- a/src/main/java/com/keuin/blame/lookup/LookupWorker.java +++ b/src/main/java/com/keuin/blame/lookup/LookupWorker.java @@ -1,23 +1,14 @@ package com.keuin.blame.lookup; -import com.keuin.blame.Blame; -import com.keuin.blame.config.MongoConfig; -import com.keuin.blame.data.LogEntry; -import com.keuin.blame.data.enums.codec.ActionTypeCodec; -import com.keuin.blame.data.enums.codec.ObjectTypeCodec; -import com.keuin.blame.data.enums.codec.WorldPosCodec; +import com.keuin.blame.data.entry.LogEntry; import com.keuin.blame.util.DatabaseUtil; -import com.mongodb.ConnectionString; -import com.mongodb.MongoClientSettings; import com.mongodb.client.*; -import org.bson.codecs.configuration.CodecRegistries; -import org.bson.codecs.configuration.CodecRegistry; -import org.bson.codecs.pojo.PojoCodecProvider; +import com.mongodb.client.model.Sorts; import java.util.concurrent.BlockingQueue; import java.util.logging.Logger; -import static org.bson.codecs.configuration.CodecRegistries.fromProviders; +import static com.keuin.blame.util.DatabaseUtil.CLIENT_SETTINGS; public class LookupWorker extends Thread { @@ -25,22 +16,6 @@ public class LookupWorker extends Thread { private final BlockingQueue<LookupFilterWithCallback> queue; private boolean running = true; - private static final MongoConfig MONGO_CONFIG = Blame.config.getMongoConfig(); - private static final CodecRegistry CODEC_REGISTRY = CodecRegistries.fromRegistries( - com.mongodb.MongoClient.getDefaultCodecRegistry(), - CodecRegistries.fromCodecs( - new ActionTypeCodec(), - new ObjectTypeCodec(), - new WorldPosCodec() -// new LogEntryCodec() - ), - fromProviders(PojoCodecProvider.builder().automatic(true).build()) - ); - private static final MongoClientSettings CLIENT_SETTINGS = MongoClientSettings.builder() - .applyConnectionString(new ConnectionString(MONGO_CONFIG.getAddress())) - .codecRegistry(CODEC_REGISTRY) - .build(); - public LookupWorker(int id, BlockingQueue<LookupFilterWithCallback> queue) { this.queue = queue; this.logger = Logger.getLogger(String.format("LookupWorker-%d", id)); @@ -67,10 +42,10 @@ public class LookupWorker extends Thread { AbstractLookupFilter filter = item.getFilter(); time = System.currentTimeMillis(); -// FindIterable<LogEntry> find = filter.find( -// collection.find().sort(Sorts.descending("timestamp_millis")) -// ); - FindIterable<LogEntry> find = collection.find();//.sort(Sorts.descending("timestamp_millis")); + FindIterable<LogEntry> find = filter.find( + collection.find().sort(Sorts.descending("timestamp_millis")) + ); +// FindIterable<LogEntry> find = collection.find();//.sort(Sorts.descending("timestamp_millis")); time = System.currentTimeMillis() - time; logger.info(String.format("Lookup finished in %d ms.", time)); callback.onLookupFinishes(find); diff --git a/src/main/java/com/keuin/blame/lookup/TestableFilter.java b/src/main/java/com/keuin/blame/lookup/TestableFilter.java deleted file mode 100644 index 8160234..0000000 --- a/src/main/java/com/keuin/blame/lookup/TestableFilter.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.keuin.blame.lookup; - -import com.keuin.blame.data.LogEntry; -import com.keuin.blame.data.enums.ActionType; -import com.keuin.blame.data.enums.codec.LogEntryNames; -import com.mongodb.client.FindIterable; -import com.mongodb.client.model.Filters; - -public class TestableFilter extends AbstractLookupFilter { - @Override - protected FindIterable<LogEntry> find(FindIterable<LogEntry> iterable) { - return iterable.filter(Filters.eq(LogEntryNames.ACTION_TYPE, ActionType.NULL.getValue())); - } -} diff --git a/src/main/java/com/keuin/blame/test/TestDatabase.java b/src/main/java/com/keuin/blame/test/TestDatabase.java index 7d817de..140ca77 100644 --- a/src/main/java/com/keuin/blame/test/TestDatabase.java +++ b/src/main/java/com/keuin/blame/test/TestDatabase.java @@ -2,20 +2,21 @@ package com.keuin.blame.test; import com.keuin.blame.Blame; import com.keuin.blame.SubmitWorker; -import com.keuin.blame.data.LogEntry; import com.keuin.blame.data.WorldPos; +import com.keuin.blame.data.entry.LogEntry; import com.keuin.blame.data.enums.ActionType; import com.keuin.blame.data.enums.ObjectType; +import com.keuin.blame.lookup.DummyFilter; import com.keuin.blame.lookup.LookupCallback; import com.keuin.blame.lookup.LookupManager; -import com.keuin.blame.lookup.TestableFilter; import org.junit.Before; import org.junit.Test; import java.util.Objects; import java.util.UUID; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class TestDatabase { @@ -27,7 +28,7 @@ public class TestDatabase { @Test public void testCreateEmptyEntry() { try { - SubmitWorker.INSTANCE.submit(LogEntry.EMPTY_ENTRY); + SubmitWorker.INSTANCE.submit(new LogEntry()); Thread.sleep(2000); } catch (Exception e) { fail(); @@ -37,9 +38,8 @@ public class TestDatabase { @Test public void testCreateNonEmptyEntry() { try { - long timeMillis = 10102020; SubmitWorker.INSTANCE.submit(new LogEntry( - timeMillis, + 111111111, "subject-id", UUID.randomUUID(), new WorldPos("world", 1, 2, 3), @@ -58,15 +58,24 @@ public class TestDatabase { public void testLookupAfterInserted() { try { final boolean[] success = {false}; - SubmitWorker.INSTANCE.submit(LogEntry.EMPTY_ENTRY); + LogEntry entry = new LogEntry( + 222222222, + "subject-id", + UUID.randomUUID(), + new WorldPos("world", 1, 2, 3), + ActionType.BLOCK_USE, + ObjectType.BLOCK, + "object-id", + new WorldPos("world", 4, 5, 6) + ); + SubmitWorker.INSTANCE.submit(entry); Thread.sleep(2000); - LookupManager.INSTANCE.lookup(new TestableFilter(), new LookupCallback() { + LookupManager.INSTANCE.lookup(new DummyFilter(), new LookupCallback() { @Override public void onLookupFinishes(Iterable<LogEntry> logEntries) { - for (LogEntry entry : logEntries) { - System.out.println(entry); - assertEquals(entry, LogEntry.EMPTY_ENTRY); - success[0] = Objects.equals(entry, LogEntry.EMPTY_ENTRY); + for (LogEntry e : logEntries) { + System.out.println(e); + success[0] |= Objects.equals(e, entry); } } }); diff --git a/src/main/java/com/keuin/blame/util/DatabaseUtil.java b/src/main/java/com/keuin/blame/util/DatabaseUtil.java index 4b1d3d4..d01ffdd 100644 --- a/src/main/java/com/keuin/blame/util/DatabaseUtil.java +++ b/src/main/java/com/keuin/blame/util/DatabaseUtil.java @@ -15,11 +15,6 @@ import static org.bson.codecs.configuration.CodecRegistries.fromProviders; public class DatabaseUtil { -// static { -// BSON.addEncodingHook(ActionType.class, new ActionTypeTransformer()); -// BSON.addEncodingHook(ObjectType.class, new ObjectTypeTransformer()); -// } - public static final MongoConfig MONGO_CONFIG = Blame.config.getMongoConfig(); public static final CodecRegistry CODEC_REGISTRY = CodecRegistries.fromRegistries( com.mongodb.MongoClient.getDefaultCodecRegistry(), @@ -27,7 +22,6 @@ public class DatabaseUtil { new ActionTypeCodec(), new ObjectTypeCodec(), new WorldPosCodec() -// new LogEntryCodec() ), fromProviders(PojoCodecProvider.builder().automatic(true).build()) ); |