From 5b76ebe34e5237f48a12baa70b33105c02fc9d5f Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 24 Dec 2020 21:11:30 +0800 Subject: clean codes, small refactor. --- src/main/java/com/keuin/blame/EventHandler.java | 2 +- src/main/java/com/keuin/blame/SubmitWorker.java | 2 +- .../com/keuin/blame/command/BlameBlockCommand.java | 2 +- src/main/java/com/keuin/blame/data/LogEntry.java | 169 --------------------- .../java/com/keuin/blame/data/LogEntryFactory.java | 1 + .../java/com/keuin/blame/data/entry/LogEntry.java | 142 +++++++++++++++++ .../keuin/blame/data/entry/LogEntryNamesV1.java | 14 ++ .../blame/data/enums/codec/LogEntryCodec.java | 104 ------------- .../blame/data/enums/codec/LogEntryNames.java | 14 -- .../keuin/blame/lookup/AbstractLookupFilter.java | 2 +- .../keuin/blame/lookup/BlockPosLookupFilter.java | 7 +- .../java/com/keuin/blame/lookup/DummyFilter.java | 11 ++ .../com/keuin/blame/lookup/LookupCallback.java | 2 +- .../java/com/keuin/blame/lookup/LookupWorker.java | 39 +---- .../com/keuin/blame/lookup/TestableFilter.java | 14 -- .../java/com/keuin/blame/test/TestDatabase.java | 33 ++-- .../java/com/keuin/blame/util/DatabaseUtil.java | 6 - 17 files changed, 205 insertions(+), 359 deletions(-) delete mode 100644 src/main/java/com/keuin/blame/data/LogEntry.java create mode 100644 src/main/java/com/keuin/blame/data/entry/LogEntry.java create mode 100644 src/main/java/com/keuin/blame/data/entry/LogEntryNamesV1.java delete mode 100644 src/main/java/com/keuin/blame/data/enums/codec/LogEntryCodec.java delete mode 100644 src/main/java/com/keuin/blame/data/enums/codec/LogEntryNames.java create mode 100644 src/main/java/com/keuin/blame/lookup/DummyFilter.java delete mode 100644 src/main/java/com/keuin/blame/lookup/TestableFilter.java (limited to 'src/main') 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/LogEntry.java b/src/main/java/com/keuin/blame/data/LogEntry.java deleted file mode 100644 index e178dc2..0000000 --- a/src/main/java/com/keuin/blame/data/LogEntry.java +++ /dev/null @@ -1,169 +0,0 @@ -package com.keuin.blame.data; - -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 java.util.Objects; -import java.util.UUID; - -public class LogEntry { - - // { - // "version": 1, // int - // "subject": { - // "uuid": player_uuid_bytes, // bytes - // "id": player_id, // string - // "pos": { - // "world": world_id, // string - // "x": pos_x, // float - // "y": pos_y, // float - // "z": pos_z, // float - // } - // }, - // "action": BLOCK_BREAK | BLOCK_PLACE | BLOCK_USE | ENTITY_USE | ENTITY_ATTACK | ITEM_USE, // int - // "object": { - // "type": OBJECT_BLOCK | OBJECT_ENTITY, // int - // "id": object_id, // string - // "pos": { - // "world": world_id, // string - // "x": pos_x, // float - // "y": pos_y, // float - // "z": pos_z, // float - // } - // } - //} - - // @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() { - } - - 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 (subjectUUID == null) - throw new IllegalArgumentException("subjectUUID cannot be null"); - if (subjectPos == null) - throw new IllegalArgumentException("subjectPos cannot be null"); - if (actionType == null) - throw new IllegalArgumentException("actionType cannot be null"); - if (objectType == null) - throw new IllegalArgumentException("objectType cannot be null"); - if (objectId == null) - throw new IllegalArgumentException("objectId cannot be null"); - if (objectPos == null) - throw new IllegalArgumentException("objectPos cannot be null"); - - this.timeMillis = timeMillis; - this.subjectUUID = subjectUUID.toString(); - this.subjectPos = subjectPos; - this.actionType = actionType; - this.objectType = objectType; - this.objectId = objectId; - 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 && - Objects.equals(subjectId, entry.subjectId) && - Objects.equals(subjectUUID, entry.subjectUUID) && - Objects.equals(subjectPos, entry.subjectPos) && - actionType == entry.actionType && - objectType == entry.objectType && - Objects.equals(objectId, entry.objectId) && - Objects.equals(objectPos, entry.objectPos); - } - - @Override - public int hashCode() { - return Objects.hash(timeMillis, subjectId, subjectUUID, subjectPos, actionType, objectType, objectId, objectPos); - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("Time: ").append(PrettyUtil.timestampToString(timeMillis)).append("\n"); - builder.append("Subject: ").append(subjectId).append("{").append(subjectUUID).append("}@") - .append(subjectPos.toString()) - .append("\n"); - builder.append("Action: ").append(actionType.toString()).append("\n"); - builder.append("Object: ").append(objectType.toString()).append("[").append(objectId).append("]@") - .append(objectPos.toString()) - .append("\n"); - builder.append("(entryVersion: ") - .append(version) - .append(", gameVersion:") - .append(gameVersion) - .append(")"); - return builder.toString(); - } -} 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/entry/LogEntry.java b/src/main/java/com/keuin/blame/data/entry/LogEntry.java new file mode 100644 index 0000000..586ba91 --- /dev/null +++ b/src/main/java/com/keuin/blame/data/entry/LogEntry.java @@ -0,0 +1,142 @@ +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 { + + // { + // "version": 1, // int + // "subject": { + // "uuid": player_uuid_bytes, // bytes + // "id": player_id, // string + // "pos": { + // "world": world_id, // string + // "x": pos_x, // float + // "y": pos_y, // float + // "z": pos_z, // float + // } + // }, + // "action": BLOCK_BREAK | BLOCK_PLACE | BLOCK_USE | ENTITY_USE | ENTITY_ATTACK | ITEM_USE, // int + // "object": { + // "type": OBJECT_BLOCK | OBJECT_ENTITY, // int + // "id": object_id, // string + // "pos": { + // "world": world_id, // string + // "x": pos_x, // float + // "y": pos_y, // float + // "z": pos_z, // float + // } + // } + //} + + @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) { + if (subjectId == null) + throw new IllegalArgumentException("subjectId cannot be null"); + if (subjectUUID == null) + throw new IllegalArgumentException("subjectUUID cannot be null"); + if (subjectPos == null) + throw new IllegalArgumentException("subjectPos cannot be null"); + if (actionType == null) + throw new IllegalArgumentException("actionType cannot be null"); + if (objectType == null) + throw new IllegalArgumentException("objectType cannot be null"); + if (objectId == null) + throw new IllegalArgumentException("objectId cannot be null"); + if (objectPos == null) + throw new IllegalArgumentException("objectPos cannot be null"); + + this.subjectId = subjectId; + this.timeMillis = timeMillis; + this.subjectUUID = subjectUUID; + this.subjectPos = subjectPos; + this.actionType = actionType; + this.objectType = objectType; + this.objectId = objectId; + this.objectPos = 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 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) && + actionType == entry.actionType && + objectType == entry.objectType && + Objects.equals(objectId, entry.objectId) && + Objects.equals(objectPos, entry.objectPos); + } + + @Override + public int hashCode() { + return Objects.hash(version, gameVersion, timeMillis, subjectId, subjectUUID, subjectPos, actionType, objectType, objectId, objectPos); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Time: ").append(PrettyUtil.timestampToString(timeMillis)).append("\n"); + builder.append("Subject: ").append(subjectId).append("{").append(subjectUUID).append("}@") + .append(subjectPos.toString()) + .append("\n"); + builder.append("Action: ").append(actionType.toString()).append("\n"); + builder.append("Object: ").append(objectType.toString()).append("[").append(objectId).append("]@") + .append(objectPos.toString()) + .append("\n"); + builder.append("(entryVersion: ") + .append(version) + .append(", gameVersion:") + .append(gameVersion) + .append(")"); + return builder.toString(); + } +} diff --git a/src/main/java/com/keuin/blame/data/entry/LogEntryNamesV1.java b/src/main/java/com/keuin/blame/data/entry/LogEntryNamesV1.java new file mode 100644 index 0000000..d21be97 --- /dev/null +++ b/src/main/java/com/keuin/blame/data/entry/LogEntryNamesV1.java @@ -0,0 +1,14 @@ +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/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 { - - private final Codec 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 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 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/data/enums/codec/LogEntryNames.java b/src/main/java/com/keuin/blame/data/enums/codec/LogEntryNames.java deleted file mode 100644 index 94edf4a..0000000 --- a/src/main/java/com/keuin/blame/data/enums/codec/LogEntryNames.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.keuin.blame.data.enums.codec; - -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"; -} 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 find(FindIterable 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 find(FindIterable 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 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 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 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 find = filter.find( -// collection.find().sort(Sorts.descending("timestamp_millis")) -// ); - FindIterable find = collection.find();//.sort(Sorts.descending("timestamp_millis")); + FindIterable find = filter.find( + collection.find().sort(Sorts.descending("timestamp_millis")) + ); +// FindIterable 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 find(FindIterable 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 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()) ); -- cgit v1.2.3