From 7b9620d38b99f0bb29aa7c161dd49e381feb0b73 Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Fri, 15 May 2015 14:57:20 +0200 Subject: [PATCH] Don't sort Colls when not required --- .../massivecraft/massivecore/store/Coll.java | 20 ++++----- .../massivecore/store/ComparatorEntityId.java | 41 +++++++++++++++++++ .../massivecore/store/Entity.java | 31 +------------- 3 files changed, 50 insertions(+), 42 deletions(-) create mode 100644 src/com/massivecraft/massivecore/store/ComparatorEntityId.java diff --git a/src/com/massivecraft/massivecore/store/Coll.java b/src/com/massivecraft/massivecore/store/Coll.java index b4111b49..1981f2eb 100644 --- a/src/com/massivecraft/massivecore/store/Coll.java +++ b/src/com/massivecraft/massivecore/store/Coll.java @@ -11,12 +11,11 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentSkipListMap; -import java.util.concurrent.ConcurrentSkipListSet; import org.bukkit.plugin.Plugin; -import com.massivecraft.massivecore.HashCodeComparator; import com.massivecraft.massivecore.MassiveCore; import com.massivecraft.massivecore.MassivePlugin; import com.massivecraft.massivecore.NaturalOrderComparator; @@ -906,21 +905,16 @@ public class Coll implements CollInterface this.collDriverObject = db.createCollDriverObject(this); // STORAGE - if (entityComparator == null && !Comparable.class.isAssignableFrom(entityClass)) - { - // Avoid "Classname cannot be cast to java.lang.Comparable" error in ConcurrentSkipListMap - entityComparator = HashCodeComparator.get(); - } - this.id2entity = new ConcurrentSkipListMap(idComparator); - this.entity2id = new ConcurrentSkipListMap(entityComparator); + this.id2entity = entityComparator != null ? new ConcurrentSkipListMap(idComparator) : new ConcurrentHashMap(); + this.entity2id = entityComparator != null ? new ConcurrentSkipListMap(entityComparator) : new ConcurrentHashMap(); // IDENTIFIED MODIFICATIONS - this.identifiedModifications = new ConcurrentSkipListMap(idComparator); + this.identifiedModifications = new ConcurrentHashMap(); // SYNCLOG - this.lastMtime = new ConcurrentSkipListMap(idComparator); - this.lastRaw = new ConcurrentSkipListMap(idComparator); - this.lastDefault = new ConcurrentSkipListSet(idComparator); + this.lastMtime = new ConcurrentHashMap(); + this.lastRaw = new ConcurrentHashMap(); + this.lastDefault = Collections.newSetFromMap(new ConcurrentHashMap()); final Coll me = this; this.tickTask = new Runnable() diff --git a/src/com/massivecraft/massivecore/store/ComparatorEntityId.java b/src/com/massivecraft/massivecore/store/ComparatorEntityId.java new file mode 100644 index 00000000..01296020 --- /dev/null +++ b/src/com/massivecraft/massivecore/store/ComparatorEntityId.java @@ -0,0 +1,41 @@ +package com.massivecraft.massivecore.store; + +import java.util.Comparator; + +import com.massivecraft.massivecore.NaturalOrderComparator; + +public class ComparatorEntityId implements Comparator> +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static ComparatorEntityId i = new ComparatorEntityId(); + public static ComparatorEntityId get() { return i; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public int compare(Entity e1, Entity e2) + { + if (e1 == null && e2 == null) return 0; + if (e1 == null) return -1; + if (e2 == null) return +1; + + if (e1.equals(e2)) return 0; + + String id1 = e1.getId(); + String id2 = e2.getId(); + + int ret = NaturalOrderComparator.get().compare(id1, id2); + + // Only return 0 if they are the same. + // We avoid that with this ugly solution. + if (ret == 0) ret = -1; + + return ret; + } + +} diff --git a/src/com/massivecraft/massivecore/store/Entity.java b/src/com/massivecraft/massivecore/store/Entity.java index 6d0df5e8..0b831306 100644 --- a/src/com/massivecraft/massivecore/store/Entity.java +++ b/src/com/massivecraft/massivecore/store/Entity.java @@ -1,7 +1,6 @@ package com.massivecraft.massivecore.store; import com.massivecraft.massivecore.MassiveCore; -import com.massivecraft.massivecore.NaturalOrderComparator; import com.massivecraft.massivecore.store.accessor.Accessor; import com.massivecraft.massivecore.xlib.gson.Gson; @@ -13,7 +12,7 @@ import com.massivecraft.massivecore.xlib.gson.Gson; // Self referencing generic. // http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ206 -public abstract class Entity> implements Comparable +public abstract class Entity> { // -------------------------------------------- // // COLL & ID @@ -156,33 +155,7 @@ public abstract class Entity> implements Comparable // -------------------------------------------- // // STANDARDS // -------------------------------------------- // - - @Override - public int compareTo(E that) - { - if (that == null) throw new NullPointerException("You cannot compare with null"); - - if (this.equals(that)) return 0; - - String thisId = this.getId(); - String thatId = that.getId(); - - if (thisId == null) return -1; - if (thatId == null) return +1; - - int ret = NaturalOrderComparator.get().compare(thisId, thatId); - - // The id's may be the same if these are objects from different collections - // We avoid zero in an ugly way like this. - // TODO: Improve by comparing collections and then databases. - if (ret == 0) - { - ret = -1; - } - - return ret; - } - + @Override public String toString() {