From b41da972f80bd80b78fb80db18b1aabe8f38794f Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 16 Apr 2014 15:18:00 +0200 Subject: [PATCH] Index the player name for fast name --> id lookup. --- .../massivecraft/mcore/MCoreMPlayerColl.java | 33 ++++++--- .../mcore/store/IndexUniqueField.java | 70 +++++++++++++++++++ 2 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 src/com/massivecraft/mcore/store/IndexUniqueField.java diff --git a/src/com/massivecraft/mcore/MCoreMPlayerColl.java b/src/com/massivecraft/mcore/MCoreMPlayerColl.java index 4a18fed8..2003bd92 100644 --- a/src/com/massivecraft/mcore/MCoreMPlayerColl.java +++ b/src/com/massivecraft/mcore/MCoreMPlayerColl.java @@ -1,10 +1,12 @@ package com.massivecraft.mcore; +import java.util.TreeMap; import java.util.UUID; import org.bukkit.entity.Player; import com.massivecraft.mcore.store.Coll; +import com.massivecraft.mcore.store.IndexUniqueField; import com.massivecraft.mcore.store.MStore; import com.massivecraft.mcore.util.MUtil; @@ -21,10 +23,31 @@ public class MCoreMPlayerColl extends Coll super("mcore_mplayer", MCoreMPlayer.class, MStore.getDb(), MCore.get(), false, false, true); } + // -------------------------------------------- // + // FIELD + // -------------------------------------------- // + + private IndexUniqueField indexName = new IndexUniqueField(new TreeMap(String.CASE_INSENSITIVE_ORDER)); + public IndexUniqueField getIndexName() { return this.indexName; } + // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // + @Override + public void postAttach(MCoreMPlayer entity, String id) + { + super.postAttach(entity, id); + this.getIndexName().update(entity, entity.getName()); + } + + @Override + public void postDetach(MCoreMPlayer entity, String id) + { + super.postDetach(entity, id); + this.getIndexName().removeObject(entity); + } + @Override public String fixId(Object oid) { @@ -43,14 +66,8 @@ public class MCoreMPlayerColl extends Coll // Handle Player Name if (MUtil.isValidPlayerName(string)) { - // TODO: Improve the speed of this using an index! - for (MCoreMPlayer mplayer : this.getAll()) - { - String name = mplayer.getName(); - if (name == null) continue; - if (!string.equals(name.toLowerCase())) continue; - return mplayer.getId(); - } + MCoreMPlayer mplayer = this.getIndexName().getObject(string); + if (mplayer != null) return mplayer.getId(); } return string; diff --git a/src/com/massivecraft/mcore/store/IndexUniqueField.java b/src/com/massivecraft/mcore/store/IndexUniqueField.java new file mode 100644 index 00000000..035d1bfc --- /dev/null +++ b/src/com/massivecraft/mcore/store/IndexUniqueField.java @@ -0,0 +1,70 @@ +package com.massivecraft.mcore.store; + +import java.util.HashMap; +import java.util.Map; + +public class IndexUniqueField +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private Map f2o; + private Map o2f; + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public IndexUniqueField(Map map) + { + this.f2o = map; + this.o2f = new HashMap(); + } + + // -------------------------------------------- // + // STUFF + // -------------------------------------------- // + + public void update(O object, F field) + { + if (object == null) return; + if (field == null) return; + + this.f2o.put(field, object); + this.o2f.put(object, field); + } + + public O removeField(F field) + { + if (field == null) return null; + + O object = this.f2o.remove(field); + if (object != null) this.o2f.remove(object); + return object; + } + + public F removeObject(O object) + { + if (object == null) return null; + + F field = this.o2f.remove(object); + if (field != null) this.f2o.remove(object); + return field; + } + + public O getObject(F field) + { + if (field == null) return null; + + return this.f2o.get(field); + } + + public F getField(O object) + { + if (object == null) return null; + + return this.o2f.get(object); + } + +}