From 6ad243c01482710b01289ab861f4915cc9b30c62 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 17 Apr 2013 14:44:08 +0200 Subject: [PATCH] Optimized indexer but that was not enough. Added back the cached index again but with new technology. --- src/com/massivecraft/factions/FPlayer.java | 76 +++++++++++++++++-- .../massivecraft/factions/FPlayerColl.java | 31 ++++++++ src/com/massivecraft/factions/Faction.java | 68 +++++++++++------ .../massivecraft/factions/FactionColl.java | 12 ++- .../factions/FactionEqualsPredictate.java | 2 +- 5 files changed, 157 insertions(+), 32 deletions(-) diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index f058677b..078f780f 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -40,7 +40,7 @@ public class FPlayer extends SenderEntity implements EconomyParticipato @Override public FPlayer load(FPlayer that) { - this.factionId = that.factionId; + this.setFactionId(that.factionId); this.role = that.role; this.title = that.title; this.power = that.power; @@ -65,17 +65,79 @@ public class FPlayer extends SenderEntity implements EconomyParticipato // -------------------------------------------- // // FIELD: factionId - private String factionId; - public Faction getFaction() { if(this.factionId == null) {return null;} return FactionColl.get().get(this.factionId); } - public String getFactionId() { return this.factionId; } - public boolean hasFaction() { return this.factionId != null && ! factionId.equals(Const.FACTIONID_NONE); } - public void setFaction(Faction faction) + // TODO: Ensure this one always is null in the nofaction case and never actually the ID of the nofaction-faction. + // TODO: The getFactionId should however NEVER return null! + + private String factionId = null; + + // The get methods never return null. + public String getFactionId() { - this.factionId = faction.getId(); + if (this.factionId == null) return Const.FACTIONID_NONE; + return this.factionId; + } + public Faction getFaction() + { + Faction ret = FactionColl.get().get(this.getFactionId()); + if (ret == null) ret = FactionColl.get().get(Const.FACTIONID_NONE); + return ret; + } + + // TODO: When is this one used? + public boolean hasFaction() + { + // TODO: Broken logic + return !this.getFactionId().equals(Const.FACTIONID_NONE); + } + + + // This setter is so long because it search for default/null case and takes care of updating the faction member index + public void setFactionId(String factionId) + { + // Avoid null input + if (factionId == null) factionId = Const.FACTIONID_NONE; + + // Get the old value + String oldFactionId = this.getFactionId(); + + // Ignore nochange + if (factionId.equals(oldFactionId)) return; + + // Apply change + if (factionId.equals(Const.FACTIONID_NONE)) + { + this.factionId = null; + } + else + { + this.factionId = factionId; + } + + // Next we must be attached and inited + if (!this.attached()) return; + if (!this.getColl().inited()) return; + + // Spout Derp SpoutFeatures.updateTitle(this, null); SpoutFeatures.updateTitle(null, this); + + // Update index + Faction oldFaction = FactionColl.get().get(oldFactionId); + Faction faction = FactionColl.get().get(factionId); + + oldFaction.fplayers.remove(this); + faction.fplayers.add(this); + + // Mark as changed + this.changed(); } + public void setFaction(Faction faction) + { + this.setFactionId(faction.getId()); + } + + // FIELD: role private Rel role; public Rel getRole() { return this.role; } diff --git a/src/com/massivecraft/factions/FPlayerColl.java b/src/com/massivecraft/factions/FPlayerColl.java index 91db7bf0..af86c37a 100644 --- a/src/com/massivecraft/factions/FPlayerColl.java +++ b/src/com/massivecraft/factions/FPlayerColl.java @@ -62,6 +62,37 @@ public class FPlayerColl extends SenderColl oldFile.renameTo(newFile); } + @Override + protected synchronized String attach(FPlayer entity, Object oid, boolean noteChange) + { + String ret = super.attach(entity, oid, noteChange); + + // If inited ... + if (!this.inited()) return ret; + + // ... update the index. + Faction faction = entity.getFaction(); + faction.fplayers.add(entity); + + return ret; + } + + @Override + public FPlayer detachId(Object oid) + { + FPlayer ret = super.detachId(oid); + if (ret == null) return null; + + // If inited ... + if (!this.inited()) return ret; + + // ... update the index. + Faction faction = ret.getFaction(); + faction.fplayers.remove(ret); + + return ret; + } + // -------------------------------------------- // // EXTRAS // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/Faction.java b/src/com/massivecraft/factions/Faction.java index 518ee295..618368f7 100644 --- a/src/com/massivecraft/factions/Faction.java +++ b/src/com/massivecraft/factions/Faction.java @@ -16,6 +16,7 @@ import com.massivecraft.factions.util.*; import com.massivecraft.mcore.mixin.Mixin; import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.store.Entity; +import com.massivecraft.mcore.util.MUtil; import com.massivecraft.mcore.util.SenderUtil; import com.massivecraft.mcore.xlib.gson.annotations.SerializedName; @@ -664,10 +665,10 @@ public class Faction extends Entity implements EconomyParticipator return RelationUtil.getColorOfThatToMe(this, observer); } - // TODO: Implement a has enough feature. // -------------------------------------------- // // POWER // -------------------------------------------- // + // TODO: Implement a has enough feature. public double getPower() { @@ -685,7 +686,7 @@ public class Faction extends Entity implements EconomyParticipator { ret = ConfServer.powerFactionMax; } - return ret + this.powerBoost; + return ret + this.getPowerBoost(); } public double getPowerMax() @@ -704,7 +705,7 @@ public class Faction extends Entity implements EconomyParticipator { ret = ConfServer.powerFactionMax; } - return ret + this.powerBoost; + return ret + this.getPowerBoost(); } public int getPowerRounded() @@ -735,48 +736,71 @@ public class Faction extends Entity implements EconomyParticipator // FOREIGN KEYS: FPLAYERS // -------------------------------------------- // - public List getFPlayers() + // TODO: With this approach null must be used as default always. + // TODO: Take a moment and reflect upon the consequenses eeeeeeh... + // TODO: This one may be to slow after all :/ Thus I must maintain an index. + + protected transient List fplayers = null; + public void reindexFPlayers() { - List ret = new ArrayList(); + this.fplayers = new ArrayList(); + + String factionId = this.getId(); + if (factionId == null) return; + for (FPlayer fplayer : FPlayerColl.get().getAll()) { - if (fplayer.getFaction() != this) continue; - ret.add(fplayer); + if (!MUtil.equals(factionId, fplayer.getFactionId())) continue; + this.fplayers.add(fplayer); } - return ret; + } + + public List getFPlayers() + { + return new ArrayList(this.fplayers); } public List getFPlayersWhereOnline(boolean online) { - List ret = new ArrayList(); - for (FPlayer fplayer : FPlayerColl.get().getAll()) + List ret = this.getFPlayers(); + Iterator iter = ret.iterator(); + while (iter.hasNext()) { - if (fplayer.getFaction() != this) continue; - if (fplayer.isOnline() != online) continue; - ret.add(fplayer); + FPlayer fplayer = iter.next(); + if (fplayer.isOnline() != online) + { + iter.remove(); + } } return ret; } public List getFPlayersWhereRole(Rel role) { - List ret = new ArrayList(); - for (FPlayer fplayer : FPlayerColl.get().getAll()) + List ret = this.getFPlayers(); + Iterator iter = ret.iterator(); + while (iter.hasNext()) { - if (fplayer.getFaction() != this) continue; - if (fplayer.getRole() != role) continue; - ret.add(fplayer); + FPlayer fplayer = iter.next(); + if (fplayer.getRole() != role) + { + iter.remove(); + } } return ret; } public FPlayer getLeader() { - for (FPlayer fplayer : FPlayerColl.get().getAll()) + List ret = this.getFPlayers(); + Iterator iter = ret.iterator(); + while (iter.hasNext()) { - if (fplayer.getFaction() != this) continue; - if (fplayer.getRole() != Rel.LEADER) continue; - return fplayer; + FPlayer fplayer = iter.next(); + if (fplayer.getRole() == Rel.LEADER) + { + return fplayer; + } } return null; } diff --git a/src/com/massivecraft/factions/FactionColl.java b/src/com/massivecraft/factions/FactionColl.java index aacc53b4..72328be3 100644 --- a/src/com/massivecraft/factions/FactionColl.java +++ b/src/com/massivecraft/factions/FactionColl.java @@ -39,8 +39,8 @@ public class FactionColl extends Coll super.init(); this.migrate(); - - this.createDefaultFactions(); + this.createDefaultFactions(); + this.reindexFPlayers(); } public void migrate() @@ -91,6 +91,14 @@ public class FactionColl extends Coll return ret; } + public void reindexFPlayers() + { + for (Faction faction : this.getAll()) + { + faction.reindexFPlayers(); + } + } + // -------------------------------------------- // // GET // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/FactionEqualsPredictate.java b/src/com/massivecraft/factions/FactionEqualsPredictate.java index a3c1751e..60a155da 100644 --- a/src/com/massivecraft/factions/FactionEqualsPredictate.java +++ b/src/com/massivecraft/factions/FactionEqualsPredictate.java @@ -34,7 +34,7 @@ public class FactionEqualsPredictate implements Predictate, Seria public boolean apply(CommandSender sender) { FPlayer fplayer = FPlayer.get(sender); - return this.getFactionId().equals(fplayer.getFactionId()); + return this.factionId.equals(fplayer.getFactionId()); } } \ No newline at end of file