Index the player name for fast name --> id lookup.

This commit is contained in:
Olof Larsson 2014-04-16 15:18:00 +02:00
parent c6fa21c247
commit b41da972f8
2 changed files with 95 additions and 8 deletions

View File

@ -1,10 +1,12 @@
package com.massivecraft.mcore; package com.massivecraft.mcore;
import java.util.TreeMap;
import java.util.UUID; import java.util.UUID;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.massivecraft.mcore.store.Coll; import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.store.IndexUniqueField;
import com.massivecraft.mcore.store.MStore; import com.massivecraft.mcore.store.MStore;
import com.massivecraft.mcore.util.MUtil; import com.massivecraft.mcore.util.MUtil;
@ -21,10 +23,31 @@ public class MCoreMPlayerColl extends Coll<MCoreMPlayer>
super("mcore_mplayer", MCoreMPlayer.class, MStore.getDb(), MCore.get(), false, false, true); super("mcore_mplayer", MCoreMPlayer.class, MStore.getDb(), MCore.get(), false, false, true);
} }
// -------------------------------------------- //
// FIELD
// -------------------------------------------- //
private IndexUniqueField<String, MCoreMPlayer> indexName = new IndexUniqueField<String, MCoreMPlayer>(new TreeMap<String, MCoreMPlayer>(String.CASE_INSENSITIVE_ORDER));
public IndexUniqueField<String, MCoreMPlayer> getIndexName() { return this.indexName; }
// -------------------------------------------- // // -------------------------------------------- //
// OVERRIDE // 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 @Override
public String fixId(Object oid) public String fixId(Object oid)
{ {
@ -43,14 +66,8 @@ public class MCoreMPlayerColl extends Coll<MCoreMPlayer>
// Handle Player Name // Handle Player Name
if (MUtil.isValidPlayerName(string)) if (MUtil.isValidPlayerName(string))
{ {
// TODO: Improve the speed of this using an index! MCoreMPlayer mplayer = this.getIndexName().getObject(string);
for (MCoreMPlayer mplayer : this.getAll()) if (mplayer != null) return mplayer.getId();
{
String name = mplayer.getName();
if (name == null) continue;
if (!string.equals(name.toLowerCase())) continue;
return mplayer.getId();
}
} }
return string; return string;

View File

@ -0,0 +1,70 @@
package com.massivecraft.mcore.store;
import java.util.HashMap;
import java.util.Map;
public class IndexUniqueField<F, O>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private Map<F, O> f2o;
private Map<O, F> o2f;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public IndexUniqueField(Map<F, O> map)
{
this.f2o = map;
this.o2f = new HashMap<O, F>();
}
// -------------------------------------------- //
// 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);
}
}